summaryrefslogtreecommitdiff
path: root/src/sketch/web.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/sketch/web.zig')
-rw-r--r--src/sketch/web.zig15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/sketch/web.zig b/src/sketch/web.zig
index 3e102ad..682bff6 100644
--- a/src/sketch/web.zig
+++ b/src/sketch/web.zig
@@ -39,7 +39,7 @@ const js = struct {
extern "gpu" fn available() u32;
extern "gpu" fn newBuffer(size: u32) u32;
extern "gpu" fn freeBuffer(id: u32) void;
- extern "gpu" fn write(id: u32, offset: u32, ptr: [*]const u8, len: usize) void;
+ extern "gpu" fn write(id: u32, offset: u32, ptr: [*]const u8, len: usize) u32;
/// A `Dispatch` or a `Pass`, described in JSON (see `describe`).
extern "gpu" fn compute(ptr: [*]const u8, len: usize) void;
extern "gpu" fn pass(ptr: [*]const u8, len: usize) void;
@@ -78,6 +78,7 @@ fn Driver(comptime App: type) type {
const G = root.Glue(App);
const gpa = std.heap.wasm_allocator;
return struct {
+ var ready = false;
var state: G.State = undefined;
var session: G.Session = undefined;
// No disk on the web: the store lasts as long as the page.
@@ -124,6 +125,7 @@ fn Driver(comptime App: type) type {
js.canvas(G.description.width, G.description.height);
const ctx = context();
G.init(&ctx, std.mem.asBytes(&state));
+ ready = G.setup(&ctx);
}
/// The page's room for the canvas, in CSS pixels.
@@ -144,6 +146,10 @@ fn Driver(comptime App: type) type {
}
fn frame(ms: f64) callconv(.c) void {
+ if (!ready) {
+ event_count = 0;
+ return;
+ }
const now: u64 = @intFromFloat(@max(0, ms) * std.time.ns_per_ms);
const dt = if (last == 0) 0 else now -| last;
last = now;
@@ -219,7 +225,7 @@ fn Driver(comptime App: type) type {
js.push(v.key, v.samples.ptr, v.samples.len, @intFromBool(v.clear));
},
.microphone => js.listen(),
- .upload => |v| js.write(@backingInt(v.buffer), v.offset, v.bytes.ptr, v.bytes.len),
+ .upload => |v| _ = js.write(@backingInt(v.buffer), v.offset, v.bytes.ptr, v.bytes.len),
.compute => |v| {
const json = describe(v);
js.compute(json.ptr, json.len);
@@ -243,6 +249,7 @@ fn Driver(comptime App: type) type {
}
fn run(line: []const u8) void {
+ if (!ready) return;
const ctx = context();
if (G.command(&ctx, std.mem.asBytes(&state), &line) == .unknown) {
var buf: [160]u8 = undefined;
@@ -394,6 +401,7 @@ fn Driver(comptime App: type) type {
.gpu = gpu,
.newBuffer = newBuffer,
.freeBuffer = freeBuffer,
+ .writeBuffer = writeBuffer,
.imageSize = imageSize,
.textSize = textSize,
.loadSound = loadSound,
@@ -424,6 +432,9 @@ fn Driver(comptime App: type) type {
fn newBuffer(_: *anyopaque, size: u32) abi.Buffer {
return @fromBackingInt(js.newBuffer(size));
}
+ fn writeBuffer(_: *anyopaque, b: abi.Buffer, offset: u32, bytes: []const u8) !void {
+ if (js.write(@backingInt(b), offset, bytes.ptr, bytes.len) == 0) return error.BufferUploadFailed;
+ }
fn freeBuffer(_: *anyopaque, b: abi.Buffer) void {
js.freeBuffer(@backingInt(b));
}