//! 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"); const js = struct { extern "host" fn log(ptr: [*]const u8, len: usize) void; extern "host" fn loadImage(ptr: [*]const u8, len: usize) u32; 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, r: u32, g: u32, b: u32, a: u32) void; /// The image (or `sw` x `sh` at `sx`, `sy`) with its corners at the /// three points: top left, top right, bottom left. extern "canvas" fn imageCorners(id: u32, x0: f32, y0: f32, x1: f32, y1: f32, x2: f32, y2: f32, sx: f32, sy: f32, sw: f32, sh: f32, r: u32, g: u32, b: u32, a: u32) void; /// Fills the subpaths `points[ends[i - 1] .. ends[i]]` nonzero: /// `points` is `len` pairs of f32, `ends` `ends_len` u32s, `colors` /// `colors_len` RGBA quadruples, one per subpath (else `r, g, b, a`). extern "canvas" fn shape(points: [*]const [2]f32, len: usize, ends: [*]const u32, ends_len: usize, colors: [*]const abi.Color, colors_len: usize, r: u32, g: u32, b: u32, a: u32) void; /// Draws into image `id`, or the canvas for 0. extern "canvas" fn target(id: u32) void; /// `abi.BlendMode` by value. extern "canvas" fn blend(mode: u32) void; extern "canvas" fn text(x: f32, y: f32, scale: f32, ptr: [*]const u8, len: usize, r: u32, g: u32, b: u32, a: u32) void; extern "canvas" fn clip(enabled: u32, x: f32, y: f32, w: f32, h: f32) void; 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`, /// `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" }); } 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 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, .scratch = scratch.allocator(), .session = gpa, .store = gpa, .session_root = std.mem.asBytes(&session), .store_root = std.mem.asBytes(&store), .commands = &commands, }; } 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)); } /// 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, .flags = @truncate(flags) }; event_count += 1; } fn frame(ms: f64) callconv(.c) void { const now: u64 = @intFromFloat(@max(0, ms) * std.time.ns_per_ms); const dt = if (last == 0) 0 else now -| last; last = now; // `in.dt` comes from the tick, as on native, so it is what a // replay of the tick would give. const tick = abi.Event.tick(now, dt); if (event_count < events.len) { events[event_count] = tick; event_count += 1; } for (events[0..event_count]) |*e| { e.time = now; input.track(e.*); } _ = scratch.reset(.retain_capacity); commands = .empty; input.now = now; input.dt = tick.tickDt(); 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; // No timeline here: only the first frame comes from nowhere. input.jumped = input.frame == 1; G.draw(&ctx, std.mem.asBytes(&state)); js.target(0); js.blend(0); js.clip(0, 0, 0, 0, 0); for (commands.items) |c| submit(c); js.target(0); js.blend(0); // 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(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| if (v.corners) |q| js.imageCorners(@backingInt(v.image), q.top_left[0], q.top_left[1], q.top_right[0], q.top_right[1], q.bottom_left[0], q.bottom_left[1], v.src.x, v.src.y, v.src.w, v.src.h, v.tint.r, v.tint.g, v.tint.b, v.tint.a) else 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.r, v.tint.g, v.tint.b, v.tint.a), .shape => |v| js.shape(v.points.ptr, v.points.len, v.ends.ptr, v.ends.len, v.colors.ptr, v.colors.len, v.color.r, v.color.g, v.color.b, v.color.a), .target => |v| js.target(@backingInt(v)), .blend => |v| js.blend(@backingInt(v)), .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| { input.width = @floatFromInt(v.w); input.height = @floatFromInt(v.h); js.canvas(v.w, v.h); }, .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, .backdrop => {}, } } 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; } /// 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 { const name = path[0..@min(len, path.len)]; // Paths may index arrays and vector lanes (`list[2].mood`). if (abi.layout.resolve(G.description.state.fields, name)) |r| { const f = r.field; if (f.kind != .@"enum") return -1; const value: u64 = @intFromFloat(field(len)); var pairs = std.mem.tokenizeScalar(u8, std.mem.span(f.tags), ','); while (pairs.next()) |pair| { const eq = std.mem.indexOfScalar(u8, pair, '=') orelse continue; if ((std.fmt.parseInt(u64, pair[eq + 1 ..], 10) catch continue) != value) continue; @memcpy(path[0..eq], pair[0..eq]); return @intCast(eq); } } 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); if (abi.layout.resolve(G.description.state.fields, name)) |r| { const f = r.field; const raw = bytes[r.offset..][0..f.size]; var value: u64 = 0; for (raw[0..@min(raw.len, 8)], 0..) |b, i| value |= @as(u64, b) << @intCast(i * 8); return switch (f.kind) { .float => if (f.size == 4) @as(f32, @bitCast(@as(u32, @truncate(value)))) else @bitCast(value), .int => blk: { const shift: u6 = @intCast(64 - f.size * 8); break :blk @floatFromInt(@as(i64, @bitCast(value << shift)) >> shift); }, .uint, .@"enum", .bool => @floatFromInt(value), else => std.math.nan(f64), }; } return std.math.nan(f64); } const vtable: abi.Host.VTable = .{ .log = log, .loadImage = loadImage, .decodeImage = unsupportedImage, .newImage = newImage, .freeImage = freeImage, .gpu = gpu, .newBuffer = newBuffer, .freeBuffer = freeBuffer, .imageSize = imageSize, .textSize = textSize, .loadSound = loadSound, .decodeSound = unsupportedSound, .samples = samples, .readFile = readFile, .env = env, .clock = clock, }; fn log(_: *anyopaque, message: []const u8) void { js.log(message.ptr, message.len); } fn loadImage(_: *anyopaque, p: []const u8) abi.Image { 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 @fromBackingInt(@intCast(js.newImage(w, h))); } fn freeImage(_: *anyopaque, img: abi.Image) void { js.freeImage(@backingInt(img)); } fn gpu(_: *anyopaque) bool { 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(@backingInt(img)), js.imageHeight(@backingInt(img)) }; } /// As host.js draws text: 8 px a character and a 16 px line at scale 1. fn textSize(_: *anyopaque, scale: f32, bytes: []const u8, _: abi.Face) [2]f32 { const count = std.unicode.utf8CountCodepoints(bytes) catch bytes.len; return .{ @as(f32, @floatFromInt(count)) * 8 * scale, 16 * scale }; } /// 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 &.{}; } fn env(_: *anyopaque, _: []const u8) ?[]const u8 { return null; } fn clock(_: *anyopaque) u64 { return @intFromFloat(@max(js.clock(), 0) * std.time.ns_per_ms); } }; }