diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:07:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 590ba7b9b579b97e34b51ab7e3460ebc85713343 (patch) | |
| tree | 6a73a7570ff4f79f41deff078455877b9bc6571b /src | |
| parent | bc8e6eb1bfef0c5e9b3c40fc1e4fd5a1659e2b03 (diff) | |
| download | sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.tar.gz sketchbook-590ba7b9b579b97e34b51ab7e3460ebc85713343.zip | |
sketchbook: Zig 0.17, a Store on disk, requests, a scope (waveform, spectrum, spectrogram), audio codecs and fonts, web host
Diffstat (limited to 'src')
30 files changed, 2722 insertions, 304 deletions
diff --git a/src/abi.zig b/src/abi.zig index 4782599..ca1333e 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -13,6 +13,9 @@ const std = @import("std"); pub const layout = @import("layout.zig"); pub const Key = @import("keys.zig").Key; +/// Bytes read back from a GPU buffer (see `Command.download`). +pub const Download = struct { key: u32, bytes: []const u8 }; + pub const Rect = extern struct { x: f32 = 0, y: f32 = 0, @@ -71,11 +74,25 @@ pub const Event = extern struct { text, /// Focus loss, or the console took the pointer: drop armed gestures. cancel, + /// Horizontal wheel delta; appended to preserve recorded event values. + wheel_horizontal, _, }; /// `flags` bit for a held key's auto-repeat. pub const repeat: u8 = 1; + /// For mouse and wheel events: the modifier keys held as it happened, + /// as `Key.Mod` bits. Read this rather than `Input.held`, which is the + /// state after the frame's events, and misses modifiers that come with + /// no key events at all (a trackpad's pinch is ctrl+wheel). + pub fn modifiers(e: Event) u32 { + return e.flags >> 4; + } + /// `flags` for an event with `mods` (`Key.Mod` bits) held. + pub fn withModifiers(mods: u32) u8 { + return @intCast((mods & 0xf) << 4); + } + pub fn wheel(e: Event) f32 { return @bitCast(e.value); } @@ -112,25 +129,38 @@ pub const Input = struct { started: bool = false, /// The console is open and owns the keyboard. console: bool = false, + /// The canvas. width: f32 = 0, height: f32 = 0, + /// The room the host has for the canvas, in the canvas's units at 1:1: + /// the window's size (CSS pixels on the web). A sketch that sets its + /// canvas to it fills any window, a phone's included, at its own scale. + window_width: f32 = 0, + window_height: f32 = 0, mouse_x: f32 = 0, mouse_y: f32 = 0, /// Held mouse buttons, bit `button - 1`. buttons: u32 = 0, /// Held keys by USB HID scancode, maintained from the events fed so far. - keys: std.StaticBitSet(256) = .initEmpty(), + keys: std.StaticBitSet(256) = .empty, streams: []const Stream = &.{}, sample_rate: u32 = 48_000, + /// Mono samples at `sample_rate` recorded since the previous frame, while + /// `draw` declares `Command.microphone`. Only `draw` sees them: they come + /// from a device, not from events, so they do not replay. + microphone: []const f32 = &.{}, + /// Finished `Command.download`s, oldest first. Like the microphone, + /// only `draw` sees them, and they do not replay. + downloads: []const Download = &.{}, /// `key` is any enum whose values are HID scancodes, like `sketch.Key`. pub fn held(in: *const Input, key: anytype) bool { - return in.keys.isSet(@intFromEnum(key)); + return in.keys.isSet(@backingInt(key)); } /// Went down this frame; auto-repeats excluded. pub fn pressed(in: *const Input, key: anytype) bool { for (in.events) |e| { - if (e.kind == .key_down and e.code == @intFromEnum(key) and e.flags & Event.repeat == 0) return true; + if (e.kind == .key_down and e.code == @backingInt(key) and e.flags & Event.repeat == 0) return true; } return false; } @@ -141,7 +171,7 @@ pub const Input = struct { /// Follows mouse, button and key state through one fed event. pub fn track(in: *Input, e: Event) void { switch (e.kind) { - .mouse_move, .wheel => { + .mouse_move, .wheel, .wheel_horizontal => { in.mouse_x = e.x; in.mouse_y = e.y; }, @@ -155,7 +185,7 @@ pub const Input = struct { .key_up => in.keys.unset(@min(e.code, 255)), .cancel => { in.buttons = 0; - in.keys = .initEmpty(); + in.keys = .empty; }, else => {}, } @@ -167,6 +197,9 @@ pub const Input = struct { pub const Image = enum(u32) { none = 0, _ }; pub const Audio = enum(u32) { none = 0, _ }; +/// GPU memory made by `Host.newBuffer`, filled by `Command.upload` or by +/// compute shaders, and read by any shader. Contents stay on the GPU. +pub const Buffer = enum(u32) { none = 0, _ }; pub const Cursor = enum { hidden, arrow, pointer }; /// Voices are reconciled, not triggered: `draw` declares every voice that @@ -185,7 +218,10 @@ pub const Voice = struct { }; /// Slices must stay alive until the host consumes the frame, right after -/// `draw` returns: literals, the frame arena and session memory all qualify. +/// `draw` returns. `sketch.Frame` copies the small ones (text, GPU +/// descriptions, uniforms) into scratch memory itself; bulk data (vertices, +/// samples, pixels, uploads) must already live in scratch, session or store +/// memory, or be a literal. pub const Command = union(enum) { clear: Color, rect: struct { dst: Rect, color: Color }, @@ -205,6 +241,25 @@ pub const Command = union(enum) { /// Renders a GPU pass into an image made by `Host.newImage`, when /// `Host.gpu` says the host can. gpu: gpu.Pass, + /// Bytes into a buffer made by `Host.newBuffer`, at `offset`. + upload: struct { buffer: Buffer, offset: u32 = 0, bytes: []const u8 }, + /// Runs a compute shader over buffers (see `abi.gpu`). + compute: gpu.Dispatch, + /// Keeps the default recording device open while declared, like a + /// voice; what it hears arrives in `Input.microphone`. + microphone, + /// Asks the user for a file. The answer arrives later as the console + /// line `<command> <path>`, so it reaches the sketch's `command` (and + /// the console log) like anything typed. `pattern` filters by extension: + /// "wav;flac;ogg", or empty for any file. + open_file: struct { command: []const u8, pattern: []const u8 = "" }, + /// Copies `size` bytes of a buffer from `offset` back to the CPU. They + /// arrive in `Input.downloads` under `key` once the GPU has got there, + /// usually a frame or two later; the frame does not wait. + download: struct { buffer: Buffer, offset: u32 = 0, size: u32, key: u32 = 0 }, + /// Runs a console line after this frame, as if typed: the way `draw`, + /// which cannot write State, hands it something only it learns. + run: []const u8, quit, }; @@ -213,13 +268,44 @@ pub const Command = union(enum) { /// description (so an edited shader simply becomes a new pipeline after a /// reload), and runs the pass. Sketches never touch a graphics API. /// -/// Shaders are SPIR-V. Resources follow SDL_GPU's SPIR-V convention: -/// vertex uniforms in set 1, fragment uniforms in set 3, binding = slot. -/// Uniform bytes must follow std140. +/// Shaders are SPIR-V for native hosts and WGSL for the web host; the +/// build makes both from GLSL (`addShaders` in build.zig), and a sketch +/// passes whichever its build gave it. Resources follow SDL_GPU's SPIR-V +/// convention, which the WGSL keeps: vertex uniforms in set 1, fragment +/// uniforms in set 3, binding = slot. Uniform bytes must follow std140. pub const gpu = struct { pub const Shader = struct { - spirv: []const u8, + /// SPIR-V, or WGSL on the web. + code: []const u8, + uniform_buffers: u32 = 0, + /// Read-only storage buffers: set 0 in a vertex shader, set 2 in a + /// fragment shader. + storage_buffers: u32 = 0, + }; + + /// A compute shader. SDL_GPU's SPIR-V sets: read-only buffers in set 0, + /// read-write buffers in set 1, uniforms in set 2. + pub const Kernel = struct { + /// SPIR-V, or WGSL on the web. + code: []const u8, + readonly_buffers: u32 = 0, + readwrite_buffers: u32 = 0, uniform_buffers: u32 = 0, + /// The shader's `local_size`. + threads: [3]u32 = .{ 64, 1, 1 }, + }; + + /// One run of a kernel, in a compute pass of its own: everything it + /// writes is visible to the commands after it. + pub const Dispatch = struct { + /// Names it in GPU timings (`--profile-gpu`). + label: []const u8 = "", + kernel: Kernel, + readonly: []const Buffer = &.{}, + readwrite: []const Buffer = &.{}, + /// Uniform slot 0, std140. + uniforms: []const u8 = &.{}, + groups: [3]u32 = .{ 1, 1, 1 }, }; pub const Format = enum { float, float2, float3, float4, ubyte4_norm }; @@ -258,12 +344,17 @@ pub const gpu = struct { pub const Draw = struct { /// Index into `Pass.pipelines`. pipeline: u32, - vertices: []const u8, + /// Empty for a pipeline without attributes, whose vertex shader + /// makes its own vertices from `gl_VertexIndex`. + vertices: []const u8 = &.{}, vertex_count: u32, stencil_reference: u8 = 0, /// Uniform slot 0 of each stage. vertex_uniforms: []const u8 = &.{}, fragment_uniforms: []const u8 = &.{}, + /// Read-only storage buffers of each stage, binding = index. + vertex_storage: []const Buffer = &.{}, + fragment_storage: []const Buffer = &.{}, scissor: ?Rect = null, }; @@ -276,9 +367,9 @@ pub const gpu = struct { pub fn attributes(comptime V: type) []const Attribute { return comptime blk: { if (@typeInfo(V).@"struct".layout != .@"extern") @compileError(@typeName(V) ++ " must be extern to be a vertex"); - const fields = @typeInfo(V).@"struct".fields; - var out: [fields.len]Attribute = undefined; - for (fields, 0..) |f, i| out[i] = .{ .location = i, .offset = @offsetOf(V, f.name), .format = formatOf(f.type) }; + const s = @typeInfo(V).@"struct"; + var out: [s.field_names.len]Attribute = undefined; + for (s.field_names, s.field_types, 0..) |name, F, i| out[i] = .{ .location = i, .offset = @offsetOf(V, name), .format = formatOf(F) }; const frozen = out; break :blk &frozen; }; @@ -336,6 +427,8 @@ pub const gpu = struct { } pub const Pass = struct { + /// Names it in GPU timings (`--profile-gpu`). + label: []const u8 = "", /// The target; its size is the image's. image: Image, width: u32, @@ -366,14 +459,22 @@ pub const Host = struct { newImage: *const fn (*anyopaque, u32, u32) Image, /// Releases an image; its handle draws nothing afterwards. freeImage: *const fn (*anyopaque, Image) void, - /// Whether `Command.gpu` runs here: the host has a GPU device. + /// Whether `Command.gpu` and `Command.compute` run here: the host + /// has a GPU device. gpu: *const fn (*anyopaque) bool, + /// A GPU buffer of `size` bytes, zeroed; `.none` without a GPU. + newBuffer: *const fn (*anyopaque, u32) Buffer, + freeBuffer: *const fn (*anyopaque, Buffer) void, /// Zero while unknown. imageSize: *const fn (*anyopaque, Image) [2]u32, /// `.none` if the file does not load. loadSound: *const fn (*anyopaque, []const u8) Audio, - /// WAV bytes in memory. Not cached. `.none` if they do not decode. + /// WAV, FLAC or Ogg Vorbis bytes in memory. Not cached. `.none` if + /// they do not decode. decodeSound: *const fn (*anyopaque, []const u8) Audio, + /// A loaded sound's samples: mono, at `Input.sample_rate`, alive as + /// long as the host. Empty for `.none`. + samples: *const fn (*anyopaque, Audio) []const f32, /// The whole file, mapped for the life of the host; empty if missing. readFile: *const fn (*anyopaque, []const u8) []const u8, env: *const fn (*anyopaque, []const u8) ?[]const u8, @@ -387,7 +488,7 @@ pub const Host = struct { } pub fn print(h: Host, comptime fmt: []const u8, args: anytype) void { var buf: [512]u8 = undefined; - h.log(std.fmt.bufPrint(&buf, fmt, args) catch "(message too long)"); + h.log(std.mem.print(&buf, fmt, args) catch "(message too long)"); } pub fn loadImage(h: Host, path: []const u8) Image { return h.vtable.loadImage(h.ctx, path); @@ -404,6 +505,12 @@ pub const Host = struct { pub fn gpu(h: Host) bool { return h.vtable.gpu(h.ctx); } + pub fn newBuffer(h: Host, size: u32) Buffer { + return h.vtable.newBuffer(h.ctx, size); + } + pub fn freeBuffer(h: Host, buffer: Buffer) void { + h.vtable.freeBuffer(h.ctx, buffer); + } pub fn imageSize(h: Host, image: Image) [2]u32 { return h.vtable.imageSize(h.ctx, image); } @@ -413,6 +520,9 @@ pub const Host = struct { pub fn decodeSound(h: Host, wav: []const u8) Audio { return h.vtable.decodeSound(h.ctx, wav); } + pub fn samples(h: Host, audio: Audio) []const f32 { + return h.vtable.samples(h.ctx, audio); + } pub fn readFile(h: Host, path: []const u8) []const u8 { return h.vtable.readFile(h.ctx, path); } @@ -424,15 +534,24 @@ pub const Host = struct { } }; -/// What the host passes to every call. +/// What the host passes to every call: services, input, and memory by +/// lifetime, shortest first. pub const Context = struct { host: Host, input: *const Input, - /// Frame lifetime: reset after the host consumes the frame. - arena: std.mem.Allocator, - /// Session lifetime: survives reloads, released when the session resets. - gpa: std.mem.Allocator, - /// Filled by `draw`, using `arena`. + /// Frame lifetime: all of it is released after the host consumes the + /// frame. + scratch: std.mem.Allocator, + /// This process: survives reloads; frees one allocation at a time, and + /// releases the rest when Session's layout changes. + session: std.mem.Allocator, + /// On disk, next to the state file: survives reloads and restarts, at + /// the same addresses, so Store may point into it. + store: std.mem.Allocator, + /// The sketch's `Session` and `Store` values. + session_root: [*]u8, + store_root: [*]u8, + /// Filled by `draw`, using `scratch`. commands: *std.ArrayList(Command), }; @@ -470,12 +589,13 @@ pub const Info = struct { height: u32, state: Layout, session: Layout, + store: Layout, /// Console completions offered by the sketch. commands: []const []const u8 = &.{}, }; /// Which memory `sketch_defaults` fills. -pub const Memory = enum(u8) { state, session }; +pub const Memory = enum(u8) { state, session, store }; /// The entry points of a sketch, exported from libraries under these names. /// What became of a console line offered to a sketch. @@ -495,10 +615,11 @@ pub const Sketch = struct { init: *const fn (*const Context, [*]u8) callconv(.c) void, /// Deterministic: new state from state and input, nothing else. update: *const fn (*const Context, [*]u8) callconv(.c) void, - /// Presentation: reads state, may change session, appends commands. - draw: *const fn (*const Context, [*]const u8, [*]u8) callconv(.c) void, - /// A console line the host did not understand; true when handled. - command: *const fn (*const Context, [*]u8, [*]u8, *const []const u8) callconv(.c) Outcome, + /// Presentation: reads state, may change session and store, appends + /// commands. + draw: *const fn (*const Context, [*]const u8) callconv(.c) void, + /// A console line the host did not understand. + command: *const fn (*const Context, [*]u8, *const []const u8) callconv(.c) Outcome, pub const symbols = .{ .info = "sketch_info", @@ -514,7 +635,7 @@ pub const Sketch = struct { pub const hash: u64 = blk: { @setEvalBranchQuota(1_000_000); var h: u64 = 0; - for (.{ Rect, Color, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| { + for (.{ Rect, Color, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| { h = std.hash.Wyhash.hash(h, @typeName(T) ++ layout.signature(T)); } break :blk h; @@ -528,7 +649,7 @@ test "vertex attributes and std140 checks come from the types" { try std.testing.expectEqual(@as(u32, 12), attrs[2].offset); const Block = extern struct { color: [4]f32, size: [2]f32, pad: [2]f32, stops: [4][4]f32 }; comptime gpu.assertStd140(Block); - const p = gpu.pipeline(Vertex, .{ .vertex = .{ .spirv = "" }, .fragment = .{ .spirv = "" } }); + const p = gpu.pipeline(Vertex, .{ .vertex = .{ .code = "" }, .fragment = .{ .code = "" } }); try std.testing.expectEqual(@as(u32, 20), p.stride); } diff --git a/src/glsl.zig b/src/glsl.zig index 995985b..a1cd2d6 100644 --- a/src/glsl.zig +++ b/src/glsl.zig @@ -27,10 +27,10 @@ pub fn source(comptime T: type, comptime name: []const u8) []const u8 { comptime { @setEvalBranchQuota(100_000); const tag = T.glsl; - const fields = @typeInfo(T).@"struct".fields; + const s = @typeInfo(T).@"struct"; var out: []const u8 = "// Generated from the Zig struct " ++ @typeName(T) ++ "; do not edit.\n"; if (@TypeOf(tag) == @TypeOf(.enum_literal) and tag == .vertex) { - for (fields, 0..) |f, i| out = out ++ std.fmt.comptimePrint("layout(location = {d}) in {s} {s};\n", .{ i, typeName(f.type, .vertex), f.name }); + for (s.field_names, s.field_types, 0..) |name_, F, i| out = out ++ std.fmt.comptimePrint("layout(location = {d}) in {s} {s};\n", .{ i, typeName(F, .vertex), name_ }); } else if (@TypeOf(tag) == @TypeOf(.enum_literal) and tag == .plain) { out = out ++ "struct " ++ name ++ " {\n" ++ members(T) ++ "};\n"; } else if (@hasField(@TypeOf(tag), "uniform")) { @@ -46,15 +46,16 @@ pub fn source(comptime T: type, comptime name: []const u8) []const u8 { fn members(comptime T: type) []const u8 { comptime { var out: []const u8 = ""; - for (@typeInfo(T).@"struct".fields) |f| { - const array_of_composites = switch (@typeInfo(f.type)) { + const s = @typeInfo(T).@"struct"; + for (s.field_names, s.field_types) |name, F| { + const array_of_composites = switch (@typeInfo(F)) { .array => |a| @typeInfo(a.child) == .array or @typeInfo(a.child) == .@"struct", else => false, }; out = out ++ if (array_of_composites) - std.fmt.comptimePrint(" {s} {s}[{d}];\n", .{ typeName(@typeInfo(f.type).array.child, .uniform), f.name, @typeInfo(f.type).array.len }) + std.fmt.comptimePrint(" {s} {s}[{d}];\n", .{ typeName(@typeInfo(F).array.child, .uniform), name, @typeInfo(F).array.len }) else - std.fmt.comptimePrint(" {s} {s};\n", .{ typeName(f.type, .uniform), f.name }); + std.fmt.comptimePrint(" {s} {s};\n", .{ typeName(F, .uniform), name }); } return out; } @@ -103,9 +104,10 @@ pub fn generate(comptime root: type, comptime paths: []const []const u8, io: std pub fn assertStd140(comptime U: type) void { comptime { if (@typeInfo(U).@"struct".layout != .@"extern") @compileError(@typeName(U) ++ " must be extern to be a uniform block"); - for (@typeInfo(U).@"struct".fields) |f| { - const want = std140Align(f.type); - if (@offsetOf(U, f.name) % want != 0) @compileError(std.fmt.comptimePrint("{s}.{s} sits at {d}; std140 wants a multiple of {d}", .{ @typeName(U), f.name, @offsetOf(U, f.name), want })); + const s = @typeInfo(U).@"struct"; + for (s.field_names, s.field_types) |name, F| { + const want = std140Align(F); + if (@offsetOf(U, name) % want != 0) @compileError(std.fmt.comptimePrint("{s}.{s} sits at {d}; std140 wants a multiple of {d}", .{ @typeName(U), name, @offsetOf(U, name), want })); } if (@sizeOf(U) % 16 != 0) @compileError(@typeName(U) ++ " must be a multiple of 16 bytes"); } diff --git a/src/host/Console.zig b/src/host/Console.zig index e6be2f0..b22facb 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -95,7 +95,7 @@ fn complete(self: *Console, extra: []const []const u8) void { first = candidate; common = candidate.len; } - const match = std.fmt.bufPrint(matches[used..], "{s} ", .{candidate}) catch continue; + const match = std.mem.print(matches[used..], "{s} ", .{candidate}) catch continue; used += match.len; count += 1; }; @@ -120,7 +120,7 @@ pub fn log(self: *Console, message: []const u8) void { pub fn print(self: *Console, comptime fmt: []const u8, args: anytype) void { var buf: [512]u8 = undefined; - self.log(std.fmt.bufPrint(&buf, fmt, args) catch "(message too long)"); + self.log(std.mem.print(&buf, fmt, args) catch "(message too long)"); } /// Runs one line. Host commands first; anything else goes to the sketch. diff --git a/src/host/Host.zig b/src/host/Host.zig index d511099..7ebe54a 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -25,6 +25,8 @@ pub const Options = struct { headless: bool = false, /// Forget the state file's contents: scripts start from a known place. fresh: bool = false, + /// Time every GPU command alone and print the times at exit. + profile_gpu: bool = false, }; gpa: std.mem.Allocator, @@ -45,6 +47,10 @@ images: gfx.Images, audio: sound.Audio, state: memory.StateFile, session: memory.Session, +store: memory.StoreFile, +/// Images and buffers the sketch made (`newImage`, `decodeImage`, +/// `newBuffer`): Session holds their handles, so they go when it resets. +made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, recordings: []align(memory.page) u8, timeline: Timeline, console: Console = .{}, @@ -71,6 +77,7 @@ started: bool = true, last_now: u64 = 0, /// Saved after the next frame is drawn (scripts). screenshot: ?[:0]const u8 = null, +dialog: Dialog = .{}, pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, linked: ?abi.Sketch) !void { if (options.headless) { @@ -93,7 +100,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std const info = sketch.info(); var title_buf: [256]u8 = undefined; - const title = std.fmt.bufPrintZ(&title_buf, "{s}", .{info.name}) catch "sketch"; + const title = std.mem.printSentinel(&title_buf, "{s}", .{info.name}, 0) catch "sketch"; const window = c.SDL_CreateWindow(title.ptr, @intCast(info.width), @intCast(info.height), c.SDL_WINDOW_RESIZABLE) orelse return sdlError("SDL_CreateWindow"); errdefer c.SDL_DestroyWindow(window); // The 2D renderer runs on the GPU device that also runs sketches' GPU @@ -117,8 +124,14 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); var inputs_buf: [std.fs.max_path_bytes]u8 = undefined; - const recordings = try mapRecordings(try std.fmt.bufPrint(&inputs_buf, "{s}.inputs", .{options.state})); + const recordings = try mapRecordings(try std.mem.print(&inputs_buf, "{s}.inputs", .{options.state})); errdefer std.posix.munmap(recordings); + var store_buf: [std.fs.max_path_bytes]u8 = undefined; + var store = memory.StoreFile.open(gpa, try std.mem.print(&store_buf, "{s}.store", .{options.state}), options.fresh) catch |err| { + if (err == error.StoreInUse) std.log.err("{s}.store is open in another process", .{options.state}); + return err; + }; + errdefer store.close(); self.* = .{ .gpa = gpa, @@ -132,6 +145,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .audio = .{ .gpa = gpa }, .state = state, .session = .init(gpa), + .store = store, .recordings = recordings, .timeline = undefined, .sketch = sketch, @@ -141,6 +155,9 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std }; self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; if (gpu) |device| self.passes = .init(gpa, device); + if (options.profile_gpu) if (self.passes) |*g| { + g.profile = .init(gpa); + }; self.audio.open() catch |err| std.log.warn("audio unavailable: {t}", .{err}); if (options.library) |path| if (linked == null) { self.watch = Watch.init(path) catch null; @@ -156,6 +173,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std } pub fn deinit(self: *Host) void { + self.dialog.deinit(); if (self.watch) |w| w.deinit(); self.audio.deinit(); if (self.passes) |*g| g.deinit(&self.images); @@ -173,6 +191,9 @@ pub fn deinit(self: *Host) void { self.files.deinit(self.gpa); std.posix.munmap(self.recordings); self.session.deinit(self.gpa); + self.store.close(); + self.made.images.deinit(self.gpa); + self.made.buffers.deinit(self.gpa); self.state.close(); self.pending.deinit(self.gpa); self.frame_arena.deinit(); @@ -242,16 +263,30 @@ fn draw(self: *Host, now: u64) !void { self.input.console = self.console.visible; self.input.width = @floatFromInt(self.canvas[0]); self.input.height = @floatFromInt(self.canvas[1]); + // Headless runs have no window to fill: the canvas is all the room. + self.input.window_width = self.input.width; + self.input.window_height = self.input.height; + if (!self.options.headless) { + var w: c_int = 0; + var h: c_int = 0; + if (c.SDL_GetWindowSize(self.window, &w, &h) and w > 0 and h > 0) { + self.input.window_width = @floatFromInt(w); + self.input.window_height = @floatFromInt(h); + } + } self.input.streams = self.audio.streamStatus(); self.input.sample_rate = sound.sample_rate; + self.input.microphone = self.audio.record(arena); + self.input.downloads = if (self.passes) |*g| try g.collect(arena) else &.{}; self.input.frame += 1; const ctx = self.context(); - self.sketch.draw(&ctx, self.live().ptr, self.session.bytes.ptr); + self.sketch.draw(&ctx, self.live().ptr); _ = c.SDL_SetRenderClipRect(self.renderer, null); _ = c.SDL_SetRenderDrawColor(self.renderer, 0, 0, 0, 255); _ = c.SDL_RenderClear(self.renderer); self.audio.beginFrame(); + var lines: std.ArrayList([]const u8) = .empty; for (self.commands.items) |command| switch (command) { .canvas => |size| self.setCanvas(size.w, size.h), .cursor => |kind| self.cursor = kind, @@ -259,9 +294,16 @@ fn draw(self: *Host, now: u64) !void { .audio => |a| self.audio.push(a.key, a.samples, a.clear), .quit => self.quit = true, .gpu => |pass| if (self.passes) |*g| g.run(&self.images, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }), + .upload => |u| if (self.passes) |*g| g.write(u.buffer, u.offset, u.bytes) catch |err| self.print("gpu upload: {t} ({s})", .{ err, c.SDL_GetError() }), + .compute => |d| if (self.passes) |*g| g.compute(d) catch |err| self.print("gpu compute: {t} ({s})", .{ err, c.SDL_GetError() }), + .microphone => self.audio.listen(), + .download => |d| if (self.passes) |*g| g.download(d) catch |err| self.print("gpu download: {t} ({s})", .{ err, c.SDL_GetError() }), + .open_file => |o| self.openFile(o.command, o.pattern), + .run => |line| try lines.append(arena, line), else => gfx.draw(self.renderer, &self.images, command), }; self.audio.endFrame(); + for (lines.items) |line| self.runLine(line, now) catch {}; _ = c.SDL_SetRenderClipRect(self.renderer, null); var extra: std.ArrayList(abi.Command) = .empty; @@ -284,12 +326,72 @@ fn draw(self: *Host, now: u64) !void { if (self.passes) |*g| g.endFrame(); } +/// A file dialog: shown on request, its answer run as a console line from +/// the main loop. SDL may answer on another thread. +const Dialog = struct { + mutex: ?*c.SDL_Mutex = null, + /// Shown and not yet answered (main thread only). + showing: bool = false, + command: [64]u8 = undefined, + command_len: usize = 0, + /// The filter SDL reads until it answers. + pattern: [128:0]u8 = @splat(0), + filter: c.SDL_DialogFileFilter = undefined, + /// "<command> <path>" once answered; empty when cancelled. + answer: [std.fs.max_path_bytes + 64]u8 = undefined, + answer_len: ?usize = null, + + fn deinit(self: *Dialog) void { + if (self.mutex) |m| c.SDL_DestroyMutex(m); + self.mutex = null; + } + + fn callback(userdata: ?*anyopaque, files: [*c]const [*c]const u8, _: c_int) callconv(.c) void { + const self: *Dialog = @ptrCast(@alignCast(userdata orelse return)); + const m = self.mutex orelse return; + c.SDL_LockMutex(m); + defer c.SDL_UnlockMutex(m); + const first = if (files != null and files[0] != null) std.mem.span(files[0]) else ""; + const line = if (first.len == 0) "" else std.mem.print(&self.answer, "{s} {s}", .{ self.command[0..self.command_len], first }) catch ""; + self.answer_len = line.len; + } + + /// The answer's console line, once; empty when cancelled. + fn take(self: *Dialog, out: []u8) ?[]const u8 { + const m = self.mutex orelse return null; + c.SDL_LockMutex(m); + defer c.SDL_UnlockMutex(m); + const len = self.answer_len orelse return null; + self.answer_len = null; + self.showing = false; + @memcpy(out[0..len], self.answer[0..len]); + return out[0..len]; + } +}; + +fn openFile(self: *Host, command: []const u8, pattern: []const u8) void { + const d = &self.dialog; + if (self.options.headless or d.showing) return; + if (command.len > d.command.len or pattern.len > d.pattern.len) return self.print("open_file: command or pattern too long", .{}); + if (d.mutex == null) d.mutex = c.SDL_CreateMutex() orelse return self.print("open_file: {s}", .{c.SDL_GetError()}); + @memcpy(d.command[0..command.len], command); + d.command_len = command.len; + @memset(&d.pattern, 0); + @memcpy(d.pattern[0..pattern.len], pattern); + d.filter = .{ .name = "Files", .pattern = &d.pattern }; + d.showing = true; + c.SDL_ShowOpenFileDialog(Dialog.callback, d, self.window, if (pattern.len > 0) &d.filter else null, @intFromBool(pattern.len > 0), null, false); +} + fn context(self: *Host) abi.Context { return .{ .host = .{ .ctx = self, .vtable = &api }, .input = &self.input, - .arena = self.frame_arena.allocator(), - .gpa = self.session.heap.allocator(), + .scratch = self.frame_arena.allocator(), + .session = self.session.heap.allocator(), + .store = self.store.allocator(), + .session_root = self.session.bytes.ptr, + .store_root = self.store.root(), .commands = &self.commands, }; } @@ -298,7 +400,7 @@ fn context(self: *Host) abi.Context { /// console; while it is open it owns the keyboard, and pointer events over it /// become a cancel, so a half-made click never lands behind it. pub fn route(self: *Host, e: abi.Event) !void { - if (e.kind == .key_down and e.code == @intFromEnum(abi.Key.grave)) { + if (e.kind == .key_down and e.code == @backingInt(abi.Key.grave)) { if (e.flags & abi.Event.repeat != 0) return; self.console.visible = !self.console.visible; return self.pending.append(self.gpa, .{ .kind = .cancel, .time = e.time }); @@ -316,7 +418,7 @@ pub fn route(self: *Host, e: abi.Event) !void { return; }, .key_up => return, - .mouse_move, .mouse_down, .mouse_up, .wheel => if (e.y >= @as(f32, @floatFromInt(self.canvas[1])) - Console.panel_height) { + .mouse_move, .mouse_down, .mouse_up, .wheel, .wheel_horizontal => if (e.y >= @as(f32, @floatFromInt(self.canvas[1])) - Console.panel_height) { return self.pending.append(self.gpa, .{ .kind = .cancel, .time = e.time }); }, else => {}, @@ -339,7 +441,7 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { self.input.now = now; const ctx = self.context(); - return self.sketch.command(&ctx, self.live().ptr, self.session.bytes.ptr, &line); + return self.sketch.command(&ctx, self.live().ptr, &line); } /// Fresh state: declared defaults, then the sketch's `init`. @@ -373,6 +475,8 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { if (info.abi != abi.hash) return error.AbiMismatch; const adoption = try self.state.adopt(self.gpa, info.state, sketch.defaults); const session_reset = try self.session.adopt(self.gpa, info.session, sketch.defaults); + if (session_reset) self.releaseMade(); + const stored = try self.store.adopt(self.gpa, info.store, sketch.defaults); _ = self.info_arena.reset(.retain_capacity); const arena = self.info_arena.allocator(); self.name = try arena.dupe(u8, info.name); @@ -388,6 +492,33 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { } if (!self.state.used(0)) self.resetState(); if (session_reset and self.reloads > 0) self.print("session layout changed: session reset", .{}); + switch (stored) { + .unchanged, .created => {}, + .migrated => |m| self.print("store layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), + } +} + +/// Frees what the old Session made: its handles are gone with it. +fn releaseMade(self: *Host) void { + for (self.made.images.items) |image| { + if (self.passes) |*g| g.forget(&self.images, image); + self.images.free(image); + } + if (self.passes) |*g| for (self.made.buffers.items) |buffer| g.freeBuffer(buffer); + self.made.images.clearRetainingCapacity(); + self.made.buffers.clearRetainingCapacity(); +} + +fn remember(self: *Host, comptime kind: enum { images, buffers }, handle: anytype) @TypeOf(handle) { + if (handle != .none) @field(self.made, @tagName(kind)).append(self.gpa, handle) catch {}; + return handle; +} + +fn forget(list: anytype, handle: anytype) void { + for (list.items, 0..) |h, i| if (h == handle) { + _ = list.swapRemove(i); + return; + }; } fn setCanvas(self: *Host, w: u32, h: u32) void { @@ -421,7 +552,7 @@ fn applyCursor(self: *Host) void { pub fn print(self: *Host, comptime fmt: []const u8, args: anytype) void { var buf: [512]u8 = undefined; - const message = std.fmt.bufPrint(&buf, fmt, args) catch "(message too long)"; + const message = std.mem.print(&buf, fmt, args) catch "(message too long)"; self.console.log(message); // Headless runs are tests: they report through their own results. if (!self.options.headless) std.debug.print("{s}\n", .{message}); @@ -436,18 +567,21 @@ pub fn poll(self: *Host) !void { switch (event.type) { c.SDL_EVENT_QUIT => self.quit = true, c.SDL_EVENT_WINDOW_FOCUS_LOST => try self.route(.{ .kind = .cancel, .time = time }), - c.SDL_EVENT_MOUSE_MOTION => try self.route(.{ .kind = .mouse_move, .time = time, .x = event.motion.x, .y = event.motion.y }), + c.SDL_EVENT_MOUSE_MOTION => try self.route(.{ .kind = .mouse_move, .time = time, .x = event.motion.x, .y = event.motion.y, .flags = held() }), c.SDL_EVENT_MOUSE_BUTTON_DOWN, c.SDL_EVENT_MOUSE_BUTTON_UP => try self.route(.{ .kind = if (event.type == c.SDL_EVENT_MOUSE_BUTTON_DOWN) .mouse_down else .mouse_up, .time = time, .x = event.button.x, .y = event.button.y, .code = event.button.button, + .flags = held(), }), c.SDL_EVENT_MOUSE_WHEEL => { - const delta = event.wheel.y * @as(f32, if (event.wheel.direction == c.SDL_MOUSEWHEEL_FLIPPED) -1 else 1); - if (!std.math.isFinite(delta) or delta == 0) continue; - try self.route(.{ .kind = .wheel, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(delta) }); + const direction: f32 = if (event.wheel.direction == c.SDL_MOUSEWHEEL_FLIPPED) -1 else 1; + const dx = event.wheel.x * direction; + const dy = event.wheel.y * direction; + if (std.math.isFinite(dy) and dy != 0) try self.route(.{ .kind = .wheel, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(dy), .flags = held() }); + if (std.math.isFinite(dx) and dx != 0) try self.route(.{ .kind = .wheel_horizontal, .time = time, .x = event.wheel.mouse_x, .y = event.wheel.mouse_y, .value = @bitCast(dx), .flags = held() }); }, c.SDL_EVENT_KEY_DOWN, c.SDL_EVENT_KEY_UP => try self.route(.{ .kind = if (event.type == c.SDL_EVENT_KEY_DOWN) .key_down else .key_up, @@ -463,6 +597,16 @@ pub fn poll(self: *Host) !void { else => {}, } } + var buf: [@sizeOf(@FieldType(Dialog, "answer"))]u8 = undefined; + if (self.dialog.take(&buf)) |line| if (line.len > 0) { + self.console.print("> {s}", .{line}); + self.runLine(line, c.SDL_GetTicksNS()) catch {}; + }; +} + +/// The modifiers held now, for a pointer event's `flags`. +fn held() u8 { + return abi.Event.withModifiers(modifiers(c.SDL_GetModState())); } fn modifiers(mod: c.SDL_Keymod) u32 { @@ -475,7 +619,7 @@ fn modifiers(mod: c.SDL_Keymod) u32 { } fn consoleKey(e: abi.Event) ?Console.Key { - const key: abi.Key = @enumFromInt(@as(u8, @truncate(e.code))); + const key: abi.Key = @fromBackingInt(@intCast(@as(u8, @truncate(e.code)))); if (key == .u and e.value & abi.Key.Mod.ctrl != 0) return .clear; if (e.flags & abi.Event.repeat != 0 and key.isEnter()) return null; if (key.isEnter()) return .submit; @@ -509,9 +653,12 @@ const api: abi.Host.VTable = .{ .newImage = apiNewImage, .freeImage = apiFreeImage, .gpu = apiGpu, + .newBuffer = apiNewBuffer, + .freeBuffer = apiFreeBuffer, .imageSize = apiImageSize, .loadSound = apiLoadSound, .decodeSound = apiDecodeSound, + .samples = apiSamples, .readFile = apiReadFile, .env = apiEnv, .clock = apiClock, @@ -527,19 +674,38 @@ fn apiLoadImage(ctx: *anyopaque, path: []const u8) abi.Image { return hostOf(ctx).images.load(path); } fn apiDecodeImage(ctx: *anyopaque, png: []const u8) abi.Image { - return hostOf(ctx).images.decode(png); + const self = hostOf(ctx); + return self.remember(.images, self.images.decode(png)); } fn apiNewImage(ctx: *anyopaque, w: u32, h: u32) abi.Image { - return hostOf(ctx).images.new(w, h); + const self = hostOf(ctx); + return self.remember(.images, self.images.new(w, h)); } fn apiFreeImage(ctx: *anyopaque, image: abi.Image) void { const self = hostOf(ctx); + forget(&self.made.images, image); if (self.passes) |*g| g.forget(&self.images, image); self.images.free(image); } fn apiGpu(ctx: *anyopaque) bool { return hostOf(ctx).gpu != null; } +fn apiNewBuffer(ctx: *anyopaque, size: u32) abi.Buffer { + const self = hostOf(ctx); + const g = &(self.passes orelse return .none); + return self.remember(.buffers, g.newBuffer(size) catch |err| blk: { + self.print("gpu buffer: {t} ({s})", .{ err, c.SDL_GetError() }); + break :blk .none; + }); +} +fn apiFreeBuffer(ctx: *anyopaque, buffer: abi.Buffer) void { + const self = hostOf(ctx); + forget(&self.made.buffers, buffer); + if (self.passes) |*g| g.freeBuffer(buffer); +} +fn apiSamples(ctx: *anyopaque, audio: abi.Audio) []const f32 { + return hostOf(ctx).audio.samplesOf(audio); +} fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 { return hostOf(ctx).images.size(image); } diff --git a/src/host/audio.zig b/src/host/audio.zig index 87332b1..aebb909 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -1,9 +1,11 @@ -//! Sound output: decoded clips played by reconciled voices, plus raw streams -//! the sketch fills itself. The audio thread only ever sees SDL's own copies -//! of the samples; sketch code and memory never run on it. +//! Sound: decoded clips played by reconciled voices, raw streams the sketch +//! fills itself, and the microphone, open while declared. The audio thread +//! only ever sees SDL's own copies of the samples; sketch code and memory +//! never run on it. const std = @import("std"); const c = @import("c"); const abi = @import("abi"); +const memory = @import("memory.zig"); pub const sample_rate = 48_000; const spec: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = 1, .freq = sample_rate }; @@ -21,6 +23,11 @@ pub const Audio = struct { streams: std.ArrayList(Stream) = .empty, status: std.ArrayList(abi.Stream) = .empty, scope: Scope = .{}, + /// The default recording device, bound to this stream while declared. + microphone: ?*c.SDL_AudioStream = null, + listening: bool = false, + /// Set when opening failed, so it is tried once per declaration. + deaf: bool = false, const Playing = struct { voice: abi.Voice, cursor: usize = 0, done: bool = false, seen: bool = true }; const Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; @@ -34,6 +41,7 @@ pub const Audio = struct { } pub fn deinit(self: *Audio) void { + if (self.microphone) |m| c.SDL_DestroyAudioStream(m); self.scope.deinit(); for (self.streams.items) |s| c.SDL_DestroyAudioStream(s.out); if (self.voices_out) |s| c.SDL_DestroyAudioStream(s); @@ -70,7 +78,7 @@ pub const Audio = struct { /// `.none` when `wav` does not decode. pub fn decode(self: *Audio, wav: []const u8) abi.Audio { - return self.add(decodeWav(self.gpa, wav) catch return .none); + return self.add(decodeBytes(self.gpa, wav) catch return .none); } fn add(self: *Audio, samples: []f32) abi.Audio { @@ -78,17 +86,52 @@ pub const Audio = struct { self.gpa.free(samples); return .none; }; - return @enumFromInt(self.clips.items.len); + return @fromBackingInt(@intCast(self.clips.items.len)); } - fn samplesOf(self: *Audio, audio: abi.Audio) []const f32 { - const index = @intFromEnum(audio); + pub fn samplesOf(self: *Audio, audio: abi.Audio) []const f32 { + const index = @backingInt(audio); if (index == 0 or index > self.clips.items.len) return &.{}; return self.clips.items[index - 1]; } pub fn beginFrame(self: *Audio) void { for (self.voices.items) |*v| v.seen = false; + self.listening = false; + } + + /// The microphone was declared this frame. + pub fn listen(self: *Audio) void { + self.listening = true; + } + + /// What the microphone heard since the last call, into `arena`. + pub fn record(self: *Audio, arena: std.mem.Allocator) []const f32 { + const m = self.microphone orelse return &.{}; + const available = c.SDL_GetAudioStreamAvailable(m); + if (available <= 0) return &.{}; + const samples = arena.alloc(f32, @as(usize, @intCast(available)) / @sizeOf(f32)) catch return &.{}; + const got = c.SDL_GetAudioStreamData(m, samples.ptr, @intCast(samples.len * @sizeOf(f32))); + if (got <= 0) return &.{}; + return samples[0 .. @as(usize, @intCast(got)) / @sizeOf(f32)]; + } + + /// Opens or closes the microphone to match this frame's declaration. + fn reconcileMicrophone(self: *Audio) void { + if (!self.listening) { + if (self.microphone) |m| c.SDL_DestroyAudioStream(m); + self.microphone = null; + self.deaf = false; + return; + } + if (self.microphone != null or self.deaf) return; + const m = c.SDL_OpenAudioDeviceStream(c.SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, null, null) orelse { + std.log.warn("microphone: {s}", .{c.SDL_GetError()}); + self.deaf = true; + return; + }; + if (!c.SDL_ResumeAudioStreamDevice(m)) std.log.warn("microphone: {s}", .{c.SDL_GetError()}); + self.microphone = m; } /// Reconciles one declared voice with what is playing. @@ -128,8 +171,10 @@ pub const Audio = struct { return &self.streams.items[self.streams.items.len - 1]; } - /// Drops undeclared voices and keeps the voice mix queued ahead. + /// Drops undeclared voices, keeps the voice mix queued ahead, and opens + /// or closes the microphone. pub fn endFrame(self: *Audio) void { + self.reconcileMicrophone(); var i: usize = 0; while (i < self.voices.items.len) { if (self.voices.items[i].seen) i += 1 else _ = self.voices.swapRemove(i); @@ -183,28 +228,23 @@ pub const Audio = struct { } }; -fn decodeWav(gpa: std.mem.Allocator, wav: []const u8) ![]f32 { - const io = c.SDL_IOFromConstMem(wav.ptr, wav.len) orelse return error.OpenWav; - var from: c.SDL_AudioSpec = undefined; - var data: [*c]u8 = null; - var len: u32 = 0; - if (!c.SDL_LoadWAV_IO(io, true, &from, &data, &len)) return error.DecodeWav; - defer c.SDL_free(data); - return convert(gpa, &from, data[0..len]); +/// WAV, FLAC or Ogg Vorbis, by content (see `vendor/codecs.h`). +fn decodeBytes(gpa: std.mem.Allocator, bytes: []const u8) ![]f32 { + var channels: u32 = 0; + var rate: u32 = 0; + var frames: u64 = 0; + const raw = c.sketchbook_decode_audio(bytes.ptr, bytes.len, &channels, &rate, &frames) orelse return error.DecodeAudio; + defer c.sketchbook_free_audio(raw); + if (rate == 0 or rate > 192000 or channels == 0 or channels > 8 or frames == 0 or frames > @as(u64, rate) * 600) return error.UnsupportedSoundFormat; + const from: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = @intCast(channels), .freq = @intCast(rate) }; + return convert(gpa, &from, std.mem.sliceAsBytes(raw[0..@intCast(frames * channels)])); } fn decodeFile(gpa: std.mem.Allocator, path: []const u8) ![]f32 { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const z = try std.fmt.bufPrintZ(&buf, "{s}", .{path}); - var info: c.SF_INFO = std.mem.zeroes(c.SF_INFO); - const file = c.sf_open(z.ptr, c.SFM_READ, &info) orelse return error.OpenSoundFile; - defer _ = c.sf_close(file); - if (info.samplerate <= 0 or info.samplerate > 192000 or info.channels <= 0 or info.channels > 8 or info.frames <= 0 or info.frames > @as(i64, info.samplerate) * 600) return error.UnsupportedSoundFormat; - const raw = try gpa.alloc(f32, @intCast(info.frames * info.channels)); - defer gpa.free(raw); - if (c.sf_readf_float(file, raw.ptr, info.frames) != info.frames) return error.TruncatedAudio; - const from: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = info.channels, .freq = info.samplerate }; - return convert(gpa, &from, std.mem.sliceAsBytes(raw)); + const bytes = memory.mapReadOnly(path); + if (bytes.len == 0) return error.OpenSoundFile; + defer std.posix.munmap(bytes); + return decodeBytes(gpa, bytes); } /// To host format: f32 mono at `sample_rate`, all finite. diff --git a/src/host/fields.zig b/src/host/fields.zig index 215f176..ba8fc03 100644 --- a/src/host/fields.zig +++ b/src/host/fields.zig @@ -100,7 +100,7 @@ test "fields format, parse and compare by kind" { const S = extern struct { mood: enum(u32) { calm, angry = 5 } = .calm, x: i16 = -3, speed: f32 = 1.5, on: bool = false }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const layout = try @import("memory.zig").Layout.copy(arena.allocator(), abi.layout.of(S, .forbidden)); + const layout = try @import("memory.zig").Layout.copy(arena.allocator(), abi.layout.of(S, .state)); var s: S = .{}; const bytes = std.mem.asBytes(&s); try std.testing.expect(matches(layout.find("x").?, bytes, "-3")); diff --git a/src/host/gfx.zig b/src/host/gfx.zig index e00d325..16148a6 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -27,7 +27,7 @@ pub const Images = struct { pub fn load(self: *Images, path: []const u8) abi.Image { if (self.paths.get(path)) |image| return image; var buf: [std.fs.max_path_bytes]u8 = undefined; - const z = std.fmt.bufPrintZ(&buf, "{s}", .{path}) catch return .none; + const z = std.mem.printSentinel(&buf, "{s}", .{path}, 0) catch return .none; const surface = c.SDL_LoadPNG(z.ptr); if (surface == null) std.log.warn("image {s}: {s}", .{ path, c.SDL_GetError() }); const image = self.add(surface); @@ -83,7 +83,7 @@ pub const Images = struct { } pub fn get(self: *Images, image: abi.Image) ?*Entry { - const index = @intFromEnum(image); + const index = @backingInt(image); if (index == 0 or index > self.entries.items.len) return null; return &self.entries.items[index - 1]; } @@ -99,7 +99,7 @@ pub const Images = struct { fn append(self: *Images, entry: Entry) abi.Image { self.entries.append(self.gpa, entry) catch return .none; - return @enumFromInt(self.entries.items.len); + return @fromBackingInt(@intCast(self.entries.items.len)); } fn setup(texture: *c.SDL_Texture) void { @@ -112,6 +112,7 @@ pub const Images = struct { pub fn draw(renderer: *c.SDL_Renderer, images: *Images, command: abi.Command) void { switch (command) { .clear => |color| { + if (@import("builtin").os.tag == .linux) c.sk_text_begin_frame(renderer); setColor(renderer, color); _ = c.SDL_RenderClear(renderer); }, @@ -146,8 +147,10 @@ pub fn draw(renderer: *c.SDL_Renderer, images: *Images, command: abi.Command) vo } } -/// SDL's built-in 8x8 font, `scale` times larger. Stops at the first NUL. +/// Native shaped text on Linux, using a 13px monospace face at scale 1. +/// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops. pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color) void { + if (@import("builtin").os.tag == .linux and c.sk_text_draw(renderer, x, y, scale, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; var buf: [1024]u8 = undefined; const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1); @memcpy(buf[0..len], bytes[0..len]); @@ -157,9 +160,22 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: [] var sy: f32 = 1; _ = c.SDL_GetRenderScale(renderer, &sx, &sy); const s = @max(scale, 1); + // The clip rect scales with the renderer: shrink it to match, to the + // whole pixels around it (so it may let up to `s - 1` pixels more through). + var clip: c.SDL_Rect = undefined; + const clipped = c.SDL_RenderClipEnabled(renderer) and c.SDL_GetRenderClipRect(renderer, &clip); _ = c.SDL_SetRenderScale(renderer, sx * s, sy * s); + if (clipped and s != 1) { + const x0 = @floor(@as(f32, @floatFromInt(clip.x)) / s); + const y0 = @floor(@as(f32, @floatFromInt(clip.y)) / s); + const x1 = @ceil(@as(f32, @floatFromInt(clip.x + clip.w)) / s); + const y1 = @ceil(@as(f32, @floatFromInt(clip.y + clip.h)) / s); + const scaled: c.SDL_Rect = .{ .x = @intFromFloat(x0), .y = @intFromFloat(y0), .w = @intFromFloat(x1 - x0), .h = @intFromFloat(y1 - y0) }; + _ = c.SDL_SetRenderClipRect(renderer, &scaled); + } _ = c.SDL_RenderDebugText(renderer, x / s, y / s, &buf); _ = c.SDL_SetRenderScale(renderer, sx, sy); + if (clipped and s != 1) _ = c.SDL_SetRenderClipRect(renderer, &clip); } fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void { diff --git a/src/host/gpu.zig b/src/host/gpu.zig index 34c817f..9c77205 100644 --- a/src/host/gpu.zig +++ b/src/host/gpu.zig @@ -1,7 +1,7 @@ -//! Runs `abi.gpu` passes with SDL_GPU: the translation from GPU work as data -//! to a real API. Shaders and pipelines are built on first use and cached by -//! the hash of their description; entries a sketch stops using (an edited -//! shader, after a reload) are released after a while. +//! Runs `abi.gpu` passes and dispatches with SDL_GPU: the translation from +//! GPU work as data to a real API. Shaders and pipelines are built on first +//! use and cached by the hash of their description; entries a sketch stops +//! using (an edited shader, after a reload) are released after a while. const Gpu = @This(); const std = @import("std"); const c = @import("c"); @@ -17,6 +17,11 @@ device: *c.SDL_GPUDevice, stencil_format: c.SDL_GPUTextureFormat, shaders: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUShader)) = .empty, pipelines: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUGraphicsPipeline)) = .empty, +kernels: std.AutoHashMapUnmanaged(u64, Cached(c.SDL_GPUComputePipeline)) = .empty, +/// Storage buffers by handle: `Buffer` n is `buffers[n - 1]`, null once freed. +buffers: std.ArrayList(?Storage) = .empty, +/// Uploads to storage buffers go through this, in chunks of its size. +staging: ?*c.SDL_GPUTransferBuffer = null, /// Render targets per image: the resolved texture the renderer shows, and /// multisampled and stencil attachments as passes ask for them. targets: std.AutoHashMapUnmanaged(abi.Image, Target) = .empty, @@ -25,11 +30,89 @@ vertex_capacity: u32 = 0, upload: ?*c.SDL_GPUTransferBuffer = null, upload_capacity: u32 = 0, frame: u64 = 0, +/// Set by `--profile-gpu`: every command is timed alone (see `Profile`). +profile: ?Profile = null, +/// Downloads in flight, oldest first, and transfer buffers to reuse. +downloads: std.ArrayList(Download) = .empty, +spare: std.ArrayList(Transfer) = .empty, + +const Transfer = struct { buffer: *c.SDL_GPUTransferBuffer, size: u32 }; +const Download = struct { key: u32, size: u32, transfer: Transfer, fence: *c.SDL_GPUFence }; + +/// GPU time per labelled command. Each command starts on an idle GPU and is +/// waited for, so its time is its own: the total of a frame is what the +/// GPU spends on it, though the waiting makes the frame itself slower. +pub const Profile = struct { + entries: std.StringArrayHashMapUnmanaged(Entry) = .empty, + arena: std.heap.ArenaAllocator, + + const Entry = struct { count: u64 = 0, total: u64 = 0, worst: u64 = 0 }; + + pub fn init(gpa: std.mem.Allocator) Profile { + return .{ .arena = .init(gpa) }; + } + + pub fn deinit(p: *Profile) void { + p.arena.deinit(); + } + + fn add(p: *Profile, label: []const u8, ns: u64) void { + const a = p.arena.allocator(); + const entry = p.entries.getOrPut(a, label) catch return; + if (!entry.found_existing) { + entry.key_ptr.* = a.dupe(u8, label) catch "?"; + entry.value_ptr.* = .{}; + } + entry.value_ptr.count += 1; + entry.value_ptr.total += ns; + entry.value_ptr.worst = @max(entry.value_ptr.worst, ns); + } + + pub fn report(p: *Profile, frames: u64) void { + std.debug.print("gpu time over {d} frames (ms)\n{s: <24} {s: >7} {s: >9} {s: >9} {s: >11}\n", .{ frames, "command", "count", "mean", "worst", "per frame" }); + var sum: u64 = 0; + var it = p.entries.iterator(); + while (it.next()) |e| { + const v = e.value_ptr.*; + sum += v.total; + std.debug.print("{s: <24} {d: >7} {d: >9.3} {d: >9.3} {d: >11.3}\n", .{ e.key_ptr.*, v.count, ms(v.total) / @as(f64, @floatFromInt(v.count)), ms(v.worst), ms(v.total) / @as(f64, @floatFromInt(@max(frames, 1))) }); + } + std.debug.print("{s: <24} {s: >7} {s: >9} {s: >9} {d: >11.3}\n", .{ "all", "", "", "", ms(sum) / @as(f64, @floatFromInt(@max(frames, 1))) }); + } + + fn ms(ns: u64) f64 { + return @as(f64, @floatFromInt(ns)) / std.time.ns_per_ms; + } +}; + +/// Submits `cmd`; when profiling, waits for it and books its time from +/// `start` (taken on an idle GPU, before recording) under `label`. +fn submit(g: *Gpu, cmd: *c.SDL_GPUCommandBuffer, label: []const u8, start: u64) !void { + const p = &(g.profile orelse { + if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; + return; + }); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + defer c.SDL_ReleaseGPUFence(g.device, fence); + _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); + p.add(label, c.SDL_GetTicksNS() - start); +} + +/// When profiling: waits for the GPU to be idle, then the time to start from. +fn startTiming(g: *Gpu) u64 { + if (g.profile != null) _ = c.SDL_WaitForGPUIdle(g.device); + return c.SDL_GetTicksNS(); +} + +/// The most bytes one buffer upload stages at a time. +const staging_size = 4 << 20; fn Cached(comptime T: type) type { return struct { object: *T, used: u64 }; } +const Storage = struct { buffer: *c.SDL_GPUBuffer, size: u32 }; + const Target = struct { width: u32, height: u32, @@ -50,27 +133,49 @@ pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu { } pub fn deinit(g: *Gpu, images: *gfx.Images) void { + if (g.profile) |*p| { + p.report(g.frame); + p.deinit(); + } var shaders = g.shaders.valueIterator(); while (shaders.next()) |s| c.SDL_ReleaseGPUShader(g.device, s.object); var pipelines = g.pipelines.valueIterator(); while (pipelines.next()) |p| c.SDL_ReleaseGPUGraphicsPipeline(g.device, p.object); + var kernels = g.kernels.valueIterator(); + while (kernels.next()) |k| c.SDL_ReleaseGPUComputePipeline(g.device, k.object); + for (g.buffers.items) |entry| if (entry) |b| c.SDL_ReleaseGPUBuffer(g.device, b.buffer); + for (g.downloads.items) |d| { + _ = c.SDL_WaitForGPUFences(g.device, true, &d.fence, 1); + c.SDL_ReleaseGPUFence(g.device, d.fence); + c.SDL_ReleaseGPUTransferBuffer(g.device, d.transfer.buffer); + } + for (g.spare.items) |t| c.SDL_ReleaseGPUTransferBuffer(g.device, t.buffer); + g.downloads.deinit(g.gpa); + g.spare.deinit(g.gpa); + if (g.staging) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); var targets = g.targets.iterator(); while (targets.next()) |entry| g.releaseTarget(images, entry.key_ptr.*, entry.value_ptr.*); if (g.vertices) |b| c.SDL_ReleaseGPUBuffer(g.device, b); if (g.upload) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); g.shaders.deinit(g.gpa); g.pipelines.deinit(g.gpa); + g.kernels.deinit(g.gpa); + g.buffers.deinit(g.gpa); g.targets.deinit(g.gpa); } /// Releases what nothing has used for a while. pub fn endFrame(g: *Gpu) void { g.frame += 1; - inline for (.{ &g.pipelines, &g.shaders }) |map| { + inline for (.{ &g.pipelines, &g.shaders, &g.kernels }) |map| { var it = map.iterator(); while (it.next()) |entry| { if (g.frame - entry.value_ptr.used < keep_frames) continue; - if (@TypeOf(map) == @TypeOf(&g.pipelines)) c.SDL_ReleaseGPUGraphicsPipeline(g.device, entry.value_ptr.object) else c.SDL_ReleaseGPUShader(g.device, entry.value_ptr.object); + switch (@TypeOf(map)) { + @TypeOf(&g.pipelines) => c.SDL_ReleaseGPUGraphicsPipeline(g.device, entry.value_ptr.object), + @TypeOf(&g.kernels) => c.SDL_ReleaseGPUComputePipeline(g.device, entry.value_ptr.object), + else => c.SDL_ReleaseGPUShader(g.device, entry.value_ptr.object), + } map.removeByPtr(entry.key_ptr); // The iterator is invalid after a removal: the rest waits a frame. break; @@ -83,6 +188,7 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { const samples = g.supportedSamples(pass.samples); const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + const start = g.startTiming(); // Every draw's vertices go up in one copy. var total: u32 = 0; for (pass.draws) |d| total += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); @@ -137,7 +243,10 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_BindGPUGraphicsPipeline(rp, built); bound = built; } - c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); + if (d.vertices.len > 0) c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = offset }, 1); + var bound_storage: [8]*c.SDL_GPUBuffer = undefined; + if (d.vertex_storage.len > 0) c.SDL_BindGPUVertexStorageBuffers(rp, 0, &bound_storage, g.resolve(d.vertex_storage, &bound_storage) catch continue); + if (d.fragment_storage.len > 0) c.SDL_BindGPUFragmentStorageBuffers(rp, 0, &bound_storage, g.resolve(d.fragment_storage, &bound_storage) catch continue); if (d.vertex_uniforms.len > 0) c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len)); if (d.fragment_uniforms.len > 0) c.SDL_PushGPUFragmentUniformData(cmd, 0, d.fragment_uniforms.ptr, @intCast(d.fragment_uniforms.len)); if (pass.stencil) c.SDL_SetGPUStencilReference(rp, d.stencil_reference); @@ -147,7 +256,171 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0); } c.SDL_EndGPURenderPass(rp); + try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); +} + +/// Runs one dispatch in a compute pass of its own. +pub fn compute(g: *Gpu, d: abi.gpu.Dispatch) !void { + if (d.groups[0] == 0 or d.groups[1] == 0 or d.groups[2] == 0) return; + const built = try g.kernel(d.kernel); + const start = g.startTiming(); + var readonly: [8]*c.SDL_GPUBuffer = undefined; + const readonly_count = try g.resolve(d.readonly, &readonly); + var readwrite: [8]*c.SDL_GPUBuffer = undefined; + const readwrite_count = try g.resolve(d.readwrite, &readwrite); + var bindings: [8]c.SDL_GPUStorageBufferReadWriteBinding = undefined; + for (readwrite[0..readwrite_count], bindings[0..readwrite_count]) |b, *binding| binding.* = std.mem.zeroInit(c.SDL_GPUStorageBufferReadWriteBinding, .{ .buffer = b }); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const pass = c.SDL_BeginGPUComputePass(cmd, null, 0, &bindings, readwrite_count) orelse { + _ = c.SDL_CancelGPUCommandBuffer(cmd); + return error.GpuComputePass; + }; + c.SDL_BindGPUComputePipeline(pass, built); + if (readonly_count > 0) c.SDL_BindGPUComputeStorageBuffers(pass, 0, &readonly, readonly_count); + if (d.uniforms.len > 0) c.SDL_PushGPUComputeUniformData(cmd, 0, d.uniforms.ptr, @intCast(d.uniforms.len)); + c.SDL_DispatchGPUCompute(pass, d.groups[0], d.groups[1], d.groups[2]); + c.SDL_EndGPUComputePass(pass); + try g.submit(cmd, if (d.label.len > 0) d.label else "compute", start); +} + +/// A zeroed storage buffer, readable by every stage and writable by compute. +pub fn newBuffer(g: *Gpu, size: u32) !abi.Buffer { + if (size == 0) return error.EmptyBuffer; + const usage = c.SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ | c.SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ | c.SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE; + const buffer = c.SDL_CreateGPUBuffer(g.device, &.{ .usage = usage, .size = size, .props = 0 }) orelse return error.GpuBuffer; + errdefer c.SDL_ReleaseGPUBuffer(g.device, buffer); + try g.buffers.append(g.gpa, .{ .buffer = buffer, .size = size }); + const handle: abi.Buffer = @fromBackingInt(@intCast(g.buffers.items.len)); + // Vulkan leaves new memory undefined; sketches get zeroes, copied from + // one zeroed staging block as many times as it takes. + const staging = try g.stagingBuffer(); + const block: u32 = @min(size, staging_size); + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, staging, true) orelse return error.GpuMap); + @memset(mapped[0..block], 0); + c.SDL_UnmapGPUTransferBuffer(g.device, staging); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + var offset: u32 = 0; + while (offset < size) : (offset += block) { + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = staging, .offset = 0 }, &.{ .buffer = buffer, .offset = offset, .size = @min(block, size - offset) }, false); + } + c.SDL_EndGPUCopyPass(copy); if (!c.SDL_SubmitGPUCommandBuffer(cmd)) return error.GpuSubmit; + return handle; +} + +fn stagingBuffer(g: *Gpu) !*c.SDL_GPUTransferBuffer { + if (g.staging == null) g.staging = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = staging_size, .props = 0 }) orelse return error.GpuBuffer; + return g.staging.?; +} + +pub fn freeBuffer(g: *Gpu, handle: abi.Buffer) void { + const index = @backingInt(handle); + if (index == 0 or index > g.buffers.items.len) return; + const entry = &g.buffers.items[index - 1]; + if (entry.*) |b| c.SDL_ReleaseGPUBuffer(g.device, b.buffer); + entry.* = null; +} + +/// Copies `bytes` into a buffer at `offset`, through the staging buffer. +pub fn write(g: *Gpu, handle: abi.Buffer, offset: u32, bytes: []const u8) !void { + const target = g.storage(handle) orelse return error.UnknownBuffer; + if (@as(u64, offset) + bytes.len > target.size) return error.UploadOutOfBounds; + const staging = try g.stagingBuffer(); + var done: usize = 0; + while (done < bytes.len) { + const chunk = bytes[done..][0..@min(staging_size, bytes.len - done)]; + const start = g.startTiming(); + // Cycling: a chunk still in flight keeps its own memory. + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, staging, true) orelse return error.GpuMap); + @memcpy(mapped[0..chunk.len], chunk); + c.SDL_UnmapGPUTransferBuffer(g.device, staging); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = staging, .offset = 0 }, &.{ .buffer = target.buffer, .offset = offset + @as(u32, @intCast(done)), .size = @intCast(chunk.len) }, false); + c.SDL_EndGPUCopyPass(copy); + try g.submit(cmd, "upload", start); + done += chunk.len; + } +} + +/// Starts copying `size` bytes of a buffer from `offset` back to the CPU; +/// `collect` hands them over once the GPU is done, a frame or more later. +pub fn download(g: *Gpu, d: @FieldType(abi.Command, "download")) !void { + const source = g.storage(d.buffer) orelse return error.UnknownBuffer; + if (d.size == 0 or @as(u64, d.offset) + d.size > source.size) return error.DownloadOutOfBounds; + const transfer = for (g.spare.items, 0..) |t, i| { + if (t.size >= d.size) break g.spare.swapRemove(i); + } else Transfer{ + .buffer = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_DOWNLOAD, .size = d.size, .props = 0 }) orelse return error.GpuBuffer, + .size = d.size, + }; + errdefer g.spare.append(g.gpa, transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, transfer.buffer); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_DownloadFromGPUBuffer(copy, &.{ .buffer = source.buffer, .offset = d.offset, .size = d.size }, &.{ .transfer_buffer = transfer.buffer, .offset = 0 }); + c.SDL_EndGPUCopyPass(copy); + const fence = c.SDL_SubmitGPUCommandBufferAndAcquireFence(cmd) orelse return error.GpuSubmit; + g.downloads.append(g.gpa, .{ .key = d.key, .size = d.size, .transfer = transfer, .fence = fence }) catch |err| { + _ = c.SDL_WaitForGPUFences(g.device, true, &fence, 1); + c.SDL_ReleaseGPUFence(g.device, fence); + return err; + }; +} + +/// The downloads the GPU has finished, copied into `arena`, in the order +/// they were asked for. +pub fn collect(g: *Gpu, arena: std.mem.Allocator) ![]const abi.Download { + var done: std.ArrayList(abi.Download) = .empty; + while (g.downloads.items.len > 0) { + const d = g.downloads.items[0]; + if (!c.SDL_QueryGPUFence(g.device, d.fence)) break; + _ = g.downloads.orderedRemove(0); + c.SDL_ReleaseGPUFence(g.device, d.fence); + defer g.spare.append(g.gpa, d.transfer) catch c.SDL_ReleaseGPUTransferBuffer(g.device, d.transfer.buffer); + const mapped: [*]const u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, d.transfer.buffer, false) orelse return error.GpuMap); + defer c.SDL_UnmapGPUTransferBuffer(g.device, d.transfer.buffer); + try done.append(arena, .{ .key = d.key, .bytes = try arena.dupe(u8, mapped[0..d.size]) }); + } + return done.items; +} + +fn storage(g: *Gpu, handle: abi.Buffer) ?Storage { + const index = @backingInt(handle); + if (index == 0 or index > g.buffers.items.len) return null; + return g.buffers.items[index - 1]; +} + +/// The SDL buffers behind `handles`, into `out`; returns how many. +fn resolve(g: *Gpu, handles: []const abi.Buffer, out: []*c.SDL_GPUBuffer) !u32 { + if (handles.len > out.len) return error.TooManyBuffers; + for (handles, out[0..handles.len]) |handle, *b| b.* = (g.storage(handle) orelse return error.UnknownBuffer).buffer; + return @intCast(handles.len); +} + +fn kernel(g: *Gpu, k: abi.gpu.Kernel) !*c.SDL_GPUComputePipeline { + var h = std.hash.Wyhash.init(0); + h.update(k.code); + inline for (.{ k.readonly_buffers, k.readwrite_buffers, k.uniform_buffers, k.threads[0], k.threads[1], k.threads[2] }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + const key = h.final(); + const entry = try g.kernels.getOrPut(g.gpa, key); + if (!entry.found_existing) { + errdefer _ = g.kernels.remove(key); + var info: c.SDL_GPUComputePipelineCreateInfo = std.mem.zeroes(c.SDL_GPUComputePipelineCreateInfo); + info.code_size = k.code.len; + info.code = k.code.ptr; + info.entrypoint = "main"; + info.format = c.SDL_GPU_SHADERFORMAT_SPIRV; + info.num_readonly_storage_buffers = k.readonly_buffers; + info.num_readwrite_storage_buffers = k.readwrite_buffers; + info.num_uniform_buffers = k.uniform_buffers; + info.threadcount_x = k.threads[0]; + info.threadcount_y = k.threads[1]; + info.threadcount_z = k.threads[2]; + entry.value_ptr.object = c.SDL_CreateGPUComputePipeline(g.device, &info) orelse return error.GpuComputePipeline; + } + entry.value_ptr.used = g.frame; + return entry.value_ptr.object; } fn supportedSamples(g: *Gpu, wanted: u8) u8 { @@ -236,21 +509,22 @@ fn reserve(g: *Gpu, bytes: u32) !void { fn shader(g: *Gpu, s: abi.gpu.Shader, stage: c.SDL_GPUShaderStage) !struct { *c.SDL_GPUShader, u64 } { var h = std.hash.Wyhash.init(stage); - h.update(s.spirv); + h.update(s.code); h.update(std.mem.asBytes(&s.uniform_buffers)); + h.update(std.mem.asBytes(&s.storage_buffers)); const key = h.final(); const entry = try g.shaders.getOrPut(g.gpa, key); if (!entry.found_existing) { errdefer _ = g.shaders.remove(key); entry.value_ptr.object = c.SDL_CreateGPUShader(g.device, &.{ - .code_size = s.spirv.len, - .code = s.spirv.ptr, + .code_size = s.code.len, + .code = s.code.ptr, .entrypoint = "main", .format = c.SDL_GPU_SHADERFORMAT_SPIRV, .stage = stage, .num_samplers = 0, .num_storage_textures = 0, - .num_storage_buffers = 0, + .num_storage_buffers = s.storage_buffers, .num_uniform_buffers = s.uniform_buffers, .props = 0, }) orelse return error.GpuShader; @@ -264,9 +538,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP const fragment, const fragment_key = try g.shader(p.fragment, c.SDL_GPU_SHADERSTAGE_FRAGMENT); // Field by field: no padding bytes in the key. var h = std.hash.Wyhash.init(vertex_key ^ (fragment_key *% 31)); - inline for (.{ p.stride, @intFromEnum(p.primitive), @intFromEnum(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); - for (p.attributes) |a| inline for (.{ a.location, @intFromEnum(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); - if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @intFromEnum(face.compare), @intFromEnum(face.pass), @intFromEnum(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + inline for (.{ p.stride, @backingInt(p.primitive), @backingInt(p.blend), @intFromBool(p.write_color), samples, @intFromBool(stencil) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + for (p.attributes) |a| inline for (.{ a.location, @backingInt(a.format), a.offset }) |v| h.update(std.mem.asBytes(&@as(u32, v))); + if (p.stencil) |st| inline for (.{ st.front, st.back }) |face| inline for (.{ @backingInt(face.compare), @backingInt(face.pass), @backingInt(face.fail) }) |v| h.update(std.mem.asBytes(&@as(u32, v))); const key = h.final(); const entry = try g.pipelines.getOrPut(g.gpa, key); if (!entry.found_existing) { @@ -310,7 +584,8 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP info.fragment_shader = fragment; info.vertex_input_state = .{ .vertex_buffer_descriptions = &.{ .slot = 0, .pitch = p.stride, .input_rate = c.SDL_GPU_VERTEXINPUTRATE_VERTEX, .instance_step_rate = 0 }, - .num_vertex_buffers = 1, + // No attributes: the vertex shader makes its own vertices. + .num_vertex_buffers = @intFromBool(count > 0), .vertex_attributes = &attributes, .num_vertex_attributes = @intCast(count), }; @@ -338,9 +613,9 @@ fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GP fn stencilFace(face: abi.gpu.StencilFace) c.SDL_GPUStencilOpState { // Both enums follow SDL's order, after its INVALID. return .{ - .compare_op = @as(u32, @intFromEnum(face.compare)) + 1, - .pass_op = @as(u32, @intFromEnum(face.pass)) + 1, - .fail_op = @as(u32, @intFromEnum(face.fail)) + 1, + .compare_op = @as(u32, @backingInt(face.compare)) + 1, + .pass_op = @as(u32, @backingInt(face.pass)) + 1, + .fail_op = @as(u32, @backingInt(face.fail)) + 1, .depth_fail_op = c.SDL_GPU_STENCILOP_KEEP, }; } diff --git a/src/host/library.zig b/src/host/library.zig index 6c9338f..6958eec 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -17,10 +17,10 @@ pub const Library = struct { const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary; errdefer c.SDL_UnloadObject(object); var sketch: abi.Sketch = undefined; - inline for (@typeInfo(abi.Sketch).@"struct".fields) |field| { - const symbol = @field(abi.Sketch.symbols, field.name); + inline for (comptime std.meta.fieldNames(abi.Sketch)) |name| { + const symbol = @field(abi.Sketch.symbols, name); const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint; - @field(sketch, field.name) = @ptrCast(address); + @field(sketch, name) = @ptrCast(address); } if (sketch.info().abi != abi.hash) return error.AbiMismatch; return .{ .object = object, .sketch = sketch }; @@ -29,7 +29,7 @@ pub const Library = struct { /// Loads `source` through a uniquely named copy in `scratch_dir`. pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { var buf: [std.fs.max_path_bytes]u8 = undefined; - const copy = try std.fmt.bufPrintZ(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }); + const copy = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); const cwd = std.Io.Dir.cwd(); try cwd.copyFile(source, cwd, copy, io, .{}); defer cwd.deleteFile(io, copy) catch {}; @@ -53,7 +53,7 @@ pub const Watch = struct { errdefer _ = linux.close(fd); var buf: [std.fs.max_path_bytes]u8 = undefined; const dir = std.fs.path.dirname(path) orelse "."; - const dir_z = try std.fmt.bufPrintZ(&buf, "{s}", .{dir}); + const dir_z = try std.mem.printSentinel(&buf, "{s}", .{dir}, 0); if (@as(isize, @bitCast(linux.inotify_add_watch(fd, dir_z, linux.IN.MOVED_TO | linux.IN.CLOSE_WRITE))) < 0) return error.WatchFailed; return .{ .fd = fd, .name = std.fs.path.basename(path) }; } diff --git a/src/host/memory.zig b/src/host/memory.zig index dd9f632..fc44e50 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -4,6 +4,12 @@ //! slots, plus a header describing the layout, so the state outlives restarts, //! a probe can read it from another process, and a reload with a changed //! layout can carry the old values over field by field. +//! +//! `StoreFile`: a heap in a file, mapped at the same address in every run, +//! so what it holds may point into it: the sketch's `Store` and everything +//! allocated for it outlive restarts. +//! +//! `Session`: this process's memory, kept across reloads. const std = @import("std"); const posix = std.posix; const linux = std.os.linux; @@ -197,7 +203,7 @@ pub const StateFile = struct { try self.resize(stride); const h = self.header(); h.* = .{ .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; - try self.writeLayout(); + try writeLayout(self.map[0..header_len], self.layout); var total: Migration = .{}; for (old_slots, 0..) |maybe_old, i| { @@ -232,33 +238,34 @@ pub const StateFile = struct { } self.map = try mapFile(self.fd, len, .{ .READ = true, .WRITE = true }); } +}; - fn writeLayout(self: *StateFile) !void { - const h = self.header(); - const fields: [*]StoredField = @ptrCast(@alignCast(self.map.ptr + @sizeOf(Header))); - const strings_start = @sizeOf(Header) + self.layout.fields.len * @sizeOf(StoredField); - var strings: usize = 0; - for (self.layout.fields, 0..) |leaf, i| { - if (strings_start + strings + leaf.path.len + leaf.tags.len > header_len) return error.LayoutTooLarge; - const base = strings_start + strings; - @memcpy(self.map[base..][0..leaf.path.len], leaf.path); - @memcpy(self.map[base + leaf.path.len ..][0..leaf.tags.len], leaf.tags); - fields[i] = .{ - .offset = leaf.offset, - .size = leaf.size, - .hash = leaf.hash, - .kind = leaf.kind, - .path = @intCast(strings), - .path_len = @intCast(leaf.path.len), - .tags = @intCast(strings + leaf.path.len), - .tags_len = @intCast(leaf.tags.len), - }; - strings += leaf.path.len + leaf.tags.len; - } - h.field_count = @intCast(self.layout.fields.len); - h.strings_len = @intCast(strings); +/// Writes `layout`'s leaves after the `Header` at the start of `region`. +fn writeLayout(region: []u8, layout: Layout) !void { + const h: *Header = @ptrCast(@alignCast(region.ptr)); + const fields: [*]StoredField = @ptrCast(@alignCast(region.ptr + @sizeOf(Header))); + const strings_start = @sizeOf(Header) + layout.fields.len * @sizeOf(StoredField); + var strings: usize = 0; + for (layout.fields, 0..) |leaf, i| { + if (strings_start + strings + leaf.path.len + leaf.tags.len > region.len) return error.LayoutTooLarge; + const base = strings_start + strings; + @memcpy(region[base..][0..leaf.path.len], leaf.path); + @memcpy(region[base + leaf.path.len ..][0..leaf.tags.len], leaf.tags); + fields[i] = .{ + .offset = leaf.offset, + .size = leaf.size, + .hash = leaf.hash, + .kind = leaf.kind, + .path = @intCast(strings), + .path_len = @intCast(leaf.path.len), + .tags = @intCast(strings + leaf.path.len), + .tags_len = @intCast(leaf.tags.len), + }; + strings += leaf.path.len + leaf.tags.len; } -}; + h.field_count = @intCast(layout.fields.len); + h.strings_len = @intCast(strings); +} /// The layout stored in a state file's header, or null if it has none. pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout { @@ -306,15 +313,211 @@ pub fn mapReadOnly(path: []const u8) []align(page) const u8 { return posix.mmap(null, @intCast(len), .{ .READ = true }, .{ .TYPE = .SHARED }, fd, 0) catch &.{}; } +/// Where every store is mapped, in every process: far from where Linux puts +/// executables (0x55...) and mappings (0x7f...). +pub const store_base: usize = 0x2000_0000_0000; +/// The most a store can grow to. Only reserved: pages exist as they are used. +pub const store_reserve: usize = 64 << 30; +pub const store_magic = "SKSTORE1".*; +/// The layout region is a state file header; the allocator's own record +/// follows, then the heap. +const meta_offset = header_len; +const heap_offset = 2 * header_len; + +/// A heap in a file, with the sketch's `Store` at its root. Mapped at +/// `store_base` in every run, so pointers it holds into itself stay valid +/// across restarts. Not crash-safe: a crash in the middle of an allocation +/// can leave it inconsistent (`--fresh` starts over). +/// +/// Blocks come in power-of-two sizes, each class with a free list threaded +/// through the free blocks, so growing a list by doubling reuses what it +/// frees. The file grows by mapping more of it in place: nothing moves. +pub const StoreFile = struct { + fd: posix.fd_t, + /// Bytes of the file mapped at `store_base`. + len: usize, + /// The root's layout, owned by `arena`. + layout: Layout, + arena: std.heap.ArenaAllocator, + + const Meta = extern struct { + magic: [8]u8 = store_magic, + base: u64 = store_base, + /// Offset of the first byte never handed out. + top: u64 = heap_offset, + /// Offset of the sketch's Store, or 0 before the first adopt. + root: u64 = 0, + /// Offsets of the first free block of each size class (2^i bytes), 0 + /// when none; a free block's first 8 bytes hold the next one's. + free: [64]u64 = @splat(0), + }; + + pub fn open(gpa: std.mem.Allocator, path: []const u8, fresh: bool) !StoreFile { + const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); + errdefer _ = posix.system.close(fd); + // The same file mapped by two processes would be two heaps writing + // over each other. + if (linux.errno(linux.flock(fd, posix.LOCK.EX | posix.LOCK.NB)) != .SUCCESS) return error.StoreInUse; + const reserved = posix.mmap(@ptrFromInt(store_base), store_reserve, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED_NOREPLACE = true }, -1, 0) catch return error.StoreAddressTaken; + // Old kernels treat FIXED_NOREPLACE as a hint. + if (@intFromPtr(reserved.ptr) != store_base) { + posix.munmap(reserved); + return error.StoreAddressTaken; + } + errdefer posix.munmap(reserved); + var self: StoreFile = .{ .fd = fd, .len = 0, .layout = .{}, .arena = .init(gpa) }; + errdefer self.arena.deinit(); + const size: usize = @intCast(try fileSize(fd)); + if (!fresh and size >= heap_offset) { + try self.grow(size); + const m = self.meta(); + if (std.mem.eql(u8, &m.magic, &store_magic) and m.base == store_base and m.top <= size) { + self.layout = try readLayout(self.arena.allocator(), self.region()) orelse .{}; + return self; + } + } + try self.wipe(); + return self; + } + + pub fn close(self: *StoreFile) void { + posix.munmap(@as([*]align(page) u8, @ptrFromInt(store_base))[0..store_reserve]); + _ = posix.system.close(self.fd); + self.arena.deinit(); + } + + /// Empties the store: no root, nothing allocated. + fn wipe(self: *StoreFile) !void { + // Mapped pages past the new end would fault: map the reserve back. + if (self.len > heap_offset) { + _ = try posix.mmap(@ptrFromInt(store_base + heap_offset), self.len - heap_offset, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED = true }, -1, 0); + self.len = heap_offset; + } + try truncate(self.fd, heap_offset); + try self.grow(heap_offset); + @memset(self.region(), 0); + self.header().* = .{ .magic = store_magic, .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 }; + self.meta().* = .{}; + self.layout = .{}; + } + + /// Maps the file up to `need` bytes, growing it when it is shorter. + fn grow(self: *StoreFile, need: usize) !void { + if (need <= self.len) return; + const len = std.mem.alignForward(usize, @max(need, self.len * 2, 1 << 20), page); + if (len > store_reserve) return error.StoreFull; + if (try fileSize(self.fd) < len) try truncate(self.fd, len); + _ = try posix.mmap(@ptrFromInt(store_base + self.len), len - self.len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED, .FIXED = true }, self.fd, self.len); + self.len = len; + } + + fn region(_: *StoreFile) []u8 { + return @as([*]u8, @ptrFromInt(store_base))[0..header_len]; + } + fn header(_: *StoreFile) *Header { + return @ptrFromInt(store_base); + } + fn meta(_: *StoreFile) *Meta { + return @ptrFromInt(store_base + meta_offset); + } + + /// The sketch's Store. + pub fn root(self: *StoreFile) [*]u8 { + const offset = self.meta().root; + return @ptrFromInt(store_base + if (offset == 0) heap_offset else offset); + } + + pub const Adoption = StateFile.Adoption; + + /// Makes the root hold `new`'s layout: the new defaults, then every field + /// whose path and type are unchanged, copied over. Pointer fields keep + /// pointing where they did, since the store never moves. + pub fn adopt(self: *StoreFile, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Adoption { + const m = self.meta(); + if (m.root != 0 and self.layout.hash == new.hash and self.layout.size == new.size) return .unchanged; + const a = self.allocator(); + const alignment: std.mem.Alignment = .fromByteUnits(@max(new.alignment, 1)); + const fresh = a.rawAlloc(@max(new.size, 1), alignment, @returnAddress()) orelse return error.StoreFull; + @memset(fresh[0..@max(new.size, 1)], 0); + defaults(.store, fresh); + + var kept = self.arena; + defer kept.deinit(); + self.arena = .init(gpa); + const old_layout = self.layout; + self.layout = try Layout.copy(self.arena.allocator(), new); + var result: Adoption = .created; + if (m.root != 0) { + const old: [*]u8 = @ptrFromInt(store_base + m.root); + result = .{ .migrated = migrate(old_layout, old[0..old_layout.size], self.layout, fresh[0..new.size]) }; + a.rawFree(old[0..@max(old_layout.size, 1)], .fromByteUnits(@max(old_layout.alignment, 1)), @returnAddress()); + } + self.header().* = .{ .magic = store_magic, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = 0, .used = 1, .field_count = 0, .strings_len = 0 }; + try writeLayout(self.region(), self.layout); + m.root = @intFromPtr(fresh) - store_base; + return result; + } + + pub fn allocator(self: *StoreFile) std.mem.Allocator { + return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + fn class(len: usize, alignment: std.mem.Alignment) u6 { + return @intCast(std.math.log2_int_ceil(usize, @max(len, alignment.toByteUnits(), 16))); + } + + fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 { + const self: *StoreFile = @ptrCast(@alignCast(ctx)); + const m = self.meta(); + const c = class(len, alignment); + const size = @as(usize, 1) << c; + if (m.free[c] != 0) { + const block: [*]u8 = @ptrFromInt(store_base + m.free[c]); + if (alignment.check(@intFromPtr(block))) { + m.free[c] = @as(*const u64, @ptrCast(@alignCast(block))).*; + return block; + } + } + const offset = std.mem.alignForward(usize, m.top, @max(@min(size, page), alignment.toByteUnits())); + self.grow(offset + size) catch return null; + m.top = offset + size; + return @ptrFromInt(store_base + offset); + } + + /// In place within the block's size class. + fn resize(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, _: usize) bool { + return class(memory.len, alignment) == class(new_len, alignment); + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) ?[*]u8 { + return if (resize(ctx, memory, alignment, new_len, ret)) memory.ptr else null; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, _: usize) void { + const self: *StoreFile = @ptrCast(@alignCast(ctx)); + const m = self.meta(); + const c = class(memory.len, alignment); + @as(*u64, @ptrCast(@alignCast(memory.ptr))).* = m.free[c]; + m.free[c] = @intFromPtr(memory.ptr) - store_base; + } +}; + +fn truncate(fd: posix.fd_t, len: usize) !void { + switch (posix.errno(posix.system.ftruncate(fd, @intCast(len)))) { + .SUCCESS => {}, + else => |err| return posix.unexpectedErrno(err), + } +} + /// Session memory: survives reloads while its layout hash is unchanged. -/// `heap` is released with it. +/// Everything allocated from `heap` is released with it. pub const Session = struct { bytes: []align(16) u8 = &.{}, hash: u64 = 0, - heap: std.heap.ArenaAllocator, + heap: Heap, pub fn init(gpa: std.mem.Allocator) Session { - return .{ .heap = .init(gpa) }; + return .{ .heap = .{ .gpa = gpa } }; } pub fn deinit(self: *Session, gpa: std.mem.Allocator) void { @@ -328,7 +531,7 @@ pub const Session = struct { if (layout.alignment > 16) return error.SessionOveraligned; gpa.free(self.bytes); self.bytes = &.{}; - _ = self.heap.reset(.free_all); + self.heap.reset(); self.bytes = try gpa.alignedAlloc(u8, .@"16", @max(layout.size, 1)); @memset(self.bytes, 0); defaults(.session, self.bytes.ptr); @@ -337,13 +540,88 @@ pub const Session = struct { } }; +/// The session's allocator: general purpose, so memory frees one allocation +/// at a time (a growing list does not leave its old copies behind), and it +/// remembers what is still live, so a session reset releases all of it. +pub const Heap = struct { + gpa: std.mem.Allocator, + live: std.AutoHashMapUnmanaged(usize, Block) = .empty, + + const Block = struct { len: usize, alignment: std.mem.Alignment }; + + pub fn allocator(self: *Heap) std.mem.Allocator { + return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; + } + + /// Releases every live allocation. + pub fn reset(self: *Heap) void { + var it = self.live.iterator(); + while (it.next()) |entry| { + const memory: [*]u8 = @ptrFromInt(entry.key_ptr.*); + self.gpa.rawFree(memory[0..entry.value_ptr.len], entry.value_ptr.alignment, @returnAddress()); + } + self.live.clearRetainingCapacity(); + } + + pub fn deinit(self: *Heap) void { + self.reset(); + self.live.deinit(self.gpa); + } + + fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, ret: usize) ?[*]u8 { + const self: *Heap = @ptrCast(@alignCast(ctx)); + self.live.ensureUnusedCapacity(self.gpa, 1) catch return null; + const memory = self.gpa.rawAlloc(len, alignment, ret) orelse return null; + self.live.putAssumeCapacity(@intFromPtr(memory), .{ .len = len, .alignment = alignment }); + return memory; + } + + fn resize(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) bool { + const self: *Heap = @ptrCast(@alignCast(ctx)); + if (!self.gpa.rawResize(memory, alignment, new_len, ret)) return false; + self.live.getPtr(@intFromPtr(memory.ptr)).?.len = new_len; + return true; + } + + fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) ?[*]u8 { + const self: *Heap = @ptrCast(@alignCast(ctx)); + self.live.ensureUnusedCapacity(self.gpa, 1) catch return null; + const moved = self.gpa.rawRemap(memory, alignment, new_len, ret) orelse return null; + _ = self.live.remove(@intFromPtr(memory.ptr)); + self.live.putAssumeCapacity(@intFromPtr(moved), .{ .len = new_len, .alignment = alignment }); + return moved; + } + + fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, ret: usize) void { + const self: *Heap = @ptrCast(@alignCast(ctx)); + _ = self.live.remove(@intFromPtr(memory.ptr)); + self.gpa.rawFree(memory, alignment, ret); + } +}; + +test "the session heap frees one allocation at a time, and the rest on reset" { + var heap: Heap = .{ .gpa = std.testing.allocator }; + defer heap.deinit(); + const a = heap.allocator(); + var list: std.ArrayList(u32) = .empty; + for (0..1000) |i| try list.append(a, @intCast(i)); + try std.testing.expectEqual(@as(usize, 1), heap.live.count()); + const loose = try a.alloc(u8, 100); + a.free(loose); + try std.testing.expectEqual(@as(usize, 1), heap.live.count()); + _ = try a.create(u64); + heap.reset(); + try std.testing.expectEqual(@as(usize, 0), heap.live.count()); + // std.testing.allocator fails the test if anything leaked. +} + test "enums migrate by tag name" { const V1 = struct { mood: enum(u8) { calm, lost, angry } = .calm, gone: enum(u8) { a, b } = .b }; const V2 = struct { mood: enum(u16) { angry = 7, calm, sleepy } = .sleepy, gone: enum(u8) { c } = .c }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .forbidden)); - const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .forbidden)); + const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .state)); + const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .state)); for ([_]struct { @FieldType(V1, "mood"), @FieldType(V2, "mood") }{ .{ .calm, .calm }, .{ .angry, .angry }, .{ .lost, .sleepy } }) |case| { const old: V1 = .{ .mood = case[0] }; var new: V2 = .{}; @@ -358,8 +636,8 @@ test "migration keeps unchanged fields and defaults the rest" { const V2 = extern struct { b: f32 = 20, a: u32 = 10, b2: [2]u8 = .{ 7, 7 } }; var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .forbidden)); - const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .forbidden)); + const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .state)); + const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .state)); const old: V1 = .{ .a = 42, .b = 0.5 }; var new: V2 = .{}; const m = migrate(l1, std.mem.asBytes(&old), l2, std.mem.asBytes(&new)); @@ -382,12 +660,12 @@ test "state file migrates live state and snapshots, and a reopen reads the layou var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/test.state", .{&tmp.sub_path}); + const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.state", .{&tmp.sub_path}); const gpa = std.testing.allocator; { var file = try StateFile.open(gpa, path); defer file.close(); - try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .forbidden), D.v1)); + try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .state), D.v1)); file.markUsed(0, true); @as(*V1, @ptrCast(file.slot(0).ptr)).score = 99; @memcpy(file.slot(3), file.slot(0)); @@ -396,7 +674,7 @@ test "state file migrates live state and snapshots, and a reopen reads the layou var file = try StateFile.open(gpa, path); defer file.close(); try std.testing.expectEqual(abi.layout.hash(V1), file.layout.hash); - const adoption = try file.adopt(gpa, abi.layout.of(V2, .forbidden), D.v2); + const adoption = try file.adopt(gpa, abi.layout.of(V2, .state), D.v2); try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); for ([_]usize{ 0, 3 }) |i| { const s: *V2 = @ptrCast(file.slot(i).ptr); @@ -404,3 +682,44 @@ test "state file migrates live state and snapshots, and a reopen reads the layou } try std.testing.expect(!file.used(1)); } + +test "a store keeps its root and what it points to across a reopen, and migrates by field" { + const V1 = struct { list: std.ArrayList(u32) = .empty, count: u32 = 0 }; + const V2 = struct { added: u8 = 7, list: std.ArrayList(u32) = .empty }; + const D = struct { + fn v1(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*V1, @ptrCast(@alignCast(p))).* = .{}; + } + fn v2(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*V2, @ptrCast(@alignCast(p))).* = .{}; + } + }; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.store", .{&tmp.sub_path}); + const gpa = std.testing.allocator; + { + var store = try StoreFile.open(gpa, path, false); + defer store.close(); + try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); + const v: *V1 = @ptrCast(@alignCast(store.root())); + for (0..10_000) |i| try v.list.append(store.allocator(), @intCast(i)); + v.count = 3; + try std.testing.expectError(error.StoreInUse, StoreFile.open(gpa, path, false)); + } + { + var store = try StoreFile.open(gpa, path, false); + defer store.close(); + try std.testing.expectEqual(StoreFile.Adoption.unchanged, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); + const adoption = try store.adopt(gpa, abi.layout.of(V2, .store), D.v2); + try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); + const v: *V2 = @ptrCast(@alignCast(store.root())); + try std.testing.expectEqual(@as(u8, 7), v.added); + try std.testing.expectEqual(@as(u32, 9999), v.list.items[9999]); + v.list.deinit(store.allocator()); + } + var store = try StoreFile.open(gpa, path, true); + defer store.close(); + try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V2, .store), D.v2)); +} diff --git a/src/host/root.zig b/src/host/root.zig index 637a325..2876c0f 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -6,6 +6,8 @@ //! --headless offscreen video, silent audio //! --script FILE play a script instead of reading devices //! --fresh start from default state: no snapshots or recordings +//! --profile-gpu time each GPU command alone, print the times at exit +//! --console LINE run a console line once the sketch is loaded (repeatable) //! --define NAME=VALUE substitute {NAME} in the script const std = @import("std"); const c = @import("c"); @@ -24,6 +26,8 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { var script_path: ?[]const u8 = null; var defines: std.ArrayList([2][]const u8) = .empty; defer defines.deinit(gpa); + var lines: std.ArrayList([]const u8) = .empty; + defer lines.deinit(gpa); var args = init.minimal.args.iterate(); _ = args.skip(); @@ -34,6 +38,10 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { options.fresh = true; } else if (std.mem.eql(u8, arg, "--headless")) { options.headless = true; + } else if (std.mem.eql(u8, arg, "--profile-gpu")) { + options.profile_gpu = true; + } else if (std.mem.eql(u8, arg, "--console")) { + try lines.append(gpa, args.next() orelse fatal("--console needs a line")); } else if (std.mem.eql(u8, arg, "--script")) { script_path = args.next() orelse fatal("--script needs a file"); } else if (std.mem.eql(u8, arg, "--define")) { @@ -56,6 +64,7 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { var host: Host = undefined; try host.init(gpa, init.io, init.environ_map, options, linked); defer host.deinit(); + for (lines.items) |line| host.runLine(line, c.SDL_GetTicksNS()) catch {}; if (script_path) |path| { const source = try std.Io.Dir.cwd().readFileAlloc(init.io, path, gpa, .limited(1 << 20)); defer gpa.free(source); @@ -71,6 +80,8 @@ const usage = \\ --headless offscreen video, silent audio, no vsync \\ --script FILE play a script instead of reading devices \\ --fresh start from default state: no snapshots or recordings + \\ --profile-gpu time each GPU command alone, print the times at exit + \\ --console LINE run a console line once the sketch is loaded (repeatable) \\ --define NAME=VALUE substitute {NAME} in the script \\ ; diff --git a/src/host/script.zig b/src/host/script.zig index 38954f3..cf551cd 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -4,7 +4,7 @@ //! # comment //! click 710 320 move there, press and release the left button //! move|down|up X Y one pointer event, then a frame -//! wheel X Y DELTA +//! wheel|wheel-x X Y DELTA //! cancel what losing focus sends //! key NAME press and release a key (`sketch.Key` names) //! type TEXT text input, as typed into a focused field @@ -56,23 +56,26 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons const eq = std.mem.eql; if (eq(u8, command, "click")) { const x, const y = try point(words); - try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y }); - try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1 }); - try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1 }); + try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y, .flags = held(host) }); + try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); + try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); } else if (eq(u8, command, "move") or eq(u8, command, "down") or eq(u8, command, "up")) { const x, const y = try point(words); const kind: abi.Event.Kind = if (eq(u8, command, "move")) .mouse_move else if (eq(u8, command, "down")) .mouse_down else .mouse_up; - try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1 }); - } else if (eq(u8, command, "wheel")) { + try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); + } else if (eq(u8, command, "wheel") or eq(u8, command, "wheel-x")) { const x, const y = try point(words); const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); - try host.route(.{ .kind = .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta) }); + try host.route(.{ .kind = if (eq(u8, command, "wheel-x")) .wheel_horizontal else .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); } else if (eq(u8, command, "cancel")) { try host.route(.{ .kind = .cancel, .time = now.* }); } else if (eq(u8, command, "key")) { const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = .key_down, .time = now.*, .code = @intFromEnum(key) }); - try host.route(.{ .kind = .key_up, .time = now.*, .code = @intFromEnum(key) }); + try host.route(.{ .kind = .key_down, .time = now.*, .code = @backingInt(key) }); + try host.route(.{ .kind = .key_up, .time = now.*, .code = @backingInt(key) }); + } else if (eq(u8, command, "hold") or eq(u8, command, "release")) { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + try host.route(.{ .kind = if (eq(u8, command, "hold")) .key_down else .key_up, .time = now.*, .code = @backingInt(key) }); } else if (eq(u8, command, "type")) { for (rest) |byte| try host.route(.{ .kind = .text, .time = now.*, .value = byte }); } else if (eq(u8, command, "console")) { @@ -109,7 +112,7 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons return; } else if (eq(u8, command, "screenshot")) { if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); - host.screenshot = try arena.dupeZ(u8, rest); + host.screenshot = try arena.dupeSentinel(u8, rest, 0); } else if (eq(u8, command, "load")) { try host.load(rest); } else if (eq(u8, command, "reload")) { @@ -131,6 +134,16 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons try advance(host, now); } +/// The modifiers `hold` has down, for a pointer event's `flags`. +fn held(host: *const Host) u8 { + var mods: u32 = 0; + if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) mods |= abi.Key.Mod.shift; + if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) mods |= abi.Key.Mod.ctrl; + if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) mods |= abi.Key.Mod.alt; + if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) mods |= abi.Key.Mod.gui; + return abi.Event.withModifiers(mods); +} + fn advance(host: *Host, now: *u64) !void { try host.frame(now.*); now.* += frame_ns; diff --git a/src/keys.zig b/src/keys.zig index b195d2a..7e13ba0 100644 --- a/src/keys.zig +++ b/src/keys.zig @@ -100,8 +100,8 @@ pub const Key = enum(u8) { test "HID values match the specification's anchors" { const std = @import("std"); - try std.testing.expectEqual(4, @intFromEnum(Key.a)); - try std.testing.expectEqual(39, @intFromEnum(Key.@"0")); - try std.testing.expectEqual(53, @intFromEnum(Key.grave)); - try std.testing.expectEqual(82, @intFromEnum(Key.up)); + try std.testing.expectEqual(4, @backingInt(Key.a)); + try std.testing.expectEqual(39, @backingInt(Key.@"0")); + try std.testing.expectEqual(53, @backingInt(Key.grave)); + try std.testing.expectEqual(82, @backingInt(Key.up)); } diff --git a/src/layout.zig b/src/layout.zig index e779260..5c97ca1 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -5,18 +5,26 @@ //! type, so a host can tell, across a reload, which bytes still mean the same //! thing. //! -//! State is saved to disk and snapshotted, so it may not hold pointers at all. -//! Session memory lives only in the host process and may point into memory -//! the host owns (its heap, mapped files), never into the sketch library. +//! What each kind of sketch memory may hold: +//! - State is saved to disk and snapshotted: no pointers, and no host +//! handles (images, sounds, buffers), which only mean something in the +//! process that made them. +//! - Session lives in this process: anything, as long as pointers go into +//! memory the host owns (its heaps, mapped files), never into the sketch +//! library, which a reload unmaps. +//! - Store is a file mapped at the same address in every run: pointers into +//! the store itself, but no host handles. const std = @import("std"); const abi = @import("abi.zig"); -pub const Pointers = enum { forbidden, host_only }; - -pub fn of(comptime T: type, comptime pointers: Pointers) abi.Layout { +pub fn of(comptime T: type, comptime memory: abi.Memory) abi.Layout { return comptime blk: { @setEvalBranchQuota(1_000_000); - if (pointers == .forbidden) noPointers(T, @typeName(T)); + switch (memory) { + .state => check(T, "State", @typeName(T), false), + .store => check(T, "Store", @typeName(T), true), + .session => {}, + } const Leaves = struct { const list = flatten(T, "", 0); const fields: [list.len]abi.Field = list[0..list.len].*; @@ -34,14 +42,15 @@ fn flatten(comptime T: type, comptime prefix: []const u8, comptime base: usize) comptime { if (@typeInfo(T) != .@"struct") return &.{leaf(T, if (prefix.len == 0) "value" else prefix, base)}; var leaves: []const abi.Field = &.{}; - for (@typeInfo(T).@"struct".fields) |field| { - if (field.is_comptime or @sizeOf(field.type) == 0) continue; - const path = if (prefix.len == 0) field.name else prefix ++ "." ++ field.name; - const offset = base + @offsetOf(T, field.name); - leaves = leaves ++ if (@typeInfo(field.type) == .@"struct" and @typeInfo(field.type).@"struct".backing_integer == null) - flatten(field.type, path, offset) + const s = @typeInfo(T).@"struct"; + for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| { + if (attrs.@"comptime" or @sizeOf(F) == 0) continue; + const path = if (prefix.len == 0) name else prefix ++ "." ++ name; + const offset = base + @offsetOf(T, name); + leaves = leaves ++ if (@typeInfo(F) == .@"struct" and @typeInfo(F).@"struct".backing_integer == null) + flatten(F, path, offset) else - &[_]abi.Field{leaf(field.type, path, offset)}; + &[_]abi.Field{leaf(F, path, offset)}; } return leaves; } @@ -68,23 +77,28 @@ fn leaf(comptime T: type, comptime path: []const u8, comptime offset: usize) abi } } -fn noPointers(comptime T: type, comptime path: []const u8) void { - comptime switch (@typeInfo(T)) { - .pointer => @compileError("State cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Store handles, indices or offsets, or keep it in Session."), - .array => |a| noPointers(a.child, path ++ "[]"), - .vector => |v| noPointers(v.child, path ++ "[]"), - .optional => |o| noPointers(o.child, path), - .@"struct" => |s| for (s.fields) |f| noPointers(f.type, path ++ "." ++ f.name), - .@"union" => |u| for (u.fields) |f| noPointers(f.type, path ++ "." ++ f.name), - else => {}, - }; +fn check(comptime T: type, comptime memory: []const u8, comptime path: []const u8, comptime pointers: bool) void { + comptime { + if (T == abi.Image or T == abi.Audio or T == abi.Buffer) @compileError(memory ++ " cannot hold host handles (" ++ path ++ "): they mean nothing to the next process. Keep them in Session."); + switch (@typeInfo(T)) { + // Not followed: types can point to themselves. + .pointer => if (!pointers) @compileError(memory ++ " cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Keep indices or offsets, or put it in Store or Session."), + .array => |a| check(a.child, memory, path ++ "[]", pointers), + .vector => |v| check(v.child, memory, path ++ "[]", pointers), + .optional => |o| check(o.child, memory, path, pointers), + .@"struct" => |s| for (s.field_names, s.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers), + .@"union" => |u| for (u.field_names, u.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers), + else => {}, + } + } } fn tags(comptime E: type) [:0]const u8 { comptime { var text: []const u8 = ""; - for (@typeInfo(E).@"enum".fields, 0..) |field, i| { - text = text ++ (if (i == 0) "" else ",") ++ std.fmt.comptimePrint("{s}={d}", .{ field.name, field.value }); + const e = @typeInfo(E).@"enum"; + for (e.field_names, e.field_values, 0..) |name, value, i| { + text = text ++ (if (i == 0) "" else ",") ++ std.fmt.comptimePrint("{s}={d}", .{ name, value }); } return std.fmt.comptimePrint("{s}", .{text}); } @@ -112,16 +126,16 @@ pub fn signature(comptime T: type) []const u8 { .optional => |o| "?" ++ signature(o.child), .@"struct" => |s| blk: { var text: []const u8 = std.fmt.comptimePrint("s{d}{{", .{@sizeOf(T)}); - for (s.fields) |field| { - if (field.is_comptime) continue; - text = text ++ std.fmt.comptimePrint("{s}@{d}:", .{ field.name, @offsetOf(T, field.name) }) ++ signature(field.type) ++ ";"; + for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| { + if (attrs.@"comptime") continue; + text = text ++ std.fmt.comptimePrint("{s}@{d}:", .{ name, @offsetOf(T, name) }) ++ signature(F) ++ ";"; } break :blk text ++ "}"; }, .@"union" => |u| blk: { var text: []const u8 = std.fmt.comptimePrint("u{d}{{", .{@sizeOf(T)}); if (u.tag_type) |tag| text = text ++ signature(tag) ++ "|"; - for (u.fields) |field| text = text ++ field.name ++ ":" ++ signature(field.type) ++ ";"; + for (u.field_names, u.field_types) |name, F| text = text ++ name ++ ":" ++ signature(F) ++ ";"; break :blk text ++ "}"; }, .pointer => |p| std.fmt.comptimePrint("*{s}:{s}", .{ @tagName(p.size), @typeName(p.child) }), @@ -135,7 +149,7 @@ pub fn signature(comptime T: type) []const u8 { test "leaves flatten structs, keep arrays whole and reject nothing pointer-free" { const Inner = extern struct { a: u8 = 0, b: enum(u8) { x, y = 5 } = .x }; const S = extern struct { inner: Inner = .{}, list: [4]f32 = @splat(0), flag: bool = false }; - const l = of(S, .forbidden); + const l = of(S, .state); const fields = l.fields; try std.testing.expectEqual(4, fields.len); try std.testing.expectEqualStrings("inner.a", std.mem.span(fields[0].path)); diff --git a/src/scope/layouts.zig b/src/scope/layouts.zig new file mode 100644 index 0000000..d8fc484 --- /dev/null +++ b/src/scope/layouts.zig @@ -0,0 +1,120 @@ +//! What the shaders read, as Zig structs: the only definition. The shaders +//! include GLSL generated from these (see sketchbook's `glsl.zig`). No +//! imports, so the shader build can compile this alone. + +/// FFT length: the analysis window, zero-padded to twice its size so the +/// spectrum is sampled twice as finely as the window resolves. +pub const fft_size = 8192; +pub const window = 4096; +/// The most columns a view can have; the column buffers hold this many. +pub const max_columns = 2048; +/// The tallest spectrogram; its buffer holds this many rows of +/// `max_columns`. +pub const max_rows = 1024; +// All three analyses read samples `offset + i` of their sample buffer for +// `0 <= i < samples`, so one buffer can hold several sounds. +/// The spectrogram's window, zero-padded to `spectrogram_fft` points. +pub const spectrogram_window = 2048; +pub const spectrogram_fft = 4096; + +/// spectrum.comp: the window ending at `end`, and the columns to fill. +pub const SpectrumParams = extern struct { + offset: u32, + /// Sample index one past the window's last sample. + end: u32, + /// Samples on the tape. + samples: u32, + columns: u32, + /// Nonzero: forget the smoothed levels and peaks. + reset: u32, + rate: f32, + min_freq: f32, + max_freq: f32, + /// Seconds since the previous analysis, for smoothing. + dt: f32, + pad0: u32 = 0, + pad1: u32 = 0, + pad2: u32 = 0, + + pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; +}; + +/// waveform.comp: column `c` covers samples `first + c * spp` up to the next +/// column's start, and is written to slot `(slot + c) % ring`: the live +/// view writes its newest columns into a ring, the others from slot 0. +pub const WaveParams = extern struct { + offset: u32, + first: u32, + samples: u32, + columns: u32, + spp: f32, + slot: u32, + ring: u32, + pad: u32 = 0, + + pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; +}; + +pub const Tint = enum(u32) { take, recording, live }; + +/// waveform.frag. Pixel column `x` shows slot `(x + scroll) % width`; only +/// the last `filled` columns have anything in them. +pub const WaveView = extern struct { + size: [2]f32, + /// Pixels; negative hides it. + playhead: f32, + gain: f32, + /// A `Tint`. + tint: u32, + scroll: u32, + filled: u32, + pad: u32 = 0, + + pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } }; +}; + +/// spectrum.frag. +pub const SpectrumView = extern struct { + size: [2]f32, + floor_db: f32, + ceil_db: f32, + min_freq: f32, + max_freq: f32, + pad: [2]f32 = .{ 0, 0 }, + + pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } }; +}; + +/// spectrogram.comp: the same columns as `WaveParams`, each analysed into +/// `rows` log-spaced frequency rows, row 0 the lowest. +/// Columns land in slots as `WaveParams` says. +pub const SpectrogramParams = extern struct { + offset: u32, + first: u32, + samples: u32, + columns: u32, + rows: u32, + spp: f32, + rate: f32, + min_freq: f32, + max_freq: f32, + slot: u32, + ring: u32, + pad: u32 = 0, + + pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } }; +}; + +/// spectrogram.frag; `scroll` and `filled` as in `WaveView`. +pub const SpectrogramView = extern struct { + size: [2]f32, + floor_db: f32, + ceil_db: f32, + /// Pixels; negative hides it. + playhead: f32, + scroll: u32, + filled: u32, + pad: f32 = 0, + + pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } }; +}; diff --git a/src/scope/root.zig b/src/scope/root.zig new file mode 100644 index 0000000..a71fa53 --- /dev/null +++ b/src/scope/root.zig @@ -0,0 +1,394 @@ +//! Live views of a stream of sound, analysed and drawn on the GPU: the last +//! few seconds as a scrolling waveform over its spectrogram, the smoothed +//! spectrum of the newest samples, and an oscilloscope. A sketch feeds a +//! `Scope` (kept in its Session) with `push` and draws it once a frame with +//! `draw`, giving the rectangles of the views it wants. +//! +//! The analyses (see the shaders for the details): +//! - spectrogram: a 2048-point Blackman-Harris window per column, and below +//! 500 Hz a second analysis decimated 4× (6 Hz resolution) averaged over +//! three windows; log-frequency rows; +//! - spectrum: a 4096-point window zero-padded to 8192, log-frequency +//! columns, fast attack and slow release, falling peaks; +//! - waveform: min, max and RMS per column, joined into a line when zoomed +//! past one sample per column. +//! +//! Columns of the scrolling views are analysed once, when everything their +//! windows reach has arrived, into rings the views read from the oldest; so +//! the spectrogram runs `reach` samples (about 130 ms) behind the newest. +const std = @import("std"); +const sk = @import("sketch"); +const gpu = sk.abi.gpu; +pub const layouts = @import("scope_layouts"); +const shaders = @import("scope_shaders"); + +pub const min_freq = 20.0; +pub const floor_db = -96.0; +/// The samples kept: enough for a frame's new columns and all they read. +pub const history = 1 << 16; +/// How far the spectrogram reads past a column's centre (its long +/// analysis: 2048 points decimated 4×, three windows a quarter apart). +pub const reach = 6160; + +pub const Tint = layouts.Tint; + +/// Where and how to draw; views left null are not analysed. +pub const Views = struct { + /// Seconds the waveform and spectrogram span, ending now. + seconds: f32 = 6, + /// Both scroll together, column for column: give them the same width. + wave: ?sk.Rect = null, + spectrogram: ?sk.Rect = null, + spectrum: ?sk.Rect = null, + oscilloscope: ?Oscilloscope = null, + gain: f32 = 1, + tint: Tint = .live, + + pub const Oscilloscope = struct { + rect: sk.Rect, + /// The samples across it. + samples: u32 = 1024, + /// How many samples before the newest it ends: a sketch steadies a + /// periodic wave by ending it at the same phase each frame. + end: u32 = 0, + }; +}; + +pub const Scope = struct { + samples: [history]f32 = @splat(0), + /// Samples pushed since the start. + heard: u64 = 0, + /// Pushed since the last `draw`, so the GPU copy is stale. + fresh: bool = false, + /// The next column to analyse, counted from the first sample; how many + /// have been since the view was (re)made; their count and width. + next: u64 = 0, + filled: u32 = 0, + columns: u32 = 0, + spp: u32 = 0, + gpu: Gpu = .{}, + + pub fn push(s: *Scope, heard: []const f32) void { + if (heard.len == 0) return; + const h = &s.samples; + if (heard.len >= h.len) { + @memcpy(h, heard[heard.len - h.len ..]); + } else { + std.mem.copyForwards(f32, h[0 .. h.len - heard.len], h[heard.len..]); + @memcpy(h[h.len - heard.len ..], heard); + } + s.heard += heard.len; + s.fresh = true; + } + + /// Analyses what is new and draws the views; needs `f.host.gpu()`. + pub fn draw(s: *Scope, f: *sk.Frame, v: Views) void { + const g = &s.gpu; + const rate: f64 = @floatFromInt(f.in.sample_rate); + g.setup(f); + if (f.in.reloaded) g.forget(); + if (s.fresh) f.upload(g.tape, 0, std.mem.sliceAsBytes(&s.samples)); + defer s.fresh = false; + + if (v.wave orelse v.spectrogram) |r| s.scroll(f, v, r, rate); + if (v.spectrum) |r| s.spectrum(f, r, rate); + if (v.oscilloscope) |o| s.oscilloscope(f, v, o); + } + + /// The waveform and spectrogram, scrolling. + fn scroll(s: *Scope, f: *sk.Frame, v: Views, r: sk.Rect, rate: f64) void { + const g = &s.gpu; + const columns: u32 = @intFromFloat(std.math.clamp(r.w, 1, layouts.max_columns)); + const spp: u32 = @max(1, @as(u32, @intFromFloat(@round(v.seconds * rate / @as(f64, @floatFromInt(columns)))))); + if (columns != s.columns or spp != s.spp or f.in.reloaded) { + s.columns = columns; + s.spp = spp; + s.filled = 0; + s.next = 0; + } + const rows: u32 = if (v.spectrogram) |sr| @intFromFloat(std.math.clamp(sr.h, 1, layouts.max_rows)) else 1; + g.levelsFor(f, rows); + + // History index of sample `i`: `i - base`. A column is ready once + // all it reads has arrived, and analysable while all it reads is kept. + const base: i64 = @as(i64, @intCast(s.heard)) - history; + const ready = (s.heard -| reach) / spp; + const kept: u64 = @intCast(@max(0, @divFloor(base + reach + spp - 1, spp))); + s.next = @max(s.next, kept, ready -| columns); + if (ready > s.next) { + const count: u32 = @intCast(ready - s.next); + const first: u32 = @intCast(@as(i64, @intCast(s.next * spp)) - base); + const slot: u32 = @intCast(s.next % columns); + if (v.wave != null) f.compute(.{ + .label = "scope waveform.comp", + .kernel = waveform_kernel, + .readonly = &.{g.tape}, + .readwrite = &.{g.wave.columns}, + .uniforms = gpu.uniform(f.scratch, layouts.WaveParams{ .offset = 0, .first = first, .samples = history, .columns = count, .spp = @floatFromInt(spp), .slot = slot, .ring = columns }), + .groups = .{ count, 1, 1 }, + }); + if (v.spectrogram != null) f.compute(.{ + .label = "scope spectrogram.comp", + .kernel = spectrogram_kernel, + .readonly = &.{g.tape}, + .readwrite = &.{g.levels}, + .uniforms = gpu.uniform(f.scratch, layouts.SpectrogramParams{ + .offset = 0, + .first = first, + .samples = history, + .columns = count, + .rows = rows, + .spp = @floatFromInt(spp), + .rate = @floatCast(rate), + .min_freq = min_freq, + .max_freq = maxFreq(rate), + .slot = slot, + .ring = columns, + }), + .groups = .{ count, 1, 1 }, + }); + s.filled = @intCast(@min(columns, s.filled + count)); + s.next = ready; + } + + const scroll_at: u32 = @intCast(s.next % columns); + if (v.wave) |wr| { + const size = target(f, &g.wave, wr, layouts.max_rows); + const view: layouts.WaveView = .{ + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + .playhead = -1, + .gain = v.gain, + .tint = @intFromEnum(v.tint), + .scroll = scroll_at, + .filled = s.filled, + }; + if (g.wave.redraw(std.hash.Wyhash.hash(0, std.mem.asBytes(&view)))) wavePass(f, &g.wave, size, view); + f.image(g.wave.image, wr, .{}); + } + if (v.spectrogram) |sr| { + const size = target(f, &g.spectrogram, sr, rows); + const view: layouts.SpectrogramView = .{ + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + .floor_db = floor_db, + .ceil_db = -6, + .playhead = -1, + .scroll = scroll_at, + .filled = s.filled, + }; + if (g.spectrogram.redraw(std.hash.Wyhash.hash(0, std.mem.asBytes(&view)))) f.gpu(.{ + .label = "scope spectrogram.frag", + .image = g.spectrogram.image, + .width = size[0], + .height = size[1], + .clear = null, + .pipelines = &spectrogram_pipelines, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, view), .fragment_storage = &.{g.levels} }}, + }); + f.image(g.spectrogram.image, sr, .{}); + } + } + + /// The spectrum of the newest samples, smoothed frame to frame. + fn spectrum(s: *Scope, f: *sk.Frame, r: sk.Rect, rate: f64) void { + const g = &s.gpu; + const size = target(f, &g.spectrum, r, layouts.max_rows); + const dt: f64 = @min(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s, 0.1); + f.compute(.{ + .label = "scope spectrum.comp", + .kernel = spectrum_kernel, + .readonly = &.{g.tape}, + .readwrite = &.{ g.scratch, g.spectrum.columns }, + .uniforms = gpu.uniform(f.scratch, layouts.SpectrumParams{ + .offset = 0, + .end = history, + .samples = history, + .columns = size[0], + .reset = @intFromBool(!g.spectrum_started), + .rate = @floatCast(rate), + .min_freq = min_freq, + .max_freq = maxFreq(rate), + .dt = @floatCast(dt), + }), + }); + g.spectrum_started = true; + f.gpu(.{ + .label = "scope spectrum.frag", + .image = g.spectrum.image, + .width = size[0], + .height = size[1], + .clear = null, + .pipelines = &spectrum_pipelines, + .draws = &.{.{ + .pipeline = 0, + .vertex_count = 3, + .fragment_uniforms = gpu.uniform(f.scratch, layouts.SpectrumView{ + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + .floor_db = floor_db, + .ceil_db = 0, + .min_freq = min_freq, + .max_freq = maxFreq(rate), + }), + .fragment_storage = &.{g.spectrum.columns}, + }}, + }); + f.image(g.spectrum.image, r, .{}); + } + + /// The newest `samples` (less `end`), as a line. + fn oscilloscope(s: *Scope, f: *sk.Frame, v: Views, o: Views.Oscilloscope) void { + const g = &s.gpu; + const size = target(f, &g.oscilloscope, o.rect, layouts.max_rows); + const n: u32 = @min(o.samples, history / 2); + const end: u32 = @min(o.end, history / 2); + const first: u32 = history - n - end; + f.compute(.{ + .label = "scope oscilloscope", + .kernel = waveform_kernel, + .readonly = &.{g.tape}, + .readwrite = &.{g.oscilloscope.columns}, + .uniforms = gpu.uniform(f.scratch, layouts.WaveParams{ + .offset = 0, + .first = first, + .samples = history, + .columns = size[0], + .spp = @as(f32, @floatFromInt(n)) / @as(f32, @floatFromInt(size[0])), + .slot = 0, + .ring = layouts.max_columns, + }), + .groups = .{ size[0], 1, 1 }, + }); + wavePass(f, &g.oscilloscope, size, .{ + .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) }, + .playhead = -1, + .gain = v.gain, + .tint = @intFromEnum(v.tint), + .scroll = 0, + .filled = size[0], + }); + f.image(g.oscilloscope.image, o.rect, .{}); + } +}; + +/// The highest frequency the views show. +pub fn maxFreq(rate: f64) f32 { + return @floatCast(@min(20_000, rate / 2)); +} + +/// Where `hz` falls across a spectrum view (or up a spectrogram, from its +/// bottom): 0 to 1, log frequency. +pub fn place(hz: f64, rate: f64) f32 { + return @floatCast(@log(hz / min_freq) / @log(maxFreq(rate) / min_freq)); +} + +// --------------------------------------------------------------------------- +// GPU objects. + +/// One view's image and columns, and the inputs it was last drawn from. +const View = struct { + image: sk.Image = .none, + size: [2]u32 = .{ 0, 0 }, + columns: sk.Buffer = .none, + drawn: u64 = 0, + + fn redraw(v: *View, key: u64) bool { + if (key == v.drawn) return false; + v.drawn = key; + return true; + } +}; + +const Gpu = struct { + ready: bool = false, + tape: sk.Buffer = .none, + scratch: sk.Buffer = .none, + levels: sk.Buffer = .none, + levels_rows: u32 = 0, + wave: View = .{}, + spectrogram: View = .{}, + spectrum: View = .{}, + oscilloscope: View = .{}, + spectrum_started: bool = false, + + fn setup(g: *Gpu, f: *sk.Frame) void { + if (g.ready) return; + g.ready = true; + g.tape = f.host.newBuffer(history * @sizeOf(f32)); + g.scratch = f.host.newBuffer(layouts.fft_size * 2 * @sizeOf([2]f32)); + g.wave.columns = f.host.newBuffer(layouts.max_columns * @sizeOf([4]f32)); + g.oscilloscope.columns = f.host.newBuffer(layouts.max_columns * @sizeOf([4]f32)); + g.spectrum.columns = f.host.newBuffer(layouts.max_columns * @sizeOf([2]f32)); + } + + /// The spectrogram's levels, `rows` of `max_columns`. + fn levelsFor(g: *Gpu, f: *sk.Frame, rows: u32) void { + if (rows <= g.levels_rows) return; + if (g.levels != .none) f.host.freeBuffer(g.levels); + g.levels = f.host.newBuffer(layouts.max_columns * rows * @sizeOf(f32)); + g.levels_rows = rows; + g.forget(); + } + + /// New shaders, or new buffers: draw everything again. + fn forget(g: *Gpu) void { + g.wave.drawn = 0; + g.spectrogram.drawn = 0; + g.spectrum_started = false; + } +}; + +/// The view's image at `r`'s size, (re)made when that changes. +fn target(f: *sk.Frame, v: *View, r: sk.Rect, max_h: u32) [2]u32 { + const want: [2]u32 = .{ + @intFromFloat(std.math.clamp(r.w, 1, layouts.max_columns)), + @intFromFloat(std.math.clamp(r.h, 1, @as(f32, @floatFromInt(max_h)))), + }; + if (v.image == .none or v.size[0] != want[0] or v.size[1] != want[1]) { + if (v.image != .none) f.host.freeImage(v.image); + v.image = f.host.newImage(want[0], want[1]); + v.size = want; + v.drawn = 0; + } + return want; +} + +fn wavePass(f: *sk.Frame, v: *View, size: [2]u32, view: layouts.WaveView) void { + f.gpu(.{ + .label = "scope waveform.frag", + .image = v.image, + .width = size[0], + .height = size[1], + .clear = null, + .pipelines = &wave_pipelines, + .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, view), .fragment_storage = &.{v.columns} }}, + }); +} + +fn viewPipeline(fragment: []const u8) gpu.Pipeline { + return .{ + .vertex = .{ .code = shaders.view_vert }, + .fragment = .{ .code = fragment, .uniform_buffers = 1, .storage_buffers = 1 }, + .stride = 0, + .attributes = &.{}, + .blend = .none, + }; +} +const wave_pipelines = [_]gpu.Pipeline{viewPipeline(shaders.waveform_frag)}; +const spectrum_pipelines = [_]gpu.Pipeline{viewPipeline(shaders.spectrum_frag)}; +const spectrogram_pipelines = [_]gpu.Pipeline{viewPipeline(shaders.spectrogram_frag)}; + +const spectrum_kernel: gpu.Kernel = .{ .code = shaders.spectrum_comp, .readonly_buffers = 1, .readwrite_buffers = 2, .uniform_buffers = 1, .threads = .{ 256, 1, 1 } }; +const spectrogram_kernel: gpu.Kernel = .{ .code = shaders.spectrogram_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1, .threads = .{ 256, 1, 1 } }; +const waveform_kernel: gpu.Kernel = .{ .code = shaders.waveform_comp, .readonly_buffers = 1, .readwrite_buffers = 1, .uniform_buffers = 1, .threads = .{ 64, 1, 1 } }; + +test "the history keeps the newest samples in order" { + var s: Scope = .{}; + s.push(&.{ 1, 2, 3 }); + s.push(&.{4}); + try std.testing.expectEqualSlices(f32, &.{ 1, 2, 3, 4 }, s.samples[history - 4 ..]); + try std.testing.expectEqual(@as(u64, 4), s.heard); +} + +test "frequencies place on a log axis" { + try std.testing.expectEqual(@as(f32, 0), place(min_freq, 48_000)); + try std.testing.expectApproxEqAbs(@as(f32, 1), place(20_000, 48_000), 1e-6); +} diff --git a/src/scope/shaders/spectrogram.comp b/src/scope/shaders/spectrogram.comp new file mode 100644 index 0000000..b32f4c5 --- /dev/null +++ b/src/scope/shaders/spectrogram.comp @@ -0,0 +1,208 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The spectrogram, one workgroup per column, the same columns as the +// waveform (waveform.comp), so the two line up in time. +// +// Two resolutions per column, because one window cannot serve both ends of +// a log axis: 2048 samples resolve 23 Hz, which is sharp in time and fine +// in the kilohertz, but leaves 20 to 100 Hz (a quarter of the height) to +// four bins. So the low end is analysed again from the signal low-passed +// and decimated to a quarter of the rate: the same 2048 points then span +// 170 ms and resolve 6 Hz. Rows take the long analysis below 250 Hz, the +// short one above 500 Hz, and blend in between. +// +// A single window's spectrum of noise scatters as much as its level (each +// bin is chi-squared with 2 degrees of freedom), which in dB shows as +// speckle full of black holes, and finer bins only make more of them. So +// the long analysis averages three windows a quarter window apart in time +// (Welch): steady lines stay as sharp, noise settles. Multitaper averaging +// was tried and measured against a reference (see test/README.md): sine +// tapers and DPSS both widen narrow lines into slabs and leak, which a +// 96 dB scale shows. +// +// Each analysis is a Blackman-Harris window zero-padded to 4096 points, +// transformed as 2048 complex points (even and odd samples as real and +// imaginary parts) by an in-place radix-2 FFT in shared memory, then split +// into the real signal's bins. Zoomed out, a column averages the power of +// up to four windows spread across its samples, so it neither skips sound +// nor flickers as the view moves; zoomed in, neighbouring windows overlap. +// Rows are log-spaced: a Catmull-Rom spline through the bins' levels where +// rows are finer than bins, the loudest bin where they are coarser. +layout(local_size_x = 256) in; + +#include "SpectrogramParams.glsl" + +layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; }; +// MAX_ROWS rows of MAX_COLUMNS levels, dBFS; row 0 is the lowest frequency. +layout(set = 1, binding = 0) buffer Levels { float levels[]; }; + +// Match layouts.zig. +const uint MAX_COLUMNS = 2048; +const uint W = 2048; +const uint N = 4096; +const uint HALF = N / 2; +const uint LOG2_HALF = 11; +const uint THREADS = 256; +// The long analysis: every DECIMATE-th sample of the low-passed signal. +const uint DECIMATE = 4; +// Its bins needed below the blend: 600 Hz at 12 kHz / 4096 is bin 205. +const uint LONG_BINS = 256; +// Windows the long analysis averages per column, a quarter window apart. +const uint WELCH = 3; +const float BLEND_LO = 250.0; +const float BLEND_HI = 500.0; +const float PI = 3.14159265358979; +const float SILENCE = -160.0; +// Columns past the end of the tape. +const float NO_TAPE = 1000.0; +const float A0 = 0.35875; +const float A1 = 0.48829; +const float A2 = 0.14128; +const float A3 = 0.01168; + +shared vec2 z[HALF]; +// Power per bin, summed over windows; then dBFS. +shared float short_bins[HALF + 1]; +shared float long_bins[LONG_BINS]; + +float sample_at(int i) { + return i >= 0 && i < int(u.samples) ? tape[u.offset + uint(i)] : 0.0; +} + +// The signal low-passed for decimation: a triangle over 7 samples (two +// boxes of 4), whose zeros sit on 12 and 24 kHz, exactly where anything +// that would fold onto the low bins comes from. +float low_passed(int i) { + return (sample_at(i - 3) + 2.0 * sample_at(i - 2) + 3.0 * sample_at(i - 1) + 4.0 * sample_at(i) + 3.0 * sample_at(i + 1) + 2.0 * sample_at(i + 2) + sample_at(i + 3)) / 16.0; +} + +float blackman_harris(uint i) { + float a = 2.0 * PI * float(i) / float(W - 1); + return A0 - A1 * cos(a) + A2 * cos(2.0 * a) - A3 * cos(3.0 * a); +} + +vec2 mul(vec2 a, vec2 b) { + return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); +} + +float windowed(int centre, uint i, bool long_window) { + if (long_window) return low_passed(centre + (int(i) - int(W / 2)) * int(DECIMATE)) * blackman_harris(i); + return sample_at(centre + int(i) - int(W / 2)) * blackman_harris(i); +} + +// Adds the power spectrum of the window centred on sample `centre`. +void analyse(int centre, bool long_window) { + uint t = gl_LocalInvocationID.x; + // Even and odd samples packed as one complex sequence, in bit-reversed + // order for the in-place transform. Past W it is all padding. + for (uint i = t; i < HALF; i += THREADS) { + vec2 v = vec2(0.0); + if (2u * i < W) v = vec2(windowed(centre, 2u * i, long_window), windowed(centre, 2u * i + 1u, long_window)); + z[bitfieldReverse(i) >> (32u - LOG2_HALF)] = v; + } + barrier(); + for (uint s = 1u; s <= LOG2_HALF; s++) { + uint half_size = 1u << (s - 1u); + for (uint j = t; j < HALF / 2u; j += THREADS) { + uint k = j & (half_size - 1u); + uint i0 = ((j >> (s - 1u)) << s) + k; + uint i1 = i0 + half_size; + float angle = -PI * float(k) / float(half_size); + vec2 b = mul(vec2(cos(angle), sin(angle)), z[i1]); + vec2 a = z[i0]; + z[i0] = a + b; + z[i1] = a - b; + } + barrier(); + } + // The real signal's bins from the packed transform: + // X[k] = E[k] + e^(-2 pi i k / N) O[k], with E and O the even and odd + // samples' spectra, recovered from Z[k] and conj(Z[HALF - k]). + uint count = long_window ? LONG_BINS : HALF + 1u; + for (uint k = t; k < count; k += THREADS) { + vec2 zk = z[k % HALF]; + vec2 zc = z[(HALF - k) % HALF]; + zc.y = -zc.y; + vec2 even = 0.5 * (zk + zc); + vec2 d = 0.5 * (zk - zc); + vec2 odd = vec2(d.y, -d.x); + float angle = -2.0 * PI * float(k) / float(N); + vec2 x = even + mul(vec2(cos(angle), sin(angle)), odd); + if (long_window) long_bins[k] += dot(x, x); + else short_bins[k] += dot(x, x); + } + barrier(); +} + +float bin_level(uint k, bool long_window) { + return long_window ? long_bins[min(k, LONG_BINS - 1u)] : short_bins[min(k, HALF)]; +} + +// The level of the row spanning bins `lo` to `hi` of one analysis. +float row_level(float lo, float hi, bool long_window) { + if (hi - lo < 1.0) { + float mid = sqrt(lo * hi); + uint i = uint(floor(mid)); + float f = mid - floor(mid); + float p0 = bin_level(max(i, 1u) - 1u, long_window); + float p1 = bin_level(i, long_window); + float p2 = bin_level(i + 1u, long_window); + float p3 = bin_level(i + 2u, long_window); + float v = p1 + 0.5 * f * (p2 - p0 + f * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + f * (3.0 * (p1 - p2) + p3 - p0))); + return clamp(v, SILENCE, max(max(p1, p2), max(p0, p3))); + } + float v = SILENCE; + for (uint i = uint(floor(lo)); i <= uint(ceil(hi)); i++) v = max(v, bin_level(i, long_window)); + return v; +} + +// Average power over `count` windows to dBFS: a full-scale sine at a bin's +// centre gives (W * A0 / 2) squared. +float to_db(float power, uint count) { + float ratio = power / float(count) / (0.25 * float(W) * A0 * float(W) * A0); + return ratio > 0.0 ? max(10.0 * log(ratio) / log(10.0), SILENCE) : SILENCE; +} + +void main() { + uint c = gl_WorkGroupID.x; + uint t = gl_LocalInvocationID.x; + for (uint k = t; k <= HALF; k += THREADS) short_bins[k] = 0.0; + for (uint k = t; k < LONG_BINS; k += THREADS) long_bins[k] = 0.0; + barrier(); + + float start = float(c) * u.spp; + uint short_windows = uint(clamp(ceil(u.spp / float(W / 2u)), 1.0, 4.0)); + for (uint w = 0u; w < short_windows; w++) { + analyse(int(float(u.first) + start + u.spp * (float(w) + 0.5) / float(short_windows)), false); + } + // Zoomed out, the windows spread across the column average already; + // zoomed in, WELCH windows a quarter window apart around its centre. + uint long_windows = uint(clamp(ceil(u.spp / float(W * DECIMATE / 2u)), 1.0, 4.0)); + if (long_windows == 1u) { + int centre = int(float(u.first) + start + 0.5 * u.spp); + for (uint w = 0u; w < WELCH; w++) analyse(centre + (int(w) - int(WELCH / 2u)) * int(W * DECIMATE / 4u), true); + long_windows = WELCH; + } else { + for (uint w = 0u; w < long_windows; w++) { + analyse(int(float(u.first) + start + u.spp * (float(w) + 0.5) / float(long_windows)), true); + } + } + + for (uint k = t; k <= HALF; k += THREADS) short_bins[k] = to_db(short_bins[k], short_windows); + for (uint k = t; k < LONG_BINS; k += THREADS) long_bins[k] = to_db(long_bins[k], long_windows); + barrier(); + + bool inside = float(u.first) + start < float(u.samples); + float short_hz = u.rate / float(N); + float long_hz = short_hz / float(DECIMATE); + float ratio = log(u.max_freq / u.min_freq); + for (uint r = t; r < u.rows; r += THREADS) { + float lo = u.min_freq * exp(ratio * float(r) / float(u.rows)); + float hi = u.min_freq * exp(ratio * float(r + 1u) / float(u.rows)); + float blend = smoothstep(BLEND_LO, BLEND_HI, sqrt(lo * hi)); + float v = 0.0; + if (blend < 1.0) v += (1.0 - blend) * row_level(lo / long_hz, hi / long_hz, true); + if (blend > 0.0) v += blend * row_level(lo / short_hz, hi / short_hz, false); + levels[r * MAX_COLUMNS + (u.slot + c) % u.ring] = inside ? v : NO_TAPE; + } +} diff --git a/src/scope/shaders/spectrogram.frag b/src/scope/shaders/spectrogram.frag new file mode 100644 index 0000000..9cb5e3d --- /dev/null +++ b/src/scope/shaders/spectrogram.frag @@ -0,0 +1,45 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The spectrogram (spectrogram.comp): time across, as the waveform above +// it, log frequency up, each level through a heat gradient (inferno: +// black, violet, red, orange, pale yellow), with the playhead. +layout(location = 0) out vec4 out_color; + +#include "SpectrogramView.glsl" + +layout(set = 2, binding = 0) readonly buffer Levels { float levels[]; }; + +// Match layouts.zig and spectrogram.comp. +const uint MAX_COLUMNS = 2048; +const float NO_TAPE = 1000.0; + +// Matplotlib's inferno, as a polynomial fit (Matt Zucker's, CC0). +vec3 inferno(float t) { + const vec3 c0 = vec3(0.0002189403691192265, 0.001651004631001012, -0.01948089843709184); + const vec3 c1 = vec3(0.1065134194856116, 0.5639564367884091, 3.932712388889277); + const vec3 c2 = vec3(11.60249308247187, -3.972853965665698, -15.9423941062914); + const vec3 c3 = vec3(-41.70399613139459, 17.43639888205313, 44.35414519872813); + const vec3 c4 = vec3(77.162935699427, -33.40235894210092, -81.80730925738993); + const vec3 c5 = vec3(-71.31942824499214, 32.62606426397723, 73.20951985803202); + const vec3 c6 = vec3(25.13112622477341, -12.24266895238567, -23.07032500287172); + return clamp(c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))), 0.0, 1.0); +} + +void main() { + vec2 p = gl_FragCoord.xy; + uint row = uint(u.size.y - p.y); + uint x = uint(p.x); + float db = levels[row * MAX_COLUMNS + (x + u.scroll) % uint(u.size.x)]; + if (x + u.filled < uint(u.size.x)) db = NO_TAPE; + vec3 color = vec3(0.035, 0.03, 0.05); + if (db < NO_TAPE) { + float t = clamp((db - u.floor_db) / (u.ceil_db - u.floor_db), 0.0, 1.0); + // A touch of contrast: quiet stays dark, loud blooms. + color = inferno(pow(t, 1.3)); + } + if (u.playhead >= 0.0) { + float d = abs(p.x - u.playhead); + color = mix(color, vec3(1.0, 0.85, 0.3), clamp(1.5 - d, 0.0, 1.0) * 0.8); + } + out_color = vec4(color, 1.0); +} diff --git a/src/scope/shaders/spectrum.comp b/src/scope/shaders/spectrum.comp new file mode 100644 index 0000000..e576e30 --- /dev/null +++ b/src/scope/shaders/spectrum.comp @@ -0,0 +1,128 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The spectrum at the playhead, entirely on the GPU, in one workgroup: +// +// 1. a Blackman-Harris window of the tape ending at `u.end`, zero-padded to +// N. Its sidelobes sit below -92 dB, so the finely sampled spectrum shows +// the signal rather than the window's ripples; +// 2. a radix-2 Stockham FFT, ping-ponging between the scratch halves; +// 3. levels in dBFS per bin; +// 4. one value per log-spaced column. Where columns are narrower than a bin +// (the low end), a Catmull-Rom spline through the bins' levels; where a +// column spans several bins (the high end), their loudest. The zero +// padding already interpolates between the window's own bins, so the +// curve is continuous at every zoom; +// 5. smoothing in time (fast attack, slow release) and falling peaks, kept +// in the column buffer between frames. +layout(local_size_x = 256) in; + +#include "SpectrumParams.glsl" + +layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; }; +// Two halves of N complex values, which the workgroup's threads read from +// each other: coherent, for Vulkan; WGSL's storageBarrier covers it. +#ifdef SKETCHBOOK_WEB +layout(set = 1, binding = 0) buffer Scratch { vec2 scratch[]; }; +#else +layout(set = 1, binding = 0) coherent buffer Scratch { vec2 scratch[]; }; +#endif +// x: smoothed level, y: peak, both dBFS. +layout(set = 1, binding = 1) buffer Columns { vec2 columns[]; }; + +// Match layouts.zig. +const uint N = 8192; +const uint LOG2_N = 13; +const uint W = 4096; +const uint THREADS = 256; +const float PI = 3.14159265358979; +const float SILENCE = -160.0; +// Blackman-Harris, 4 terms. A0 is also its coherent gain. +const float A0 = 0.35875; +const float A1 = 0.48829; +const float A2 = 0.14128; +const float A3 = 0.01168; + +void sync() { + memoryBarrierBuffer(); + barrier(); +} + +void main() { + uint t = gl_LocalInvocationID.x; + + // 1. Window. + int first = int(u.end) - int(W); + for (uint i = t; i < N; i += THREADS) { + float x = 0.0; + int at = first + int(i); + if (i < W && at >= 0 && at < int(u.samples)) { + float a = 2.0 * PI * float(i) / float(W - 1); + x = tape[u.offset + uint(at)] * (A0 - A1 * cos(a) + A2 * cos(2.0 * a) - A3 * cos(3.0 * a)); + } + scratch[i] = vec2(x, 0.0); + } + sync(); + + // 2. FFT: after stage s, sub-transforms of 2p = 2^(s+1) points, in order. + for (uint s = 0; s < LOG2_N; s++) { + uint p = 1u << s; + uint from = (s & 1u) * N; + uint to = N - from; + for (uint i = t; i < N / 2; i += THREADS) { + uint k = i & (p - 1u); + vec2 a = scratch[from + i]; + vec2 b = scratch[from + i + N / 2]; + float angle = -PI * float(k) / float(p); + vec2 w = vec2(cos(angle), sin(angle)); + b = vec2(b.x * w.x - b.y * w.y, b.x * w.y + b.y * w.x); + uint j = (i << 1u) - k; + scratch[to + j] = a + b; + scratch[to + j + p] = a - b; + } + sync(); + } + uint result = (LOG2_N & 1u) * N; + + // 3. Levels into the other half. A full-scale sine peaks at W * A0 / 2: + // the window sums to W * A0, and a real sine splits between ±f. + uint levels = N - result; + for (uint i = t; i <= N / 2; i += THREADS) { + float amplitude = length(scratch[result + i]) * 2.0 / (float(W) * A0); + scratch[levels + i].x = amplitude > 0.0 ? max(20.0 * log(amplitude) / log(10.0), SILENCE) : SILENCE; + } + sync(); + + // 4 and 5. Columns. + float bin_hz = u.rate / float(N); + float ratio = log(u.max_freq / u.min_freq); + for (uint c = t; c < u.columns; c += THREADS) { + float lo = u.min_freq * exp(ratio * float(c) / float(u.columns)) / bin_hz; + float hi = u.min_freq * exp(ratio * float(c + 1u) / float(u.columns)) / bin_hz; + float mid = sqrt(lo * hi); + float v; + if (hi - lo < 1.0) { + uint i = uint(floor(mid)); + float f = mid - floor(mid); + float p0 = scratch[levels + min(max(i, 1u) - 1u, N / 2)].x; + float p1 = scratch[levels + min(i, N / 2)].x; + float p2 = scratch[levels + min(i + 1u, N / 2)].x; + float p3 = scratch[levels + min(i + 2u, N / 2)].x; + v = p1 + 0.5 * f * (p2 - p0 + f * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + f * (3.0 * (p1 - p2) + p3 - p0))); + v = clamp(v, SILENCE, max(max(p1, p2), max(p0, p3))); + } else { + v = SILENCE; + uint end = min(uint(ceil(hi)), N / 2); + for (uint i = uint(floor(lo)); i <= end; i++) v = max(v, scratch[levels + i].x); + } + vec2 old = columns[c]; + vec2 now; + if (u.reset != 0u) { + now = vec2(v, v); + } else { + float rate = v > old.x ? 40.0 : 6.0; + now.x = mix(old.x, v, 1.0 - exp(-rate * u.dt)); + now.y = max(now.x, old.y - 24.0 * u.dt); + } + columns[c] = now; + } +} diff --git a/src/scope/shaders/spectrum.frag b/src/scope/shaders/spectrum.frag new file mode 100644 index 0000000..e88b091 --- /dev/null +++ b/src/scope/shaders/spectrum.frag @@ -0,0 +1,51 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The spectrum from its columns (spectrum.comp): one column per pixel on a +// log frequency axis. The curve is drawn by its distance along the normal, +// from the neighbours' slope, so steep parts stay as thick as flat ones. +layout(location = 0) out vec4 out_color; + +#include "SpectrumView.glsl" + +layout(set = 2, binding = 0) readonly buffer Columns { vec2 columns[]; }; + +float y_of(float db) { + return u.size.y * (1.0 - clamp((db - u.floor_db) / (u.ceil_db - u.floor_db), 0.0, 1.0)); +} + +float x_of(float hz) { + return u.size.x * log(hz / u.min_freq) / log(u.max_freq / u.min_freq); +} + +// How close `p` is to a grid line, 0..1. +float grid(vec2 p) { + const float lines[10] = float[](20.0, 50.0, 100.0, 200.0, 500.0, 1000.0, 2000.0, 5000.0, 10000.0, 20000.0); + float near = 1e9; + for (int i = 0; i < 10; i++) near = min(near, abs(p.x - x_of(lines[i]))); + float step = u.size.y * 12.0 / (u.ceil_db - u.floor_db); + float db = mod(p.y, step); + near = min(near, min(db, step - db)); + return clamp(1.0 - near, 0.0, 1.0); +} + +void main() { + vec2 p = gl_FragCoord.xy; + int n = int(u.size.x); + int x = int(p.x); + float y = y_of(columns[x].x); + float slope = (y_of(columns[min(x + 1, n - 1)].x) - y_of(columns[max(x - 1, 0)].x)) * 0.5; + float distance = abs(p.y - y) / sqrt(1.0 + slope * slope); + + vec3 color = vec3(0.055, 0.06, 0.07); + color = mix(color, vec3(0.16, 0.17, 0.2), grid(p)); + + // Fill under the curve, by frequency, fading downwards. + float along = p.x / u.size.x; + vec3 hue = mix(vec3(0.2, 0.75, 0.9), vec3(0.85, 0.35, 0.9), along); + if (p.y > y) color = mix(color, hue, 0.35 * (1.0 - (p.y - y) / max(u.size.y - y, 1.0)) + 0.08); + // Peaks, then the curve. + float peak = abs(p.y - y_of(columns[x].y)); + color = mix(color, vec3(0.9, 0.9, 0.95), clamp(1.0 - peak, 0.0, 1.0) * 0.45); + color = mix(color, mix(hue, vec3(1.0), 0.5), clamp(1.6 - distance, 0.0, 1.0)); + out_color = vec4(color, 1.0); +} diff --git a/src/scope/shaders/view.vert b/src/scope/shaders/view.vert new file mode 100644 index 0000000..d9461e9 --- /dev/null +++ b/src/scope/shaders/view.vert @@ -0,0 +1,7 @@ +#version 450 +// One triangle over the whole target, from the vertex index alone: the +// fragment shaders work in pixels (gl_FragCoord). +void main() { + vec2 p = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2); + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/src/scope/shaders/waveform.comp b/src/scope/shaders/waveform.comp new file mode 100644 index 0000000..d1d64e5 --- /dev/null +++ b/src/scope/shaders/waveform.comp @@ -0,0 +1,72 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// One workgroup per waveform column: the lowest and highest the signal goes +// in it, and its RMS. Each column also includes the linearly interpolated +// signal at its own edges, which it shares with its neighbours, so that +// zoomed in past one sample per column the columns still join into one +// continuous line. +layout(local_size_x = 64) in; + +#include "WaveParams.glsl" + +layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; }; +// x: min, y: max, z: RMS, w: 1 where the column has tape under it. +layout(set = 1, binding = 0) buffer Columns { vec4 columns[]; }; + +const uint THREADS = 64; +shared float lows[THREADS]; +shared float highs[THREADS]; +shared float squares[THREADS]; + +float sample_at(int i) { + return i >= 0 && i < int(u.samples) ? tape[u.offset + uint(i)] : 0.0; +} + +// The signal at `t` samples after `u.first`, between samples linearly. +float signal(float t) { + float whole = floor(t); + int i = int(u.first) + int(whole); + return mix(sample_at(i), sample_at(i + 1), t - whole); +} + +void main() { + uint c = gl_WorkGroupID.x; + uint t = gl_LocalInvocationID.x; + float start = float(c) * u.spp; + float end = start + u.spp; + int first = int(ceil(start)); + int last = min(int(floor(end)), int(u.samples) - int(u.first) - 1); + + float lo = 1e9; + float hi = -1e9; + float sq = 0.0; + for (int i = first + int(t); i <= last; i += int(THREADS)) { + float v = tape[u.offset + u.first + uint(i)]; + lo = min(lo, v); + hi = max(hi, v); + sq += v * v; + } + if (t == 0u) { + float a = signal(start); + float b = signal(min(end, float(u.samples - u.first - 1u))); + lo = min(lo, min(a, b)); + hi = max(hi, max(a, b)); + } + lows[t] = lo; + highs[t] = hi; + squares[t] = sq; + barrier(); + for (uint stride = THREADS / 2u; stride > 0u; stride >>= 1u) { + if (t < stride) { + lows[t] = min(lows[t], lows[t + stride]); + highs[t] = max(highs[t], highs[t + stride]); + squares[t] += squares[t + stride]; + } + barrier(); + } + if (t == 0u) { + float count = float(max(last - first + 1, 1)); + float inside = float(u.first) + start < float(u.samples) ? 1.0 : 0.0; + columns[(u.slot + c) % u.ring] = vec4(lows[0], highs[0], sqrt(squares[0] / count), inside); + } +} diff --git a/src/scope/shaders/waveform.frag b/src/scope/shaders/waveform.frag new file mode 100644 index 0000000..37be7ce --- /dev/null +++ b/src/scope/shaders/waveform.frag @@ -0,0 +1,56 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The waveform from its columns (waveform.comp): one column per pixel, the +// band between its low and high filled, anti-aliased, at least a pixel +// thick; the RMS inside it brighter; played and unplayed in two colors, +// red while recording, green for the live microphone. +layout(location = 0) out vec4 out_color; + +#include "WaveView.glsl" + +layout(set = 2, binding = 0) readonly buffer Columns { vec4 columns[]; }; + +// Match layouts.Tint. +const uint TINT_RECORDING = 1u; +const uint TINT_LIVE = 2u; + +void main() { + vec2 p = gl_FragCoord.xy; + uint x = uint(p.x); + vec4 col = columns[(x + u.scroll) % uint(u.size.x)]; + if (x + u.filled < uint(u.size.x)) col.w = 0.0; + float mid = u.size.y * 0.5; + float scale = u.size.y * 0.46 * u.gain; + + vec3 color = vec3(0.055, 0.06, 0.07); + if (col.w > 0.0) color = vec3(0.075, 0.08, 0.095); + // Center line. + color = mix(color, vec3(0.2, 0.22, 0.26), clamp(1.0 - abs(p.y - mid), 0.0, 1.0) * 0.6); + + if (col.w > 0.0) { + float top = mid - col.y * scale - 0.5; + float bottom = mid - col.x * scale + 0.5; + float outside = max(top - p.y, p.y - bottom); + float cover = clamp(0.5 - outside, 0.0, 1.0); + float rms = col.z * scale; + float inner = clamp(rms - abs(p.y - mid) + 0.5, 0.0, 1.0); + bool played = u.playhead >= 0.0 && p.x < u.playhead; + vec3 band = played ? vec3(0.35, 0.8, 0.95) : vec3(0.45, 0.5, 0.58); + vec3 core = played ? vec3(0.7, 0.95, 1.0) : vec3(0.65, 0.7, 0.78); + if (u.tint == TINT_RECORDING) { + band = vec3(0.95, 0.35, 0.35); + core = vec3(1.0, 0.6, 0.55); + } else if (u.tint == TINT_LIVE) { + band = vec3(0.35, 0.75, 0.5); + core = vec3(0.65, 1.0, 0.75); + } + color = mix(color, mix(band, core, inner), cover); + } + + // Playhead. + if (u.playhead >= 0.0) { + float d = abs(p.x - u.playhead); + color = mix(color, vec3(1.0, 0.85, 0.3), clamp(1.5 - d, 0.0, 1.0)); + } + out_color = vec4(color, 1.0); +} diff --git a/src/sketch/request.zig b/src/sketch/request.zig new file mode 100644 index 0000000..0b8b400 --- /dev/null +++ b/src/sketch/request.zig @@ -0,0 +1,59 @@ +//! Requests from `update` that `draw` carries out once. `update` sees only +//! State and input, so when a key should do something to Session or Store +//! (start the microphone, open a dialog, seek a stream), State holds the +//! request and Session remembers which ones it has handled: +//! +//! // State +//! seek: sk.Request(f32) = .{}, +//! // Session +//! seek: sk.Handled = .{}, +//! +//! // update +//! if (in.pressed(.home)) s.seek.send(0); +//! // draw +//! if (ss.seek.take(s.seek)) |to| cursor = to * len; +const std = @import("std"); + +/// A request carrying `T`, starting at `T`'s zero value. +pub fn Request(comptime T: type) type { + return struct { + value: T = std.mem.zeroes(T), + /// Bumped by every `send`, so the same value can be asked for twice. + serial: u32 = 0, + + pub const Value = T; + + pub fn send(r: *@This(), value: T) void { + r.value = value; + r.serial +%= 1; + } + }; +} + +/// Which request a Session has carried out. +pub const Handled = struct { + serial: ?u32 = null, + + /// The request's value if it is new to this Session. A fresh Session (a + /// restart, or Session's layout changed) counts earlier requests as + /// handled: nothing re-fires from saved state, as `Input.started` asks + /// of `update`. So take every request on every frame, even when its + /// value will be ignored: the first take only catches up. + pub fn take(h: *Handled, request: anytype) ?@TypeOf(request).Value { + defer h.serial = request.serial; + const last = h.serial orelse return null; + return if (last == request.serial) null else request.value; + } +}; + +test "a request is taken once, and none from before the session" { + var r: Request(f32) = .{}; + r.send(0.5); + var h: Handled = .{}; + try std.testing.expectEqual(@as(?f32, null), h.take(r)); + r.send(0.25); + try std.testing.expectEqual(@as(?f32, 0.25), h.take(r)); + try std.testing.expectEqual(@as(?f32, null), h.take(r)); + r.send(0.25); + try std.testing.expectEqual(@as(?f32, 0.25), h.take(r)); +} diff --git a/src/sketch/root.zig b/src/sketch/root.zig index 0567839..94ce617 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -5,17 +5,21 @@ //! pub const name = "my sketch"; //! pub const width = 960; //! pub const height = 600; -//! pub const State = struct { ... }; // persisted and snapshotted: no pointers -//! pub const Session = struct { ... }; // survives reloads, never saved +//! pub const State = struct { ... }; // saved, snapshotted, replayed: no pointers +//! pub const Session = struct { ... }; // this process: survives reloads +//! pub const Store = struct { ... }; // on disk: survives restarts too //! pub const commands = .{"hello"}; // console completions //! pub fn init(state: *State, host: sk.Host) void //! pub fn update(state: *State, in: *const sk.Input) void -//! pub fn draw(state: *const State, session: *Session, f: *sk.Frame) void -//! pub fn command(state: *State, session: *Session, c: *sk.Console, line: []const u8) !bool +//! pub fn draw(state: *const State, session: *Session, store: *Store, f: *sk.Frame) void +//! pub fn command(state: *State, store: *Store, c: *sk.Console, line: []const u8) !bool //! -//! `update` must be deterministic: the host replays recorded input through it. -//! `draw` presents state as pictures and sound, and may only change `Session`. -//! Geometry and colors are duck-typed: see `shape`. +//! Functions take the parameters they name, by type, in any order: `draw` +//! may ask for `*Session`, `*Store`, both or neither. `update` must be +//! deterministic, so it gets `State` and `Input` only: the host replays +//! recorded input through it. `draw` presents state as pictures and sound, +//! and changes only Session and Store. Geometry and colors are duck-typed: +//! see `shape`. const std = @import("std"); pub const abi = @import("abi"); pub const shape = @import("shape.zig"); @@ -23,6 +27,9 @@ pub const Key = abi.Key; pub const Camera = @import("camera.zig").Camera; pub const tween = @import("tween.zig"); pub const web = @import("web.zig"); +const request = @import("request.zig"); +pub const Request = request.Request; +pub const Handled = request.Handled; pub const Rect = abi.Rect; pub const Color = abi.Color; @@ -32,25 +39,36 @@ pub const Mode = abi.Mode; pub const Host = abi.Host; pub const Image = abi.Image; pub const Audio = abi.Audio; +pub const Buffer = abi.Buffer; pub const Cursor = abi.Cursor; pub const Voice = abi.Voice; -/// What `draw` receives: input, host services, allocators and the command list. +/// What `draw` receives: input, host services, memory by lifetime and the +/// command list. Commands may be built from anything: the methods copy what +/// they keep (text, GPU descriptions) into `scratch`, except bulk data +/// (vertices, samples, pixels, uploads), which must outlive the frame. pub const Frame = struct { ctx: *const abi.Context, in: *const Input, host: Host, - /// Frame lifetime. - arena: std.mem.Allocator, - /// Session lifetime; survives reloads. - gpa: std.mem.Allocator, + /// Released once the frame is drawn. + scratch: std.mem.Allocator, + /// For Session: this process. + session: std.mem.Allocator, + /// For Store: on disk, across restarts. + store: std.mem.Allocator, pub fn init(ctx: *const abi.Context) Frame { - return .{ .ctx = ctx, .in = ctx.input, .host = ctx.host, .arena = ctx.arena, .gpa = ctx.gpa }; + return .{ .ctx = ctx, .in = ctx.input, .host = ctx.host, .scratch = ctx.scratch, .session = ctx.session, .store = ctx.store }; } pub fn emit(f: *Frame, command: abi.Command) void { - f.ctx.commands.append(f.arena, command) catch f.host.log("frame arena full: command dropped"); + f.ctx.commands.append(f.scratch, command) catch f.host.log("out of scratch memory: command dropped"); + } + + /// A scratch copy of `items`. + pub fn keep(f: *Frame, comptime T: type, items: []const T) []const T { + return f.scratch.dupe(T, items) catch &.{}; } pub fn clear(f: *Frame, color: anytype) void { @@ -69,17 +87,15 @@ pub const Frame = struct { .tint = if (@hasField(O, "tint")) shape.color(options.tint) else .white, } }); } - /// `bytes` must live until the frame is consumed: literals, `arena` and - /// session memory all do. pub fn text(f: *Frame, x: f32, y: f32, scale: f32, bytes: []const u8, color: anytype) void { - f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = shape.color(color), .bytes = bytes } }); + f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = shape.color(color), .bytes = f.keep(u8, bytes) } }); } pub fn print(f: *Frame, x: f32, y: f32, scale: f32, color: anytype, comptime fmt: []const u8, args: anytype) void { - f.text(x, y, scale, f.format(fmt, args), color); + f.emit(.{ .text = .{ .x = x, .y = y, .scale = scale, .color = shape.color(color), .bytes = f.format(fmt, args) } }); } - /// Formats into the frame arena. + /// Formats into scratch memory. pub fn format(f: *Frame, comptime fmt: []const u8, args: anytype) []const u8 { - return std.fmt.allocPrint(f.arena, fmt, args) catch "?"; + return f.scratch.print(fmt, args) catch "?"; } /// A rect, or `null` to stop clipping. pub fn clip(f: *Frame, r: anytype) void { @@ -101,18 +117,67 @@ pub const Frame = struct { f.emit(.{ .pixels = .{ .image = img, .w = w, .h = h, .rgba = rgba, .premultiplied = options.premultiplied } }); } /// Runs GPU work described as data (see `abi.gpu`) into `pass.image`. + /// Vertices must outlive the frame; the rest is copied. pub fn gpu(f: *Frame, pass: abi.gpu.Pass) void { - f.emit(.{ .gpu = pass }); + var kept = pass; + kept.label = f.keep(u8, pass.label); + kept.pipelines = f.keep(abi.gpu.Pipeline, pass.pipelines); + const draws = f.scratch.dupe(abi.gpu.Draw, pass.draws) catch return; + for (draws) |*d| { + d.vertex_uniforms = f.keep(u8, d.vertex_uniforms); + d.fragment_uniforms = f.keep(u8, d.fragment_uniforms); + d.vertex_storage = f.keep(abi.Buffer, d.vertex_storage); + d.fragment_storage = f.keep(abi.Buffer, d.fragment_storage); + } + kept.draws = draws; + f.emit(.{ .gpu = kept }); + } + /// `bytes` into a buffer made by `Host.newBuffer`. + pub fn upload(f: *Frame, buffer: abi.Buffer, offset: u32, bytes: []const u8) void { + f.emit(.{ .upload = .{ .buffer = buffer, .offset = offset, .bytes = bytes } }); + } + /// Runs a compute shader; later commands see what it wrote. + pub fn compute(f: *Frame, dispatch: abi.gpu.Dispatch) void { + var kept = dispatch; + kept.label = f.keep(u8, dispatch.label); + kept.readonly = f.keep(abi.Buffer, dispatch.readonly); + kept.readwrite = f.keep(abi.Buffer, dispatch.readwrite); + kept.uniforms = f.keep(u8, dispatch.uniforms); + f.emit(.{ .compute = kept }); + } + /// Keeps the microphone open this frame: next frame's `in.microphone` + /// holds what it heard. + pub fn microphone(f: *Frame) void { + f.emit(.microphone); + } + /// Reads `size` bytes of a GPU buffer back: they arrive in a later + /// frame's `in.downloads` under `key`. Results lag a frame or two, so + /// it suits measurements (a detected pitch, a level), not every pixel. + pub fn download(f: *Frame, buffer: abi.Buffer, options: struct { offset: u32 = 0, size: u32, key: u32 = 0 }) void { + f.emit(.{ .download = .{ .buffer = buffer, .offset = options.offset, .size = options.size, .key = options.key } }); + } + /// Asks the user for a file; the answer comes back as the console line + /// `<command> <path>`. Emit it once per request, not every frame. + pub fn openFile(f: *Frame, command: []const u8, options: struct { pattern: []const u8 = "" }) void { + f.emit(.{ .open_file = .{ .command = f.keep(u8, command), .pattern = f.keep(u8, options.pattern) } }); + } + /// Runs `line` through the console (and so the sketch's `command`) + /// after this frame, before the next update. + pub fn run(f: *Frame, line: []const u8) void { + f.emit(.{ .run = f.keep(u8, line) }); } pub fn quit(f: *Frame) void { f.emit(.quit); } }; -/// What `command` receives: somewhere to answer, and the current input. +/// What `command` receives: somewhere to answer, the current input, and the +/// allocators for Session and Store. pub const Console = struct { host: Host, in: *const Input, + session: std.mem.Allocator, + store: std.mem.Allocator, pub fn log(c: *Console, message: []const u8) void { c.host.log(message); @@ -144,25 +209,64 @@ pub fn Glue(comptime App: type) type { return struct { pub const State = if (@hasDecl(App, "State")) App.State else struct {}; pub const Session = if (@hasDecl(App, "Session")) App.Session else struct {}; + pub const Store = if (@hasDecl(App, "Store")) App.Store else struct {}; pub const description: abi.Info = .{ .abi = abi.hash, .name = if (@hasDecl(App, "name")) App.name else "sketch", .width = if (@hasDecl(App, "width")) App.width else 960, .height = if (@hasDecl(App, "height")) App.height else 600, - .state = abi.layout.of(State, .forbidden), - .session = abi.layout.of(Session, .host_only), + .state = abi.layout.of(State, .state), + .session = abi.layout.of(Session, .session), + .store = abi.layout.of(Store, .store), .commands = if (@hasDecl(App, "commands")) &App.commands else &.{}, }; - fn state(ptr: [*]u8) *State { - return @ptrCast(@alignCast(ptr)); - } - fn session(ptr: [*]u8) *Session { - return @ptrCast(@alignCast(ptr)); - } - fn arity(comptime func: anytype) usize { - return @typeInfo(@TypeOf(func)).@"fn".params.len; + /// What a call can offer, by type. + const Offer = struct { + ctx: *const abi.Context, + state: [*]u8, + frame: ?*Frame = null, + console: ?*Console = null, + line: []const u8 = "", + }; + + /// Calls `func` with the arguments its parameter types ask for. + fn call(comptime func: anytype, comptime name: []const u8, offer: Offer) @typeInfo(@TypeOf(func)).@"fn".return_type.? { + const F = @TypeOf(func); + var args: std.meta.ArgsTuple(F) = undefined; + inline for (@typeInfo(F).@"fn".param_types, 0..) |P, i| { + const T = P orelse @compileError(name ++ ": generic parameters are not supported"); + const is = struct { + fn named(comptime n: []const u8) bool { + return std.mem.eql(u8, name, n); + } + }; + if (comptime is.named("update") and (T == *Session or T == *Store)) + @compileError("update must be deterministic: it gets State and Input only, so a replay reproduces it"); + if (comptime is.named("draw") and T == *State) @compileError("draw only reads State: take *const State"); + if (comptime T == *Frame and !is.named("draw")) @compileError(name ++ " gets no Frame: only draw does"); + if (comptime T == *Console and !is.named("command")) @compileError(name ++ " gets no Console: only command does"); + args[i] = if (T == *State or T == *const State) + @ptrCast(@alignCast(offer.state)) + else if (T == *Session or T == *const Session) + @ptrCast(@alignCast(offer.ctx.session_root)) + else if (T == *Store or T == *const Store) + @ptrCast(@alignCast(offer.ctx.store_root)) + else if (T == *const Input) + offer.ctx.input + else if (T == Host) + offer.ctx.host + else if (T == *Frame) + offer.frame.? + else if (T == *Console) + offer.console.? + else if (T == []const u8) + offer.line + else + @compileError(name ++ ": no argument of type " ++ @typeName(T)); + } + return @call(.auto, func, args); } pub fn info() callconv(.c) *const abi.Info { @@ -170,36 +274,26 @@ pub fn Glue(comptime App: type) type { } pub fn defaults(which: abi.Memory, ptr: [*]u8) callconv(.c) void { switch (which) { - .state => state(ptr).* = .{}, - .session => session(ptr).* = .{}, + .state => @as(*State, @ptrCast(@alignCast(ptr))).* = .{}, + .session => @as(*Session, @ptrCast(@alignCast(ptr))).* = .{}, + .store => @as(*Store, @ptrCast(@alignCast(ptr))).* = .{}, } } pub fn init(ctx: *const abi.Context, s: [*]u8) callconv(.c) void { - if (!@hasDecl(App, "init")) return; - switch (comptime arity(App.init)) { - 1 => App.init(state(s)), - else => App.init(state(s), ctx.host), - } + if (@hasDecl(App, "init")) call(App.init, "init", .{ .ctx = ctx, .state = s }); } pub fn update(ctx: *const abi.Context, s: [*]u8) callconv(.c) void { - if (@hasDecl(App, "update")) App.update(state(s), ctx.input); + if (@hasDecl(App, "update")) call(App.update, "update", .{ .ctx = ctx, .state = s }); } - pub fn draw(ctx: *const abi.Context, s: [*]const u8, ss: [*]u8) callconv(.c) void { + pub fn draw(ctx: *const abi.Context, s: [*]const u8) callconv(.c) void { if (!@hasDecl(App, "draw")) return; var f: Frame = .init(ctx); - const current: *const State = @ptrCast(@alignCast(s)); - switch (comptime arity(App.draw)) { - 2 => App.draw(current, &f), - else => App.draw(current, session(ss), &f), - } + call(App.draw, "draw", .{ .ctx = ctx, .state = @constCast(s), .frame = &f }); } - pub fn command(ctx: *const abi.Context, s: [*]u8, ss: [*]u8, line: *const []const u8) callconv(.c) abi.Outcome { + pub fn command(ctx: *const abi.Context, s: [*]u8, line: *const []const u8) callconv(.c) abi.Outcome { if (!@hasDecl(App, "command")) return .unknown; - var c: Console = .{ .host = ctx.host, .in = ctx.input }; - const taken = switch (comptime arity(App.command)) { - 3 => App.command(state(s), &c, line.*), - else => App.command(state(s), session(ss), &c, line.*), - } catch |err| { + var c: Console = .{ .host = ctx.host, .in = ctx.input, .session = ctx.session, .store = ctx.store }; + const taken = call(App.command, "command", .{ .ctx = ctx, .state = s, .console = &c, .line = line.* }) catch |err| { c.print("{t}. Type help for usage.", .{err}); return .failed; }; @@ -209,6 +303,7 @@ pub fn Glue(comptime App: type) type { } test { + _ = request; _ = abi.layout; _ = shape; _ = @import("camera.zig"); diff --git a/src/sketch/shape.zig b/src/sketch/shape.zig index 9bd4cee..e7575e1 100644 --- a/src/sketch/shape.zig +++ b/src/sketch/shape.zig @@ -52,7 +52,7 @@ pub const Point = struct { x: f32 = 0, y: f32 = 0 }; /// `abi.Rect`. pub fn Out(comptime T: type) type { if (@typeInfo(T) != .@"struct") return T; - for (@typeInfo(T).@"struct".fields) |field| switch (@typeInfo(field.type)) { + for (@typeInfo(T).@"struct".field_types) |F| switch (@typeInfo(F)) { .comptime_int, .comptime_float => return if (isRect(T)) abi.Rect else Point, else => {}, }; @@ -84,19 +84,20 @@ pub fn make(comptime T: type, x: f32, y: f32, w: f32, h: f32) T { .array => |a| return if (a.len == 2) .{ cast(a.child, x), cast(a.child, y) } else .{ cast(a.child, x), cast(a.child, y), cast(a.child, w), cast(a.child, h) }, .@"struct" => { var result: T = undefined; - inline for (@typeInfo(T).@"struct".fields) |field| { - @field(result, field.name) = if (comptime eql(field.name, "x")) - cast(field.type, x) - else if (comptime eql(field.name, "y")) - cast(field.type, y) - else if (comptime eql(field.name, "w") or eql(field.name, "width")) - cast(field.type, w) - else if (comptime eql(field.name, "h") or eql(field.name, "height")) - cast(field.type, h) - else if (field.defaultValue()) |default| + const s = @typeInfo(T).@"struct"; + inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| { + @field(result, name) = if (comptime eql(name, "x")) + cast(F, x) + else if (comptime eql(name, "y")) + cast(F, y) + else if (comptime eql(name, "w") or eql(name, "width")) + cast(F, w) + else if (comptime eql(name, "h") or eql(name, "height")) + cast(F, h) + else if (comptime attrs.defaultValue(F)) |default| default else - @compileError(@typeName(T) ++ "." ++ field.name ++ " has no default"); + @compileError(@typeName(T) ++ "." ++ name ++ " has no default"); } return result; }, diff --git a/src/sketch/tween.zig b/src/sketch/tween.zig index d3b53a1..6d1cef1 100644 --- a/src/sketch/tween.zig +++ b/src/sketch/tween.zig @@ -25,7 +25,7 @@ pub fn lerp(a: anytype, b: @TypeOf(a), t: f32) @TypeOf(a) { }, .@"struct" => |s| blk: { var out: T = undefined; - inline for (s.fields) |field| @field(out, field.name) = lerp(@field(a, field.name), @field(b, field.name), t); + inline for (s.field_names) |name| @field(out, name) = lerp(@field(a, name), @field(b, name), t); break :blk out; }, else => if (t >= 1) b else a, @@ -49,14 +49,14 @@ pub fn lerpFields(a: anytype, b: @TypeOf(a), t: f32, comptime names: anytype) @T pub fn toward(a: anytype, target: anytype, t: f32) @TypeOf(a) { const T = @TypeOf(a); var out = a; - inline for (@typeInfo(@TypeOf(target)).@"struct".fields) |field| { - if (!@hasField(T, field.name)) @compileError(@typeName(T) ++ " has no field '" ++ field.name ++ "' to animate"); - const F = @FieldType(T, field.name); + inline for (@typeInfo(@TypeOf(target)).@"struct".field_names) |name| { + if (!@hasField(T, name)) @compileError(@typeName(T) ++ " has no field '" ++ name ++ "' to animate"); + const F = @FieldType(T, name); const goal: F = switch (@typeInfo(F)) { - .@"struct" => toward(@field(a, field.name), @field(target, field.name), 1), - else => @field(target, field.name), + .@"struct" => toward(@field(a, name), @field(target, name), 1), + else => @field(target, name), }; - @field(out, field.name) = lerp(@field(a, field.name), goal, t); + @field(out, name) = lerp(@field(a, name), goal, t); } return out; } 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 &.{}; } diff --git a/src/vector/Gpu.zig b/src/vector/Gpu.zig index a557537..5853145 100644 --- a/src/vector/Gpu.zig +++ b/src/vector/Gpu.zig @@ -19,8 +19,8 @@ const shaders = @import("vector_shaders"); pub const samples = 8; -const vertex: abi.gpu.Shader = .{ .spirv = shaders.path_vert, .uniform_buffers = 1 }; -const stencil_only: abi.gpu.Shader = .{ .spirv = shaders.stencil_frag }; +const vertex: abi.gpu.Shader = .{ .code = shaders.path_vert, .uniform_buffers = 1 }; +const stencil_only: abi.gpu.Shader = .{ .code = shaders.stencil_frag }; const pipelines = [_]abi.gpu.Pipeline{ // 0: nonzero winding. @@ -42,7 +42,7 @@ const pipelines = [_]abi.gpu.Pipeline{ // 2: cover. abi.gpu.pipeline(layouts.Vertex, .{ .vertex = vertex, - .fragment = .{ .spirv = shaders.cover_frag, .uniform_buffers = 1 }, + .fragment = .{ .code = shaders.cover_frag, .uniform_buffers = 1 }, .stencil = .{ .front = .{ .compare = .not_equal, .pass = .zero }, .back = .{ .compare = .not_equal, .pass = .zero }, @@ -116,8 +116,8 @@ fn block(paint: *const Paint, alpha: f32) layouts.Paint { const m = paint.to_gradient; var out: layouts.Paint = .{ .color = paint.color, - .row0 = .{ m.a, m.c, m.e, @floatFromInt(@intFromEnum(paint.kind)) }, - .row1 = .{ m.b, m.d, m.f, @floatFromInt(@intFromEnum(paint.spread)) }, + .row0 = .{ m.a, m.c, m.e, @floatFromInt(@backingInt(paint.kind)) }, + .row1 = .{ m.b, m.d, m.f, @floatFromInt(@backingInt(paint.spread)) }, .points = .{ paint.p0[0], paint.p0[1], paint.p1[0], paint.p1[1] }, .extra = .{ paint.radius, alpha, 0, 0 }, .offsets = @splat(@splat(0)), @@ -157,7 +157,7 @@ pub fn pass(g: *const Gpu) abi.gpu.Pass { test "a fill is a stencil fan and a covering quad with its paint" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); - var g: Gpu = .{ .width = 100, .height = 50, .image = @enumFromInt(1) }; + var g: Gpu = .{ .width = 100, .height = 50, .image = @fromBackingInt(@intCast(1)) }; g.begin(arena.allocator(), .{ 0, 0, 0, 1 }); var p: Polygons = .init(arena.allocator()); try p.points.appendSlice(arena.allocator(), &.{ .{ 10, 10 }, .{ 40, 10 }, .{ 40, 30 }, .{ 10, 30 } }); diff --git a/src/vector/root.zig b/src/vector/root.zig index 7556948..0414286 100644 --- a/src/vector/root.zig +++ b/src/vector/root.zig @@ -69,12 +69,12 @@ pub fn Renderer(comptime backend: Backend) type { return r.target.height; } - /// Starts a picture. `f.arena` holds what the GPU pass needs until + /// Starts a picture. `f.scratch` holds what the GPU pass needs until /// the frame is consumed. pub fn begin(r: *Self, f: anytype, clear: Drawing.Color) void { switch (backend) { .cpu => r.target.clear(clear), - .gpu => r.target.begin(f.arena, clear), + .gpu => r.target.begin(f.scratch, clear), } } |
