diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 21:34:41 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-12 13:22:02 -0300 |
| commit | 2348776791e9dd17afeb12a06ba42a4a71917bcf (patch) | |
| tree | 780c4b12b76c8b97a28408dd40944d65f159fdc7 /src/host | |
| parent | 590ba7b9b579b97e34b51ab7e3460ebc85713343 (diff) | |
| download | sketchbook-2348776791e9dd17afeb12a06ba42a4a71917bcf.tar.gz sketchbook-2348776791e9dd17afeb12a06ba42a4a71917bcf.zip | |
sketchbook: a drawing layer on top, host fixes for replay and migration
Diffstat (limited to 'src/host')
| -rw-r--r-- | src/host/Console.zig | 26 | ||||
| -rw-r--r-- | src/host/Host.zig | 120 | ||||
| -rw-r--r-- | src/host/fields.zig | 137 | ||||
| -rw-r--r-- | src/host/gfx.zig | 475 | ||||
| -rw-r--r-- | src/host/gpu.zig | 74 | ||||
| -rw-r--r-- | src/host/gpu.zig.orig | 641 | ||||
| -rw-r--r-- | src/host/memory.zig | 482 | ||||
| -rw-r--r-- | src/host/overlay.zig | 13 | ||||
| -rw-r--r-- | src/host/probe.zig | 26 | ||||
| -rw-r--r-- | src/host/root.zig | 16 | ||||
| -rw-r--r-- | src/host/script.zig | 200 | ||||
| -rw-r--r-- | src/host/timeline.zig | 216 |
12 files changed, 2245 insertions, 181 deletions
diff --git a/src/host/Console.zig b/src/host/Console.zig index b22facb..40845f7 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -133,7 +133,7 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const rest = words.rest(); if (eq(command, "help")) { - self.log("save|load [1..7] snapshot N | record|play [0..15] recording N | stop | pause | step N | seek start|end|<ms>"); + self.log("save|load [1..7] snapshot N | record|play [0..15] recording N | stop | pause | step N (frames) | seek start|end|<ms>"); self.log("diff [N] | reset | get <field> | set <field> <value> | overlay memory|sound | quit"); if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); return &.{}; @@ -145,10 +145,12 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const self.print("{s} overlay: {s}", .{ name, if (flag.*) "on" else "off" }); } else if (eq(command, "get")) { const path = arg orelse return error.ExpectedField; - const leaf = host.state.layout.find(path) orelse return error.UnknownField; var buf: [160]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - fields.format(leaf, host.live(), &w) catch {}; + fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { + error.UnknownField => return error.UnknownField, + error.WriteFailed => {}, // shown cut off + }; self.print("{s} = {s}", .{ path, w.buffered() }); } else if (eq(command, "set")) { const path = arg orelse return error.ExpectedField; @@ -176,16 +178,17 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const h.bank.save(index); self.print("Saved snapshot {d}. Recordings from its old contents are invalidated.", .{index}); } else { - try h.bank.restore(index); + try h.restore(index); self.print("Loaded snapshot {d}.", .{index}); } } else if (eq(command, "record") or eq(command, "play")) { const index = if (arg) |a| try slot(a, 0, timeline.recording_count - 1) else self.recording; if (eq(command, "play")) _ = try h.bank.get(index); h.stop(now); - if (eq(command, "record")) try h.startRecording(index, self.snapshot, now) else try h.play(index, now); + if (eq(command, "record")) try h.startRecording(index, self.snapshot, now, host.build) else try h.play(index, now); self.recording = index; self.print("{s} recording {d} from snapshot {d}.", .{ if (eq(command, "record")) "Started" else "Playing", index, h.bank.recording(index).snapshot }); + if (eq(command, "play")) self.warnBuild(host, h.bank.recording(index)); } else if (eq(command, "stop")) { h.stop(now); } else if (eq(command, "pause")) { @@ -197,6 +200,7 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const if (h.mode == .recording) h.stop(now); if (h.mode == .live) try h.play(self.recording, now); const r = try h.bank.get(h.selected); + if (h.mode == .playing and h.cursor == 0) self.warnBuild(host, r); if (eq(command, "step")) return h.step(std.fmt.parseInt(i64, value, 10) catch return error.InvalidPosition); if (eq(value, "start")) return h.seekCount(0); if (eq(value, "end")) return h.seekCount(r.count); @@ -206,7 +210,12 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const index = if (arg) |a| try slot(a, 1, memory.slot_count - 1) else self.snapshot; if (!h.bank.hasSnapshot(index)) return error.EmptySnapshot; const diff = h.bank.compare(0, index); - self.print("Live vs snapshot {d}: {d} differing bytes; first={any}, last={any}", .{ index, diff.count, diff.first, diff.last }); + if (diff.first) |first| { + var buf: [96]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + first.leaf.name(first.offset, &w) catch {}; + self.print("Live vs snapshot {d}: {d} differing values, first {s}", .{ index, diff.count, w.buffered() }); + } else self.print("Live vs snapshot {d}: same", .{index}); } else { // Sketch commands edit state outside the recorded input. h.stop(now); @@ -237,6 +246,11 @@ pub fn draw(self: *const Console, host: *Host, out: *std.ArrayList(abi.Command), try out.append(arena, .{ .rect = .{ .dst = .{ .x = @floatFromInt(28 + self.cursor * 8), .y = top + 120, .w = 1, .h = 12 }, .color = hint } }); } +/// A replay of a recording made by other code may diverge: say so. +fn warnBuild(self: *Console, host: *const Host, r: *const timeline.Recording) void { + if (r.build() != 0 and host.build != 0 and r.build() != host.build) self.log("Warning: recorded by another build of the sketch; the replay may differ."); +} + fn eq(a: []const u8, b: []const u8) bool { return std.mem.eql(u8, a, b); } diff --git a/src/host/Host.zig b/src/host/Host.zig index 7ebe54a..de19b99 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -6,8 +6,9 @@ const std = @import("std"); const c = @import("c"); const abi = @import("abi"); const memory = @import("memory.zig"); -const Timeline = @import("timeline.zig").Timeline; -const mapRecordings = @import("timeline.zig").mapRecordings; +const playback = @import("timeline.zig"); +const Timeline = playback.Timeline; +const mapRecordings = playback.mapRecordings; const gfx = @import("gfx.zig"); const sound = @import("audio.zig"); const Gpu = @import("gpu.zig"); @@ -44,6 +45,8 @@ passes: ?Gpu = null, offscreen: ?*c.SDL_Texture = null, pointer_cursor: ?*c.SDL_Cursor = null, images: gfx.Images, +/// Plays the 2D commands: targets, blend, antialiased shapes. +painter: gfx.Painter, audio: sound.Audio, state: memory.StateFile, session: memory.Session, @@ -58,6 +61,9 @@ sketch: abi.Sketch, library: ?Library = null, watch: ?Watch = null, reloads: usize = 0, +/// Identifies the running build of the sketch (a hash of its library or +/// executable file): recordings carry it, and `play` warns on a mismatch. +build: u64 = 0, /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, name: []const u8 = "", @@ -73,6 +79,9 @@ canvas: [2]u32 = .{ 0, 0 }, cursor: abi.Cursor = .arrow, quit: bool = false, reloaded: bool = true, +/// For `Input.jumped`: state changed other than by `update` since the +/// last `draw` (a seek, a load, a `set`...). The first frame is one. +jumped: bool = true, started: bool = true, last_now: u64 = 0, /// Saved after the next frame is drawn (scripts). @@ -115,8 +124,10 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std std.log.warn("no GPU renderer ({s}); drawing without one", .{c.SDL_GetError()}); renderer = c.SDL_CreateRenderer(window, null) orelse return sdlError("SDL_CreateRenderer"); } - errdefer c.SDL_DestroyRenderer(renderer); + // Deferred in reverse: the renderer goes first, then the device it + // draws through (the other order crashed in SDL_DestroyRenderer). errdefer if (gpu) |device| c.SDL_DestroyGPUDevice(device); + errdefer c.SDL_DestroyRenderer(renderer); if (!options.headless) _ = c.SDL_SetRenderVSync(renderer, 1); // Colors carry alpha: fills blend, as they do on the web. _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); @@ -142,6 +153,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .renderer = renderer.?, .gpu = gpu, .images = .{ .gpa = gpa, .renderer = renderer.? }, + .painter = .{ .renderer = renderer.?, .images = undefined, .smooth = gpu != null }, .audio = .{ .gpa = gpa }, .state = state, .session = .init(gpa), @@ -154,6 +166,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .frame_arena = .init(gpa), }; self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; + self.painter.images = &self.images; if (gpu) |device| self.passes = .init(gpa, device); if (options.profile_gpu) if (self.passes) |*g| { g.profile = .init(gpa); @@ -164,6 +177,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std }; self.setCanvas(info.width, info.height); _ = c.SDL_StartTextInput(self.window); + self.build = fileHash(options.library orelse "/proc/self/exe"); try self.adopt(sketch); if (options.fresh) { self.state.header().used = 0; @@ -177,6 +191,7 @@ pub fn deinit(self: *Host) void { if (self.watch) |w| w.deinit(); self.audio.deinit(); if (self.passes) |*g| g.deinit(&self.images); + self.painter.deinit(); self.images.deinit(); if (self.pointer_cursor) |cursor| c.SDL_DestroyCursor(cursor); if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); @@ -229,10 +244,10 @@ pub fn frame(self: *Host, now: u64) !void { .playing => self.update(h.tick(now)), .paused => {}, .live, .recording => { - try self.pending.append(self.gpa, .{ .kind = .tick, .time = now, .x = @floatCast(@as(f64, @floatFromInt(dt)) / std.time.ns_per_s) }); - for (self.pending.items) |e| h.capture(e) catch |err| self.print("Recording stopped: {t}", .{err}); - self.input.now = now; - self.input.dt = dt; + // One frame: what was routed since the last one, then its tick. + // Recorded first, so `update` sees the times a replay will. + try self.pending.append(self.gpa, abi.Event.tick(now, dt)); + h.capture(self.pending.items) catch |err| self.print("Recording stopped: {t}", .{err}); self.update(self.pending.items); }, } @@ -240,26 +255,60 @@ pub fn frame(self: *Host, now: u64) !void { try self.draw(if (h.mode == .live or h.mode == .recording) now else h.position); } -/// Feeds events to the sketch's deterministic `update`. +/// Feeds whole frames to the sketch's deterministic `update`, one call per +/// frame (events up to and including a tick), live or replayed alike. pub fn update(self: *Host, events: []const abi.Event) void { - // Input follows the events actually fed, live or replayed. - for (events) |e| self.input.track(e); - if (events.len > 0 and self.timeline.mode != .live and self.timeline.mode != .recording) self.input.now = events[events.len - 1].time; - self.input.events = events; - self.input.mode = self.timeline.mode; - self.input.started = self.started; - self.started = false; - const ctx = self.context(); - self.sketch.update(&ctx, self.live().ptr); + var frames = playback.frames(events); + while (frames.next()) |one| { + // Input follows the events actually fed, frame by frame. + for (one) |e| self.input.track(e); + const tick = one[one.len - 1]; + self.input.now = tick.time; + self.input.dt = tick.tickDt(); + self.input.events = one; + self.input.mode = self.timeline.mode; + self.input.started = self.started; + self.started = false; + const ctx = self.context(); + self.sketch.update(&ctx, self.live().ptr); + } self.input.events = &.{}; } +/// After live state was restored from a snapshot: the pointer, buttons and +/// keys tracked so far belong to other bytes, so they start over. A new +/// recording then begins with events that bring back what is held now, so +/// its replay starts from the same input (they are fed and recorded as the +/// first frame's first events). +fn restoredInput(self: *Host, now: u64) !void { + const h = &self.timeline; + if (!h.restored) return; + h.restored = false; + const was = self.input; + self.input.mouse_x = 0; + self.input.mouse_y = 0; + self.input.buttons = 0; + self.input.keys = .empty; + if (h.mode != .recording or h.bank.recording(h.selected).count != 0) return; + var seed: std.ArrayList(abi.Event) = .empty; + defer seed.deinit(self.gpa); + if (was.mouse_x != 0 or was.mouse_y != 0) try seed.append(self.gpa, .{ .kind = .mouse_move, .time = now, .x = was.mouse_x, .y = was.mouse_y }); + for (0..32) |bit| if (was.buttons & (@as(u32, 1) << @intCast(bit)) != 0) { + try seed.append(self.gpa, .{ .kind = .mouse_down, .time = now, .x = was.mouse_x, .y = was.mouse_y, .code = @intCast(bit + 1) }); + }; + var keys = was.keys.iterator(.{}); + while (keys.next()) |key| try seed.append(self.gpa, .{ .kind = .key_down, .time = now, .code = @intCast(key) }); + try self.pending.insertSlice(self.gpa, 0, seed.items); +} + fn draw(self: *Host, now: u64) !void { const arena = self.frame_arena.allocator(); self.input.now = now; self.input.mode = self.timeline.mode; self.input.reloaded = self.reloaded; + self.input.jumped = self.jumped or self.reloaded; self.reloaded = false; + self.jumped = false; self.input.console = self.console.visible; self.input.width = @floatFromInt(self.canvas[0]); self.input.height = @floatFromInt(self.canvas[1]); @@ -286,9 +335,14 @@ fn draw(self: *Host, now: u64) !void { _ = c.SDL_SetRenderDrawColor(self.renderer, 0, 0, 0, 255); _ = c.SDL_RenderClear(self.renderer); self.audio.beginFrame(); + self.painter.begin(self.offscreen, self.canvas, arena); var lines: std.ArrayList([]const u8) = .empty; for (self.commands.items) |command| switch (command) { - .canvas => |size| self.setCanvas(size.w, size.h), + .canvas => |size| { + self.painter.end(); + self.setCanvas(size.w, size.h); + self.painter.begin(self.offscreen, self.canvas, arena); + }, .cursor => |kind| self.cursor = kind, .voice => |voice| self.audio.declare(voice), .audio => |a| self.audio.push(a.key, a.samples, a.clear), @@ -300,11 +354,11 @@ fn draw(self: *Host, now: u64) !void { .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), + else => self.painter.draw(command), }; + self.painter.end(); 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; if (self.console.overlay_memory) try overlay.drawMemory(&extra, arena, self.state.layout, self.live(), self.input.width); @@ -313,7 +367,8 @@ fn draw(self: *Host, now: u64) !void { try overlay.drawScope(&extra, arena, &samples, self.audio.scope.rate, self.canvas); } if (self.console.visible) try self.console.draw(self, &extra, arena); - for (extra.items) |command| gfx.draw(self.renderer, &self.images, command); + for (extra.items) |command| self.painter.draw(command); + self.painter.end(); self.applyCursor(); if (self.screenshot) |path| { @@ -432,11 +487,25 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { const replay = self.console.execute(self, line, now) catch |err| { // The sketch has printed its own error already. if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); + if (self.timeline.restored) self.jumped = true; + try self.restoredInput(now); return err; }; + if (self.timeline.restored or movesState(line)) self.jumped = true; + try self.restoredInput(now); if (self.timeline.mode == .paused) self.update(replay); } +/// Whether a console line that succeeded may have changed State other than +/// through `update` (restoring a snapshot is caught apart from this). +fn movesState(line: []const u8) bool { + var words = std.mem.tokenizeAny(u8, line, " \t"); + const verb = words.next() orelse return false; + const still = [_][]const u8{ "help", "overlay", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit" }; + for (still) |w| if (std.mem.eql(u8, verb, w)) return false; + return true; +} + /// Offers a line to the sketch. pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { self.input.now = now; @@ -467,6 +536,7 @@ pub fn load(self: *Host, path: []const u8) !void { if (self.library) |*old| old.close(); self.library = library; self.reloads += 1; + self.build = fileHash(path); self.print("reload {d}: {s}", .{ self.reloads, path }); } @@ -636,6 +706,14 @@ fn consoleKey(e: abi.Event) ?Console.Key { }; } +/// A hash of a file's bytes; 0 when it cannot be read. +fn fileHash(path: []const u8) u64 { + const bytes = memory.mapReadOnly(path); + if (bytes.len == 0) return 0; + defer std.posix.munmap(bytes); + return std.hash.Wyhash.hash(0, bytes) | 1; +} + fn scratchDir(environ: *const std.process.Environ.Map) []const u8 { return environ.get("XDG_RUNTIME_DIR") orelse "/tmp"; } diff --git a/src/host/fields.zig b/src/host/fields.zig index ba8fc03..2e70414 100644 --- a/src/host/fields.zig +++ b/src/host/fields.zig @@ -2,7 +2,82 @@ //! Shared by the console, the memory overlay, scripts and the probe. const std = @import("std"); const abi = @import("abi"); -const Leaf = @import("memory.zig").Layout.Leaf; +const Layout = @import("memory.zig").Layout; +const Leaf = Layout.Leaf; + +/// Writes what `path` names: one value for a number, bool or enum leaf, +/// or every leaf under it for an array, a vector or a struct, in memory +/// order (for a Zig struct not always the declared one): `{ 250, 3 }` +/// when they are its elements, otherwise `{ .count = 12, .hz = 60 }` or +/// `{ [0].x = 1, [1].x = 2 }`. +/// Shows the first `max_values`, then how many more there are. +pub fn formatPath(layout: Layout, memory: []const u8, path: []const u8, w: *std.Io.Writer) (std.Io.Writer.Error || error{UnknownField})!void { + if (layout.find(path)) |leaf| if (!leaf.whole()) return format(leaf, memory, w); + // Names under `path`, found leaf by leaf; sorted by offset afterwards. + const Found = struct { offset: usize, leaf: Leaf }; + var found: [max_values]Found = undefined; + var count: usize = 0; + var total: usize = 0; + var plain = true; + var name_buf: [256]u8 = undefined; + for (layout.fields) |leaf| { + if (leaf.whole()) continue; + var it = leaf.elements(); + while (it.next()) |offset| { + var nw: std.Io.Writer = .fixed(&name_buf); + leaf.name(offset, &nw) catch continue; + const name = nw.buffered(); + if (!under(name, path)) continue; + total += 1; + if (!isIndex(name[path.len..])) plain = false; + if (count < max_values) { + found[count] = .{ .offset = offset, .leaf = leaf }; + count += 1; + } else { + // Keep the lowest offsets. + var highest: usize = 0; + for (found[1..], 1..) |f, i| if (f.offset > found[highest].offset) { + highest = i; + }; + if (offset < found[highest].offset) found[highest] = .{ .offset = offset, .leaf = leaf }; + } + } + } + if (total == 0) return error.UnknownField; + std.mem.sortUnstable(Found, found[0..count], {}, struct { + fn less(_: void, a: Found, b: Found) bool { + return a.offset < b.offset; + } + }.less); + try w.writeAll("{ "); + for (found[0..count], 0..) |f, i| { + if (i > 0) try w.writeAll(", "); + if (!plain) { + var nw: std.Io.Writer = .fixed(&name_buf); + f.leaf.name(f.offset, &nw) catch {}; + try w.print("{s} = ", .{nw.buffered()[path.len..]}); + } + try format(f.leaf.at(f.offset), memory, w); + } + if (total > count) try w.print(", ... {d} more", .{total - count}); + try w.writeAll(" }"); +} + +pub const max_values = 64; + +/// `name` is `path` itself or a field or element inside it. +fn under(name: []const u8, path: []const u8) bool { + if (!std.mem.startsWith(u8, name, path)) return false; + if (name.len == path.len) return true; + return name[path.len] == '.' or name[path.len] == '['; +} + +/// `[3]`: one index and nothing more. +fn isIndex(suffix: []const u8) bool { + if (suffix.len < 3 or suffix[0] != '[' or suffix[suffix.len - 1] != ']') return false; + for (suffix[1 .. suffix.len - 1]) |ch| if (!std.ascii.isDigit(ch)) return false; + return true; +} pub fn format(leaf: Leaf, memory: []const u8, w: *std.Io.Writer) std.Io.Writer.Error!void { const bytes = leaf.bytes(memory); @@ -64,6 +139,22 @@ pub fn matches(leaf: Leaf, memory: []const u8, text: []const u8) bool { return false; } +/// A number, bool or enum leaf's value as a float; null for other kinds. +pub fn number(leaf: Leaf, memory: []const u8) ?f64 { + const bytes = leaf.bytes(memory); + return switch (leaf.kind) { + .int, .@"enum" => @floatFromInt(readInt(i64, bytes)), + .uint => @floatFromInt(readInt(u64, bytes)), + .bool => if (bytes[0] != 0) 1 else 0, + .float => switch (leaf.size) { + 4 => @as(f32, @bitCast(bytes[0..4].*)), + 8 => @as(f64, @bitCast(bytes[0..8].*)), + else => null, + }, + else => null, + }; +} + pub fn readInt(comptime T: type, bytes: []const u8) T { const n: usize = @min(bytes.len, 8); const value = std.mem.readVarInt(u64, bytes[0..n], .little); @@ -114,3 +205,47 @@ test "fields format, parse and compare by kind" { try std.testing.expect(s.on); try std.testing.expectError(error.InvalidValue, set(layout.find("x").?, bytes, "banana")); } + +test "indexed paths reach array elements and vector lanes" { + const S = struct { pos: @Vector(2, f32) = .{ 250, 3 }, boids: struct { buffer: [4]struct { pos: [2]f32 = .{ 0, 0 }, mood: enum(u8) { calm = 1, wild = 2 } = .calm } = @splat(.{}), len: u32 = 0 } = .{} }; + 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, .state)); + var s: S = .{}; + const bytes = std.mem.asBytes(&s); + try std.testing.expect(matches(layout.find("pos[0]").?, bytes, "250")); + try std.testing.expectEqual(@as(?f64, 3), number(layout.find("pos[1]").?, bytes)); + try set(layout.find("boids.buffer[3].pos[1]").?, bytes, "20.5"); + try std.testing.expectEqual(@as(f32, 20.5), s.boids.buffer[3].pos[1]); + try set(layout.find("boids.buffer[2].mood").?, bytes, "wild"); + try std.testing.expectEqual(.wild, s.boids.buffer[2].mood); + try std.testing.expect(layout.find("boids.buffer[4].mood") == null); + try std.testing.expect(layout.find("pos[2]") == null); +} + +test "paths to vectors, arrays and structs print every value under them" { + const S = struct { pos: @Vector(2, f32) = .{ 250, 3 }, clock: struct { hz: f32 = 60, count: u64 = 12 } = .{}, list: struct { buffer: [3]struct { x: i32 = 1, on: bool = true } = @splat(.{}), len: u32 = 2 } = .{}, big: [100]u8 = @splat(7) }; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const layout = try Layout.copy(arena.allocator(), abi.layout.of(S, .state)); + var s: S = .{}; + s.list.buffer[1].x = -4; + const bytes = std.mem.asBytes(&s); + var buf: [1024]u8 = undefined; + const cases = [_][2][]const u8{ + .{ "pos", "{ 250, 3 }" }, + .{ "pos[1]", "3" }, + .{ "clock", "{ .count = 12, .hz = 60 }" }, // memory order + .{ "list.buffer[1]", "{ .x = -4, .on = true }" }, + .{ "list.buffer", "{ [0].x = 1, [0].on = true, [1].x = -4, [1].on = true, [2].x = 1, [2].on = true }" }, + }; + for (cases) |case| { + var w: std.Io.Writer = .fixed(&buf); + try formatPath(layout, bytes, case[0], &w); + try std.testing.expectEqualStrings(case[1], w.buffered()); + } + var w: std.Io.Writer = .fixed(&buf); + try formatPath(layout, bytes, "big", &w); + try std.testing.expect(std.mem.endsWith(u8, w.buffered(), "7, 7, ... 36 more }")); + try std.testing.expectError(error.UnknownField, formatPath(layout, bytes, "po", &w)); +} diff --git a/src/host/gfx.zig b/src/host/gfx.zig index 16148a6..20b4dcc 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -14,10 +14,29 @@ pub const Images = struct { texture: ?*c.SDL_Texture = null, w: u32 = 0, h: u32 = 0, + /// Holds premultiplied colour: a target drawn into, a GPU pass's + /// output, or premultiplied `pixels`. + premultiplied: bool = false, + /// Made by `new`: a render target for `Command.target`. + target: bool = false, + /// Scales smoothly (GPU pass output), not nearest. + smooth: bool = false, + /// A target's supersampled self (`factor` times the size), which + /// 2D commands draw into; `texture` is its average, brought up to + /// date (`stale`) before the image is drawn. + big: ?*c.SDL_Texture = null, + factor: u32 = 1, + stale: bool = false, + + fn release(entry: *Entry) void { + if (entry.texture) |t| c.SDL_DestroyTexture(t); + if (entry.big) |t| c.SDL_DestroyTexture(t); + entry.* = .{}; + } }; pub fn deinit(self: *Images) void { - for (self.entries.items) |entry| if (entry.texture) |t| c.SDL_DestroyTexture(t); + for (self.entries.items) |*entry| entry.release(); self.entries.deinit(self.gpa); var keys = self.paths.keyIterator(); while (keys.next()) |key| self.gpa.free(key.*); @@ -41,10 +60,20 @@ pub const Images = struct { return self.add(c.SDL_LoadPNG_IO(io, true)); } + /// A blank (transparent) image, which `Command.pixels` fills and + /// `Command.target` draws into: a render target, cleared now, since a + /// new SDL texture's contents are undefined (a canvas starts clear). pub fn new(self: *Images, w: u32, h: u32) abi.Image { - const texture = c.SDL_CreateTexture(self.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_STREAMING, @intCast(w), @intCast(h)); - if (texture) |t| setup(t); - return self.append(.{ .texture = texture, .w = w, .h = h }); + const texture = c.SDL_CreateTexture(self.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_TARGET, @intCast(w), @intCast(h)); + if (texture) |t| { + setup(t); + const previous = c.SDL_GetRenderTarget(self.renderer); + _ = c.SDL_SetRenderTarget(self.renderer, t); + _ = c.SDL_SetRenderDrawColor(self.renderer, 0, 0, 0, 0); + _ = c.SDL_RenderClear(self.renderer); + _ = c.SDL_SetRenderTarget(self.renderer, previous); + } + return self.append(.{ .texture = texture, .w = w, .h = h, .premultiplied = true, .target = texture != null }); } /// Makes `image` show a GPU render target (GPU renderer only). @@ -59,9 +88,8 @@ pub const Images = struct { const texture = c.SDL_CreateTextureWithProperties(self.renderer, props) orelse return error.WrapTexture; // Rendered output: smooth scaling, and already premultiplied. _ = c.SDL_SetTextureScaleMode(texture, c.SDL_SCALEMODE_LINEAR); - _ = c.SDL_SetTextureBlendMode(texture, c.SDL_BLENDMODE_BLEND_PREMULTIPLIED); - if (entry.texture) |old| c.SDL_DestroyTexture(old); - entry.* = .{ .texture = texture, .w = w, .h = h }; + entry.release(); + entry.* = .{ .texture = texture, .w = w, .h = h, .premultiplied = true, .smooth = true }; } /// Stops showing a wrapped render target, before the target goes away. @@ -73,8 +101,7 @@ pub const Images = struct { pub fn free(self: *Images, image: abi.Image) void { const entry = self.get(image) orelse return; - if (entry.texture) |t| c.SDL_DestroyTexture(t); - entry.* = .{}; + entry.release(); } pub fn size(self: *Images, image: abi.Image) [2]u32 { @@ -104,47 +131,397 @@ pub const Images = struct { fn setup(texture: *c.SDL_Texture) void { _ = c.SDL_SetTextureScaleMode(texture, c.SDL_SCALEMODE_NEAREST); - _ = c.SDL_SetTextureBlendMode(texture, c.SDL_BLENDMODE_BLEND); } }; -/// Draws one graphics command. Other commands are not graphics: ignored. -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); - }, - .rect => |r| { - setColor(renderer, r.color); - _ = c.SDL_RenderFillRect(renderer, &frect(r.dst)); - }, - .image => |img| { - const entry = images.get(img.image); - const texture = if (entry) |e| e.texture else null; - const t = texture orelse return missing(renderer, img.dst); - _ = c.SDL_SetTextureColorMod(t, img.tint.r, img.tint.g, img.tint.b); - _ = c.SDL_SetTextureAlphaMod(t, img.tint.a); - const src = frect(img.src); - _ = c.SDL_RenderTexture(renderer, t, if (img.src.w > 0) &src else null, &frect(img.dst)); - }, - .text => |text| drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color), - .clip => |clip| { - if (clip) |r| { - const rect: c.SDL_Rect = .{ .x = @intFromFloat(r.x), .y = @intFromFloat(r.y), .w = @intFromFloat(r.w), .h = @intFromFloat(r.h) }; - _ = c.SDL_SetRenderClipRect(renderer, &rect); - } else _ = c.SDL_SetRenderClipRect(renderer, null); - }, - .pixels => |p| { - const entry = images.get(p.image) orelse return; - const texture = entry.texture orelse return; - if (p.w != entry.w or p.h != entry.h or p.rgba.len < @as(usize, p.w) * p.h * 4) return; - _ = c.SDL_SetTextureBlendMode(texture, if (p.premultiplied) c.SDL_BLENDMODE_BLEND_PREMULTIPLIED else c.SDL_BLENDMODE_BLEND); - _ = c.SDL_UpdateTexture(texture, null, p.rgba.ptr, @intCast(p.w * 4)); - }, - else => {}, +/// Plays 2D commands into the renderer, keeping what they leave behind +/// (target, blend) and the antialiasing textures. One per host; `begin` +/// and `end` bracket a frame. +/// +/// Antialiasing (with a GPU renderer) is supersampling, 4x4 samples a +/// pixel, averaged by halving twice (bilinear sampling at the centre of a +/// 2x2 block is their exact mean), so coverage resolves per sample: +/// - On the canvas, consecutive shapes are drawn into `layer` and +/// composited together, premultiplied, when another command comes. +/// - A target is supersampled whole (`Entry.big`): everything drawn into +/// it is, and it is averaged when drawn. Its picture is therefore the +/// same however its commands were split into frames, so a trail +/// repainted at once after a seek equals the one painted step by step. +pub const Painter = struct { + renderer: *c.SDL_Renderer, + images: *Images, + /// The canvas's render target: null for the window, or the headless + /// host's offscreen texture. + canvas: ?*c.SDL_Texture = null, + canvas_size: [2]u32 = .{ 0, 0 }, + /// Antialias (a GPU renderer); false draws everything aliased. + smooth: bool = false, + /// Per-vertex colours, for one frame. + arena: std.mem.Allocator = undefined, + target: abi.Image = .none, + blend: abi.BlendMode = .alpha, + layer: Layer = .{}, + /// Half-way through averaging (2x), shared; `half_size` in its pixels. + half: ?*c.SDL_Texture = null, + half_size: [2]u32 = .{ 0, 0 }, + + const Layer = struct { + big: ?*c.SDL_Texture = null, + /// Room in `big`, in its pixels. + w: u32 = 0, + h: u32 = 0, + /// Open: shapes went into it since the last composite. + open: bool = false, + blend: abi.BlendMode = .alpha, + factor: u32 = 4, + /// What the open layer's shapes cover, in canvas pixels (x0, y0, + /// x1, y1); after a composite, what still holds them, cleared + /// when the next layer opens. + dirty: [4]u32 = .{ 0, 0, 0, 0 }, + }; + + /// 16 samples a pixel, unless that takes more than 16 Mi of them; 4, + /// then; else none. + const max_samples = 16 << 20; + + fn factorFor(size: [2]u32) u32 { + const pixels = @as(u64, size[0]) * size[1]; + return if (pixels * 16 <= max_samples) 4 else if (pixels * 4 <= max_samples) 2 else 1; + } + + pub fn deinit(p: *Painter) void { + if (p.layer.big) |t| c.SDL_DestroyTexture(t); + if (p.half) |t| c.SDL_DestroyTexture(t); + p.layer = .{}; + p.half = null; + p.half_size = .{ 0, 0 }; + } + + pub fn begin(p: *Painter, canvas: ?*c.SDL_Texture, size: [2]u32, arena: std.mem.Allocator) void { + p.canvas = canvas; + p.canvas_size = size; + p.arena = arena; + p.target = .none; + p.blend = .alpha; + } + + /// Back to the canvas, blending alpha, unclipped: where overlays draw + /// and screenshots read. + pub fn end(p: *Painter) void { + p.flush(); + if (p.target != .none) { + p.target = .none; + _ = c.SDL_SetRenderTarget(p.renderer, p.canvas); + } + p.blend = .alpha; + _ = c.SDL_SetRenderClipRect(p.renderer, null); + } + + /// Draws one graphics command. Other commands are not graphics: ignored. + pub fn draw(p: *Painter, command: abi.Command) void { + if (command != .shape) p.flush(); + const renderer = p.renderer; + switch (command) { + .clear => |color| { + if (@import("builtin").os.tag == .linux and p.target == .none) c.sk_text_begin_frame(renderer); + // A target holds premultiplied colour, as a canvas cleared + // to a translucent colour does. + setColor(renderer, if (p.target == .none) color else premultiply(color)); + _ = c.SDL_RenderClear(renderer); + }, + .rect => |r| { + setColor(renderer, r.color); + _ = c.SDL_SetRenderDrawBlendMode(renderer, drawBlend(p.blend)); + _ = c.SDL_RenderFillRect(renderer, &frect(r.dst)); + }, + .shape => |s| p.shape(s.color, s.points, s.ends, s.indices, s.colors), + .image => |img| p.drawImage(img.image, img.dst, img.src, img.tint, img.corners), + .text => |text| { + _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); + drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color); + }, + .clip => |clip| { + if (clip) |r| { + const rect: c.SDL_Rect = .{ .x = @intFromFloat(r.x), .y = @intFromFloat(r.y), .w = @intFromFloat(r.w), .h = @intFromFloat(r.h) }; + _ = c.SDL_SetRenderClipRect(renderer, &rect); + } else _ = c.SDL_SetRenderClipRect(renderer, null); + }, + .pixels => |px| { + const entry = p.images.get(px.image) orelse return; + const texture = entry.texture orelse return; + if (px.w != entry.w or px.h != entry.h or px.rgba.len < @as(usize, px.w) * px.h * 4) return; + entry.premultiplied = px.premultiplied; + _ = c.SDL_UpdateTexture(texture, null, px.rgba.ptr, @intCast(px.w * 4)); + if (entry.big != null) p.spread(entry); + }, + .target => |img| p.setTarget(img), + .blend => |mode| p.blend = mode, + else => {}, + } + } + + fn setTarget(p: *Painter, handle: abi.Image) void { + const texture = if (handle == .none) p.canvas else blk: { + const entry = p.images.get(handle) orelse return; + if (!entry.target or entry.texture == null) return; + if (entry.big == null and p.smooth) p.supersample(entry); + entry.stale = entry.big != null; + break :blk entry.big orelse entry.texture; + }; + p.target = handle; + _ = c.SDL_SetRenderTarget(p.renderer, texture); + _ = c.SDL_SetRenderClipRect(p.renderer, null); + } + + /// Gives a target its supersampled self, holding what it shows now. + fn supersample(p: *Painter, entry: *Images.Entry) void { + const factor = factorFor(.{ entry.w, entry.h }); + if (factor == 1) return; + const big = c.SDL_CreateTexture(p.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_TARGET, @intCast(entry.w * factor), @intCast(entry.h * factor)) orelse return; + _ = c.SDL_SetTextureScaleMode(big, c.SDL_SCALEMODE_LINEAR); + entry.big = big; + entry.factor = factor; + const f: f32 = @floatFromInt(factor); + const previous = c.SDL_GetRenderTarget(p.renderer); + _ = c.SDL_SetRenderTarget(p.renderer, big); + _ = c.SDL_SetRenderScale(p.renderer, f, f); + _ = c.SDL_SetRenderTarget(p.renderer, previous); + p.spread(entry); + } + + /// Copies a target's `texture` into its supersampled self (after + /// `pixels` replaced it), each pixel into its samples. + fn spread(p: *Painter, entry: *Images.Entry) void { + const previous = c.SDL_GetRenderTarget(p.renderer); + _ = c.SDL_SetRenderTarget(p.renderer, entry.big); + const t = entry.texture.?; + _ = c.SDL_SetTextureBlendMode(t, c.SDL_BLENDMODE_NONE); + _ = c.SDL_SetTextureColorMod(t, 255, 255, 255); + _ = c.SDL_SetTextureAlphaMod(t, 255); + _ = c.SDL_RenderTexture(p.renderer, t, null, &.{ .x = 0, .y = 0, .w = @floatFromInt(entry.w), .h = @floatFromInt(entry.h) }); + _ = c.SDL_SetRenderTarget(p.renderer, previous); + entry.stale = false; + } + + /// Brings a target's `texture` up to date: its samples averaged. + fn resolve(p: *Painter, entry: *Images.Entry) void { + entry.stale = false; + const previous = c.SDL_GetRenderTarget(p.renderer); + p.average(entry.big.?, entry.factor, .{ 0, 0, entry.w, entry.h }, entry.texture.?, c.SDL_BLENDMODE_NONE); + _ = c.SDL_SetRenderTarget(p.renderer, previous); + } + + /// Draws `box` (destination pixels) of `from`, `factor` times larger, + /// averaged into `to` with `blend`. Leaves `to` the render target. + fn average(p: *Painter, from: *c.SDL_Texture, factor: u32, box: [4]u32, to: ?*c.SDL_Texture, blend: c.SDL_BlendMode) void { + var source = from; + var k: f32 = @floatFromInt(factor); + if (factor == 4) half: { + const need: [2]u32 = .{ box[2] * 2, box[3] * 2 }; + if (p.half == null or p.half_size[0] < need[0] or p.half_size[1] < need[1]) { + if (p.half) |t| c.SDL_DestroyTexture(t); + const size: [2]u32 = .{ @max(need[0], p.half_size[0]), @max(need[1], p.half_size[1]) }; + p.half = c.SDL_CreateTexture(p.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_TARGET, @intCast(size[0]), @intCast(size[1])); + p.half_size = if (p.half != null) size else .{ 0, 0 }; + if (p.half) |t| _ = c.SDL_SetTextureScaleMode(t, c.SDL_SCALEMODE_LINEAR) else break :half; + } + _ = c.SDL_SetRenderTarget(p.renderer, p.half); + plain(from, c.SDL_BLENDMODE_NONE); + _ = c.SDL_RenderTexture(p.renderer, from, &frect(scaledBox(box, 4)), &frect(scaledBox(box, 2))); + source = p.half.?; + k = 2; + } + _ = c.SDL_SetRenderTarget(p.renderer, to); + plain(source, blend); + _ = c.SDL_RenderTexture(p.renderer, source, &frect(scaledBox(box, k)), &frect(scaledBox(box, 1))); + } + + /// The current destination's size in its own pixels. + fn destinationSize(p: *Painter) [2]u32 { + if (p.target == .none) return p.canvas_size; + const entry = p.images.get(p.target) orelse return p.canvas_size; + return .{ entry.w, entry.h }; + } + + fn drawImage(p: *Painter, handle: abi.Image, dst: abi.Rect, src_rect: abi.Rect, tint: abi.Color, corners: ?abi.Corners) void { + const renderer = p.renderer; + const entry = p.images.get(handle); + const texture = if (entry) |e| e.texture else null; + const t = texture orelse return missing(renderer, dst); + const e = entry.?; + if (handle == p.target) return; // Drawing into itself: nothing. + if (e.stale) p.resolve(e); + // A premultiplied texture needs its colour scaled by the tint's + // alpha too, which SDL's alpha mod does not do. + const scale: u32 = if (e.premultiplied) tint.a else 255; + _ = c.SDL_SetTextureColorMod(t, @intCast(@as(u32, tint.r) * scale / 255), @intCast(@as(u32, tint.g) * scale / 255), @intCast(@as(u32, tint.b) * scale / 255)); + _ = c.SDL_SetTextureAlphaMod(t, tint.a); + _ = c.SDL_SetTextureBlendMode(t, textureBlend(p.blend, e.premultiplied)); + const src = frect(src_rect); + const srcp = if (src_rect.w > 0) &src else null; + if (corners) |q| { + const origin: c.SDL_FPoint = .{ .x = q.top_left[0], .y = q.top_left[1] }; + const right: c.SDL_FPoint = .{ .x = q.top_right[0], .y = q.top_right[1] }; + const down: c.SDL_FPoint = .{ .x = q.bottom_left[0], .y = q.bottom_left[1] }; + _ = c.SDL_RenderTextureAffine(renderer, t, srcp, &origin, &right, &down); + } else _ = c.SDL_RenderTexture(renderer, t, srcp, &frect(dst)); + } + + /// `Command.shape`: its triangles in one geometry call, one colour for + /// every vertex (stride 0, nothing copied) or each subpath's. The + /// subpaths are for the web; native needs only the triangles. + fn shape(p: *Painter, color: abi.Color, points: []const [2]f32, ends: []const u32, indices: []const u32, colors: []const abi.Color) void { + const count = indices.len - indices.len % 3; + if (count == 0 or points.len == 0 or points.len > std.math.maxInt(c_int) or count > std.math.maxInt(c_int)) return; + for (indices[0..count]) |i| if (i >= points.len) return; + var one = fcolor(color); + var vertex_colors: []const c.SDL_FColor = (&one)[0..1]; + var stride: c_int = 0; + if (colors.len > 0) { + const each = p.arena.alloc(c.SDL_FColor, points.len) catch return; + @memset(each, one); + var start: usize = 0; + for (ends, 0..) |subpath_end, i| { + if (i >= colors.len) break; + const stop = @min(subpath_end, points.len); + if (stop > start) @memset(each[start..stop], fcolor(colors[i])); + start = @max(start, stop); + } + vertex_colors = each; + stride = @sizeOf(c.SDL_FColor); + } + // A supersampled target takes shapes as they are; the canvas + // through the layer. + if (p.target == .none and p.smooth) { + const box = bounds(points, indices[0..count], p.canvas_size) orelse return; + if (!p.openLayer(box)) return; + } + _ = c.SDL_SetRenderDrawBlendMode(p.renderer, drawBlend(p.blend)); + _ = c.SDL_RenderGeometryRaw(p.renderer, null, @ptrCast(points.ptr), @sizeOf([2]f32), vertex_colors.ptr, stride, null, 0, @intCast(points.len), indices.ptr, @intCast(count), @sizeOf(u32)); + } + + /// Makes the layer the render target for a shape covering `box` + /// (canvas pixels): the open one, if for this blend, else a new one. + /// False if there is no room for one (the shape is then drawn on the + /// canvas, aliased). + fn openLayer(p: *Painter, box: [4]u32) bool { + const l = &p.layer; + if (l.open and l.blend == p.blend) { + l.dirty = .{ @min(l.dirty[0], box[0]), @min(l.dirty[1], box[1]), @max(l.dirty[2], box[2]), @max(l.dirty[3], box[3]) }; + return true; + } + p.flush(); + const size = p.canvas_size; + const factor = factorFor(size); + if (factor == 1) return true; + const w = size[0] * factor; + const h = size[1] * factor; + if (l.big == null or l.w < w or l.h < h) { + if (l.big) |t| c.SDL_DestroyTexture(t); + l.* = .{ .w = w, .h = h }; + l.big = c.SDL_CreateTexture(p.renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_TARGET, @intCast(w), @intCast(h)); + const big = l.big orelse { + std.log.warn("no antialiasing layer: {s}", .{c.SDL_GetError()}); + p.smooth = false; + return true; + }; + _ = c.SDL_SetTextureScaleMode(big, c.SDL_SCALEMODE_LINEAR); + // Cleared whole once; afterwards only what a composite left. + _ = c.SDL_SetRenderTarget(p.renderer, big); + _ = c.SDL_SetRenderDrawColor(p.renderer, 0, 0, 0, 0); + _ = c.SDL_RenderClear(p.renderer); + } + _ = c.SDL_SetRenderTarget(p.renderer, l.big); + const f: f32 = @floatFromInt(factor); + _ = c.SDL_SetRenderScale(p.renderer, f, f); + if (l.dirty[2] > l.dirty[0]) { + // The last layer's shapes, gone: at the scale it drew them. + const old: f32 = @floatFromInt(l.factor); + _ = c.SDL_SetRenderDrawBlendMode(p.renderer, c.SDL_BLENDMODE_NONE); + _ = c.SDL_SetRenderDrawColor(p.renderer, 0, 0, 0, 0); + _ = c.SDL_RenderFillRect(p.renderer, &frect(scaledBox(l.dirty, old / f))); + } + l.factor = factor; + l.blend = p.blend; + l.open = true; + l.dirty = box; + return true; + } + + /// Composites an open layer onto the canvas: the rect its shapes + /// cover, nothing else. + fn flush(p: *Painter) void { + const l = &p.layer; + if (!l.open) return; + l.open = false; + p.average(l.big.?, l.factor, l.dirty, p.canvas, textureBlend(l.blend, true)); + } +}; + +/// A texture drawn as it is: no tint, `blend`. +fn plain(t: *c.SDL_Texture, blend: c.SDL_BlendMode) void { + _ = c.SDL_SetTextureBlendMode(t, blend); + _ = c.SDL_SetTextureColorMod(t, 255, 255, 255); + _ = c.SDL_SetTextureAlphaMod(t, 255); +} + +/// The pixels a shape's triangles may touch, one pixel around, clamped to +/// the destination: null if none. +fn bounds(points: []const [2]f32, indices: []const u32, size: [2]u32) ?[4]u32 { + var lo: [2]f32 = .{ std.math.inf(f32), std.math.inf(f32) }; + var hi: [2]f32 = .{ -std.math.inf(f32), -std.math.inf(f32) }; + for (indices) |i| for (0..2) |axis| { + lo[axis] = @min(lo[axis], points[i][axis]); + hi[axis] = @max(hi[axis], points[i][axis]); + }; + var box: [4]u32 = undefined; + for (0..2) |axis| { + const limit: f32 = @floatFromInt(size[axis]); + const a = std.math.clamp(@floor(lo[axis]) - 1, 0, limit); + const b = std.math.clamp(@ceil(hi[axis]) + 1, 0, limit); + if (!(b > a)) return null; // off the destination, or NaN + box[axis] = @intFromFloat(a); + box[axis + 2] = @intFromFloat(b); } + return box; +} + +/// `box` (x0, y0, x1, y1) times `k`, as a rect. +fn scaledBox(box: [4]u32, k: f32) abi.Rect { + const x: f32 = @floatFromInt(box[0]); + const y: f32 = @floatFromInt(box[1]); + return .{ .x = x * k, .y = y * k, .w = @as(f32, @floatFromInt(box[2] - box[0])) * k, .h = @as(f32, @floatFromInt(box[3] - box[1])) * k }; +} + +/// Adds colour times alpha, and alpha, as canvas `lighter` does (SDL's +/// `ADD` keeps the destination's alpha). +fn addBlend(premultiplied: bool) c.SDL_BlendMode { + const src: c.SDL_BlendFactor = if (premultiplied) c.SDL_BLENDFACTOR_ONE else c.SDL_BLENDFACTOR_SRC_ALPHA; + return c.SDL_ComposeCustomBlendMode(src, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDOPERATION_ADD, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDFACTOR_ONE, c.SDL_BLENDOPERATION_ADD); +} + +/// Solid colours (straight alpha) onto the destination. +fn drawBlend(blend: abi.BlendMode) c.SDL_BlendMode { + return switch (blend) { + .alpha => c.SDL_BLENDMODE_BLEND, + .add => addBlend(false), + }; +} + +fn textureBlend(blend: abi.BlendMode, premultiplied: bool) c.SDL_BlendMode { + return switch (blend) { + .alpha => if (premultiplied) c.SDL_BLENDMODE_BLEND_PREMULTIPLIED else c.SDL_BLENDMODE_BLEND, + .add => addBlend(premultiplied), + }; +} + +fn fcolor(color: abi.Color) c.SDL_FColor { + return .{ + .r = @as(f32, @floatFromInt(color.r)) / 255, + .g = @as(f32, @floatFromInt(color.g)) / 255, + .b = @as(f32, @floatFromInt(color.b)) / 255, + .a = @as(f32, @floatFromInt(color.a)) / 255, + }; } /// Native shaped text on Linux, using a 13px monospace face at scale 1. @@ -178,7 +555,13 @@ pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: [] if (clipped and s != 1) _ = c.SDL_SetRenderClipRect(renderer, &clip); } +fn premultiply(color: abi.Color) abi.Color { + const a: u32 = color.a; + return .{ .r = @intCast((color.r * a + 127) / 255), .g = @intCast((color.g * a + 127) / 255), .b = @intCast((color.b * a + 127) / 255), .a = color.a }; +} + fn missing(renderer: *c.SDL_Renderer, dst: abi.Rect) void { + _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); setColor(renderer, .{ .r = 110, .g = 30, .b = 70 }); _ = c.SDL_RenderFillRect(renderer, &frect(dst)); drawText(renderer, dst.x + 4, dst.y + 4, 1, "missing image", .{ .r = 255, .g = 210, .b = 240 }); diff --git a/src/host/gpu.zig b/src/host/gpu.zig index 9c77205..94b2e39 100644 --- a/src/host/gpu.zig +++ b/src/host/gpu.zig @@ -119,6 +119,10 @@ const Target = struct { texture: *c.SDL_GPUTexture, samples: u8 = 1, msaa: ?*c.SDL_GPUTexture = null, + /// Not rendered into yet: undefined natively, so the first pass that + /// would load clears to zero (WebGPU's starting contents). + fresh: bool = true, + msaa_fresh: bool = true, stencil_samples: u8 = 0, stencil: ?*c.SDL_GPUTexture = null, }; @@ -187,6 +191,12 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return; const samples = g.supportedSamples(pass.samples); const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + // Each pipeline is looked up on first use in the pass, not per draw: + // the lookup hashes the shaders' code. Outer null: not looked up yet; + // inner null: it failed (warned once). + const built = try g.gpa.alloc(??*c.SDL_GPUGraphicsPipeline, pass.pipelines.len); + defer g.gpa.free(built); + @memset(built, null); const start = g.startTiming(); // Every draw's vertices go up in one copy. @@ -210,16 +220,23 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { const clear = pass.clear orelse .{ 0, 0, 0, 0 }; var color: c.SDL_GPUColorTargetInfo = std.mem.zeroes(c.SDL_GPUColorTargetInfo); color.clear_color = .{ .r = clear[0], .g = clear[1], .b = clear[2], .a = clear[3] }; - // A multisampled pass cannot load resolved pixels: it always clears. - color.load_op = if (pass.clear != null or samples > 1) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_LOAD; + // `clear = null` keeps what the attachment holds, as on the web: a + // multisampled pass loads its multisampled attachment, which every + // pass therefore stores as well as resolves. A new attachment is + // cleared to zero instead, since Vulkan starts it undefined where + // WebGPU starts it zeroed. + const fresh = if (samples > 1) &t.msaa_fresh else &t.fresh; + color.load_op = if (pass.clear != null or fresh.*) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_LOAD; + fresh.* = false; if (samples > 1) { color.texture = t.msaa; - color.store_op = c.SDL_GPU_STOREOP_RESOLVE; + color.store_op = c.SDL_GPU_STOREOP_RESOLVE_AND_STORE; color.resolve_texture = t.texture; } else { color.texture = t.texture; color.store_op = c.SDL_GPU_STOREOP_STORE; } + t.fresh = false; var depth: c.SDL_GPUDepthStencilTargetInfo = std.mem.zeroes(c.SDL_GPUDepthStencilTargetInfo); if (pass.stencil) { depth.texture = t.stencil; @@ -231,29 +248,59 @@ pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { } const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil) &depth else null) orelse return error.GpuRenderPass; var bound: ?*c.SDL_GPUGraphicsPipeline = null; + // State already set in this pass, so draws that repeat it skip the call. + var vertex_uniforms: ?[]const u8 = null; + var stencil_reference: ?u8 = null; + var scissor: ?c.SDL_Rect = null; + var vertex_base: ?u32 = null; var offset: u32 = 0; for (pass.draws) |d| { defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); if (d.pipeline >= pass.pipelines.len or d.vertex_count == 0) continue; - const built = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| { + const slot = &built[d.pipeline]; + if (slot.* == null) slot.* = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| blk: { std.log.warn("gpu pipeline {d}: {t} ({s})", .{ d.pipeline, err, c.SDL_GetError() }); - continue; + break :blk @as(?*c.SDL_GPUGraphicsPipeline, null); }; - if (bound != built) { - c.SDL_BindGPUGraphicsPipeline(rp, built); - bound = built; + const chosen = slot.*.? orelse continue; + if (bound != chosen) { + c.SDL_BindGPUGraphicsPipeline(rp, chosen); + bound = chosen; + } + // Vertices at a multiple of the stride are reached from one binding + // at 0 by the draw's first vertex; others get a binding of their own. + var first: u32 = 0; + if (d.vertices.len > 0) { + const stride = pass.pipelines[d.pipeline].stride; + const base = if (stride > 0 and offset % stride == 0) 0 else offset; + if (base == 0 and stride > 0) first = offset / stride; + if (vertex_base != base) { + c.SDL_BindGPUVertexBuffers(rp, 0, &.{ .buffer = g.vertices, .offset = base }, 1); + vertex_base = base; + } } - 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)); + // Pushed uniforms stay for later draws: the same bytes (sketches + // share one slice for a pass's viewport) need no second push. + if (d.vertex_uniforms.len > 0) if (vertex_uniforms == null or vertex_uniforms.?.ptr != d.vertex_uniforms.ptr or vertex_uniforms.?.len != d.vertex_uniforms.len) { + c.SDL_PushGPUVertexUniformData(cmd, 0, d.vertex_uniforms.ptr, @intCast(d.vertex_uniforms.len)); + vertex_uniforms = d.vertex_uniforms; + }; 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); + if (pass.stencil and stencil_reference != d.stencil_reference) { + c.SDL_SetGPUStencilReference(rp, d.stencil_reference); + stencil_reference = d.stencil_reference; + } const full: abi.Rect = .{ .w = @floatFromInt(pass.width), .h = @floatFromInt(pass.height) }; const s = d.scissor orelse full; - c.SDL_SetGPUScissor(rp, &.{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) }); - c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, 0, 0); + const rect: c.SDL_Rect = .{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) }; + if (scissor == null or !std.meta.eql(scissor.?, rect)) { + c.SDL_SetGPUScissor(rp, &rect); + scissor = rect; + } + c.SDL_DrawGPUPrimitives(rp, d.vertex_count, 1, first, 0); } c.SDL_EndGPURenderPass(rp); try g.submit(cmd, if (pass.label.len > 0) pass.label else "pass", start); @@ -457,6 +504,7 @@ fn renderTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, w: u32, h: u32, if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); t.msaa = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, samples); t.samples = samples; + t.msaa_fresh = true; } if (stencil and (t.stencil == null or t.stencil_samples != samples)) { if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); diff --git a/src/host/gpu.zig.orig b/src/host/gpu.zig.orig new file mode 100644 index 0000000..7854c99 --- /dev/null +++ b/src/host/gpu.zig.orig @@ -0,0 +1,641 @@ +//! 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"); +const abi = @import("abi"); +const gfx = @import("gfx.zig"); + +pub const color_format = c.SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM; +/// Frames an unused shader or pipeline survives. +const keep_frames = 300; + +gpa: std.mem.Allocator, +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, +vertices: ?*c.SDL_GPUBuffer = null, +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, + texture: *c.SDL_GPUTexture, + samples: u8 = 1, + msaa: ?*c.SDL_GPUTexture = null, + /// Not rendered into yet: undefined natively, so the first pass that + /// would load clears to zero (WebGPU's starting contents). + fresh: bool = true, + msaa_fresh: bool = true, + stencil_samples: u8 = 0, + stencil: ?*c.SDL_GPUTexture = null, +}; + +pub fn init(gpa: std.mem.Allocator, device: *c.SDL_GPUDevice) Gpu { + const d24 = c.SDL_GPUTextureSupportsFormat(device, c.SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT, c.SDL_GPU_TEXTURETYPE_2D, c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET); + return .{ + .gpa = gpa, + .device = device, + .stencil_format = if (d24) c.SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT else c.SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT, + }; +} + +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, &g.kernels }) |map| { + var it = map.iterator(); + while (it.next()) |entry| { + if (g.frame - entry.value_ptr.used < keep_frames) continue; + 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; + } + } +} + +pub fn run(g: *Gpu, images: *gfx.Images, pass: abi.gpu.Pass) !void { + if (pass.width == 0 or pass.height == 0 or pass.draws.len == 0 and pass.clear == null) return; + const samples = g.supportedSamples(pass.samples); + const t = try g.renderTarget(images, pass.image, pass.width, pass.height, samples, pass.stencil); + // Each pipeline is looked up on first use in the pass, not per draw: + // the lookup hashes the shaders' code. Outer null: not looked up yet; + // inner null: it failed (warned once). + const built = try g.gpa.alloc(??*c.SDL_GPUGraphicsPipeline, pass.pipelines.len); + defer g.gpa.free(built); + @memset(built, null); + + 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)); + const cmd = c.SDL_AcquireGPUCommandBuffer(g.device) orelse return error.GpuCommandBuffer; + if (total > 0) { + try g.reserve(total); + const mapped: [*]u8 = @ptrCast(c.SDL_MapGPUTransferBuffer(g.device, g.upload, true) orelse return error.GpuMap); + var offset: usize = 0; + for (pass.draws) |d| { + @memcpy(mapped[offset..][0..d.vertices.len], d.vertices); + offset += std.mem.alignForward(usize, d.vertices.len, 4); + } + c.SDL_UnmapGPUTransferBuffer(g.device, g.upload); + const copy = c.SDL_BeginGPUCopyPass(cmd); + c.SDL_UploadToGPUBuffer(copy, &.{ .transfer_buffer = g.upload, .offset = 0 }, &.{ .buffer = g.vertices, .offset = 0, .size = total }, true); + c.SDL_EndGPUCopyPass(copy); + } + + const clear = pass.clear orelse .{ 0, 0, 0, 0 }; + var color: c.SDL_GPUColorTargetInfo = std.mem.zeroes(c.SDL_GPUColorTargetInfo); + color.clear_color = .{ .r = clear[0], .g = clear[1], .b = clear[2], .a = clear[3] }; + // `clear = null` keeps what the attachment holds, as on the web: a + // multisampled pass loads its multisampled attachment, which every + // pass therefore stores as well as resolves. A new attachment is + // cleared to zero instead, since Vulkan starts it undefined where + // WebGPU starts it zeroed. + const fresh = if (samples > 1) &t.msaa_fresh else &t.fresh; + color.load_op = if (pass.clear != null or fresh.*) c.SDL_GPU_LOADOP_CLEAR else c.SDL_GPU_LOADOP_LOAD; + fresh.* = false; + if (samples > 1) { + color.texture = t.msaa; + color.store_op = c.SDL_GPU_STOREOP_RESOLVE_AND_STORE; + color.resolve_texture = t.texture; + } else { + color.texture = t.texture; + color.store_op = c.SDL_GPU_STOREOP_STORE; + } + t.fresh = false; + var depth: c.SDL_GPUDepthStencilTargetInfo = std.mem.zeroes(c.SDL_GPUDepthStencilTargetInfo); + if (pass.stencil) { + depth.texture = t.stencil; + depth.load_op = c.SDL_GPU_LOADOP_DONT_CARE; + depth.store_op = c.SDL_GPU_STOREOP_DONT_CARE; + depth.stencil_load_op = c.SDL_GPU_LOADOP_CLEAR; + depth.stencil_store_op = c.SDL_GPU_STOREOP_DONT_CARE; + depth.clear_stencil = 0; + } + const rp = c.SDL_BeginGPURenderPass(cmd, &color, 1, if (pass.stencil) &depth else null) orelse return error.GpuRenderPass; + var bound: ?*c.SDL_GPUGraphicsPipeline = null; + var offset: u32 = 0; + for (pass.draws) |d| { + defer offset += @intCast(std.mem.alignForward(usize, d.vertices.len, 4)); + if (d.pipeline >= pass.pipelines.len or d.vertex_count == 0) continue; + const slot = &built[d.pipeline]; + if (slot.* == null) slot.* = g.pipeline(pass.pipelines[d.pipeline], samples, pass.stencil) catch |err| blk: { + std.log.warn("gpu pipeline {d}: {t} ({s})", .{ d.pipeline, err, c.SDL_GetError() }); + break :blk @as(?*c.SDL_GPUGraphicsPipeline, null); + }; + const chosen = slot.*.? orelse continue; + if (bound != chosen) { + c.SDL_BindGPUGraphicsPipeline(rp, chosen); + bound = chosen; + } + 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); + const full: abi.Rect = .{ .w = @floatFromInt(pass.width), .h = @floatFromInt(pass.height) }; + const s = d.scissor orelse full; + c.SDL_SetGPUScissor(rp, &.{ .x = @intFromFloat(@max(s.x, 0)), .y = @intFromFloat(@max(s.y, 0)), .w = @intFromFloat(@max(s.w, 0)), .h = @intFromFloat(@max(s.h, 0)) }); + 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 { + var s: u8 = if (wanted >= 8) 8 else if (wanted >= 4) 4 else if (wanted >= 2) 2 else 1; + while (s > 1 and !c.SDL_GPUTextureSupportsSampleCount(g.device, color_format, sampleCount(s))) s /= 2; + return s; +} + +fn sampleCount(s: u8) c.SDL_GPUSampleCount { + return switch (s) { + 8 => c.SDL_GPU_SAMPLECOUNT_8, + 4 => c.SDL_GPU_SAMPLECOUNT_4, + 2 => c.SDL_GPU_SAMPLECOUNT_2, + else => c.SDL_GPU_SAMPLECOUNT_1, + }; +} + +/// The image's render target, (re)created to fit the pass. +fn renderTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, w: u32, h: u32, samples: u8, stencil: bool) !*Target { + var entry = try g.targets.getOrPut(g.gpa, image); + if (entry.found_existing and (entry.value_ptr.width != w or entry.value_ptr.height != h)) { + g.releaseTarget(images, image, entry.value_ptr.*); + entry.found_existing = false; + } + if (!entry.found_existing) { + errdefer _ = g.targets.remove(image); + const resolved = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | c.SDL_GPU_TEXTUREUSAGE_SAMPLER, 1); + errdefer c.SDL_ReleaseGPUTexture(g.device, resolved); + try images.wrap(image, resolved, w, h); + entry.value_ptr.* = .{ .width = w, .height = h, .texture = resolved }; + } + const t = entry.value_ptr; + if (samples > 1 and (t.msaa == null or t.samples != samples)) { + if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); + t.msaa = try g.texture(w, h, color_format, c.SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, samples); + t.samples = samples; + t.msaa_fresh = true; + } + if (stencil and (t.stencil == null or t.stencil_samples != samples)) { + if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); + t.stencil = try g.texture(w, h, g.stencil_format, c.SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET, samples); + t.stencil_samples = samples; + } + return t; +} + +fn releaseTarget(g: *Gpu, images: *gfx.Images, image: abi.Image, t: Target) void { + images.unwrap(image); + c.SDL_ReleaseGPUTexture(g.device, t.texture); + if (t.msaa) |m| c.SDL_ReleaseGPUTexture(g.device, m); + if (t.stencil) |s| c.SDL_ReleaseGPUTexture(g.device, s); +} + +/// Forgets an image's target, when the image itself is freed. +pub fn forget(g: *Gpu, images: *gfx.Images, image: abi.Image) void { + const t = g.targets.fetchRemove(image) orelse return; + g.releaseTarget(images, image, t.value); +} + +fn texture(g: *Gpu, w: u32, h: u32, format: c.SDL_GPUTextureFormat, usage: c.SDL_GPUTextureUsageFlags, samples: u8) !*c.SDL_GPUTexture { + var info: c.SDL_GPUTextureCreateInfo = std.mem.zeroes(c.SDL_GPUTextureCreateInfo); + info.type = c.SDL_GPU_TEXTURETYPE_2D; + info.format = format; + info.usage = usage; + info.width = w; + info.height = h; + info.layer_count_or_depth = 1; + info.num_levels = 1; + info.sample_count = sampleCount(samples); + return c.SDL_CreateGPUTexture(g.device, &info) orelse error.GpuTexture; +} + +fn reserve(g: *Gpu, bytes: u32) !void { + if (bytes > g.vertex_capacity) { + const capacity = @max(bytes, g.vertex_capacity * 2, 64 * 1024); + if (g.vertices) |b| c.SDL_ReleaseGPUBuffer(g.device, b); + g.vertices = c.SDL_CreateGPUBuffer(g.device, &.{ .usage = c.SDL_GPU_BUFFERUSAGE_VERTEX, .size = capacity, .props = 0 }) orelse return error.GpuBuffer; + g.vertex_capacity = capacity; + } + if (bytes > g.upload_capacity) { + const capacity = @max(bytes, g.upload_capacity * 2, 64 * 1024); + if (g.upload) |b| c.SDL_ReleaseGPUTransferBuffer(g.device, b); + g.upload = c.SDL_CreateGPUTransferBuffer(g.device, &.{ .usage = c.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, .size = capacity, .props = 0 }) orelse return error.GpuBuffer; + g.upload_capacity = capacity; + } +} + +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.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.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 = s.storage_buffers, + .num_uniform_buffers = s.uniform_buffers, + .props = 0, + }) orelse return error.GpuShader; + } + entry.value_ptr.used = g.frame; + return .{ entry.value_ptr.object, key }; +} + +fn pipeline(g: *Gpu, p: abi.gpu.Pipeline, samples: u8, stencil: bool) !*c.SDL_GPUGraphicsPipeline { + const vertex, const vertex_key = try g.shader(p.vertex, c.SDL_GPU_SHADERSTAGE_VERTEX); + 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, @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) { + errdefer _ = g.pipelines.remove(key); + var attributes: [16]c.SDL_GPUVertexAttribute = undefined; + const count = @min(p.attributes.len, attributes.len); + for (p.attributes[0..count], attributes[0..count]) |a, *out| out.* = .{ + .location = a.location, + .buffer_slot = 0, + .offset = a.offset, + .format = switch (a.format) { + .float => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT, + .float2 => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, + .float3 => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, + .float4 => c.SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, + .ubyte4_norm => c.SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM, + }, + }; + var blend: c.SDL_GPUColorTargetBlendState = std.mem.zeroes(c.SDL_GPUColorTargetBlendState); + blend.enable_blend = p.blend != .none; + blend.color_blend_op = c.SDL_GPU_BLENDOP_ADD; + blend.alpha_blend_op = c.SDL_GPU_BLENDOP_ADD; + blend.src_color_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; + blend.src_alpha_blendfactor = c.SDL_GPU_BLENDFACTOR_ONE; + const dst: c.SDL_GPUBlendFactor = if (p.blend == .additive) c.SDL_GPU_BLENDFACTOR_ONE else c.SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; + blend.dst_color_blendfactor = dst; + blend.dst_alpha_blendfactor = dst; + blend.enable_color_write_mask = true; + blend.color_write_mask = if (p.write_color) c.SDL_GPU_COLORCOMPONENT_R | c.SDL_GPU_COLORCOMPONENT_G | c.SDL_GPU_COLORCOMPONENT_B | c.SDL_GPU_COLORCOMPONENT_A else 0; + const target_description: c.SDL_GPUColorTargetDescription = .{ .format = color_format, .blend_state = blend }; + var depth_stencil: c.SDL_GPUDepthStencilState = std.mem.zeroes(c.SDL_GPUDepthStencilState); + if (p.stencil) |st| { + depth_stencil.enable_stencil_test = true; + depth_stencil.compare_mask = 0xff; + depth_stencil.write_mask = 0xff; + depth_stencil.front_stencil_state = stencilFace(st.front); + depth_stencil.back_stencil_state = stencilFace(st.back); + } + var info: c.SDL_GPUGraphicsPipelineCreateInfo = std.mem.zeroes(c.SDL_GPUGraphicsPipelineCreateInfo); + info.vertex_shader = vertex; + 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 }, + // No attributes: the vertex shader makes its own vertices. + .num_vertex_buffers = @intFromBool(count > 0), + .vertex_attributes = &attributes, + .num_vertex_attributes = @intCast(count), + }; + info.primitive_type = switch (p.primitive) { + .triangle_list => c.SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, + .triangle_strip => c.SDL_GPU_PRIMITIVETYPE_TRIANGLESTRIP, + .line_list => c.SDL_GPU_PRIMITIVETYPE_LINELIST, + .line_strip => c.SDL_GPU_PRIMITIVETYPE_LINESTRIP, + }; + info.rasterizer_state.fill_mode = c.SDL_GPU_FILLMODE_FILL; + info.rasterizer_state.cull_mode = c.SDL_GPU_CULLMODE_NONE; + info.rasterizer_state.front_face = c.SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE; + info.multisample_state.sample_count = sampleCount(samples); + info.depth_stencil_state = depth_stencil; + info.target_info.color_target_descriptions = &target_description; + info.target_info.num_color_targets = 1; + info.target_info.depth_stencil_format = g.stencil_format; + info.target_info.has_depth_stencil_target = stencil; + entry.value_ptr.object = c.SDL_CreateGPUGraphicsPipeline(g.device, &info) orelse return error.GpuPipeline; + } + entry.value_ptr.used = g.frame; + return entry.value_ptr.object; +} + +fn stencilFace(face: abi.gpu.StencilFace) c.SDL_GPUStencilOpState { + // Both enums follow SDL's order, after its INVALID. + return .{ + .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/memory.zig b/src/host/memory.zig index fc44e50..5fbb88f 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -20,7 +20,11 @@ pub const page = std.heap.page_size_min; /// Slot 0 is live; 1..7 are snapshots. pub const slot_count = 8; pub const header_len = 64 * 1024; -pub const file_magic = "SKETCH01".*; +/// Layouts with element leaves (`list[].x`, `StoredField` with `dims`). +pub const file_magic = "SKETCH02".*; +/// What hosts before element leaves wrote: arrays are whole leaves +/// (`StoredFieldV1`). Read, and rewritten as `file_magic` on adopt. +pub const file_magic_v1 = "SKETCH01".*; pub const Header = extern struct { magic: [8]u8 = file_magic, @@ -36,7 +40,8 @@ pub const Header = extern struct { reserved: u32 = 0, }; -/// A leaf as stored in the file: strings are offsets into the string table. +/// A leaf as stored in the file: strings are offsets into the string table, +/// and so is `dims`, `rank` little-endian (count, stride) u32 pairs. pub const StoredField = extern struct { offset: u32, size: u32, @@ -46,9 +51,27 @@ pub const StoredField = extern struct { tags_len: u16, tags: u32, kind: abi.Field.Kind, + rank: u8 = 0, + reserved: [2]u8 = @splat(0), + dims: u32 = 0, + reserved2: u32 = 0, +}; + +/// A leaf as `file_magic_v1` files store it: no element leaves. +pub const StoredFieldV1 = extern struct { + offset: u32, + size: u32, + hash: u64, + path: u32, + path_len: u16, + tags_len: u16, + tags: u32, + kind: abi.Field.Kind, reserved: [3]u8 = @splat(0), }; +const Dim = abi.Field.Dim; + /// A layout the host owns: copied out of a sketch, or read from a file. pub const Layout = struct { size: usize = 0, @@ -56,6 +79,8 @@ pub const Layout = struct { hash: u64 = 0, fields: []const Leaf = &.{}, + /// A leaf with `dims` stands for all its elements (`list[].x`), at + /// `offset` plus index times stride per dimension. `find` resolves one. pub const Leaf = struct { path: []const u8, offset: u32, @@ -63,10 +88,38 @@ pub const Layout = struct { kind: abi.Field.Kind, hash: u64, tags: []const u8, + dims: []const Dim = &.{}, pub fn bytes(leaf: Leaf, memory: []const u8) []const u8 { return memory[leaf.offset..][0..leaf.size]; } + /// The leaf at `offset` (from `elements`), as a leaf of its own. + pub fn at(leaf: Leaf, offset: usize) Leaf { + var one = leaf; + one.offset = @intCast(offset); + one.dims = &.{}; + return one; + } + /// A whole array, kept for copying in one piece; its elements are + /// leaves of their own. + pub fn whole(leaf: Leaf) bool { + return leaf.kind == .array; + } + /// Offsets of every element, first index slowest. + pub fn elements(leaf: Leaf) Elements { + return .init(leaf.offset, leaf.dims, leaf.dims, null); + } + /// Writes the path naming the element at `offset`, indices filled in. + pub fn name(leaf: Leaf, offset: usize, w: *std.Io.Writer) std.Io.Writer.Error!void { + var rest = offset - leaf.offset; + var parts = std.mem.splitSequence(u8, leaf.path, "[]"); + try w.writeAll(parts.first()); + for (leaf.dims) |d| { + try w.print("[{d}]", .{rest / d.stride}); + rest %= d.stride; + try w.writeAll(parts.next() orelse ""); + } + } }; pub fn copy(arena: std.mem.Allocator, l: abi.Layout) !Layout { @@ -78,37 +131,149 @@ pub const Layout = struct { .kind = from.kind, .hash = from.hash, .tags = try arena.dupe(u8, std.mem.span(from.tags)), + .dims = try arena.dupe(Dim, from.dims[0..from.rank]), }; return .{ .size = l.size, .alignment = l.alignment, .hash = l.hash, .fields = fields }; } + /// The leaf `path` names, with indices for every array it is in + /// (`list[3].pos[0]`), as a leaf of its own; null when there is none + /// or an index is out of range. pub fn find(self: Layout, path: []const u8) ?Leaf { + for (self.fields) |leaf| { + const delta = abi.layout.match(leaf.path, leaf.dims, path) orelse continue; + return leaf.at(leaf.offset + delta); + } + return null; + } + + /// The leaf whose pattern is exactly `pattern` (`list[].x`). + pub fn pattern(self: Layout, path: []const u8) ?Leaf { for (self.fields) |leaf| if (std.mem.eql(u8, leaf.path, path)) return leaf; return null; } }; +/// Walks element offsets of one leaf, or of the elements two layouts of a +/// leaf share (`limit`, the smaller count per dimension), with offsets in +/// both. +pub const Elements = struct { + index: [abi.layout.max_rank]u32 = @splat(0), + limit: [abi.layout.max_rank]u32 = @splat(0), + rank: usize, + a: Side, + b: Side, + done: bool, + + const Side = struct { base: usize, dims: []const Dim }; + + pub fn init(base: usize, dims: []const Dim, other_dims: []const Dim, other_base: ?usize) Elements { + var e: Elements = .{ .rank = dims.len, .a = .{ .base = base, .dims = dims }, .b = .{ .base = other_base orelse base, .dims = other_dims }, .done = false }; + for (dims, other_dims, 0..) |x, y, i| { + e.limit[i] = @min(x.count, y.count); + if (e.limit[i] == 0) e.done = true; + } + return e; + } + + fn offset(e: *const Elements, side: Side) usize { + var at = side.base; + for (side.dims, 0..) |d, i| at += @as(usize, e.index[i]) * d.stride; + return at; + } + + pub fn next(e: *Elements) ?usize { + const pair = e.nextPair() orelse return null; + return pair[0]; + } + + pub fn nextPair(e: *Elements) ?[2]usize { + if (e.done) return null; + const pair: [2]usize = .{ e.offset(e.a), e.offset(e.b) }; + var i = e.rank; + while (true) { + if (i == 0) { + e.done = true; + break; + } + i -= 1; + e.index[i] += 1; + if (e.index[i] < e.limit[i]) break; + e.index[i] = 0; + } + return pair; + } +}; + pub const Migration = struct { kept: usize = 0, reset: usize = 0 }; /// Copies every leaf of `from` whose path and type are unchanged into `to`, -/// which already holds the new defaults. +/// which already holds the new defaults. Arrays copy element by element: +/// the elements both layouts have (the common prefix of each dimension; a +/// shrink drops the rest, a growth keeps the new defaults), field by field +/// inside each, enums by tag name. An array whose type is unchanged copies +/// in one piece. Counts are of leaves, not elements. pub fn migrate(from: Layout, old: []const u8, to: Layout, new: []u8) Migration { var result: Migration = .{}; + // The array copied whole most recently: its elements' leaves follow it. + var copied: ?[]const u8 = null; for (to.fields) |leaf| { - const before = from.find(leaf.path) orelse { - result.reset += 1; + if (copied) |prefix| { + if (leaf.path.len > prefix.len and std.mem.startsWith(u8, leaf.path, prefix) and leaf.path[prefix.len] == '[') continue; + copied = null; + } + const before = from.pattern(leaf.path) orelse { + if (!leaf.whole()) result.reset += 1; continue; }; + if (before.dims.len != leaf.dims.len) { + if (!leaf.whole()) result.reset += 1; + continue; + } if (before.hash == leaf.hash and before.size == leaf.size) { - @memcpy(new[leaf.offset..][0..leaf.size], before.bytes(old)); + var it: Elements = .init(leaf.offset, leaf.dims, before.dims, before.offset); + while (it.nextPair()) |pair| @memcpy(new[pair[0]..][0..leaf.size], old[pair[1]..][0..leaf.size]); + if (leaf.whole()) copied = leaf.path; result.kept += 1; - } else if (before.kind == .@"enum" and leaf.kind == .@"enum" and remapEnum(before, old, leaf, new)) { - result.kept += 1; - } else result.reset += 1; + } else if (before.kind == .@"enum" and leaf.kind == .@"enum") { + var it: Elements = .init(leaf.offset, leaf.dims, before.dims, before.offset); + var any = false; + var seen: usize = 0; + while (it.nextPair()) |pair| : (seen += 1) { + if (remapEnum(before.at(pair[1]), old, leaf.at(pair[0]), new)) any = true; + } + if (any or seen == 0) result.kept += 1 else result.reset += 1; + } else if (!leaf.whole()) result.reset += 1; } return result; } +pub const Diff = struct { + /// Leaf values (elements counted one by one) that differ. + count: usize = 0, + first: ?Place = null, + last: ?Place = null, + + pub const Place = struct { leaf: Layout.Leaf, offset: usize }; +}; + +/// Compares two copies of state leaf by leaf, so padding (a Debug +/// `@Vector`'s spare lane, the gaps between fields) never differs. +pub fn compare(layout: Layout, a: []const u8, b: []const u8) Diff { + var diff: Diff = .{}; + for (layout.fields) |leaf| { + if (leaf.whole()) continue; + var it = leaf.elements(); + while (it.next()) |offset| { + if (std.mem.eql(u8, a[offset..][0..leaf.size], b[offset..][0..leaf.size])) continue; + diff.count += 1; + if (diff.first == null) diff.first = .{ .leaf = leaf, .offset = offset }; + diff.last = .{ .leaf = leaf, .offset = offset }; + } + } + return diff; +} + /// Carries an enum over by tag name; false when its name no longer exists. fn remapEnum(from: Layout.Leaf, old: []const u8, to: Layout.Leaf, new: []u8) bool { const name = field_value.tagName(from.tags, field_value.readInt(i64, from.bytes(old))) orelse return false; @@ -124,22 +289,23 @@ pub const StateFile = struct { layout: Layout, arena: std.heap.ArenaAllocator, - /// Opens or creates `path`. A missing or foreign file has no layout and no - /// used slots: the first `adopt` fills it. + /// Opens or creates `path`. A new (empty) file has no layout and no used + /// slots: the first `adopt` fills it. A file that is not a state file + /// this host can read is refused, never truncated: it may be someone's + /// work, written by another build. pub fn open(gpa: std.mem.Allocator, path: []const u8) !StateFile { const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); errdefer _ = posix.system.close(fd); var self: StateFile = .{ .fd = fd, .map = &.{}, .layout = .{}, .arena = .init(gpa) }; errdefer self.arena.deinit(); const len = try fileSize(fd); - if (len >= header_len) { + if (len > 0) { + if (len < header_len) return error.NotAStateFile; self.map = try mapFile(fd, @intCast(len), .{ .READ = true, .WRITE = true }); - if (self.valid()) { - self.layout = try readLayout(self.arena.allocator(), self.map) orelse .{}; - return self; - } - posix.munmap(self.map); - self.map = &.{}; + errdefer posix.munmap(self.map); + try self.check(); + self.layout = try readLayout(self.arena.allocator(), self.map) orelse .{}; + return self; } try self.resize(0); self.header().* = .{ .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 }; @@ -181,7 +347,18 @@ pub const StateFile = struct { /// Makes the file hold `new`'s layout. Used slots are migrated: `defaults` /// writes the new default values, then unchanged fields are copied over. pub fn adopt(self: *StateFile, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Adoption { - if (self.layout.hash == new.hash and self.layout.size == new.size and self.header().used & 1 != 0) return .unchanged; + if (self.layout.hash == new.hash and self.layout.size == new.size and self.header().used & 1 != 0) { + // Same type, older header: describe it again, bytes untouched. + if (std.mem.eql(u8, &self.header().magic, &file_magic_v1)) { + var kept = self.arena; + defer kept.deinit(); + self.arena = .init(gpa); + self.layout = try Layout.copy(self.arena.allocator(), new); + try writeLayout(self.map[0..header_len], self.layout); + self.header().magic = file_magic; + } + return .unchanged; + } const had_state = self.header().used & 1 != 0; // Keep the old bytes and layout while the file is rebuilt. @@ -202,7 +379,7 @@ pub const StateFile = struct { const stride = std.mem.alignForward(usize, @max(new.size, 1), page); 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 }; + h.* = .{ .magic = file_magic, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; try writeLayout(self.map[0..header_len], self.layout); var total: Migration = .{}; @@ -222,10 +399,10 @@ pub const StateFile = struct { return .{ .migrated = total }; } - fn valid(self: *StateFile) bool { + fn check(self: *StateFile) !void { const h = self.header(); - if (!std.mem.eql(u8, &h.magic, &file_magic)) return false; - return self.map.len >= header_len + slot_count * h.stride and h.size <= h.stride; + if (!std.mem.eql(u8, &h.magic, &file_magic) and !std.mem.eql(u8, &h.magic, &file_magic_v1)) return error.NotAStateFile; + if (self.map.len < header_len + slot_count * h.stride or h.size > h.stride) return error.CorruptStateFile; } fn resize(self: *StateFile, stride: usize) !void { @@ -240,17 +417,24 @@ pub const StateFile = struct { } }; -/// Writes `layout`'s leaves after the `Header` at the start of `region`. +/// Writes `layout`'s leaves after the `Header` at the start of `region`, +/// in the current format (`StoredField`); the caller sets the magic. 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 dims_len = leaf.dims.len * @sizeOf(Dim); + if (strings_start + strings + leaf.path.len + leaf.tags.len + dims_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); + for (leaf.dims, 0..) |d, j| { + const at = base + leaf.path.len + leaf.tags.len + j * @sizeOf(Dim); + std.mem.writeInt(u32, region[at..][0..4], d.count, .little); + std.mem.writeInt(u32, region[at + 4 ..][0..4], d.stride, .little); + } fields[i] = .{ .offset = leaf.offset, .size = leaf.size, @@ -260,24 +444,42 @@ fn writeLayout(region: []u8, layout: Layout) !void { .path_len = @intCast(leaf.path.len), .tags = @intCast(strings + leaf.path.len), .tags_len = @intCast(leaf.tags.len), + .rank = @intCast(leaf.dims.len), + .dims = @intCast(strings + leaf.path.len + leaf.tags.len), }; - strings += leaf.path.len + leaf.tags.len; + strings += leaf.path.len + leaf.tags.len + dims_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. +/// The layout stored in a state or store file's header, or null if it has +/// none. Reads both formats, by magic: a v1 header (`file_magic_v1`, +/// `store_layout_magic_v1`) has no element leaves, so its arrays are whole +/// leaves only, which migrate while their type is unchanged. pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout { const h: *const Header = @ptrCast(@alignCast(map.ptr)); if (h.hash == 0 and h.field_count == 0) return null; - const stored: [*]const StoredField = @ptrCast(@alignCast(map.ptr + @sizeOf(Header))); - const strings_start = @sizeOf(Header) + h.field_count * @sizeOf(StoredField); - if (strings_start + h.strings_len > header_len) return error.CorruptHeader; + const v1 = std.mem.eql(u8, &h.magic, &file_magic_v1) or std.mem.eql(u8, &h.magic, &store_layout_magic_v1); + const stride: usize = if (v1) @sizeOf(StoredFieldV1) else @sizeOf(StoredField); + const strings_start = @sizeOf(Header) + @as(usize, h.field_count) * stride; + if (strings_start + h.strings_len > header_len or map.len < header_len) return error.CorruptHeader; const strings = map[strings_start..][0..h.strings_len]; const leaves = try arena.alloc(Layout.Leaf, h.field_count); - for (leaves, stored[0..h.field_count]) |*leaf, f| { - if (f.path + f.path_len > strings.len or f.tags + f.tags_len > strings.len) return error.CorruptHeader; + for (leaves, 0..) |*leaf, i| { + const at = @sizeOf(Header) + i * stride; + const f: StoredField = if (v1) blk: { + const old: *const StoredFieldV1 = @ptrCast(@alignCast(map[at..].ptr)); + break :blk .{ .offset = old.offset, .size = old.size, .hash = old.hash, .path = old.path, .path_len = old.path_len, .tags = old.tags, .tags_len = old.tags_len, .kind = old.kind }; + } else @as(*const StoredField, @ptrCast(@alignCast(map[at..].ptr))).*; + const dims_len = @as(usize, f.rank) * @sizeOf(Dim); + if (f.path + f.path_len > strings.len or f.tags + f.tags_len > strings.len or f.dims + dims_len > strings.len) return error.CorruptHeader; + if (f.rank > abi.layout.max_rank) return error.CorruptHeader; + const dims = try arena.alloc(Dim, f.rank); + for (dims, 0..) |*d, j| { + const raw = strings[f.dims + j * @sizeOf(Dim) ..]; + d.* = .{ .count = std.mem.readInt(u32, raw[0..4], .little), .stride = std.mem.readInt(u32, raw[4..8], .little) }; + } // Copied: the mapping moves when the file is resized. leaf.* = .{ .path = try arena.dupe(u8, strings[f.path..][0..f.path_len]), @@ -286,7 +488,12 @@ pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout { .kind = f.kind, .hash = f.hash, .tags = try arena.dupe(u8, strings[f.tags..][0..f.tags_len]), + .dims = dims, }; + // Every element must lie inside the state. + var end: usize = @as(usize, leaf.offset) + leaf.size; + for (dims) |d| end += @as(usize, d.stride) * (d.count -| 1); + if (end > h.size) return error.CorruptHeader; } return .{ .size = h.size, .alignment = h.alignment, .hash = h.hash, .fields = leaves }; } @@ -318,7 +525,11 @@ pub fn mapReadOnly(path: []const u8) []align(page) const u8 { 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; +/// The allocator's record (`Meta`); unchanged by the layout format. pub const store_magic = "SKSTORE1".*; +/// The store's layout header, like `file_magic` and `file_magic_v1`. +pub const store_layout_magic = "SKSTORE2".*; +pub const store_layout_magic_v1 = "SKSTORE1".*; /// The layout region is a state file header; the allocator's own record /// follows, then the heap. const meta_offset = header_len; @@ -368,13 +579,15 @@ pub const StoreFile = struct { 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) { + if (!fresh and size > 0) { + // Checked before mapping, which may lengthen the file: a file + // that is not a store is refused untouched, never wiped. + var m: Meta = undefined; + const read = linux.pread(fd, std.mem.asBytes(&m), @sizeOf(Meta), meta_offset); + if (size < heap_offset or read != @sizeOf(Meta) or !std.mem.eql(u8, &m.magic, &store_magic) or m.base != store_base or m.top > size) return error.NotAStoreFile; 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; - } + self.layout = try readLayout(self.arena.allocator(), self.region()) orelse .{}; + return self; } try self.wipe(); return self; @@ -396,7 +609,7 @@ pub const StoreFile = struct { 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.header().* = .{ .magic = store_layout_magic, .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 }; self.meta().* = .{}; self.layout = .{}; } @@ -452,7 +665,7 @@ pub const StoreFile = struct { 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 }; + self.header().* = .{ .magic = store_layout_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; @@ -723,3 +936,192 @@ test "a store keeps its root and what it points to across a reopen, and migrates defer store.close(); try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V2, .store), D.v2)); } + +test "arrays migrate element by element: fields, enums by name, shrink and growth" { + const E1 = struct { x: f32 = 0, mood: enum(u8) { calm = 1, wild = 2 } = .calm }; + const E2 = struct { added: u8 = 9, mood: enum(u8) { wild = 5, calm = 6, sleepy = 7 } = .sleepy, x: f32 = 0 }; + const V1 = struct { list: struct { buffer: [4]E1 = @splat(.{}), len: u32 = 0 } = .{}, samples: [4]u16 = @splat(0), pos: [2]f32 = @splat(0), grid: [2][3]u8 = @splat(@splat(0)) }; + const V2 = struct { list: struct { buffer: [2]E2 = @splat(.{}), len: u32 = 0 } = .{}, samples: [6]u16 = @splat(5), pos: @Vector(2, f32) = @splat(0), grid: [3][2]u8 = @splat(@splat(0)) }; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .state)); + const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .state)); + var old: V1 = .{ .samples = .{ 1, 2, 3, 4 }, .pos = .{ 3, 4 }, .grid = .{ .{ 1, 2, 3 }, .{ 4, 5, 6 } } }; + for (&old.list.buffer, 0..) |*e, i| e.* = .{ .x = @floatFromInt(i + 1), .mood = if (i % 2 == 0) .calm else .wild }; + old.list.len = 4; + var new: V2 = .{}; + _ = migrate(l1, std.mem.asBytes(&old), l2, std.mem.asBytes(&new)); + try std.testing.expectEqual(E2{ .added = 9, .mood = .calm, .x = 1 }, new.list.buffer[0]); + try std.testing.expectEqual(E2{ .added = 9, .mood = .wild, .x = 2 }, new.list.buffer[1]); + // `len` is kept as it was: a List clamps it where it is read. + try std.testing.expectEqual(@as(u32, 4), new.list.len); + try std.testing.expectEqual([6]u16{ 1, 2, 3, 4, 5, 5 }, new.samples); + try std.testing.expectEqual(@as(f32, 4), new.pos[1]); + try std.testing.expectEqual([3][2]u8{ .{ 1, 2 }, .{ 4, 5 }, .{ 0, 0 } }, new.grid); + + // Growing back: the kept elements return, new ones take defaults. + var back: V1 = .{}; + _ = migrate(l2, std.mem.asBytes(&new), l1, std.mem.asBytes(&back)); + try std.testing.expectEqual(@as(f32, 2), back.list.buffer[1].x); + try std.testing.expectEqual(@as(f32, 0), back.list.buffer[2].x); + try std.testing.expectEqual(.wild, back.list.buffer[1].mood); +} + +test "an array of songs keeps its songs when a song gains a field" { + const Note = struct { pitch: u8 = 0, length: u8 = 1 }; + const Song1 = struct { name: [8]u8 = @splat(0), notes: [16]Note = @splat(.{}), tempo: u16 = 120 }; + const Song2 = struct { tempo: u16 = 120, swing: f32 = 0.5, notes: [16]Note = @splat(.{}), name: [8]u8 = @splat(0) }; + const V1 = struct { songs: [8]Song1 = @splat(.{}), other: u32 = 0 }; + const V2 = struct { added: u8 = 1, songs: [8]Song2 = @splat(.{}) }; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const l1 = try Layout.copy(arena.allocator(), abi.layout.of(V1, .state)); + const l2 = try Layout.copy(arena.allocator(), abi.layout.of(V2, .state)); + var old: V1 = .{}; + for (&old.songs, 0..) |*song, i| { + song.name = "song 0".*; + song.name[7] = '0' + @as(u8, @intCast(i)); + song.tempo = @intCast(100 + i); + for (&song.notes, 0..) |*n, j| n.* = .{ .pitch = @intCast(i * 16 + j), .length = 2 }; + } + var new: V2 = .{}; + const m = migrate(l1, std.mem.asBytes(&old), l2, std.mem.asBytes(&new)); + for (old.songs, new.songs) |a, b| { + try std.testing.expectEqualSlices(u8, &a.name, &b.name); + try std.testing.expectEqual(a.tempo, b.tempo); + try std.testing.expectEqual(@as(f32, 0.5), b.swing); + for (a.notes, b.notes) |x, y| try std.testing.expectEqual(x, y); + } + // songs[].notes is unchanged, so it copies whole; swing and added are new. + try std.testing.expectEqual(@as(usize, 2), m.reset); + // The same type on both sides copies in one piece. + var same: V1 = .{}; + try std.testing.expectEqual(@as(usize, 2), migrate(l1, std.mem.asBytes(&old), l1, std.mem.asBytes(&same)).kept); + try std.testing.expectEqualDeep(old, same); +} + +test "compare goes leaf by leaf: padding never differs" { + const S = struct { a: u8 = 1, b: u32 = 2, v: @Vector(3, f32) = .{ 1, 2, 3 }, list: [2]struct { c: u8 = 0, d: u16 = 0 } = @splat(.{}) }; + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const layout = try Layout.copy(arena.allocator(), abi.layout.of(S, .state)); + var x: S = .{}; + var y: S = .{}; + const a = std.mem.asBytes(&x); + const b = std.mem.asBytes(&y); + @memcpy(b, a); + // Every byte no leaf covers. + var covered: [@sizeOf(S)]bool = @splat(false); + for (layout.fields) |leaf| { + if (leaf.whole()) continue; + var it = leaf.elements(); + while (it.next()) |offset| @memset(covered[offset..][0..leaf.size], true); + } + var padding: usize = 0; + for (covered, 0..) |c, i| if (!c) { + b[i] ^= 0xff; + padding += 1; + }; + try std.testing.expect(padding >= 4); + try std.testing.expectEqual(@as(usize, 0), compare(layout, a, b).count); + y.list[1].d = 7; + y.v[2] = 0; + const diff = compare(layout, a, b); + try std.testing.expectEqual(@as(usize, 2), diff.count); + var buf: [64]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + try diff.last.?.leaf.name(diff.last.?.offset, &w); + try std.testing.expectEqualStrings("list[1].d", w.buffered()); +} + +fn testPath(buf: []u8, tmp: anytype, name: []const u8) ![]const u8 { + return std.mem.print(buf, ".zig-cache/tmp/{s}/{s}", .{ &tmp.sub_path, name }); +} + +test "a file that is not a state file is refused, untouched" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const io = std.testing.io; + var buf: [std.fs.max_path_bytes]u8 = undefined; + for ([_]usize{ 100, 3 * header_len }) |len| { + const path = try testPath(&buf, tmp, "notes.state"); + const text = try std.testing.allocator.alloc(u8, len); + defer std.testing.allocator.free(text); + @memset(text, 'x'); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = text }); + try std.testing.expectError(error.NotAStateFile, StateFile.open(std.testing.allocator, path)); + const after = try std.Io.Dir.cwd().readFileAlloc(io, path, std.testing.allocator, .unlimited); + defer std.testing.allocator.free(after); + try std.testing.expectEqualSlices(u8, text, after); + } +} + +test "a state file from before element leaves (drat-meout's) keeps every value" { + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = try testPath(&buf, tmp, "game.state"); + const original = try std.Io.Dir.cwd().readFileAlloc(io, "test/fixtures/drat-meout-v1.state-fixture", gpa, .unlimited); + defer gpa.free(original); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = original }); + try std.testing.expectEqualStrings(&file_magic_v1, original[0..8]); + + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + const old = (try readLayout(a, original)).?; + try std.testing.expect(old.find("tracker.songs") != null); + const h: *const Header = @ptrCast(@alignCast(original.ptr)); + const live = original[header_len..][0..h.size]; + + // The layout the game had when it wrote the file, as a sketch would + // describe it: unchanged, so nothing migrates and the header is + // rewritten in the new format. + const same = try asSketchLayout(a, old, &.{}); + { + var file = try StateFile.open(gpa, path); + defer file.close(); + try std.testing.expectEqual(StateFile.Adoption.unchanged, try file.adopt(gpa, same, NoDefaults.f)); + try std.testing.expectEqualStrings(&file_magic, &file.header().magic); + } + var file = try StateFile.open(gpa, path); + defer file.close(); + try std.testing.expectEqual(old.fields.len, file.layout.fields.len); + try std.testing.expectEqualSlices(u8, live, file.slot(0)); + + // A changed layout (one field gone, one new): every other leaf keeps + // its value, the songs included (an unchanged array type, so whole). + const changed = try asSketchLayout(a, old, "scene"); + _ = try file.adopt(gpa, changed, NoDefaults.f); + for (old.fields) |leaf| { + if (std.mem.eql(u8, leaf.path, "scene")) continue; + try std.testing.expectEqualSlices(u8, leaf.bytes(live), leaf.bytes(file.slot(0))); + } + try std.testing.expectEqual(@as(u8, 0), file.slot(0)[h.size]); +} + +const NoDefaults = struct { + fn f(_: abi.Memory, _: [*]u8) callconv(.c) void {} +}; + +/// `l` as `abi.Layout`, without the leaf at `drop` and with a new byte +/// leaf `added` past the end, the type hash changed accordingly. +fn asSketchLayout(a: std.mem.Allocator, l: Layout, drop: []const u8) !abi.Layout { + var fields: std.ArrayList(abi.Field) = .empty; + for (l.fields) |leaf| { + if (std.mem.eql(u8, leaf.path, drop)) continue; + try fields.append(a, .{ + .path = try a.dupeSentinel(u8, leaf.path, 0), + .offset = leaf.offset, + .size = leaf.size, + .kind = leaf.kind, + .hash = leaf.hash, + .tags = try a.dupeSentinel(u8, leaf.tags, 0), + }); + } + if (drop.len == 0) return .{ .size = l.size, .alignment = l.alignment, .hash = l.hash, .fields = fields.items }; + try fields.append(a, .{ .path = "added", .offset = @intCast(l.size), .size = 1, .kind = .uint, .hash = abi.layout.hash(u8) }); + return .{ .size = l.size + 1, .alignment = l.alignment, .hash = l.hash ^ 1, .fields = fields.items }; +} diff --git a/src/host/overlay.zig b/src/host/overlay.zig index 919fff6..5219a83 100644 --- a/src/host/overlay.zig +++ b/src/host/overlay.zig @@ -16,7 +16,10 @@ fn shade(path: []const u8) abi.Color { } pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, state: []const u8, width: f32) !void { - const rows = (layout.fields.len + 1) / 2; + // Arrays show as wholes: their element leaves (`list[].x`) are left out. + var top_level: usize = 0; + for (layout.fields) |leaf| top_level += @intFromBool(leaf.dims.len == 0); + const rows = (top_level + 1) / 2; const legend_top: f32 = 24; const hex_top = legend_top + @as(f32, @floatFromInt(rows)) * 14 + 16; const shown = @min(state.len, shown_bytes); @@ -26,7 +29,11 @@ pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, s // Legend: two fixed-width columns filled down then across. const column = (width - 60) / 2; - for (layout.fields, 0..) |leaf, i| { + var row: usize = 0; + for (layout.fields) |leaf| { + if (leaf.dims.len != 0) continue; + const i = row; + row += 1; var value: std.Io.Writer.Allocating = .init(arena); try fields.format(leaf, state, &value.writer); const name = if (leaf.path.len > 26) try std.fmt.allocPrint(arena, "{s}...", .{leaf.path[0..23]}) else leaf.path; @@ -42,7 +49,7 @@ pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, s var colors: [shown_bytes]abi.Color = @splat(.{ .r = 0x9f, .g = 0x9f, .b = 0x9f }); for (layout.fields) |leaf| { - if (leaf.offset >= shown) continue; + if (leaf.offset >= shown or leaf.dims.len != 0) continue; @memset(colors[leaf.offset..@min(shown, leaf.offset + leaf.size)], shade(leaf.path)); } for (state[0..shown], 0..) |byte, i| { diff --git a/src/host/probe.zig b/src/host/probe.zig index 4a693ec..d8ed166 100644 --- a/src/host/probe.zig +++ b/src/host/probe.zig @@ -30,6 +30,8 @@ pub fn main(init: std.process.Init) !void { const file = path orelse fatal("no state file given"); const map = memory.mapReadOnly(file); if (map.len < memory.header_len) fatal("not a sketchbook state file"); + const magic = map[0..8]; + if (!std.mem.eql(u8, magic, &memory.file_magic) and !std.mem.eql(u8, magic, &memory.file_magic_v1)) fatal("not a sketchbook state file"); defer std.posix.munmap(map); var arena: std.heap.ArenaAllocator = .init(init.gpa); defer arena.deinit(); @@ -52,9 +54,29 @@ fn show(path: []const u8, layout: memory.Layout, live: []const u8) void { var w: std.Io.Writer = .fixed(&buf); std.debug.print("{s}: {d} bytes, layout {x:0>16}\n", .{ path, live.len, layout.hash }); for (layout.fields) |leaf| { + // Whole arrays are shown by their elements' leaves. + if (leaf.whole()) continue; w.end = 0; - fields.format(leaf, live, &w) catch {}; - std.debug.print(" +0x{x:0>4} {s:<28} {s}\n", .{ leaf.offset, leaf.path, w.buffered() }); + if (leaf.dims.len == 0) { + fields.format(leaf, live, &w) catch {}; + } else { + // Elements in index order, the first few. + var it = leaf.elements(); + var n: usize = 0; + while (it.next()) |offset| : (n += 1) { + if (n == 8) { + w.writeAll(" ...") catch {}; + break; + } + if (n > 0) w.writeAll(" ") catch {}; + fields.format(leaf.at(offset), live, &w) catch {}; + } + } + var name: [128]u8 = undefined; + var nw: std.Io.Writer = .fixed(&name); + nw.writeAll(leaf.path) catch {}; + for (leaf.dims) |d| nw.print(" [{d}]", .{d.count}) catch {}; + std.debug.print(" +0x{x:0>4} {s:<28} {s}\n", .{ leaf.offset, nw.buffered(), w.buffered() }); } } diff --git a/src/host/root.zig b/src/host/root.zig index 2876c0f..8cc15e1 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -59,6 +59,22 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { } if (linked == null and options.library == null) fatal("no sketch library given (see --help)"); + // A scripted run from fresh state works on files of its own, moved to + // the given path at exit: two runs of one build step (two `zig build`s + // at once share its output path) never share a state, store or + // recordings file, nor fail on the store's lock. + var private_buf: [std.fs.max_path_bytes]u8 = undefined; + const given = options.state; + const private = script_path != null and options.fresh; + if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long"); + defer if (private) for ([_][]const u8{ "", ".inputs", ".store" }) |suffix| { + var from: [std.fs.max_path_bytes + 16]u8 = undefined; + var to: [std.fs.max_path_bytes + 16]u8 = undefined; + const a = std.mem.printSentinel(&from, "{s}{s}", .{ options.state, suffix }, 0) catch continue; + const b = std.mem.printSentinel(&to, "{s}{s}", .{ given, suffix }, 0) catch continue; + _ = std.os.linux.rename(a.ptr, b.ptr); + }; + sdl_heap.install(); { var host: Host = undefined; diff --git a/src/host/script.zig b/src/host/script.zig index cf551cd..7818c6b 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -8,21 +8,46 @@ //! 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 -//! console LINE run a console command +//! console LINE run a console command (`console get` +//! takes no frame: it only reads) //! frames N advance N frames without input -//! expect FIELD VALUE compare a state field (enums by name or number) -//! await FIELD VALUE run frames until it holds (a minute at most) +//! expect FIELD VALUE compare a state field (enums by name or number); +//! FIELD may index arrays and vector lanes, +//! `boids.buffer[3].pos[0]` +//! expect FIELD VALUE ~T numbers within T of VALUE +//! await FIELD VALUE [~T] run frames until it holds (a minute at most) +//! print FIELD write a field's value to stderr (a vector, +//! array or struct: every value in it), no frame +//! expect-snapshot N live state equals snapshot N, leaf by leaf +//! (padding ignored), exactly +//! expect-log TEXT a recent console line contains TEXT +//! expect-no-log TEXT no recent console line does //! expect-audio KEY stream KEY has received samples //! screenshot FILE save the next frame as PNG +//! expect-same-screenshot A B [MAX [X Y W H]] +//! two saved PNGs match, every channel within MAX +//! (default 0), in the rect if given (pixels) +//! expect-different-screenshot A B [X Y W H] +//! two saved PNGs differ somewhere (in the rect) +//! expect-pixel FILE X Y R G B A [TOL] +//! a saved PNG's pixel, every channel within TOL //! load LIBRARY swap in another build of the sketch //! reload load the watched library again //! await-reload SECONDS run frames until a build replaces the library //! //! `{name}` is replaced by `--define name=value` from the command line. +//! +//! Each line but `frames`, `print`, `console get`, the expectations and +//! `await-reload` is followed by one frame; `update` gets one call per +//! frame, live or replayed, so +//! `console play` then `frames N` replays N recorded frames at the +//! script's 60 Hz. const std = @import("std"); +const c = @import("c"); const abi = @import("abi"); const Host = @import("Host.zig"); const fields = @import("fields.zig"); +const memory = @import("memory.zig"); pub const frame_ns: u64 = std.time.ns_per_s / 60; /// The most frames `await` plays: a minute. @@ -84,28 +109,102 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons std.debug.print(" console {s}: {t}\n", .{ rest, err }); return err; }; + if (std.mem.startsWith(u8, rest, "get ")) return; + } else if (eq(u8, command, "print")) { + const path = words.next() orelse return error.ExpectedField; + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { + error.UnknownField => return error.UnknownField, + error.WriteFailed => {}, + }; + std.debug.print("{s} = {s}\n", .{ path, w.buffered() }); + return; } else if (eq(u8, command, "frames")) { const count = try std.fmt.parseInt(usize, rest, 10); for (0..count) |_| try advance(host, now); return; } else if (eq(u8, command, "expect") or eq(u8, command, "await")) { const path = words.next() orelse return error.ExpectedField; - const want = std.mem.trim(u8, words.rest(), " \t"); - const leaf = host.state.layout.find(path) orelse return error.UnknownField; + const want, const tolerance = try expected(std.mem.trim(u8, words.rest(), " \t")); + // Looked up again after each frame: a reload may move the field. + _ = host.state.layout.find(path) orelse return error.UnknownField; // `await` plays frames until the field holds the value, for up to a // minute: for waits whose length is the sketch's business. if (eq(u8, command, "await")) { var waited: usize = 0; - while (!fields.matches(leaf, host.live(), want) and waited < await_limit) : (waited += 1) try advance(host, now); + while (!holds(host, path, want, tolerance) and waited < await_limit) : (waited += 1) try advance(host, now); } - if (!fields.matches(leaf, host.live(), want)) { + if (!holds(host, path, want, tolerance)) { var buf: [128]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - fields.format(leaf, host.live(), &w) catch {}; - std.debug.print(" {s}: expected {s}, found {s}\n", .{ path, want, w.buffered() }); + fields.formatPath(host.state.layout, host.live(), path, &w) catch |err| switch (err) { + error.UnknownField => w.writeAll("no such field") catch {}, + error.WriteFailed => {}, + }; + if (tolerance) |t| { + std.debug.print(" {s}: expected {s} ~{d}, found {s}\n", .{ path, want, t, w.buffered() }); + } else std.debug.print(" {s}: expected {s}, found {s}\n", .{ path, want, w.buffered() }); + return error.ExpectationFailed; + } + return; + } else if (eq(u8, command, "expect-snapshot")) { + const index = std.fmt.parseInt(usize, rest, 10) catch return error.InvalidSlot; + if (!host.timeline.bank.hasSnapshot(index)) return error.EmptySnapshot; + if (!same(host, index)) return error.ExpectationFailed; + return; + } else if (eq(u8, command, "expect-log") or eq(u8, command, "expect-no-log")) { + var found = false; + for (&host.console.lines) |*l| { + if (std.mem.indexOf(u8, l.bytes[0..l.len], rest) != null) found = true; + } + if (found != eq(u8, command, "expect-log")) { + std.debug.print(" console log:\n", .{}); + for (&host.console.lines) |*l| std.debug.print(" {s}\n", .{l.bytes[0..l.len]}); return error.ExpectationFailed; } return; + } else if (eq(u8, command, "expect-same-screenshot") or eq(u8, command, "expect-different-screenshot")) { + const a = words.next() orelse return error.ExpectedFile; + const b = words.next() orelse return error.ExpectedFile; + const want_same = eq(u8, command, "expect-same-screenshot"); + const max: u8 = if (!want_same) 0 else if (words.next()) |m| std.fmt.parseInt(u8, m, 10) catch return error.InvalidValue else 0; + var rect: ?[4]usize = null; + if (words.next()) |x| { + var r: [4]usize = undefined; + r[0] = std.fmt.parseInt(usize, x, 10) catch return error.InvalidRect; + for (r[1..]) |*v| v.* = std.fmt.parseInt(usize, words.next() orelse return error.InvalidRect, 10) catch return error.InvalidRect; + rect = r; + } + const diff = try comparePng(arena, a, b, max, rect); + if ((diff.count == 0) != want_same) { + if (want_same) { + std.debug.print(" {s} vs {s}: {d} pixels differ by more than {d}, first at {d},{d}\n", .{ a, b, diff.count, max, diff.first[0], diff.first[1] }); + } else std.debug.print(" {s} and {s} are the same\n", .{ a, b }); + return error.ExpectationFailed; + } + return; + } else if (eq(u8, command, "expect-pixel")) { + const file = words.next() orelse return error.ExpectedFile; + var n: [7]u8 = .{ 0, 0, 0, 0, 0, 0, 0 }; + var x: usize = 0; + var y: usize = 0; + for (0..7) |i| { + const word = words.next() orelse if (i == 6) break else return error.ExpectedPixel; + if (i < 2) { + (if (i == 0) &x else &y).* = std.fmt.parseInt(usize, word, 10) catch return error.ExpectedPixel; + } else n[i - 2] = std.fmt.parseInt(u8, word, 0) catch return error.ExpectedPixel; + } + const tol = n[4]; + const surface = try loadPng(arena, file); + defer c.SDL_DestroySurface(surface); + if (x >= surface.*.w or y >= surface.*.h) return error.OutsideScreenshot; + const p = (@as([*]const u8, @ptrCast(surface.*.pixels.?)) + y * @as(usize, @intCast(surface.*.pitch)) + x * 4)[0..4]; + for (p, n[0..4]) |got, want| if (@max(got, want) - @min(got, want) > tol) { + std.debug.print(" {s} at {d},{d}: expected {d} {d} {d} {d}, found {d} {d} {d} {d}\n", .{ file, x, y, n[0], n[1], n[2], n[3], p[0], p[1], p[2], p[3] }); + return error.ExpectationFailed; + }; + return; } else if (eq(u8, command, "expect-audio")) { const key = try std.fmt.parseInt(u32, rest, 10); if (host.input.stream(key).pushed == 0) return error.ExpectationFailed; @@ -144,6 +243,89 @@ fn held(host: *const Host) u8 { return abi.Event.withModifiers(mods); } +/// "VALUE" or "VALUE ~TOLERANCE". +fn expected(text: []const u8) !struct { []const u8, ?f64 } { + const tilde = std.mem.lastIndexOfScalar(u8, text, '~') orelse return .{ text, null }; + const tolerance = std.fmt.parseFloat(f64, std.mem.trim(u8, text[tilde + 1 ..], " \t")) catch return error.InvalidTolerance; + return .{ std.mem.trim(u8, text[0..tilde], " \t"), tolerance }; +} + +fn holds(host: *Host, path: []const u8, want: []const u8, tolerance: ?f64) bool { + const leaf = host.state.layout.find(path) orelse return false; + const t = tolerance orelse return fields.matches(leaf, host.live(), want); + const value = fields.number(leaf, host.live()) orelse return false; + const target = std.fmt.parseFloat(f64, want) catch return false; + return @abs(value - target) <= t; +} + +/// Live state against snapshot `index`, leaf by leaf; prints the first few +/// differences. +fn same(host: *Host, index: usize) bool { + const layout = host.state.layout; + const live = host.live(); + const saved = host.timeline.bank.slot(index); + var count: usize = 0; + for (layout.fields) |leaf| { + if (leaf.whole()) continue; + var it = leaf.elements(); + while (it.next()) |offset| { + if (std.mem.eql(u8, live[offset..][0..leaf.size], saved[offset..][0..leaf.size])) continue; + count += 1; + if (count > 5) continue; + var buf: [256]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + leaf.name(offset, &w) catch {}; + w.writeAll(": live ") catch {}; + fields.format(leaf.at(offset), live, &w) catch {}; + w.print(", snapshot {d} ", .{index}) catch {}; + fields.format(leaf.at(offset), saved, &w) catch {}; + std.debug.print(" {s}\n", .{w.buffered()}); + } + } + if (count > 5) std.debug.print(" ... {d} differing values in all\n", .{count}); + return count == 0; +} + +const PngDiff = struct { count: usize = 0, first: [2]usize = .{ 0, 0 } }; + +/// Pixels whose channels differ by more than `max`; a size mismatch +/// counts every pixel. +fn comparePng(arena: std.mem.Allocator, a: []const u8, b: []const u8, max: u8, rect: ?[4]usize) !PngDiff { + const sa = try loadPng(arena, a); + defer c.SDL_DestroySurface(sa); + const sb = try loadPng(arena, b); + defer c.SDL_DestroySurface(sb); + if (sa.*.w != sb.*.w or sa.*.h != sb.*.h) return .{ .count = @intCast(@max(sa.*.w * sa.*.h, sb.*.w * sb.*.h, 1)) }; + var diff: PngDiff = .{}; + const w: usize = @intCast(sa.*.w); + const h: usize = @intCast(sa.*.h); + const r = rect orelse [4]usize{ 0, 0, w, h }; + for (@min(r[1], h)..@min(r[1] + r[3], h)) |y| { + const ra: [*]const u8 = @as([*]const u8, @ptrCast(sa.*.pixels.?)) + y * @as(usize, @intCast(sa.*.pitch)); + const rb: [*]const u8 = @as([*]const u8, @ptrCast(sb.*.pixels.?)) + y * @as(usize, @intCast(sb.*.pitch)); + for (@min(r[0], w)..@min(r[0] + r[2], w)) |x| { + const differs = for (0..4) |i| { + const p = ra[x * 4 + i]; + const q = rb[x * 4 + i]; + if (@max(p, q) - @min(p, q) > max) break true; + } else false; + if (!differs) continue; + if (diff.count == 0) diff.first = .{ x, y }; + diff.count += 1; + } + } + return diff; +} + +fn loadPng(arena: std.mem.Allocator, path: []const u8) !*c.SDL_Surface { + const loaded = c.SDL_LoadPNG((try arena.dupeSentinel(u8, path, 0)).ptr) orelse { + std.debug.print(" {s}: {s}\n", .{ path, c.SDL_GetError() }); + return error.ScreenshotUnreadable; + }; + defer c.SDL_DestroySurface(loaded); + return c.SDL_ConvertSurface(loaded, c.SDL_PIXELFORMAT_RGBA32) orelse error.ScreenshotUnreadable; +} + fn advance(host: *Host, now: *u64) !void { try host.frame(now.*); now.* += frame_ns; diff --git a/src/host/timeline.zig b/src/host/timeline.zig index cd65478..8a5448a 100644 --- a/src/host/timeline.zig +++ b/src/host/timeline.zig @@ -3,6 +3,11 @@ //! //! Playback is host state, not sketch state: wall clocks, modes and cursors //! must not contaminate byte comparisons between two runs of a sketch. +//! +//! Recordings are frames: each frame's events, then its `tick`. Playback, +//! seek and step only ever stop between frames, and `frames` cuts a run of +//! events into them, so `update` sees exactly the frames it saw live, one +//! call each. const std = @import("std"); const posix = std.posix; const abi = @import("abi"); @@ -19,13 +24,52 @@ pub const Recording = extern struct { count: u32, snapshot: u32, magic: u32, - reserved: [3]u32, + /// The build that recorded it (`build`, two halves), 0 when unknown. + build_low: u32, + build_high: u32, + reserved: u32, events: [capacity]abi.Event, slack: [recording_len - 32 - capacity * @sizeOf(abi.Event)]u8, pub fn slice(r: *const Recording) []const abi.Event { return r.events[0..r.count]; } + pub fn build(r: *const Recording) u64 { + return @as(u64, r.build_high) << 32 | r.build_low; + } + /// Events up to the end of the last whole frame. + pub fn whole(r: *const Recording) usize { + return frameStart(r.slice(), r.count); + } +}; + +/// The largest frame boundary at or before `index`: 0, or just after a tick. +pub fn frameStart(events: []const abi.Event, index: usize) usize { + var i = @min(index, events.len); + while (i > 0 and events[i - 1].kind != .tick) i -= 1; + return i; +} + +/// Cuts events into frames, each ending with its tick. Events after the +/// last tick (a frame not finished) are never returned. +pub fn frames(events: []const abi.Event) Frames { + return .{ .events = events }; +} + +pub const Frames = struct { + events: []const abi.Event, + start: usize = 0, + + pub fn next(f: *Frames) ?[]const abi.Event { + var i = f.start; + while (i < f.events.len) : (i += 1) { + if (f.events[i].kind != .tick) continue; + const frame = f.events[f.start .. i + 1]; + f.start = i + 1; + return frame; + } + return null; + } }; comptime { @@ -65,16 +109,9 @@ pub const Bank = struct { if (r.magic != recording_magic or r.count > capacity or !self.hasSnapshot(r.snapshot)) return error.EmptyRecording; return r; } - pub const Diff = struct { count: usize = 0, first: ?usize = null, last: ?usize = null }; - pub fn compare(self: Bank, a: usize, b: usize) Diff { - var diff: Diff = .{}; - for (self.slot(a), self.slot(b), 0..) |x, y, i| { - if (x == y) continue; - diff.count += 1; - if (diff.first == null) diff.first = i; - diff.last = i; - } - return diff; + /// Leaf by leaf (`memory.compare`): padding never differs. + pub fn compare(self: Bank, a: usize, b: usize) memory.Diff { + return memory.compare(self.state.layout, self.slot(a), self.slot(b)); } }; @@ -85,37 +122,55 @@ pub const Timeline = struct { cursor: usize = 0, position: u64 = 0, clock: u64 = 0, + /// Set whenever live state is restored from a snapshot, for the host + /// to reset the input state that went with the old bytes. + restored: bool = false, + + /// Restores a snapshot into live state. + pub fn restore(self: *Timeline, index: usize) !void { + try self.bank.restore(index); + self.restored = true; + } - pub fn startRecording(self: *Timeline, index: usize, snapshot: usize, now: u64) !void { + /// `build` identifies the code recording (0: unknown); `play` warns + /// when it differs. + pub fn startRecording(self: *Timeline, index: usize, snapshot: usize, now: u64, build: u64) !void { // An unused snapshot captures the current state; an existing one is // restored, so several recordings can branch from exactly those bytes. if (!self.bank.hasSnapshot(snapshot)) self.bank.save(snapshot); - try self.bank.restore(snapshot); + try self.restore(snapshot); const r = self.bank.recording(index); @memset(std.mem.asBytes(r)[0..32], 0); r.magic = recording_magic; r.snapshot = @intCast(snapshot); + r.build_low = @truncate(build); + r.build_high = @truncate(build >> 32); self.selected = index; self.cursor = 0; self.position = 0; self.clock = now; self.mode = .recording; } - pub fn capture(self: *Timeline, event: abi.Event) !void { + /// Records one frame (its events, then its tick) and rewrites their + /// times as recorded, so live `update` sees exactly what a replay will. + /// A frame that does not fit ends the recording before it. + pub fn capture(self: *Timeline, frame: []abi.Event) !void { if (self.mode != .recording) return; const r = self.bank.recording(self.selected); - if (r.count == capacity) { + if (frame.len > capacity - r.count) { self.mode = .live; return error.RecordingFull; } - var e = event; - // Queued events can predate the start command. Keep insertion order and - // nondecreasing times, including several events at the same timestamp. - e.time = @max(e.time -| self.clock, r.duration); - r.events[r.count] = e; - r.count += 1; - r.duration = e.time; - self.position = e.time; + for (frame) |*e| { + // Queued events can predate the start command. Keep insertion + // order and nondecreasing times, including several events at the + // same timestamp. + e.time = @max(e.time -| self.clock, r.duration); + r.events[r.count] = e.*; + r.count += 1; + r.duration = e.time; + } + self.position = r.duration; self.cursor = r.count; } pub fn stop(self: *Timeline, now: u64) void { @@ -128,7 +183,7 @@ pub const Timeline = struct { } pub fn play(self: *Timeline, index: usize, now: u64) !void { const r = try self.bank.get(index); - try self.bank.restore(r.snapshot); + try self.restore(r.snapshot); self.selected = index; self.cursor = 0; self.position = 0; @@ -143,53 +198,61 @@ pub const Timeline = struct { self.mode = .playing; } } - /// The recorded events due by `now`. Feed them to the same `update` that - /// live input goes through. + /// The recorded frames due by `now`: whole frames, whose ticks are due + /// (a frame's input comes before its tick, so input alone is never + /// due). Feed them to the same `update` that live input goes through. pub fn tick(self: *Timeline, now: u64) []const abi.Event { if (self.mode != .playing) return &.{}; const r = self.bank.recording(self.selected); self.position = @min(r.duration, self.position +| (now -| self.clock)); self.clock = now; const start = self.cursor; - while (self.cursor < r.count and r.events[self.cursor].time <= self.position) self.cursor += 1; + var i = self.cursor; + while (i < r.count and r.events[i].time <= self.position) : (i += 1) { + if (r.events[i].kind == .tick) self.cursor = i + 1; + } if (self.position == r.duration) self.mode = .paused; return r.events[start..self.cursor]; } - /// Restores the snapshot and returns the first `count` events to replay, - /// so seeking backwards works too. + /// Restores the snapshot and returns the frames that make up the first + /// `count` events (rounded down to a whole frame) to replay, so seeking + /// backwards works too. pub fn seekCount(self: *Timeline, count: usize) ![]const abi.Event { const r = try self.bank.get(self.selected); - try self.bank.restore(r.snapshot); - self.cursor = @min(count, r.count); + try self.restore(r.snapshot); + self.cursor = frameStart(r.slice(), count); self.position = if (self.cursor == 0) 0 else r.events[self.cursor - 1].time; self.mode = .paused; return r.events[0..self.cursor]; } + /// To the last frame whose tick is at or before `time`. pub fn seekTime(self: *Timeline, time: u64) ![]const abi.Event { const r = try self.bank.get(self.selected); const position = @min(time, r.duration); var count: usize = 0; - while (count < r.count and r.events[count].time <= position) count += 1; + for (r.slice(), 0..) |e, i| { + if (e.time > position) break; + if (e.kind == .tick) count = i + 1; + } const events = try self.seekCount(count); self.position = position; return events; } - /// Moves by `steps` input events, passing over frame ticks. + /// Moves by `steps` frames, forwards or back. pub fn step(self: *Timeline, steps: i64) ![]const abi.Event { const r = try self.bank.get(self.selected); - var cursor = self.cursor; + const events = r.slice(); + var cursor = frameStart(events, self.cursor); var remaining = @abs(steps); while (remaining > 0) : (remaining -= 1) { if (steps > 0) { - while (cursor < r.count and r.events[cursor].kind == .tick) cursor += 1; - if (cursor == r.count) break; + const end = r.whole(); + if (cursor == end) break; cursor += 1; - // Include the frame's tick so time-driven sketches advance too. - while (cursor < r.count and r.events[cursor].kind == .tick) cursor += 1; + while (cursor < end and events[cursor - 1].kind != .tick) cursor += 1; } else { - while (cursor > 0 and r.events[cursor - 1].kind == .tick) cursor -= 1; if (cursor == 0) break; - cursor -= 1; + cursor = frameStart(events, cursor - 1); } } return self.seekCount(cursor); @@ -206,3 +269,76 @@ pub fn mapRecordings(path: []const u8) ![]align(memory.page) u8 { }; return memory.mapFile(fd, recordings_len, .{ .READ = true, .WRITE = true }); } + +test "frames are whole: input waits for its tick, live and in playback" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const a = arena.allocator(); + // A bank on plain memory: one slot of 8 bytes, snapshot 1 used. + var file: memory.StateFile = undefined; + file.map = try a.alignedAlloc(u8, .fromByteUnits(memory.page), memory.header_len + memory.slot_count * memory.page); + @memset(file.map, 0); + file.header().* = .{ .size = 8, .alignment = 1, .hash = 1, .stride = memory.page, .used = 0b11, .field_count = 0, .strings_len = 0 }; + file.layout = .{}; + const inputs = try a.alignedAlloc(u8, .fromByteUnits(memory.page), recording_len); + @memset(inputs, 0); + var t: Timeline = .{ .bank = .{ .state = &file, .inputs = inputs } }; + // Frame k: a move at 10k ms, its tick at 10k + 5 ms; then a move with + // no tick (a frame cut off). + try t.startRecording(0, 1, 1000, 7); + try std.testing.expect(t.restored); + for (0..4) |k| { + var frame = [_]abi.Event{ + .{ .kind = .mouse_move, .time = 1000 + k * 10_000_000, .x = @floatFromInt(k) }, + abi.Event.tick(1000 + k * 10_000_000 + 5_000_000, 16_666_667), + }; + try t.capture(&frame); + // Live update sees the recorded times. + try std.testing.expectEqual(@as(u64, k * 10_000_000 + 5_000_000), frame[1].time); + } + const r = t.bank.recording(0); + r.events[r.count] = .{ .kind = .mouse_move, .time = 40_000_000 }; + r.count += 1; + t.stop(1000 + 41_000_000); + try std.testing.expectEqual(@as(u64, 7), r.build()); + + try t.play(0, 0); + var delivered: usize = 0; + // Every moment between 0 and the end, 1 ms apart. + var now: u64 = 0; + while (t.mode == .playing) : (now += 1_000_000) { + const events = t.tick(now); + if (events.len == 0) continue; + try std.testing.expectEqual(abi.Event.Kind.tick, events[events.len - 1].kind); + var it = frames(events); + while (it.next()) |frame| { + try std.testing.expectEqual(@as(usize, 2), frame.len); + delivered += 1; + } + } + try std.testing.expectEqual(@as(usize, 4), delivered); + try std.testing.expectEqual(@as(usize, 8), t.cursor); + + // A seek between a frame's input and its tick stops before the frame. + try std.testing.expectEqual(@as(usize, 4), (try t.seekTime(22_000_000)).len); + try std.testing.expectEqual(@as(usize, 8), (try t.seekCount(r.count)).len); + try std.testing.expectEqual(@as(usize, 6), (try t.step(-1)).len); + try std.testing.expectEqual(@as(usize, 8), (try t.step(5)).len); + try std.testing.expectEqual(@as(usize, 2), (try t.step(-3)).len); + // The cut-off frame is never fed. + var it = frames(r.slice()); + var n: usize = 0; + while (it.next()) |_| n += 1; + try std.testing.expectEqual(@as(usize, 4), n); +} + +test "a tick's dt reads back the same live and replayed" { + // E4: nanoseconds do not survive f32 seconds; both sides read the f32. + for ([_]u64{ 16_666_667, 16_666_666, 15_000_001, 33_333_333, 0, 1 }) |dt| { + const live = abi.Event.tick(5, dt); + var recorded = live; + recorded.time = 0; + try std.testing.expectEqual(live.tickDt(), recorded.tickDt()); + try std.testing.expect(@abs(@as(i64, @intCast(live.tickDt())) - @as(i64, @intCast(dt))) < 8); + } +} |
