diff options
Diffstat (limited to 'src/sketch/web.zig')
| -rw-r--r-- | src/sketch/web.zig | 80 |
1 files changed, 62 insertions, 18 deletions
diff --git a/src/sketch/web.zig b/src/sketch/web.zig index 3e102ad..e12acb3 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.zig @@ -2,7 +2,7 @@ //! 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. +//! persists. const std = @import("std"); const abi = @import("abi"); const root = @import("root.zig"); @@ -15,6 +15,11 @@ const js = struct { extern "host" fn newImage(w: u32, h: u32) u32; extern "host" fn freeImage(image: u32) void; extern "host" fn clock() f64; + /// canvas measureText: the run's width, ascent and height into `out`, + /// and with `xs_len` (the run's length) each byte's x. + extern "host" fn textRun(face: u32, scale: f32, ptr: [*]const u8, len: usize, xs: [*]f32, xs_len: usize, out: *[3]f32) void; + /// A FontFace from the bytes: its face, or -1. + extern "host" fn font(ptr: [*]const u8, len: usize) i32; 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; @@ -30,7 +35,7 @@ const js = struct { 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 text(x: f32, y: f32, scale: f32, face: u32, 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; @@ -39,14 +44,17 @@ 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; + extern "gpu" fn postProcess(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; + /// `count` samples from a GPU buffer, queued on arrival. + extern "audio" fn buffer(key: u32, id: u32, offset: u32, count: u32) void; extern "audio" fn queued(key: u32) u32; /// Keeps the microphone open; it answers through `sk_microphone`. extern "audio" fn listen() void; @@ -75,13 +83,12 @@ pub fn exportWeb(comptime App: type) void { } fn Driver(comptime App: type) type { - const G = root.Glue(App); + 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. - var store: G.Store = undefined; var events: [1024]abi.Event = undefined; var event_count: usize = 0; var input: abi.Input = .{}; @@ -103,9 +110,7 @@ fn Driver(comptime App: type) type { .input = &input, .scratch = scratch.allocator(), .session = gpa, - .store = gpa, .session_root = std.mem.asBytes(&session), - .store_root = std.mem.asBytes(&store), .commands = &commands, }; } @@ -113,7 +118,6 @@ 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) { @@ -124,6 +128,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 +149,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; @@ -203,7 +212,7 @@ fn Driver(comptime App: type) type { .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), + .text => |v| js.text(v.x, v.y, v.scale, @backingInt(v.face), 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); @@ -218,8 +227,12 @@ fn Driver(comptime App: type) type { s.pushed += v.samples.len; js.push(v.key, v.samples.ptr, v.samples.len, @intFromBool(v.clear)); }, + .audio_buffer => |v| { + stream(v.key).pushed += v.count; + js.buffer(v.key, @backingInt(v.buffer), v.offset, v.count); + }, .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); @@ -228,11 +241,15 @@ fn Driver(comptime App: type) type { const json = describe(v); js.pass(json.ptr, json.len); }, + .post_process => |v| { + const json = describe(v); + js.postProcess(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 + // Voices (and so their groups) are not on the web yet; quitting a page // means nothing; `run` lines go after the frame. - .voice, .quit, .run, .backdrop => {}, + .voice, .gain, .quit, .run, .backdrop => {}, } } @@ -243,6 +260,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,12 +412,16 @@ fn Driver(comptime App: type) type { .gpu = gpu, .newBuffer = newBuffer, .freeBuffer = freeBuffer, + .writeBuffer = writeBuffer, .imageSize = imageSize, - .textSize = textSize, + .textRun = textRun, + .font = font, .loadSound = loadSound, .decodeSound = unsupportedSound, + .newSound = newSound, .samples = samples, .readFile = readFile, + .appendFile = appendFile, .env = env, .clock = clock, }; @@ -424,16 +446,24 @@ 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)); } 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 }; + /// As host.js draws text: canvas measureText, in the page's fonts. + fn textRun(_: *anyopaque, face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun { + var out: [3]f32 = .{ 0, 0, 0 }; + js.textRun(@backingInt(face), scale, bytes.ptr, bytes.len, xs.ptr, xs.len, &out); + return .{ .width = out[0], .ascent = out[1], .height = out[2] }; + } + fn font(_: *anyopaque, bytes: []const u8) ?abi.Face { + const face = js.font(bytes.ptr, bytes.len); + return if (face < 0) null else @enumFromInt(face); } /// Only sounds the page has decoded (opened or dropped files): the /// browser decodes asynchronously, so there is no reading on demand. @@ -444,6 +474,16 @@ fn Driver(comptime App: type) type { fn unsupportedSound(_: *anyopaque, _: []const u8) abi.Audio { return .none; } + /// Kept, unnamed, for `samples` (voices are silent on the web). + fn newSound(_: *anyopaque, ptr: [*]const f32, len: usize) callconv(.c) abi.Audio { + for (ptr[0..len]) |v| if (!std.math.isFinite(v)) return .none; + const copy = gpa.dupe(f32, ptr[0..len]) catch return .none; + sounds.append(gpa, .{ .name = "", .samples = copy }) catch { + gpa.free(copy); + return .none; + }; + return @fromBackingInt(@intCast(sounds.items.len)); + } 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; @@ -451,6 +491,10 @@ fn Driver(comptime App: type) type { fn readFile(_: *anyopaque, _: []const u8) []const u8 { return &.{}; } + /// No files on the web: nothing is written, and the path is "". + fn appendFile(_: *anyopaque, _: [*]const u8, _: usize, _: [*]const u8, _: usize, _: bool) callconv(.c) [*:0]const u8 { + return ""; + } fn env(_: *anyopaque, _: []const u8) ?[]const u8 { return null; } |
