diff options
Diffstat (limited to 'src/sketch/web.zig')
| -rw-r--r-- | src/sketch/web.zig | 267 |
1 files changed, 236 insertions, 31 deletions
diff --git a/src/sketch/web.zig b/src/sketch/web.zig index e91a0ad..bc7fead 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.zig @@ -1,6 +1,8 @@ -//! Runs a sketch in a browser. `web/host.js` forwards input and implements a -//! handful of drawing imports; state, `update` and `draw` are the same Zig as -//! on native. There is no console, timeline or persistence on the web. +//! Runs a sketch in a browser. `web/host.js` forwards input and implements +//! the imports below: drawing on a 2D canvas, GPU work on WebGPU, sound and +//! the microphone on Web Audio, files through the page. State, `update` and +//! `draw` are the same Zig as on native. There is no timeline, and nothing +//! persists: Store lasts as long as the page. const std = @import("std"); const abi = @import("abi"); const root = @import("root.zig"); @@ -11,7 +13,9 @@ const js = struct { extern "host" fn imageWidth(image: u32) u32; extern "host" fn imageHeight(image: u32) u32; extern "host" fn newImage(w: u32, h: u32) u32; + extern "host" fn freeImage(image: u32) void; extern "host" fn clock() f64; + extern "host" fn openFile(command: [*]const u8, command_len: usize, pattern: [*]const u8, pattern_len: usize) void; extern "canvas" fn clear(r: u32, g: u32, b: u32, a: u32) void; extern "canvas" fn fill(x: f32, y: f32, w: f32, h: f32, r: u32, g: u32, b: u32, a: u32) void; extern "canvas" fn image(id: u32, x: f32, y: f32, w: f32, h: f32, sx: f32, sy: f32, sw: f32, sh: f32, alpha: u32) void; @@ -20,16 +24,40 @@ const js = struct { extern "canvas" fn canvas(w: u32, h: u32) void; extern "canvas" fn cursor(kind: u32) void; extern "canvas" fn pixels(id: u32, w: u32, h: u32, ptr: [*]const u8, len: usize, premultiplied: u32) void; + /// Nonzero once the page has a WebGPU device. + 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; + /// 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; + /// Answers through `sk_download`. + extern "gpu" fn download(id: u32, offset: u32, size: u32, key: u32) void; + extern "audio" fn rate() u32; + extern "audio" fn push(key: u32, ptr: [*]const f32, len: usize, clear: u32) void; + extern "audio" fn queued(key: u32) u32; + /// Keeps the microphone open; it answers through `sk_microphone`. + extern "audio" fn listen() void; }; -/// Exports `App` for `web/host.js`: `sk_init`, `sk_event`, `sk_frame`, and -/// for tests to read state by path, `sk_field` (a number) and `sk_tag` (an -/// enum's tag name). +/// Exports `App` for `web/host.js`: `sk_init`, `sk_event`, `sk_frame`, +/// `sk_window` (the room for the canvas); +/// `sk_alloc` and the deliveries into what it returns (`sk_microphone`, +/// `sk_download`, `sk_sound`, `sk_command`); and for tests to read state by +/// path, `sk_buffer`, `sk_field` (a number) and `sk_tag` (an enum's tag +/// name). pub fn exportWeb(comptime App: type) void { const D = Driver(App); @export(&D.init, .{ .name = "sk_init" }); @export(&D.event, .{ .name = "sk_event" }); @export(&D.frame, .{ .name = "sk_frame" }); + @export(&D.window, .{ .name = "sk_window" }); + @export(&D.alloc, .{ .name = "sk_alloc" }); + @export(&D.microphone, .{ .name = "sk_microphone" }); + @export(&D.download, .{ .name = "sk_download" }); + @export(&D.sound, .{ .name = "sk_sound" }); + @export(&D.command, .{ .name = "sk_command" }); @export(&D.buffer, .{ .name = "sk_buffer" }); @export(&D.field, .{ .name = "sk_field" }); @export(&D.tag, .{ .name = "sk_tag" }); @@ -37,24 +65,36 @@ pub fn exportWeb(comptime App: type) void { fn Driver(comptime App: type) type { const G = root.Glue(App); + const gpa = std.heap.wasm_allocator; return struct { var state: G.State = undefined; var session: G.Session = undefined; + // No disk on the web: the store lasts as long as the page. + var store: G.Store = undefined; var events: [1024]abi.Event = undefined; var event_count: usize = 0; var input: abi.Input = .{}; var commands: std.ArrayList(abi.Command) = .empty; - var arena: std.heap.ArenaAllocator = .init(std.heap.wasm_allocator); - var session_heap: std.heap.ArenaAllocator = .init(std.heap.wasm_allocator); + var scratch: std.heap.ArenaAllocator = .init(gpa); var last: u64 = 0; var path: [256]u8 = undefined; + /// What arrived since the last frame, handed to the next one. + var heard: std.ArrayList(f32) = .empty; + var downloads: std.ArrayList(abi.Download) = .empty; + /// Streams the sketch has pushed to, by key. + var streams: std.ArrayList(abi.Stream) = .empty; + /// Sounds the page decoded, by name: `Audio` n is `sounds[n - 1]`. + var sounds: std.ArrayList(struct { name: []const u8, samples: []const f32 }) = .empty; fn context() abi.Context { return .{ .host = .{ .ctx = @ptrCast(&input), .vtable = &vtable }, .input = &input, - .arena = arena.allocator(), - .gpa = session_heap.allocator(), + .scratch = scratch.allocator(), + .session = gpa, + .store = gpa, + .session_root = std.mem.asBytes(&session), + .store_root = std.mem.asBytes(&store), .commands = &commands, }; } @@ -62,21 +102,33 @@ fn Driver(comptime App: type) type { fn init() callconv(.c) void { G.defaults(.state, std.mem.asBytes(&state)); G.defaults(.session, std.mem.asBytes(&session)); + G.defaults(.store, std.mem.asBytes(&store)); input.width = G.description.width; input.height = G.description.height; + if (input.window_width == 0) { + input.window_width = input.width; + input.window_height = input.height; + } + input.sample_rate = js.rate(); js.canvas(G.description.width, G.description.height); const ctx = context(); G.init(&ctx, std.mem.asBytes(&state)); } - fn event(kind: u32, code: u32, x: f32, y: f32, value: u32) callconv(.c) void { + /// The page's room for the canvas, in CSS pixels. + fn window(w: f32, h: f32) callconv(.c) void { + input.window_width = w; + input.window_height = h; + } + + fn event(kind: u32, code: u32, x: f32, y: f32, value: u32, flags: u32) callconv(.c) void { const k = std.enums.fromInt(abi.Event.Kind, kind) orelse return; if (event_count == events.len) { // Never leave a gesture armed when the queue overflows. events[0] = .{ .kind = .cancel }; event_count = 1; } - events[event_count] = .{ .kind = k, .code = @intCast(code), .x = x, .y = y, .value = value }; + events[event_count] = .{ .kind = k, .code = @intCast(code), .x = x, .y = y, .value = value, .flags = @truncate(flags) }; event_count += 1; } @@ -92,27 +144,39 @@ fn Driver(comptime App: type) type { e.time = now; input.track(e.*); } - _ = arena.reset(.retain_capacity); + _ = scratch.reset(.retain_capacity); commands = .empty; input.now = now; input.dt = dt; input.frame += 1; input.events = events[0..event_count]; input.started = input.frame == 1; + for (streams.items) |*s| s.queued = js.queued(s.key); + input.streams = streams.items; + input.microphone = heard.items; + input.downloads = downloads.items; const ctx = context(); G.update(&ctx, std.mem.asBytes(&state)); input.events = &.{}; event_count = 0; - G.draw(&ctx, std.mem.asBytes(&state), std.mem.asBytes(&session)); + G.draw(&ctx, std.mem.asBytes(&state)); js.clip(0, 0, 0, 0, 0); - for (commands.items) |command| submit(command); + for (commands.items) |c| submit(c); + // After the frame, as on native: lines `draw` asked to run. + for (commands.items) |c| if (c == .run) run(c.run); + + heard.clearRetainingCapacity(); + for (downloads.items) |d| gpa.free(d.bytes); + downloads.clearRetainingCapacity(); + input.microphone = &.{}; + input.downloads = &.{}; } - fn submit(command: abi.Command) void { - switch (command) { + fn submit(c: abi.Command) void { + switch (c) { .clear => |v| js.clear(v.r, v.g, v.b, v.a), .rect => |v| js.fill(v.dst.x, v.dst.y, v.dst.w, v.dst.h, v.color.r, v.color.g, v.color.b, v.color.a), - .image => |v| js.image(@intFromEnum(v.image), v.dst.x, v.dst.y, v.dst.w, v.dst.h, v.src.x, v.src.y, v.src.w, v.src.h, v.tint.a), + .image => |v| js.image(@backingInt(v.image), v.dst.x, v.dst.y, v.dst.w, v.dst.h, v.src.x, v.src.y, v.src.w, v.src.h, v.tint.a), .text => |v| js.text(v.x, v.y, v.scale, v.bytes.ptr, v.bytes.len, v.color.r, v.color.g, v.color.b, v.color.a), .clip => |v| if (v) |r| js.clip(1, r.x, r.y, r.w, r.h) else js.clip(0, 0, 0, 0, 0), .canvas => |v| { @@ -120,20 +184,138 @@ fn Driver(comptime App: type) type { input.height = @floatFromInt(v.h); js.canvas(v.w, v.h); }, - .cursor => |v| js.cursor(@intFromEnum(v)), - .pixels => |v| js.pixels(@intFromEnum(v.image), v.w, v.h, v.rgba.ptr, v.rgba.len, @intFromBool(v.premultiplied)), - // No sound or GPU passes on the web yet (Host.gpu says so); - // quitting a page means nothing. - .voice, .audio, .gpu, .quit => {}, + .cursor => |v| js.cursor(@backingInt(v)), + .pixels => |v| js.pixels(@backingInt(v.image), v.w, v.h, v.rgba.ptr, v.rgba.len, @intFromBool(v.premultiplied)), + .audio => |v| { + const s = stream(v.key); + if (v.clear) s.pushed = 0; + s.pushed += v.samples.len; + 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), + .compute => |v| { + const json = describe(v); + js.compute(json.ptr, json.len); + }, + .gpu => |v| { + const json = describe(v); + js.pass(json.ptr, json.len); + }, + .download => |v| js.download(@backingInt(v.buffer), v.offset, v.size, v.key), + .open_file => |v| js.openFile(v.command.ptr, v.command.len, v.pattern.ptr, v.pattern.len), + // Synthesised voices are not on the web yet; quitting a page + // means nothing; `run` lines go after the frame. + .voice, .quit, .run => {}, } } + fn stream(key: u32) *abi.Stream { + for (streams.items) |*s| if (s.key == key) return s; + streams.append(gpa, .{ .key = key }) catch return &streams.items[0]; + return &streams.items[streams.items.len - 1]; + } + + fn run(line: []const u8) void { + const ctx = context(); + if (G.command(&ctx, std.mem.asBytes(&state), &line) == .unknown) { + var buf: [160]u8 = undefined; + const message = std.fmt.bufPrint(&buf, "unknown command: {s}", .{line[0..@min(line.len, 120)]}) catch return; + js.log(message.ptr, message.len); + } + } + + /// GPU work as JSON for host.js: buffers by handle, bytes (code, + /// uniforms, vertices) as `[pointer, length]` into wasm memory, and + /// each shader with a hash of its code to cache it by. + fn describe(value: anytype) []const u8 { + var out: std.Io.Writer.Allocating = .init(scratch.allocator()); + var json: std.json.Stringify = .{ .writer = &out.writer }; + write(&json, value) catch return "{}"; + return out.written(); + } + + fn write(json: *std.json.Stringify, value: anytype) !void { + const T = @TypeOf(value); + if (T == []const u8) { + try json.beginArray(); + try json.write(@intFromPtr(value.ptr)); + try json.write(value.len); + return json.endArray(); + } + if (T == abi.gpu.Shader or T == abi.gpu.Kernel) { + // The code once more, by hash: compiled modules are cached. + try json.beginObject(); + try json.objectField("hash"); + try json.print("\"{x}\"", .{std.hash.Wyhash.hash(0, value.code)}); + inline for (@typeInfo(T).@"struct".field_names) |name| { + try json.objectField(name); + try write(json, @field(value, name)); + } + return json.endObject(); + } + switch (@typeInfo(T)) { + .@"struct" => |s| { + try json.beginObject(); + inline for (s.field_names) |name| { + try json.objectField(name); + try write(json, @field(value, name)); + } + try json.endObject(); + }, + .pointer => { + try json.beginArray(); + for (value) |item| try write(json, item); + try json.endArray(); + }, + .array => { + try json.beginArray(); + for (value) |item| try write(json, item); + try json.endArray(); + }, + .optional => if (value) |v| try write(json, v) else try json.write(null), + .@"enum" => |e| if (e.mode == .exhaustive) try json.write(@tagName(value)) else try json.write(@backingInt(value)), + else => try json.write(value), + } + } + + /// Memory for host.js to write a delivery into; the delivery takes + /// it over. + fn alloc(len: usize) callconv(.c) ?[*]u8 { + const bytes = gpa.alloc(u8, len) catch return null; + return bytes.ptr; + } + + fn microphone(ptr: [*]f32, len: usize) callconv(.c) void { + heard.appendSlice(gpa, ptr[0..len]) catch {}; + gpa.free(std.mem.sliceAsBytes(ptr[0..len])); + } + + fn download(key: u32, ptr: [*]u8, len: usize) callconv(.c) void { + downloads.append(gpa, .{ .key = key, .bytes = ptr[0..len] }) catch gpa.free(ptr[0..len]); + } + + /// A decoded sound for `loadSound(name)`; a name seen before is + /// replaced. + fn sound(name: [*]u8, name_len: usize, decoded: [*]f32, len: usize) callconv(.c) void { + for (sounds.items) |*s| if (std.mem.eql(u8, s.name, name[0..name_len])) { + gpa.free(name[0..name_len]); + s.samples = decoded[0..len]; + return; + }; + sounds.append(gpa, .{ .name = name[0..name_len], .samples = decoded[0..len] }) catch {}; + } + + /// Runs a console line through the sketch's `command`. + fn command(ptr: [*]u8, len: usize) callconv(.c) void { + run(ptr[0..len]); + gpa.free(ptr[0..len]); + } + fn buffer() callconv(.c) [*]u8 { return &path; } - /// The state field at the path written into `sk_buffer`, as a number - /// (enums by value); NaN when there is no such scalar field. /// Writes the tag name of the enum field at the path in `buffer` over /// it, and returns its length; -1 when there is no such enum field. fn tag(len: usize) callconv(.c) i32 { @@ -152,6 +334,8 @@ fn Driver(comptime App: type) type { return -1; } + /// The state field at the path written into `sk_buffer`, as a number + /// (enums by value); NaN when there is no such scalar field. fn field(len: usize) callconv(.c) f64 { const name = path[0..@min(len, path.len)]; const bytes = std.mem.asBytes(&state); @@ -180,9 +364,12 @@ fn Driver(comptime App: type) type { .newImage = newImage, .freeImage = freeImage, .gpu = gpu, + .newBuffer = newBuffer, + .freeBuffer = freeBuffer, .imageSize = imageSize, - .loadSound = unsupportedSound, + .loadSound = loadSound, .decodeSound = unsupportedSound, + .samples = samples, .readFile = readFile, .env = env, .clock = clock, @@ -191,24 +378,42 @@ fn Driver(comptime App: type) type { js.log(message.ptr, message.len); } fn loadImage(_: *anyopaque, p: []const u8) abi.Image { - return @enumFromInt(js.loadImage(p.ptr, p.len)); + return @fromBackingInt(@intCast(js.loadImage(p.ptr, p.len))); } fn unsupportedImage(_: *anyopaque, _: []const u8) abi.Image { return .none; } fn newImage(_: *anyopaque, w: u32, h: u32) abi.Image { - return @enumFromInt(js.newImage(w, h)); + return @fromBackingInt(@intCast(js.newImage(w, h))); + } + fn freeImage(_: *anyopaque, img: abi.Image) void { + js.freeImage(@backingInt(img)); } - fn freeImage(_: *anyopaque, _: abi.Image) void {} fn gpu(_: *anyopaque) bool { - return false; + return js.available() != 0; + } + fn newBuffer(_: *anyopaque, size: u32) abi.Buffer { + return @fromBackingInt(js.newBuffer(size)); + } + fn freeBuffer(_: *anyopaque, b: abi.Buffer) void { + js.freeBuffer(@backingInt(b)); } fn imageSize(_: *anyopaque, img: abi.Image) [2]u32 { - return .{ js.imageWidth(@intFromEnum(img)), js.imageHeight(@intFromEnum(img)) }; + return .{ js.imageWidth(@backingInt(img)), js.imageHeight(@backingInt(img)) }; + } + /// Only sounds the page has decoded (opened or dropped files): the + /// browser decodes asynchronously, so there is no reading on demand. + fn loadSound(_: *anyopaque, name: []const u8) abi.Audio { + for (sounds.items, 1..) |s, i| if (std.mem.eql(u8, s.name, name)) return @fromBackingInt(@intCast(i)); + return .none; } fn unsupportedSound(_: *anyopaque, _: []const u8) abi.Audio { return .none; } + fn samples(_: *anyopaque, a: abi.Audio) []const f32 { + const i = @backingInt(a); + return if (i == 0 or i > sounds.items.len) &.{} else sounds.items[i - 1].samples; + } fn readFile(_: *anyopaque, _: []const u8) []const u8 { return &.{}; } |
