diff options
Diffstat (limited to 'src/host')
| -rw-r--r-- | src/host/Console.zig | 529 | ||||
| -rw-r--r-- | src/host/Glyphs.zig | 279 | ||||
| -rw-r--r-- | src/host/Host.zig | 751 | ||||
| -rw-r--r-- | src/host/Presentation.zig | 331 | ||||
| -rw-r--r-- | src/host/Sessions.zig | 1599 | ||||
| -rw-r--r-- | src/host/Timing.zig | 169 | ||||
| -rw-r--r-- | src/host/TimingAtlas.zig | 139 | ||||
| -rw-r--r-- | src/host/audio.zig | 266 | ||||
| -rw-r--r-- | src/host/control.zig | 707 | ||||
| -rw-r--r-- | src/host/fields.zig | 37 | ||||
| -rw-r--r-- | src/host/gfx.zig | 92 | ||||
| -rw-r--r-- | src/host/gpu.zig | 669 | ||||
| -rw-r--r-- | src/host/library.zig | 118 | ||||
| -rw-r--r-- | src/host/memory.zig | 586 | ||||
| -rw-r--r-- | src/host/overlay.zig | 52 | ||||
| -rw-r--r-- | src/host/probe.zig | 86 | ||||
| -rw-r--r-- | src/host/root.zig | 289 | ||||
| -rw-r--r-- | src/host/script.zig | 471 | ||||
| -rw-r--r-- | src/host/state_files.zig | 303 | ||||
| -rw-r--r-- | src/host/timeline.zig | 327 | ||||
| -rw-r--r-- | src/host/timing_marker.zig | 45 |
21 files changed, 5866 insertions, 1979 deletions
diff --git a/src/host/Console.zig b/src/host/Console.zig index 40845f7..6fdfba4 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -1,21 +1,73 @@ //! The developer console: a line editor, a short log, and the host commands -//! (snapshots, recordings, replay, field inspection). Lines the host does not -//! know go to the sketch's `command`. +//! (marks, clips, replay, sessions, field inspection). Lines the host does +//! not know go to the sketch's `command`. const Console = @This(); const std = @import("std"); const abi = @import("abi"); const Host = @import("Host.zig"); const fields = @import("fields.zig"); const memory = @import("memory.zig"); -const timeline = @import("timeline.zig"); +const Sessions = @import("Sessions.zig"); pub const panel_height = 144; pub const Key = enum { left, right, home, end, backspace, delete, clear, tab, submit }; -const Line = struct { bytes: [118]u8 = @splat(0), len: usize = 0 }; +const Line = struct { + bytes: [width]u8 = @splat(0), + len: usize = 0, + const width = 118; +}; + +/// The console's words, a row each: what `help` prints, Tab completes, +/// `execute` runs (its switch) and `Host.runLine` asks `moves_state` of. +/// A line whose first word is none of them goes to the sketch. +pub const Verb = enum { help, get, set, reset, save, load, diff, snapshot, marks, record, recording, clips, play, stop, pause, step, seek, sessions, @"export-session", import, overlay, timing, quit, exit }; +pub const Row = struct { + args: []const u8 = "", + doc: []const u8, + /// May change State other than through `update`: a jump. + moves_state: bool = false, +}; +pub const verbs = std.enums.EnumArray(Verb, Row).init(.{ + .help = .{ .args = "[VERB]", .doc = "the verbs, or what one does; then the sketch's help" }, + .get = .{ .args = "FIELD", .doc = "a State field's value (`pos[0]`, `boids.buffer[3]`); a vector, array or struct shows every value in it, in memory order" }, + .set = .{ .args = "FIELD VALUE...", .doc = "a field to a value (enums by name or number), or an array of numbers to all its values, in memory order", .moves_state = true }, + .reset = .{ .doc = "State to its defaults, then the sketch's init; marks are kept", .moves_state = true }, + .save = .{ .args = "[MARK]", .doc = "marks the State now, MARK or the snapshot one" }, + .load = .{ .args = "[MARK|#K]", .doc = "brings a mark back (#K: the K-th in marks), migrated to today's layout", .moves_state = true }, + .diff = .{ .args = "[MARK]", .doc = "live State against a mark, leaf by leaf" }, + .snapshot = .{ .args = "MARK", .doc = "the mark save, load, diff and record use when given none" }, + .marks = .{ .doc = "the last marks" }, + .record = .{ .args = "[CLIP]", .doc = "starts a clip from the snapshot mark (saved first if new, else restored)", .moves_state = true }, + .recording = .{ .args = "CLIP", .doc = "the clip record, play, seek and step use when given none" }, + .clips = .{ .doc = "the last clips" }, + .play = .{ .args = "session S [at SECONDS] | [CLIP]", .doc = "replays a past session (paused at SECONDS), or a clip", .moves_state = true }, + .stop = .{ .doc = "ends the clip being recorded or played: live from here" }, + .pause = .{ .doc = "pauses or resumes a replay" }, + .step = .{ .args = "N", .doc = "N frames on (or back) in the replay", .moves_state = true }, + .seek = .{ .args = "start|end|MS", .doc = "to a place in the replay", .moves_state = true }, + .sessions = .{ .doc = "where this run records, and the last sessions" }, + .@"export-session" = .{ .args = "[S] FILE", .doc = "session S (this run's) with its marks and clips, to a database of its own" }, + .import = .{ .args = "FILE", .doc = "the sessions of such a file, its marks and clips as NAME@<file>" }, + .overlay = .{ .args = "memory|sound|timing", .doc = "shows or hides an overlay" }, + .timing = .{ .args = "[on|off|frame|work|reset|stats]", .doc = "the frame timing graph: shown, its metric, cleared, or its numbers" }, + .quit = .{ .doc = "ends the run" }, + .exit = .{ .doc = "ends the run" }, +}); -const host_commands = [_][]const u8{ - "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", - "record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit", +/// What Tab completes from: every verb, and with each of the words its +/// first argument may be (`seek start`). +const completions = blk: { + @setEvalBranchQuota(100_000); + var list: []const []const u8 = &.{}; + for (std.enums.values(Verb)) |v| { + list = list ++ .{@tagName(v)}; + const args = verbs.get(v).args; + var words = std.mem.tokenizeScalar(u8, std.mem.trim(u8, args[0 .. std.mem.indexOfScalar(u8, args, ' ') orelse args.len], "[]"), '|'); + while (words.next()) |word| if (std.ascii.isLower(word[0])) { + list = list ++ .{@tagName(v) ++ " " ++ word}; + }; + } + break :blk list; }; visible: bool = false, @@ -23,11 +75,34 @@ buffer: [96]u8 = @splat(0), len: usize = 0, cursor: usize = 0, lines: [5]Line = @splat(.{}), -snapshot: usize = 1, -recording: usize = 0, +/// The mark `save`, `load`, `diff` and `record` use when given none. +snapshot: Name = .of("1"), +/// The clip `record`, `play`, `seek` and `step` use when given none. +recording: Name = .of("0"), overlay_memory: bool = false, overlay_sound: bool = false, +pub const Name = struct { + bytes: [Sessions.max_name]u8 = undefined, + len: usize = 0, + + pub fn of(text_: []const u8) Name { + var n: Name = .{ .len = text_.len }; + @memcpy(n.bytes[0..text_.len], text_); + return n; + } + pub fn get(n: *const Name) []const u8 { + return n.bytes[0..n.len]; + } +}; + +/// A mark or clip name: a word of at most `Sessions.max_name` bytes. +fn nameOr(arg: ?[]const u8, default: *const Name) ![]const u8 { + const n = arg orelse return default.get(); + if (n.len > Sessions.max_name) return error.NameTooLong; + return n; +} + pub fn text(self: *const Console) []const u8 { return self.buffer[0..self.len]; } @@ -85,7 +160,7 @@ fn complete(self: *Console, extra: []const []const u8) void { var matches: [256]u8 = undefined; var used: usize = 0; var count: usize = 0; - for ([_][]const []const u8{ &host_commands, extra }) |list| for (list) |candidate| { + for ([_][]const []const u8{ completions, extra }) |list| for (list) |candidate| { if (!std.mem.startsWith(u8, candidate, self.text())) continue; if (first) |previous| { var i: usize = 0; @@ -131,99 +206,199 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const command = words.next() orelse return &.{}; const arg = words.next(); 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 (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 &.{}; - } - if (eq(command, "overlay")) { - const name = arg orelse return error.ExpectedOverlay; - const flag = if (eq(name, "memory")) &self.overlay_memory else if (eq(name, "sound")) &self.overlay_sound else return error.UnknownOverlay; - flag.* = !flag.*; - self.print("{s} overlay: {s}", .{ name, if (flag.*) "on" else "off" }); - } else if (eq(command, "get")) { - const path = arg orelse return error.ExpectedField; - var buf: [160]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 => {}, // shown cut off - }; - self.print("{s} = {s}", .{ path, w.buffered() }); - } else if (eq(command, "set")) { - const path = arg orelse return error.ExpectedField; - const leaf = host.state.layout.find(path) orelse return error.UnknownField; - if (rest.len == 0) return error.ExpectedValue; - h.stop(now); - try fields.set(leaf, host.live(), std.mem.trim(u8, rest, " ")); - self.print("{s} = {s}", .{ path, std.mem.trim(u8, rest, " ") }); - } else if (eq(command, "quit") or eq(command, "exit")) { - h.stop(now); - host.quit = true; - } else if (eq(command, "reset")) { - h.stop(now); - host.resetState(); - self.log("Live state reset. Snapshots are kept."); - } else if (eq(command, "snapshot")) { - self.snapshot = try slot(arg orelse return error.ExpectedSlot, 1, memory.slot_count - 1); - } else if (eq(command, "recording")) { - self.recording = try slot(arg orelse return error.ExpectedSlot, 0, timeline.recording_count - 1); - } else if (eq(command, "save") or eq(command, "load")) { - const index = if (arg) |a| try slot(a, 1, memory.slot_count - 1) else self.snapshot; - if (eq(command, "load") and !h.bank.hasSnapshot(index)) return error.EmptySnapshot; - h.stop(now); - if (eq(command, "save")) { - h.bank.save(index); - self.print("Saved snapshot {d}. Recordings from its old contents are invalidated.", .{index}); - } else { - 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, 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")) { - if (h.mode != .playing and h.mode != .paused) return error.NotReplaying; - h.pause(now); - } else if (eq(command, "step") or eq(command, "seek")) { - const value = arg orelse return error.ExpectedPosition; - _ = try h.bank.get(if (h.mode == .live or h.mode == .recording) self.recording else h.selected); - 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); - const ms = std.fmt.parseInt(u64, value, 10) catch return error.InvalidPosition; - return h.seekTime(ms *| std.time.ns_per_ms); - } else if (eq(command, "diff")) { - 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); - 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 { + const verb = std.meta.stringToEnum(Verb, command) orelse { // Sketch commands edit state outside the recorded input. - h.stop(now); + try stop(host); switch (host.forward(self, line, now)) { .unknown => return error.UnknownCommand, .failed => return error.CommandFailed, .done => {}, } + return &.{}; + }; + switch (verb) { + .help => { + if (std.meta.stringToEnum(Verb, arg orelse "")) |v| { + self.print("{t} {s}: {s}", .{ v, verbs.get(v).args, verbs.get(v).doc }); + return &.{}; + } + var buf: [Line.width + 3]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + // Names only, so the sketch's help fits the log below them. + w.writeAll("help VERB says more:") catch {}; + for (std.enums.values(Verb)) |v| { + if (w.end + @tagName(v).len + 1 > Line.width) { + self.log(w.buffered()); + w.end = 0; + } + w.print(" {t}", .{v}) catch {}; + } + self.log(w.buffered()); + if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); + }, + .overlay => { + const name = arg orelse return error.ExpectedOverlay; + if (rest.len != 0) return error.UnexpectedArgument; + const flag = if (eq(name, "memory")) &self.overlay_memory else if (eq(name, "sound")) &self.overlay_sound else if (eq(name, "timing")) &host.timing.visible else return error.UnknownOverlay; + flag.* = !flag.*; + host.timing.dirty = true; + self.print("{s} overlay: {s}", .{ name, if (flag.*) "on" else "off" }); + }, + .timing => { + if (rest.len != 0) return error.UnexpectedArgument; + if (eq(arg orelse "", "stats")) { + self.print("Timing: last {d}/240 frames; frame includes present, work excludes present (wall clock).", .{host.timing.count}); + inline for (.{ .frame, .work }) |metric| { + const s = host.timing.stats(metric); + self.print("{s}: avg {d:.3} ms | p90 {d:.3} ms | p99 {d:.3} ms | max {d:.3} ms", .{ @tagName(metric), s.mean, s.p90, s.p99, s.max }); + } + } else { + try host.timing.command(arg); + self.print("Timing overlay: {s}, graph: {t}, samples: {d}", .{ if (host.timing.visible) "on" else "off", host.timing.metric, host.timing.count }); + } + }, + .get => { + const path = arg orelse return error.ExpectedField; + var buf: [160]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 => {}, // shown cut off + }; + self.print("{s} = {s}", .{ path, w.buffered() }); + }, + .set => { + const path = arg orelse return error.ExpectedField; + const leaf = host.state.layout.find(path) orelse return error.UnknownField; + if (rest.len == 0) return error.ExpectedValue; + try stop(host); + if (leaf.whole()) try fields.setRow(host.state.layout, host.live(), path, rest) else try fields.set(leaf, host.live(), std.mem.trim(u8, rest, " ")); + self.print("{s} = {s}", .{ path, std.mem.trim(u8, rest, " ") }); + }, + .quit, .exit => { + try stop(host); + host.quit = true; + }, + .reset => { + try stop(host); + host.resetState(); + self.log("Live state reset. Snapshots are kept."); + }, + .snapshot, .recording => { + const n = try nameOr(arg orelse return error.ExpectedName, &self.snapshot); + (if (verb == .snapshot) &self.snapshot else &self.recording).* = .of(n); + }, + .save => { + const n = try nameOr(arg, &self.snapshot); + try stop(host); + _ = try host.sessions.mark(n, .mark, host.live()); + self.print("Saved snapshot {s}.", .{n}); + }, + .load => { + const n = try nameOr(arg, &self.snapshot); + const key = try loadMark(host, n); + defer key.free(host.gpa); + try stop(host); + try h.restore(key); + self.print("Loaded snapshot {s}.", .{n}); + }, + .record => { + const n = try nameOr(arg, &self.recording); + const from = self.snapshot.get(); + try stop(host); + // An unused mark captures the current state; an existing one is + // restored, so several clips can branch from exactly those bytes. + const k = if (try host.sessions.findMark(from) == null) try host.sessions.mark(from, .mark, host.live()) else blk: { + const key = try loadMark(host, from); + defer key.free(host.gpa); + try h.restore(key); + break :blk try host.sessions.keyframe(.jump, host.live()); + }; + // The input held now is restored first in the clip (Host.restoredInput). + h.restored = true; + try host.sessions.startClip(n, k); + h.startRecording(); + self.recording = .of(n); + self.print("Started recording {s} from snapshot {s}.", .{ n, from }); + }, + .play => if (arg != null and eq(arg.?, "session")) { + var more = std.mem.tokenizeAny(u8, rest, " \t"); + const index = std.fmt.parseInt(u64, more.next() orelse return error.ExpectedSession, 10) catch return error.InvalidSession; + var at: ?u64 = null; + if (more.next()) |word| { + if (!eq(word, "at")) return error.UnexpectedArgument; + const seconds = std.fmt.parseFloat(f64, more.next() orelse return error.ExpectedPosition) catch return error.InvalidPosition; + at = @intFromFloat(@max(0, seconds) * std.time.ns_per_s); + } + try stop(host); + return self.playSession(host, index, at, now); + } else { + const n = try nameOr(arg, &self.recording); + try stop(host); + try loadClip(host, n); + try h.play(now); + self.recording = .of(n); + self.print("Playing recording {s}.", .{n}); + self.warnBuild(host); + }, + .stop => { + try stop(host); + }, + .pause => { + if (h.mode != .playing and h.mode != .paused) return error.NotReplaying; + h.pause(now); + }, + .step, .seek => { + const value = arg orelse return error.ExpectedPosition; + if (h.mode == .live or h.mode == .recording) { + try stop(host); + try loadClip(host, self.recording.get()); + try h.play(now); + self.warnBuild(host); + } + if (verb == .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(h.events.items.len); + const ms = std.fmt.parseInt(u64, value, 10) catch return error.InvalidPosition; + return h.seekTime(ms *| std.time.ns_per_ms); + }, + .diff => { + const n = try nameOr(arg, &self.snapshot); + var arena: std.heap.ArenaAllocator = .init(host.gpa); + defer arena.deinit(); + const saved = try markState(host, arena.allocator(), n); + const diff = memory.compareKept(saved.layout, host.state.layout, host.live(), saved.bytes); + 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 {s}: {d} differing values, first {s}", .{ n, diff.count, w.buffered() }); + } else self.print("Live vs snapshot {s}: same", .{n}); + }, + .sessions => { + if (arg != null) return error.UnexpectedArgument; + try self.listSessions(host); + }, + .@"export-session" => { + const s = &host.sessions; + const first = arg orelse return error.ExpectedFile; + const index = if (rest.len == 0) s.session else std.fmt.parseInt(u64, first, 10) catch return error.InvalidSession; + const path = if (rest.len == 0) first else std.mem.trim(u8, rest, " \t"); + try s.exportSession(index, path); + self.print("Exported session {d} to {s}.", .{ index, path }); + }, + .import => { + const path = arg orelse return error.ExpectedFile; + if (rest.len != 0) return error.UnexpectedArgument; + const s = &host.sessions; + const first = try s.import(path); + const n = try s.count(.sessions); + self.print("Imported {s} as session {d}..{d}; marks and clips are NAME@{s}.", .{ path, first, n - 1, std.fs.path.basename(path) }); + }, + .marks, .clips => { + if (arg != null) return error.UnexpectedArgument; + try self.listNamed(host, verb == .marks); + }, } return &.{}; } @@ -235,8 +410,12 @@ pub fn draw(self: *const Console, host: *Host, out: *std.ArrayList(abi.Command), const width: f32 = @floatFromInt(host.canvas[0]); const h = &host.timeline; try out.append(arena, .{ .rect = .{ .dst = .{ .x = 0, .y = top, .w = width, .h = panel_height }, .color = .{ .r = 24, .g = 28, .b = 28 } } }); - const status = try std.fmt.allocPrint(arena, "{s} | {t} | snapshot {d} | recording {d} (active {d}) | event {d} | {d}ms", .{ - host.name, h.mode, self.snapshot, self.recording, h.selected, h.cursor, h.position / std.time.ns_per_ms, + const s = &host.sessions; + const playing = h.mode == .playing or h.mode == .paused; + const frame = if (playing) (if (h.key) |k| k.frame else 0) + h.fed else s.frames; + const at = (if (playing) h.position else s.last_time -| s.first_time) / std.time.ns_per_s; + const status = try std.fmt.allocPrint(arena, "{s} | {t} | session {d} frame {d} ({d}:{d:0>2}) | mark {s} | clip {s} | rec {t}", .{ + host.name, h.mode, if (playing) h.session orelse s.session else s.session, frame, at / 60, at % 60, self.snapshot.get(), self.recording.get(), s.mode(), }); try out.append(arena, .{ .text = .{ .x = 12, .y = top + 8, .scale = 1, .color = hint, .bytes = status } }); for (&self.lines, 0..) |*line, i| { @@ -246,19 +425,134 @@ 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."); +/// Where this run records, then the last sessions: index, start, frames, +/// stretches (loads), and how it ended. +fn listSessions(self: *Console, host: *Host) !void { + const s = &host.sessions; + switch (s.mode()) { + .file => self.print("recording to {s}", .{s.path}), + .memory => self.log("recording in memory (scripted run)"), + .off => return self.log("not recording (an export)"), + } + const n = try s.count(.sessions); + var i = n -| 4; + while (i < n) : (i += 1) { + const m = try s.summary(i); + const secs = std.time.epoch.EpochSeconds{ .secs = m.started / std.time.ns_per_s }; + const day = secs.getEpochDay().calculateYearDay(); + const md = day.calculateMonthDay(); + const ds = secs.getDaySeconds(); + var end_buf: [32]u8 = undefined; + const end = if (i == s.session) "(this run)" else if (m.ended == 0) "(crashed)" else blk: { + const d = (m.ended -| m.started) / std.time.ns_per_s; + break :blk std.fmt.bufPrint(&end_buf, "{d}:{d:0>2}", .{ d / 60, d % 60 }) catch ""; + }; + self.print("{d}: {d:0>4}-{d:0>2}-{d:0>2} {d:0>2}:{d:0>2} builds {d} frames {d} {s} {s}", .{ i, day.year, md.month.numeric(), md.day_index + 1, ds.getHoursIntoDay(), ds.getMinutesIntoHour(), m.stretches, m.frames, end, m.from() }); + } } -fn eq(a: []const u8, b: []const u8) bool { - return std.mem.eql(u8, a, b); +/// Ends a clip being recorded or played: live from here. +fn stop(host: *Host) !void { + if (host.timeline.mode == .recording) try host.sessions.stopClip(); + host.timeline.stop(); +} + +/// The keyframe of the newest mark `n`, or of mark number K for "#K". +pub fn loadMark(host: *Host, n: []const u8) !Sessions.Loaded { + const s = &host.sessions; + const found = if (n.len > 1 and n[0] == '#') + try s.markAt(std.fmt.parseInt(u64, n[1..], 10) catch return error.InvalidName) + else + try s.findMark(n) orelse return error.EmptySnapshot; + return s.load(found.session, found.key); +} + +/// Mark `n`'s State in today's layout (`arena`'s), and the layout it was +/// taken in: compare them over the leaves migration kept (`memory.compareKept`). +pub fn markState(host: *Host, arena: std.mem.Allocator, n: []const u8) !struct { bytes: []u8, layout: memory.Layout } { + const key = try loadMark(host, n); + defer key.free(host.gpa); + const bytes = try arena.alignedAlloc(u8, .fromByteUnits(memory.page), host.state.layout.size); + const layout = try memory.restore(arena, key.layout, key.state, host.state.layout, bytes, host.timeline.defaults); + return .{ .bytes = bytes, .layout = layout }; } -fn slot(value: []const u8, min: usize, max: usize) !usize { - const index = std.fmt.parseInt(usize, value, 10) catch return error.InvalidSlot; - if (index < min or index > max) return error.InvalidSlot; - return index; +/// Makes clip `n` the one the timeline plays: its keyframe and frames. +fn loadClip(host: *Host, n: []const u8) !void { + const s = &host.sessions; + const clip = try s.findClip(n) orelse return error.EmptyRecording; + const key = try s.load(clip.session, clip.key); + errdefer key.free(host.gpa); + var events: std.ArrayList(abi.Event) = .empty; + errdefer events.deinit(host.gpa); + // A clip a crash left open ends where its session does. + const end = if (clip.end != 0) clip.end else (try s.summary(clip.session)).frames; + try s.events(clip.session, key.batch, clip.start, end, &events); + host.timeline.load(key, events); +} + +/// Plays session `index` from its start, or paused `at` nanoseconds into +/// it: from the newest keyframe before then, replayed up to it. +fn playSession(self: *Console, host: *Host, index: u64, at: ?u64, now: u64) ![]const abi.Event { + const s = &host.sessions; + const h = &host.timeline; + if (index == s.session) try s.commit(.none); + const sum = try s.summary(index); + if (sum.keys == 0) return error.EmptyRecording; + const t0 = (try s.keyAt(index, 0)).time; + const k = if (at) |t| try s.keyBefore(index, .time, t0 + t) orelse 0 else 0; + const key = try s.load(index, k); + errdefer key.free(host.gpa); + var events: std.ArrayList(abi.Event) = .empty; + errdefer events.deinit(host.gpa); + try s.events(index, key.batch, key.frame, sum.frames, &events); + var checks: @TypeOf(h.checks) = .empty; + errdefer checks.deinit(host.gpa); + for (k + 1..sum.keys) |j| { + const later = try s.keyAt(index, j); + try checks.append(host.gpa, .{ .frame = later.frame, .key = j, .observed = switch (later.why) { + .periodic, .mark, .before_reload, .exit => true, + else => false, + } }); + } + h.load(key, events); + h.session = index; + h.checks = checks; + try h.play(now); + self.print("Playing session {d} from frame {d}.", .{ index, key.frame }); + if (key.build != host.build or !eq(key.source, host.source)) self.print("session {d} frame {d} was built from {s}; replaying with {s}", .{ index, key.frame, key.source, host.source }); + if (at) |t| return h.seekTime((t0 + t) -| h.base); + return &.{}; +} + +/// A replay of a clip made by other code may diverge: say so. +fn warnBuild(self: *Console, host: *const Host) void { + const key = host.timeline.key orelse return; + if (key.build != 0 and host.build != 0 and key.build != host.build) self.log("Warning: recorded by another build of the sketch; the replay may differ."); +} + +/// The last marks or clips: index, name, session and frame. +fn listNamed(self: *Console, host: *Host, marks: bool) !void { + const s = &host.sessions; + const n = try s.count(if (marks) .marks else .clips); + if (n == 0) return self.log(if (marks) "No marks: save NAME makes one." else "No clips: record NAME makes one."); + var i = n -| 5; + while (i < n) : (i += 1) { + const e = if (marks) try s.markAt(i) else try s.clipAt(i); + const label = e.name(); + if (marks) { + // An older mark of a name may have gone with history. + const key = s.keyAt(e.session, e.key) catch { + self.print("#{d} {s}: session {d}, dropped", .{ i, label, e.session }); + continue; + }; + self.print("#{d} {s}: session {d} frame {d}", .{ i, label, e.session, key.frame }); + } else self.print("#{d} {s}: session {d} frames {d}..{d}", .{ i, label, e.session, e.start, e.end }); + } +} + +fn eq(a: []const u8, b: []const u8) bool { + return std.mem.eql(u8, a, b); } test "line editing, completion and submission" { @@ -276,3 +570,22 @@ test "line editing, completion and submission" { try std.testing.expectEqualStrings("", c.text()); try std.testing.expect(c.edit(.submit, &.{}) == null); } + +test "timing console controls and validation" { + var host: Host = undefined; + host.timing = .{}; + var console: Console = .{}; + _ = try console.execute(&host, "timing work", 0); + try std.testing.expect(host.timing.visible and host.timing.metric == .work); + _ = try console.execute(&host, "overlay timing", 0); + try std.testing.expect(!host.timing.visible); + host.timing.add(.{ .frame = 17000000, .work = 500000 }); + _ = try console.execute(&host, "timing stats", 0); + const last = console.lines[console.lines.len - 1]; + try std.testing.expect(std.mem.indexOf(u8, last.bytes[0..last.len], "p99 0.500 ms") != null); + try std.testing.expectError(error.UnexpectedArgument, console.execute(&host, "timing on extra", 0)); + try std.testing.expect(!host.timing.visible); + try std.testing.expectError(error.ExpectedTimingOnOffFrameWorkResetStats, console.execute(&host, "timing bogus", 0)); + _ = try console.execute(&host, "timing reset", 0); + try std.testing.expectEqual(@as(usize, 0), host.timing.count); +} diff --git a/src/host/Glyphs.zig b/src/host/Glyphs.zig new file mode 100644 index 0000000..b3fdbfb --- /dev/null +++ b/src/host/Glyphs.zig @@ -0,0 +1,279 @@ +//! The host's text. A `Command.text` is shaped by vendor/text.c at its +//! canvas size (what `textRun` measured), and each glyph is rasterized by +//! pardes's font.c at the size it lands on the device, kept in one +//! 2048x2048 coverage atlas: shelves of trimmed ink boxes, cleared and +//! refilled when full (pardes's policy: no eviction). The texture holds +//! white premultiplied by coverage, so a vertex colour is the ink and the +//! coverage is mixed once, with no gamma. The glyphs of consecutive text +//! commands are one geometry call (`flush`, which the Painter calls before +//! anything else it draws). Upright text lands on whole device pixels and +//! samples NEAREST; `textScaled` (and a glyph past 512 device pixels, +//! rastered at 512) is placed unsnapped and samples LINEAR. +const Glyphs = @This(); +const std = @import("std"); +const abi = @import("abi"); +const c = @import("c"); +const Atlas = @import("font_atlas"); + +const side = Atlas.atlas_w; +/// The largest raster, in device pixels: a bigger glyph is this one, stretched. +const max_px = 512; + +/// A glyph of a face at a device size, in 64ths of a pixel. +const Key = packed struct(u64) { face: u8, glyph: u32, px: u24 }; +/// Its ink box in the atlas, and where that box sits from the pen's origin +/// on the baseline (device pixels, y down). Empty: nothing to draw. +const Slot = struct { u: u16 = 0, v: u16 = 0, w: u16 = 0, h: u16 = 0, dx: i16 = 0, dy: i16 = 0 }; + +gpa: std.mem.Allocator, +/// Coverage, a byte a texel: `texture`'s contents to come. +stage: []u8 = &.{}, +texture: ?*c.SDL_Texture = null, +slots: std.AutoHashMapUnmanaged(Key, Slot) = .empty, +/// The shelf being filled: its top, its height so far, and the pen in it. +shelf: struct { x: u32 = 0, y: u32 = 0, h: u32 = 0 } = .{}, +/// What the stage changed since the last upload: x0, y0, x1, y1. +dirty: [4]u32 = .{ 0, 0, side, side }, +/// A glyph's raster before it is trimmed, and the shaped glyphs of a run. +raster: std.ArrayList(u8) = .empty, +shaped: std.ArrayList(c.SkGlyph) = .empty, +/// The pending geometry: six vertices a glyph, in logical units. +batch: std.ArrayList(c.SDL_Vertex) = .empty, +linear: bool = false, + +pub fn deinit(g: *Glyphs) void { + if (g.texture) |t| c.SDL_DestroyTexture(t); + g.gpa.free(g.stage); + g.slots.deinit(g.gpa); + g.raster.deinit(g.gpa); + g.shaped.deinit(g.gpa); + g.batch.deinit(g.gpa); + g.* = .{ .gpa = g.gpa }; +} + +/// The canvas size `scale` is, in pixels: scale 1 is 13. +pub fn pixels(scale: f32) f32 { + return 13 * scale; +} + +/// One `Command.text` into the batch. +pub fn draw(g: *Glyphs, renderer: *c.SDL_Renderer, t: @FieldType(abi.Command, "text")) void { + const raster = if (t.raster > 0) t.raster else t.scale; + if (t.bytes.len == 0 or t.color.a == 0 or !(t.scale > 0) or !std.math.isFinite(t.scale) or !std.math.isFinite(raster)) return; + if (g.texture == null and !g.start(renderer)) return; + // Shaped at the size it was measured at; `k` stretches that layout to + // the size drawn (textScaled), `dpi` to the device. + var advance: i32 = 0; + var ascent: i32 = 0; + var height: i32 = 0; + const face: c_int = @backingInt(t.face); + var n: usize = 0; + while (true) { + n = @intCast(c.sk_text_shape(face, pixels(raster), t.bytes.ptr, t.bytes.len, g.shaped.items.ptr, @intCast(g.shaped.items.len), &advance, &ascent, &height)); + if (n <= g.shaped.items.len) break; + g.shaped.resize(g.gpa, n) catch return; + } + const k = t.scale / raster; + const dpi = density(renderer); + const wanted = pixels(t.scale) * dpi; + const px = @min(wanted, max_px); + const stretch = wanted / px; + const snapped = k == 1 and stretch == 1; + if (g.linear != !snapped) { + g.flush(renderer); + g.linear = !snapped; + } + const ink: c.SDL_FColor = .{ + .r = @as(f32, @floatFromInt(t.color.r)) * @as(f32, @floatFromInt(t.color.a)) / (255 * 255), + .g = @as(f32, @floatFromInt(t.color.g)) * @as(f32, @floatFromInt(t.color.a)) / (255 * 255), + .b = @as(f32, @floatFromInt(t.color.b)) * @as(f32, @floatFromInt(t.color.a)) / (255 * 255), + .a = @as(f32, @floatFromInt(t.color.a)) / 255, + }; + const unit = k * dpi; // device pixels a layout pixel + const origin: [2]f32 = .{ t.x * dpi, (t.y + @as(f32, @floatFromInt(ascent)) * k) * dpi }; + for (g.shaped.items[0..n]) |glyph| { + const key: Key = .{ .face = glyph.face, .glyph = glyph.glyph, .px = @intFromFloat(@round(px * 64)) }; + const slot = g.slotFor(renderer, key) orelse continue; + if (slot.w == 0) continue; + var at: [2]f32 = .{ origin[0] + @as(f32, @floatFromInt(glyph.x)) * unit, origin[1] + @as(f32, @floatFromInt(glyph.y)) * unit }; + if (snapped) at = .{ @round(origin[0]) + @round(@as(f32, @floatFromInt(glyph.x)) * unit), @round(origin[1]) + @round(@as(f32, @floatFromInt(glyph.y)) * unit) }; + const x0 = (at[0] + @as(f32, @floatFromInt(slot.dx)) * stretch) / dpi; + const y0 = (at[1] + @as(f32, @floatFromInt(slot.dy)) * stretch) / dpi; + const x1 = x0 + @as(f32, @floatFromInt(slot.w)) * stretch / dpi; + const y1 = y0 + @as(f32, @floatFromInt(slot.h)) * stretch / dpi; + const s: f32 = 1.0 / @as(f32, side); + const s0 = @as(f32, @floatFromInt(slot.u)) * s; + const t0 = @as(f32, @floatFromInt(slot.v)) * s; + const s1 = @as(f32, @floatFromInt(slot.u + slot.w)) * s; + const t1 = @as(f32, @floatFromInt(slot.v + slot.h)) * s; + const quad = g.batch.addManyAsArray(g.gpa, 6) catch return; + const tl: c.SDL_Vertex = .{ .position = .{ .x = x0, .y = y0 }, .color = ink, .tex_coord = .{ .x = s0, .y = t0 } }; + const tr: c.SDL_Vertex = .{ .position = .{ .x = x1, .y = y0 }, .color = ink, .tex_coord = .{ .x = s1, .y = t0 } }; + const bl: c.SDL_Vertex = .{ .position = .{ .x = x0, .y = y1 }, .color = ink, .tex_coord = .{ .x = s0, .y = t1 } }; + const br: c.SDL_Vertex = .{ .position = .{ .x = x1, .y = y1 }, .color = ink, .tex_coord = .{ .x = s1, .y = t1 } }; + quad.* = .{ tl, tr, bl, tr, br, bl }; + } +} + +/// Draws the batch: the stage's new rows uploaded, then one geometry call. +pub fn flush(g: *Glyphs, renderer: *c.SDL_Renderer) void { + if (g.batch.items.len == 0) return; + defer g.batch.clearRetainingCapacity(); + const texture = g.texture orelse return; + g.upload(); + _ = c.SDL_SetTextureScaleMode(texture, if (g.linear) c.SDL_SCALEMODE_LINEAR else c.SDL_SCALEMODE_NEAREST); + _ = c.SDL_RenderGeometry(renderer, texture, g.batch.items.ptr, @intCast(g.batch.items.len), null, 0); +} + +fn start(g: *Glyphs, renderer: *c.SDL_Renderer) bool { + g.stage = g.gpa.alloc(u8, side * side) catch return false; + @memset(g.stage, 0); + const texture = c.SDL_CreateTexture(renderer, c.SDL_PIXELFORMAT_RGBA32, c.SDL_TEXTUREACCESS_STATIC, side, side) orelse { + g.gpa.free(g.stage); + g.stage = &.{}; + return false; + }; + _ = c.SDL_SetTextureBlendMode(texture, c.SDL_BLENDMODE_BLEND_PREMULTIPLIED); + g.texture = texture; + g.dirty = .{ 0, 0, side, side }; + return true; +} + +/// The stage's changed rectangle into the texture, coverage as white +/// premultiplied (r = g = b = a), 64 rows at a time. +fn upload(g: *Glyphs) void { + const d = g.dirty; + if (d[2] <= d[0] or d[3] <= d[1]) return; + const w = d[2] - d[0]; + const band = 64; + g.raster.resize(g.gpa, w * band * 4) catch return; + var top = d[1]; + while (top < d[3]) : (top += band) { + const h = @min(band, d[3] - top); + for (0..h) |y| for (0..w) |x| @memset(g.raster.items[(y * w + x) * 4 ..][0..4], g.stage[(top + y) * side + d[0] + x]); + const rect: c.SDL_Rect = .{ .x = @intCast(d[0]), .y = @intCast(top), .w = @intCast(w), .h = @intCast(h) }; + _ = c.SDL_UpdateTexture(g.texture, &rect, g.raster.items.ptr, @intCast(w * 4)); + } + g.dirty = .{ side, side, 0, 0 }; +} + +/// `key`'s slot, rasterized on first use; when the atlas is full, what is +/// pending is drawn and the atlas starts over. Null if it cannot hold it. +fn slotFor(g: *Glyphs, renderer: *c.SDL_Renderer, key: Key) ?Slot { + if (g.slots.get(key)) |s| return s; + return g.rasterize(key) orelse { + g.flush(renderer); + g.slots.clearRetainingCapacity(); + @memset(g.stage, 0); + g.shelf = .{}; + g.dirty = .{ 0, 0, side, side }; + return g.rasterize(key); + }; +} + +/// font.c's raster of `key`, its advance centred on the pen as pardes +/// centres a cell's, a margin of half its size around it; then trimmed to +/// its ink and shelved. Null when the atlas has no room. +fn rasterize(g: *Glyphs, key: Key) ?Slot { + const px = @as(f32, @floatFromInt(key.px)) / 64; + const font = c.sk_text_ui(key.face) orelse return null; + var cell_w: c_int = 0; + var height: c_int = 0; + var ascent: c_int = 0; + c.ui_font_cell_metrics(font, px, &cell_w, &height, &ascent); + const advance = @max(0, c.sk_text_advance(key.face, px, key.glyph)); + const pad: c_int = @intFromFloat(@ceil(px / 2) + 2); + const w: usize = @intCast(advance + 2 * pad); + const h: usize = @intCast(height + 2 * pad); + g.raster.resize(g.gpa, w * h) catch return null; + const blank = c.ui_font_raster(font, px, key.glyph, 0, g.raster.items.ptr, @intCast(w), @intCast(w), @intCast(h), pad, advance, ascent + pad) == 0; + const box = if (blank) null else Atlas.inkBounds(g.raster.items, w, w, h); + const b = box orelse { + g.slots.put(g.gpa, key, .{}) catch {}; + return .{}; + }; + const bw: u32 = @intCast(b.max_x - b.min_x + 1); + const bh: u32 = @intCast(b.max_y - b.min_y + 1); + // A new shelf when this one is full; a gap of a texel around each box, + // so LINEAR sampling at its edge reads nothing of a neighbour. + if (g.shelf.x + bw + 1 > side) g.shelf = .{ .y = g.shelf.y + g.shelf.h }; + if (g.shelf.y + bh + 1 > side or bw + 1 > side) return null; + const slot: Slot = .{ .u = @intCast(g.shelf.x), .v = @intCast(g.shelf.y), .w = @intCast(bw), .h = @intCast(bh), .dx = @intCast(@as(i64, @intCast(b.min_x)) - pad), .dy = @intCast(@as(i64, @intCast(b.min_y)) - pad - ascent) }; + g.slots.put(g.gpa, key, slot) catch return null; + for (0..bh) |y| @memcpy(g.stage[(slot.v + y) * side + slot.u ..][0..bw], g.raster.items[(b.min_y + y) * w + b.min_x ..][0..bw]); + g.dirty = .{ @min(g.dirty[0], slot.u), @min(g.dirty[1], slot.v), @max(g.dirty[2], slot.u + bw), @max(g.dirty[3], slot.v + bh) }; + g.shelf.x += bw + 1; + g.shelf.h = @max(g.shelf.h, bh + 1); + return slot; +} + +/// Device pixels a canvas unit: the render scale (a supersampled target's +/// 4) times the window's logical presentation, if it is drawing there. +fn density(renderer: *c.SDL_Renderer) f32 { + var sx: f32 = 1; + var sy: f32 = 1; + _ = c.SDL_GetRenderScale(renderer, &sx, &sy); + var w: c_int = 0; + var h: c_int = 0; + var mode: c.SDL_RendererLogicalPresentation = undefined; + var rect: c.SDL_FRect = undefined; + if (c.SDL_GetRenderLogicalPresentation(renderer, &w, &h, &mode) and mode != c.SDL_LOGICAL_PRESENTATION_DISABLED and w > 0 and h > 0 and c.SDL_GetRenderLogicalPresentationRect(renderer, &rect)) { + sx *= rect.w / @as(f32, @floatFromInt(w)); + sy *= rect.h / @as(f32, @floatFromInt(h)); + } + return @max(0.25, @max(sx, sy)); +} + +test "the atlas's glyphs are pardes's: the same coverage bytes and the same place" { + const gpa = std.testing.allocator; + var g: Glyphs = .{ .gpa = gpa }; + g.stage = try gpa.alloc(u8, side * side); + @memset(g.stage, 0); + defer g.deinit(); + var compared: usize = 0; + var clipped: usize = 0; + // Grid faces (every advance the cell's) and a proportional one, which + // pardes centres in its cell. + for ([_]abi.Face{ .mono, .mono_bold, .mono_italic, .code, .code_medium, .sans }) |face| for ([_]f32{ 13, 19.5, 26, 52 }) |px| { + // pardes's Atlas over the same face: its own stage, its cells. + var pardes: Atlas = .{ .font = c.sk_text_ui(@backingInt(face)).?, .atlas_stage = try gpa.alloc(u8, side * side), .glyphs = .init(gpa) }; + defer { + gpa.free(pardes.atlas_stage); + pardes.glyphs.deinit(); + } + var cell_w: c_int = 0; + var height: c_int = 0; + var ascent: c_int = 0; + pardes.cellMetrics(px, &cell_w, &height, &ascent); + const body: Atlas.Size = .{ .px = px, .cell_w = @intCast(cell_w), .height = @intCast(height), .baseline = ascent }; + pardes.reset(body.cell_w); + for (0x21..0x7f) |cp| { + const glyph = c.ui_font_glyph(pardes.font, @intCast(cp)); + const ours = g.rasterize(.{ .face = @backingInt(face), .glyph = glyph, .px = @intFromFloat(px * 64) }).?; + // pardes centres the advance in its cell: nothing for a grid + // face, whose advances are all the cell's. + const shift = @divTrunc(cell_w - c.sk_text_advance(@backingInt(face), px, glyph), 2); + const x = shift + ours.dx; + const y = ascent + ours.dy; + // Ink pardes's cell cuts off is not comparable: count it. + if (x < 0 or y < 0 or x + ours.w > cell_w or y + ours.h > height) { + clipped += 1; + continue; + } + const at = pardes.cachedGlyph(.{ .face = 0, .glyph = glyph, .role = .body }, body, body, 1, 1).?; + const cell = pardes.atlas_stage[at.v * side + at.u ..]; + const theirs = Atlas.inkBounds(cell, side, body.cell_w, body.height).?; + try std.testing.expectEqual(theirs.max_x - theirs.min_x + 1, ours.w); + try std.testing.expectEqual(theirs.max_y - theirs.min_y + 1, ours.h); + try std.testing.expectEqual(@as(i64, @intCast(theirs.min_x)), x); + try std.testing.expectEqual(@as(i64, @intCast(theirs.min_y)), y); + for (0..ours.h) |row| try std.testing.expectEqualSlices(u8, cell[(theirs.min_y + row) * side + theirs.min_x ..][0..ours.w], g.stage[(ours.v + row) * side + ours.u ..][0..ours.w]); + compared += 1; + } + }; + // 6 faces at 4 sizes, 94 printable ASCII glyphs each: 2256, a few of + // which reach out of pardes's cell (`_` under it, `|` over it). + std.debug.print("pardes's raster: {d} glyphs equal byte for byte, {d} clipped by its cell\n", .{ compared, clipped }); + try std.testing.expectEqual(@as(usize, 2256), compared + clipped); + try std.testing.expect(compared > 1900); +} diff --git a/src/host/Host.zig b/src/host/Host.zig index 43f030a..9a2c0fa 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -3,31 +3,46 @@ //! exercise exactly what a player does. const Host = @This(); const std = @import("std"); +const tracy = @import("tracy"); const c = @import("c"); const abi = @import("abi"); const memory = @import("memory.zig"); 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"); +const Gpu = @import("gpu"); const overlay = @import("overlay.zig"); const Console = @import("Console.zig"); +const Timing = @import("Timing.zig"); +const Presentation = @import("Presentation.zig"); +const timing_marker = @import("timing_marker.zig"); +const Control = @import("control.zig").Control; const Library = @import("library.zig").Library; +const Sessions = @import("Sessions.zig"); const Watch = @import("library.zig").Watch; pub const Options = struct { /// A hot-reloaded sketch library. Unused when a sketch is linked in. library: ?[]const u8 = null, - /// State file; recordings go next to it with an `.inputs` suffix. - state: []const u8 = "sketch.state", + /// Local 9P control socket, opt-in. + ninep: ?[]const u8 = null, + /// The save, a sessions database: `<stem>.xitdb`, for a path + /// `<stem>.xitdb`, `<stem>.state` (where a state file from before is + /// imported from) or `<stem>`. Empty: `$XDG_DATA_HOME/<name>/<name>.xitdb` + /// (`~/.local/share` without it), for a shipped game. + state: []const u8 = "", + /// Playing a script: from fresh state, sessions go to memory only. + script: bool = false, /// Offscreen video, silent audio, no vsync: for scripts and CI. headless: bool = false, - /// Forget the state file's contents: scripts start from a known place. + /// Start from State's defaults, not the save's present: scripts start + /// from a known place. fresh: bool = false, /// Time every GPU command alone and print the times at exit. profile_gpu: bool = false, + /// Binary frame/time marker for external video measurements. + timing_marker: bool = false, /// Export every frame: PNGs `<dir>/000000.png`, ..., or for "-" raw RGBA /// rows on stdout. Frames run on a clock of their own (see `fps`). export_to: ?[]const u8 = null, @@ -48,6 +63,7 @@ renderer: *c.SDL_Renderer, /// Runs sketches' GPU passes, and the 2D renderer draws through it too. /// Null when the machine has no usable GPU driver. gpu: ?*c.SDL_GPUDevice, +presentation: ?*Presentation = null, passes: ?Gpu = null, /// GPU canvas, shared by native/headless drawing, backdrop filters and screenshots. offscreen: ?*c.SDL_Texture = null, @@ -56,25 +72,37 @@ images: gfx.Images, /// Plays the 2D commands: targets, blend, antialiased shapes. painter: gfx.Painter, audio: sound.Audio, -state: memory.StateFile, +state: memory.State, session: memory.Session, -store: memory.StoreFile, +/// A failed resource setup disables app callbacks while keeping host controls alive. +session_ready: bool = false, /// Images and buffers the sketch made (`newImage`, `decodeImage`, /// `newBuffer`): Session holds their handles, so they go when it resets. made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, -recordings: []align(memory.page) u8, timeline: Timeline, +/// This run's session and the ones before it (Sessions.zig). +sessions: Sessions, console: Console = .{}, +timing: Timing = .{}, +timing_atlas: overlay.TimingAtlas = .{}, 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. +/// Identifies the running build of the sketch (its library or executable +/// file's inode, size and modification time): sessions' stretches carry it, +/// and `play` warns on a mismatch. build: u64 = 0, +/// The save's path when the host chose it (`Options.state`), `gpa`'s. +state_path: []const u8 = "", +/// `Options.state` without `.xitdb` or `.state`: the save is +/// `<stem>.xitdb`, the sketch's own files `<stem>.<name>`. +stem: []const u8 = "", /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, name: []const u8 = "", +/// The sketch's `Info.source`, for the session's stretches. +source: []const u8 = "", completions: []const []const u8 = &.{}, /// Frame lifetime: sketch scratch and this frame's commands. frame_arena: std.heap.ArenaAllocator, @@ -83,6 +111,11 @@ commands: std.ArrayList(abi.Command) = .empty, pending: std.ArrayList(abi.Event) = .empty, input: abi.Input = .{}, files: std.StringHashMapUnmanaged([]align(memory.page) const u8) = .empty, +/// Mappings of files `writeFile` replaced: the sketch may still hold them. +retired: std.ArrayList([]align(memory.page) const u8) = .empty, +/// The paths `appendFile` wrote, each once, 0-terminated: the sketch keeps +/// them. +written: std.StringHashMapUnmanaged(void) = .empty, canvas: [2]u32 = .{ 0, 0 }, cursor: abi.Cursor = .arrow, quit: bool = false, @@ -92,13 +125,16 @@ reloaded: bool = true, jumped: bool = true, started: bool = true, last_now: u64 = 0, +last_frame_end: u64 = 0, /// Saved after the next frame is drawn (scripts). screenshot: ?[:0]const u8 = null, /// Frames exported so far (`Options.export_to`). exported: u64 = 0, dialog: Dialog = .{}, +control: ?*Control = null, -pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, options: Options, linked: ?abi.Sketch) !void { +pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std.process.Environ.Map, given: Options, linked: ?abi.Sketch) !void { + var options = given; if (options.headless) { // Overriding: a plain hint loses to the environment, and a user's // SDL_VIDEO_DRIVER=wayland would open real windows during tests. @@ -112,7 +148,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std var library: ?Library = null; const sketch = linked orelse blk: { const path = options.library orelse return error.NoSketch; - library = try Library.openCopy(io, path, scratchDir(environ), 0); + library = try Library.openFresh(io, path, scratchDir(environ), 0); break :blk library.?.sketch; }; errdefer if (library) |*l| l.close(); @@ -142,17 +178,16 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std // Colors carry alpha: fills blend, as they do on the web. _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); - var state = try memory.StateFile.open(gpa, options.state); - errdefer state.close(); - var inputs_buf: [std.fs.max_path_bytes]u8 = undefined; - const recordings = try mapRecordings(try std.mem.print(&inputs_buf, "{s}.inputs", .{options.state})); - errdefer std.posix.munmap(recordings); - var store_buf: [std.fs.max_path_bytes]u8 = undefined; - var store = memory.StoreFile.open(gpa, try std.mem.print(&store_buf, "{s}.store", .{options.state}), options.fresh) catch |err| { - if (err == error.StoreInUse) std.log.err("{s}.store is open in another process", .{options.state}); - return err; + var data_buf: [std.fs.max_path_bytes]u8 = undefined; + const state_path = if (given.state.len > 0) "" else blk: { + const data = environ.get("XDG_DATA_HOME") orelse try std.fmt.bufPrint(&data_buf, "{s}/.local/share", .{environ.get("HOME") orelse "."}); + const path = try std.fmt.allocPrint(gpa, "{s}/{s}/{s}.xitdb", .{ data, info.name, info.name }); + errdefer gpa.free(path); + try std.Io.Dir.cwd().createDirPath(io, std.fs.path.dirname(path).?); + break :blk path; }; - errdefer store.close(); + errdefer gpa.free(state_path); + if (state_path.len > 0) options.state = state_path; self.* = .{ .gpa = gpa, @@ -163,50 +198,57 @@ 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 }, + .painter = .{ .renderer = renderer.?, .images = undefined, .smooth = gpu != null, .glyphs = .{ .gpa = gpa } }, .audio = .{ .gpa = gpa }, - .state = state, + .state = .init(gpa), .session = .init(gpa), - .store = store, - .recordings = recordings, .timeline = undefined, + .sessions = .{ .gpa = gpa, .io = io }, .sketch = sketch, .library = library, + .state_path = state_path, + .stem = stemOf(options.state), .info_arena = .init(gpa), .frame_arena = .init(gpa), .input = .{ .exporting = options.export_to != null }, }; - self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; + self.timeline = .{ .gpa = gpa, .state = &self.state, .defaults = sketch.defaults }; 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); }; - self.audio.open() catch |err| std.log.warn("audio unavailable: {t}", .{err}); + self.audio.open(info.audio_pull) catch |err| std.log.warn("audio unavailable: {t}", .{err}); if (options.library) |path| if (linked == null) { self.watch = Watch.init(path) catch null; }; const size = options.size orelse [2]u32{ info.width, info.height }; self.setCanvas(size[0], size[1]); _ = c.SDL_StartTextInput(self.window); - self.build = fileHash(options.library orelse "/proc/self/exe"); + self.build = fileId(io, options.library orelse "/proc/self/exe"); try self.adopt(sketch); - if (options.fresh) { - self.state.header().used = 0; - @memset(self.recordings, 0); - self.resetState(); + self.openSessions(info.record); + // Before --fresh, which never touches the save. + self.openPresent(); + if (options.fresh) self.resetState(); + if (!options.headless and options.export_to == null) { + self.presentation = Presentation.init(gpa, self.window) catch null; + if (options.timing_marker) self.print("presentation tracing: {s}", .{if (self.presentation) |p| p.status else "disabled_or_allocation_failed"}); } } pub fn deinit(self: *Host) void { + self.sessions.close(self.live(), wallClock(self.io)); + if (self.control) |control| control.deinit(); self.dialog.deinit(); 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.passes) |*g| g.deinit(); if (self.pointer_cursor) |cursor| c.SDL_DestroyCursor(cursor); if (self.offscreen) |texture| c.SDL_DestroyTexture(texture); + if (self.presentation) |p| p.deinit(self.gpa); c.SDL_DestroyRenderer(self.renderer); if (self.gpu) |device| c.SDL_DestroyGPUDevice(device); c.SDL_DestroyWindow(self.window); @@ -216,15 +258,25 @@ pub fn deinit(self: *Host) void { std.posix.munmap(entry.value_ptr.*); } self.files.deinit(self.gpa); - std.posix.munmap(self.recordings); + for (self.retired.items) |bytes| std.posix.munmap(bytes); + self.retired.deinit(self.gpa); + // A scripted run from fresh state, or an export, leaves no files. + const private = (self.options.script or self.options.export_to != null) and self.options.fresh; + var written = self.written.keyIterator(); + while (written.next()) |path| { + if (private) std.Io.Dir.cwd().deleteFile(self.io, path.*) catch {}; + self.gpa.free(path.ptr[0 .. path.len + 1]); + } + self.written.deinit(self.gpa); + self.timeline.deinit(); self.session.deinit(self.gpa); - self.store.close(); self.made.images.deinit(self.gpa); self.made.buffers.deinit(self.gpa); - self.state.close(); + self.state.deinit(self.gpa); self.pending.deinit(self.gpa); self.frame_arena.deinit(); self.info_arena.deinit(); + self.gpa.free(self.state_path); if (self.library) |*l| l.close(); c.SDL_Quit(); } @@ -232,20 +284,77 @@ pub fn deinit(self: *Host) void { /// The interactive loop. An export runs it on a clock of its own, frame k /// at k/fps seconds (from 1 s, as scripts do), whatever the wall clock says. pub fn run(self: *Host) !void { + // A run a person watches posts its tree as sketchbook/<name>; --9p + // names another, or a socket path. + const o = self.options; + const ninep = o.ninep orelse if (o.headless or o.script or o.export_to != null) null else self.name; + if (ninep) |where| self.control = Control.open(self.gpa, self.environ, where) catch |err| blk: { + if (o.ninep != null) return err; + self.print("not posted as sketchbook/{s}: {t}", .{ where, err }); + break :blk null; + }; while (!self.quit) { + if (self.control) |control| control.poll(self); try self.poll(); - try self.frame(if (self.options.export_to == null) c.SDL_GetTicksNS() else std.time.ns_per_s + self.exported * std.time.ns_per_s / self.options.fps); + if (self.presentation) |p| p.poll(); + if (!self.options.headless and self.options.export_to == null and + c.SDL_GetWindowFlags(self.window) & (c.SDL_WINDOW_OCCLUDED | c.SDL_WINDOW_MINIMIZED | c.SDL_WINDOW_HIDDEN) != 0 and + self.screenshot == null and !(self.control != null and self.control.?.capture_pending)) + { + // Occluded Wayland presentation can block for a second; keep controls alive and resume without a camera jump. + self.last_now = 0; + self.last_frame_end = 0; + _ = c.SDL_Delay(10); + continue; + } + // GPU fences do not limit presentation rate; follow the measured display clock. + const wait_ns = if (self.presentation) |p| p.waitNs() else 0; + if (wait_ns != 0) { + c.SDL_DelayPrecise(@min(wait_ns, std.time.ns_per_ms)); + continue; + } + const wall = c.SDL_GetTicksNS(); + const now = if (self.options.export_to != null) + std.time.ns_per_s + self.exported * std.time.ns_per_s / self.options.fps + else + frameTime(wall, if (self.presentation) |p| p.sampleNs(wall) else wall, self.last_now, self.timeline.mode, self.pending.items); + try self.frame(now); } - self.timeline.stop(c.SDL_GetTicksNS()); + self.timeline.stop(); +} + +// Live animation uses the display clock, but a tick must not predate its input. +fn frameTime(wall: u64, scheduled: u64, previous: u64, mode: abi.Mode, events: []const abi.Event) u64 { + if (mode != .live and mode != .recording) return wall; + var now = @max(scheduled, previous); + for (events) |event| now = @max(now, event.time); + return now; +} + +test "timing clock drives ordinary ticks without moving input or replay time" { + const expectEqual = std.testing.expectEqual; + try expectEqual(@as(u64, 100), frameTime(103, 100, 80, .live, &.{})); + try expectEqual(@as(u64, 120), frameTime(127, 120, 100, .recording, &.{})); + const events = [_]abi.Event{ .{ .kind = .key_down, .time = 102 }, .{ .kind = .key_up, .time = 101 } }; + try expectEqual(@as(u64, 102), frameTime(103, 100, 80, .live, &events)); + try expectEqual(@as(u64, 102), events[0].time); + try expectEqual(@as(u64, 101), events[1].time); + try expectEqual(@as(u64, 103), frameTime(103, 103, 80, .live, &.{})); // No feedback. + try expectEqual(@as(u64, 110), frameTime(103, 100, 110, .live, &.{})); // Clock reversal. + for ([_]abi.Mode{ .playing, .paused }) |mode| try expectEqual(@as(u64, 103), frameTime(103, 100, 110, mode, &events)); } pub fn live(self: *Host) []u8 { - return self.state.slot(0); + return self.state.live(); } /// One frame: reload if a build replaced the library, feed input through /// `update`, then `draw` and present. pub fn frame(self: *Host, now: u64) !void { + const started = c.SDL_GetTicksNS(); + const zone = tracy.zone(@src(), "frame"); + tracy.frameMark(); + defer zone.end(); _ = self.frame_arena.reset(.retain_capacity); self.commands = .empty; if (self.watch) |w| if (w.changed()) self.reload() catch |err| self.print("reload failed: {t}", .{err}); @@ -254,8 +363,11 @@ pub fn frame(self: *Host, now: u64) !void { self.last_now = now; const h = &self.timeline; switch (h.mode) { - .playing => self.update(h.tick(now)), - .paused => {}, + .playing => { + self.sessions.away = true; + self.update(h.tick(now)); + }, + .paused => self.sessions.away = true, .live, .recording => { // One frame: what was routed since the last one, then its tick. // Recorded first, so `update` sees the times a replay will. @@ -263,17 +375,23 @@ pub fn frame(self: *Host, now: u64) !void { // An export's tick is 1/fps itself, not whole nanoseconds of it. if (self.options.export_to != null and dt != 0) tick.x = 1 / @as(f32, @floatFromInt(self.options.fps)); try self.pending.append(self.gpa, tick); - h.capture(self.pending.items) catch |err| self.print("Recording stopped: {t}", .{err}); + const why: ?Sessions.Why = if (self.sessions.frames == 0) .start else if (self.reloaded) .after_reload else if (self.jumped or self.sessions.away) .jump else null; + self.sessions.away = false; + self.sessions.frame(self.pending.items, self.live(), why) catch |err| self.print("session: {t}", .{err}); self.update(self.pending.items); }, } self.pending.clearRetainingCapacity(); - try self.draw(if (h.mode == .live or h.mode == .recording) now else h.position); + const work = try self.draw(if (h.mode == .live or h.mode == .recording) now else h.base + h.position, started); + const ended = c.SDL_GetTicksNS(); + self.timing.add(.{ .frame = ended - (if (self.last_frame_end == 0) started else self.last_frame_end), .work = work }); + self.last_frame_end = ended; } /// 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 { + if (!self.session_ready) return; var frames = playback.frames(events); while (frames.next()) |one| { // Input follows the events actually fed, frame by frame. @@ -287,10 +405,42 @@ pub fn update(self: *Host, events: []const abi.Event) void { self.started = false; const ctx = self.context(); self.sketch.update(&ctx, self.live().ptr); + if (self.timeline.mode == .playing or self.timeline.mode == .paused) self.resync(); } self.input.events = &.{}; } +/// After a replayed frame of a past session: at each keyframe it reaches, +/// live state is compared with the keyframe (over the leaves migration +/// kept), any difference said, and the keyframe restored. +fn resync(self: *Host) void { + const h = &self.timeline; + h.fed += 1; + const session = h.session orelse return; + const reached = h.key.?.frame + h.fed; + while (h.next_check < h.checks.items.len and h.checks.items[h.next_check].frame <= reached) { + const check = h.checks.items[h.next_check]; + h.next_check += 1; + if (check.frame < reached) continue; + const key = self.sessions.load(session, check.key) catch |err| return self.print("session {d} keyframe {d}: {t}", .{ session, check.key, err }); + defer key.free(self.gpa); + var arena: std.heap.ArenaAllocator = .init(self.gpa); + defer arena.deinit(); + const saved = arena.allocator().alignedAlloc(u8, .fromByteUnits(memory.page), self.state.layout.size) catch return; + const from = memory.restore(arena.allocator(), key.layout, key.state, self.state.layout, saved, self.sketch.defaults) catch |err| return self.print("session {d} keyframe {d}: {t}", .{ session, check.key, err }); + if (check.observed) { + const diff = memory.compareKept(from, self.state.layout, self.live(), saved); + 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("diverged before frame {d}: {d} values, first {s}", .{ reached, diff.count, w.buffered() }); + } + } + @memcpy(self.live(), saved); + } +} + /// 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 @@ -305,7 +455,7 @@ fn restoredInput(self: *Host, now: u64) !void { 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; + if (h.mode != .recording) 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 }); @@ -317,7 +467,9 @@ fn restoredInput(self: *Host, now: u64) !void { try self.pending.insertSlice(self.gpa, 0, seed.items); } -fn draw(self: *Host, now: u64) !void { +fn draw(self: *Host, now: u64, started: u64) !u64 { + const zone = tracy.zone(@src(), "Host.zig:draw"); + defer zone.end(); const arena = self.frame_arena.allocator(); self.input.now = now; self.input.mode = self.timeline.mode; @@ -342,12 +494,13 @@ fn draw(self: *Host, now: u64) !void { } } self.input.streams = self.audio.streamStatus(); + self.input.lead = self.audio.lead; self.input.sample_rate = sound.sample_rate; self.input.microphone = self.audio.record(arena); self.input.downloads = if (self.passes) |*g| try g.collect(arena) else &.{}; self.input.frame += 1; const ctx = self.context(); - self.sketch.draw(&ctx, self.live().ptr); + if (self.session_ready) self.sketch.draw(&ctx, self.live().ptr); _ = c.SDL_SetRenderTarget(self.renderer, self.offscreen); _ = c.SDL_SetRenderClipRect(self.renderer, null); @@ -356,47 +509,65 @@ fn draw(self: *Host, now: u64) !void { 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.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), - .quit => self.quit = true, - .gpu => |pass| if (self.passes) |*g| g.run(&self.images, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }), - .upload => |u| if (self.passes) |*g| g.write(u.buffer, u.offset, u.bytes) catch |err| self.print("gpu upload: {t} ({s})", .{ err, c.SDL_GetError() }), - .compute => |d| if (self.passes) |*g| g.compute(d) catch |err| self.print("gpu compute: {t} ({s})", .{ err, c.SDL_GetError() }), - .microphone => self.audio.listen(), - .download => |d| if (self.passes) |*g| g.download(d) catch |err| self.print("gpu download: {t} ({s})", .{ err, c.SDL_GetError() }), - .open_file => |o| self.openFile(o.command, o.pattern), - .run => |line| try lines.append(arena, line), - else => self.painter.draw(command), - }; - self.painter.end(); - self.audio.endFrame(); + { + const paint_zone = tracy.zone(@src(), "paint commands"); + defer paint_zone.end(); + for (self.commands.items) |command| switch (command) { + .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), + .gain => |g| self.audio.gains[g.group] = g.gain, + .audio => |a| self.audio.push(a.key, a.samples, a.clear), + .audio_buffer => |a| if (self.passes) |*g| if (a.count > 0) { + const samples = try arena.alloc(f32, a.count); + if (g.read(a.buffer, a.offset, std.mem.sliceAsBytes(samples))) self.audio.push(a.key, samples, false) else |err| self.print("gpu audio: {t} ({s})", .{ err, c.SDL_GetError() }); + }, + .quit => self.quit = true, + .gpu => |pass| if (self.passes) |*g| self.runGpu(g, pass) catch |err| self.print("gpu pass: {t} ({s})", .{ err, c.SDL_GetError() }), + .post_process => |p| if (self.passes) |*g| self.runPostProcess(g, p.source, p.target, p.effect) catch |err| self.print("gpu postprocess: {t} ({s})", .{ err, c.SDL_GetError() }), + .upload => |u| if (self.passes) |*g| g.write(u.buffer, u.offset, u.bytes) catch |err| self.print("gpu upload: {t} ({s})", .{ err, c.SDL_GetError() }), + .compute => |d| if (self.passes) |*g| g.compute(d) catch |err| self.print("gpu compute: {t} ({s})", .{ err, c.SDL_GetError() }), + .microphone => self.audio.listen(), + .download => |d| if (self.passes) |*g| g.download(d) catch |err| self.print("gpu download: {t} ({s})", .{ err, c.SDL_GetError() }), + .open_file => |o| self.openFile(o.command, o.pattern), + .run => |line| try lines.append(arena, line), + else => self.painter.draw(command), + }; + self.painter.end(); + self.audio.endFrame(); + } for (lines.items) |line| self.runLine(line, now) catch {}; 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); if (self.console.overlay_sound) { const samples = self.audio.scope.snapshot(); - try overlay.drawScope(&extra, arena, &samples, self.audio.scope.rate, self.canvas); + try overlay.drawScope(&extra, arena, &samples, self.audio.scope.rate, self.audio.pull, self.canvas); + } + if (self.timing.visible) { + if (self.timing_atlas.image == .none) self.timing_atlas = try overlay.TimingAtlas.init(&self.images, &self.painter); + for (extra.items) |command| self.painter.draw(command); + extra.clearRetainingCapacity(); + try overlay.drawTiming(&self.painter, &self.timing, self.canvas, self.timing_atlas); } if (self.console.visible) try self.console.draw(self, &extra, arena); + if (self.options.timing_marker) try timing_marker.draw(&extra, arena, self.canvas, self.input.frame, self.input.now); for (extra.items) |command| self.painter.draw(command); self.painter.end(); self.applyCursor(); - if (self.screenshot != null or self.options.export_to != null) { + if (self.screenshot != null or self.options.export_to != null or (self.control != null and self.control.?.capture_pending)) { const surface = c.SDL_RenderReadPixels(self.renderer, null) orelse return sdlError("SDL_RenderReadPixels"); defer c.SDL_DestroySurface(surface); if (self.screenshot) |path| { self.screenshot = null; if (!c.SDL_SavePNG(surface, path.ptr)) return sdlError("SDL_SavePNG"); } + if (self.control) |control| if (control.capture_pending) control.capture(surface); if (self.options.export_to) |to| try self.exportFrame(surface, to); } if (!self.options.headless) if (self.offscreen) |canvas| { @@ -404,8 +575,15 @@ fn draw(self: *Host, now: u64) !void { _ = c.SDL_SetTextureBlendMode(canvas, c.SDL_BLENDMODE_NONE); _ = c.SDL_RenderTexture(self.renderer, canvas, null, null); }; + // Present otherwise encodes the deferred 2D commands after the work timestamp. + if (!c.SDL_FlushRenderer(self.renderer)) return sdlError("SDL_FlushRenderer"); + const work = c.SDL_GetTicksNS() - started; + const present_zone = tracy.zone(@src(), "present"); + if (self.presentation) |p| p.request(self.input.frame, self.input.now); _ = c.SDL_RenderPresent(self.renderer); + present_zone.end(); if (self.passes) |*g| g.endFrame(); + return work; } /// One exported frame: `<to>/<its number, six digits>.png`, or for "-" its @@ -495,9 +673,7 @@ fn context(self: *Host) abi.Context { .input = &self.input, .scratch = self.frame_arena.allocator(), .session = self.session.heap.allocator(), - .store = self.store.allocator(), .session_root = self.session.bytes.ptr, - .store_root = self.store.root(), .commands = &self.commands, }; } @@ -535,30 +711,44 @@ pub fn route(self: *Host, e: abi.Event) !void { /// Runs a console line now, replaying whatever a seek asks for. An error is /// printed to the console, and returned for scripts to fail on. pub fn runLine(self: *Host, line: []const u8, now: u64) !void { + const zone = tracy.zone(@src(), "Host.zig:runLine"); + defer zone.end(); 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 (err == error.NotRecording) { + self.console.log("Not recording (an export): no marks or clips."); + } else if (err == error.HistoryDropped) { + self.console.log("That part of the history is gone: a save keeps 64 MiB, the newest."); + } else if (err == error.SaveFull) { + self.console.log("No room for a new mark: marks and the present fill the save. Save over a mark's name (its older one goes with history), or start another save (--state)."); + } else 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; + if (self.timeline.restored or movesState(line)) { + self.jumped = true; + self.sessions.noteLine(line); + } 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). +/// through `update` (restoring a snapshot is caught apart from this): a +/// sketch's command may. 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; + const verb = std.meta.stringToEnum(Console.Verb, words.next() orelse return false) orelse return true; + return Console.verbs.get(verb).moves_state; } /// Offers a line to the sketch. pub fn forward(self: *Host, _: *Console, line: []const u8, now: u64) abi.Outcome { + if (!self.session_ready) { + self.print("session setup failed; restart after fixing the resource error", .{}); + return .failed; + } self.input.now = now; const ctx = self.context(); return self.sketch.command(&ctx, self.live().ptr, &line); @@ -571,7 +761,6 @@ pub fn resetState(self: *Host) void { self.sketch.defaults(.state, bytes.ptr); const ctx = self.context(); self.sketch.init(&ctx, bytes.ptr); - self.state.markUsed(0, true); } /// Loads the watched library again. @@ -581,48 +770,49 @@ pub fn reload(self: *Host) !void { /// Replaces the running sketch with the library at `path`, keeping state. pub fn load(self: *Host, path: []const u8) !void { - var library = try Library.openCopy(self.io, path, scratchDir(self.environ), self.reloads + 1); + var library = try Library.openFresh(self.io, path, scratchDir(self.environ), self.reloads + 1); errdefer library.close(); + // The State in the layout it is leaving, before migration drops any of it. + if (self.sessions.mode() != .off) _ = self.sessions.keyframe(.before_reload, self.live()) catch |err| self.print("session: {t}", .{err}); try self.adopt(library.sketch); if (self.library) |*old| old.close(); self.library = library; self.reloads += 1; - self.build = fileHash(path); + self.build = fileId(self.io, path); + self.sessions.stretch(.{ .build = self.build, .source = self.source, .layout = self.state.header }) catch |err| self.print("session: {t}", .{err}); self.print("reload {d}: {s}", .{ self.reloads, path }); } fn adopt(self: *Host, sketch: abi.Sketch) !void { const info = sketch.info(); if (info.abi != abi.hash) return error.AbiMismatch; - const adoption = try self.state.adopt(self.gpa, info.state, sketch.defaults); + const migrated = try self.state.adopt(self.gpa, info.state, sketch.defaults); const session_reset = try self.session.adopt(self.gpa, info.session, sketch.defaults); if (session_reset) self.releaseMade(); - const stored = try self.store.adopt(self.gpa, info.store, sketch.defaults); _ = self.info_arena.reset(.retain_capacity); const arena = self.info_arena.allocator(); self.name = try arena.dupe(u8, info.name); + self.source = try arena.dupe(u8, info.source); const completions = try arena.alloc([]const u8, info.commands.len); for (completions, info.commands) |*to, from| to.* = try arena.dupe(u8, from); self.completions = completions; self.sketch = sketch; + self.timeline.defaults = sketch.defaults; self.reloaded = true; - switch (adoption) { - .unchanged => {}, - .created => {}, - .migrated => |m| self.print("state layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), + if (migrated) |m| self.print("state layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }); + if (session_reset) { + const ctx = self.context(); + self.session_ready = self.sketch.setup(&ctx); + if (!self.session_ready) self.console.visible = true; } - if (!self.state.used(0)) self.resetState(); if (session_reset and self.reloads > 0) self.print("session layout changed: session reset", .{}); - switch (stored) { - .unchanged, .created => {}, - .migrated => |m| self.print("store layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), - } } /// Frees what the old Session made: its handles are gone with it. fn releaseMade(self: *Host) void { for (self.made.images.items) |image| { - if (self.passes) |*g| g.forget(&self.images, image); + self.images.unwrap(image); + if (self.passes) |*g| g.forget(image); self.images.free(image); } if (self.passes) |*g| for (self.made.buffers.items) |buffer| g.freeBuffer(buffer); @@ -681,6 +871,8 @@ pub fn print(self: *Host, comptime fmt: []const u8, args: anytype) void { /// Translates SDL events into sketch events and routes them. pub fn poll(self: *Host) !void { + const zone = tracy.zone(@src(), "Host.zig:poll"); + defer zone.end(); var event: c.SDL_Event = undefined; while (c.SDL_PollEvent(&event)) { _ = c.SDL_ConvertEventToRenderCoordinates(self.renderer, &event); @@ -757,15 +949,102 @@ 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; +/// Opens the save (Sessions.zig) and starts this run's session in it: a +/// file, `<stem>.xitdb`; in memory for a script from fresh state; nowhere +/// for an export. A save that cannot be opened is said, and the run goes +/// on unsaved. +fn openSessions(self: *Host, record: bool) void { + const o = self.options; + const mode: Sessions.Mode = if (o.export_to != null) .off else if (o.script and o.fresh) .memory else .file; + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = std.fmt.bufPrint(&buf, "{s}.xitdb", .{self.stem}) catch "sketch.xitdb"; + const first: Sessions.Stretch = .{ .build = self.build, .source = self.source, .layout = self.state.header }; + self.sessions = Sessions.open(self.gpa, self.io, mode, path, first, wallClock(self.io)) catch |err| blk: { + self.print("not saved: {s}: {t}", .{ path, err }); + break :blk .{ .gpa = self.gpa, .io = self.io }; + }; + // Without `.record`, history is trimmed to the present and the marks. + self.sessions.history = record; + if (o.headless or self.sessions.mode() != .file) return; + self.print("{s} to {s}", .{ if (record) "recording" else "saving", path }); +} + +/// The State this run starts from: the save's present, its newest keyframe +/// and the frames logged after it, replayed; in a new save, a state file +/// from before (`<stem>.state`), imported once and left as it was; else +/// `init`'s. +fn openPresent(self: *Host) void { + var events: std.ArrayList(abi.Event) = .empty; + defer events.deinit(self.gpa); + const found = self.sessions.present(&events) catch |err| blk: { + self.print("the save's present did not read ({t}): starting from defaults", .{err}); + break :blk null; + }; + const key = found orelse { + if (self.sessions.session != 0 or self.sessions.mode() != .file) return self.resetState(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const old = std.fmt.bufPrint(&buf, "{s}.state", .{self.stem}) catch return self.resetState(); + const n = importOld(self.gpa, old, &self.state, &self.sessions, self.sketch.defaults) catch |err| { + if (err != error.FileNotFound) self.print("{s} not imported ({t}): kept as it was", .{ old, err }); + return self.resetState(); + }; + return self.print("{s} imported: its State, and {d} snapshots as marks; the file is kept as it was", .{ old, n }); + }; + defer key.free(self.gpa); + self.timeline.restore(key) catch |err| { + self.print("the save's present did not restore ({t}): starting from defaults", .{err}); + return self.resetState(); + }; + self.timeline.restored = false; + var frames: usize = 0; + for (events.items) |e| frames += @intFromBool(e.kind == .tick); + if (frames == 0) return; + if (key.build != self.build) return self.print("{d} frames played after the save's last keyframe not replayed: another build", .{frames}); + // As they were fed: `started` only for a run's first frame. + self.started = key.frame == 0; + self.update(events.items); + self.started = true; +} + +/// A state file from before (`path`) into a new save: its live State into +/// `state`, its snapshots (slots 1..7) marks "1".."7", in `state`'s layout; +/// the file is left as it was. Returns the number of marks. +fn importOld(gpa: std.mem.Allocator, path: []const u8, state: *memory.State, sessions: *Sessions, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !usize { + const old = try memory.OldState.open(path); + defer old.close(); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const scratch = try arena.allocator().alignedAlloc(u8, .fromByteUnits(memory.page), state.layout.size); + var n: usize = 0; + for (1..old.slots) |i| if (old.slot(i)) |image| { + _ = try memory.restore(arena.allocator(), old.layoutBytes(), image, state.layout, scratch, defaults); + _ = try sessions.mark(&.{'0' + @as(u8, @intCast(i))}, .import, scratch); + n += 1; + }; + _ = try memory.restore(arena.allocator(), old.layoutBytes(), old.slot(0) orelse return error.CorruptStateFile, state.layout, state.live(), defaults); + return n; +} + +/// `path` without `.xitdb` or `.state`. +pub fn stemOf(path: []const u8) []const u8 { + for ([_][]const u8{ ".xitdb", ".state" }) |suffix| if (std.mem.endsWith(u8, path, suffix)) return path[0 .. path.len - suffix.len]; + return path; +} + +fn wallClock(io: std.Io) u64 { + return @intCast(@max(0, std.Io.Timestamp.now(io, .real).nanoseconds)); +} + +/// Identifies the file at `path` without reading it (a Debug library is tens +/// of megabytes): a hash of its inode, size and modification time; 0 when +/// there is none. A build replaces the file, so each build gets a new one. +fn fileId(io: std.Io, path: []const u8) u64 { + const stat = std.Io.Dir.cwd().statFile(io, path, .{}) catch return 0; + const id = [3]u64{ stat.inode, stat.size, @truncate(@as(u96, @bitCast(stat.mtime.nanoseconds))) }; + return std.hash.Wyhash.hash(0, std.mem.asBytes(&id)) | 1; } -fn scratchDir(environ: *const std.process.Environ.Map) []const u8 { +pub fn scratchDir(environ: *const std.process.Environ.Map) []const u8 { return environ.get("XDG_RUNTIME_DIR") orelse "/tmp"; } @@ -784,12 +1063,16 @@ const api: abi.Host.VTable = .{ .gpu = apiGpu, .newBuffer = apiNewBuffer, .freeBuffer = apiFreeBuffer, + .writeBuffer = apiWriteBuffer, .imageSize = apiImageSize, - .textSize = apiTextSize, + .textRun = apiTextRun, + .font = apiFont, .loadSound = apiLoadSound, .decodeSound = apiDecodeSound, + .newSound = apiNewSound, .samples = apiSamples, .readFile = apiReadFile, + .appendFile = apiAppendFile, .env = apiEnv, .clock = apiClock, }; @@ -814,7 +1097,8 @@ fn apiNewImage(ctx: *anyopaque, w: u32, h: u32) abi.Image { fn apiFreeImage(ctx: *anyopaque, image: abi.Image) void { const self = hostOf(ctx); forget(&self.made.images, image); - if (self.passes) |*g| g.forget(&self.images, image); + self.images.unwrap(image); + if (self.passes) |*g| g.forget(image); self.images.free(image); } fn apiGpu(ctx: *anyopaque) bool { @@ -822,12 +1106,17 @@ fn apiGpu(ctx: *anyopaque) bool { } fn apiNewBuffer(ctx: *anyopaque, size: u32) abi.Buffer { const self = hostOf(ctx); - const g = &(self.passes orelse return .none); + const g = if (self.passes) |*passes| passes else return .none; return self.remember(.buffers, g.newBuffer(size) catch |err| blk: { self.print("gpu buffer: {t} ({s})", .{ err, c.SDL_GetError() }); break :blk .none; }); } +fn apiWriteBuffer(ctx: *anyopaque, buffer: abi.Buffer, offset: u32, bytes: []const u8) !void { + const self = hostOf(ctx); + const gpu = if (self.passes) |*passes| passes else return error.GpuUnavailable; + try gpu.write(buffer, offset, bytes); +} fn apiFreeBuffer(ctx: *anyopaque, buffer: abi.Buffer) void { const self = hostOf(ctx); forget(&self.made.buffers, buffer); @@ -839,8 +1128,12 @@ fn apiSamples(ctx: *anyopaque, audio: abi.Audio) []const f32 { fn apiImageSize(ctx: *anyopaque, image: abi.Image) [2]u32 { return hostOf(ctx).images.size(image); } -fn apiTextSize(ctx: *anyopaque, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { - return gfx.textSize(hostOf(ctx).renderer, scale, bytes, face); +fn apiTextRun(_: *anyopaque, face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun { + return gfx.textRun(face, scale, bytes, xs); +} +fn apiFont(_: *anyopaque, bytes: []const u8) ?abi.Face { + const face = c.sk_text_font(bytes.ptr, bytes.len); + return if (face < 0) null else @enumFromInt(face); } fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio { return hostOf(ctx).audio.load(path); @@ -848,6 +1141,9 @@ fn apiLoadSound(ctx: *anyopaque, path: []const u8) abi.Audio { fn apiDecodeSound(ctx: *anyopaque, wav: []const u8) abi.Audio { return hostOf(ctx).audio.decode(wav); } +fn apiNewSound(ctx: *anyopaque, samples: [*]const f32, len: usize) callconv(.c) abi.Audio { + return hostOf(ctx).audio.new(samples[0..len]); +} fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 { const self = hostOf(ctx); if (self.files.get(path)) |bytes| return bytes; @@ -857,9 +1153,252 @@ fn apiReadFile(ctx: *anyopaque, path: []const u8) []const u8 { self.files.put(self.gpa, key, bytes) catch self.gpa.free(key); return bytes; } +fn apiAppendFile(ctx: *anyopaque, name_ptr: [*]const u8, name_len: usize, bytes: [*]const u8, len: usize, replace: bool) callconv(.c) [*:0]const u8 { + const self = hostOf(ctx); + const name = name_ptr[0..name_len]; + if (name.len == 0 or std.mem.indexOfScalar(u8, name, '/') != null) { + self.print("appendFile: {s}: a name, not a path", .{name}); + return ""; + } + var buf: [std.fs.max_path_bytes]u8 = undefined; + const o = self.options; + const private = (o.script or o.export_to != null) and o.fresh; + // A scripted run from fresh state, or an export, writes files of its own + // (two runs of one build step may share a directory), gone at exit. + const path = (if (private) std.mem.print(&buf, "{s}.{d}.{s}", .{ self.stem, std.os.linux.getpid(), name }) else std.mem.print(&buf, "{s}.{s}", .{ self.stem, name })) catch return ""; + if (replace) { + // A new file: a mapping of the old one stays valid. + std.Io.Dir.cwd().deleteFile(self.io, path) catch {}; + if (self.files.fetchRemove(path)) |old| { + self.gpa.free(old.key); + self.retired.append(self.gpa, old.value) catch {}; + } + } + const file = std.Io.Dir.cwd().createFile(self.io, path, .{ .truncate = false }) catch |err| { + self.print("appendFile: {s}: {t}", .{ path, err }); + return ""; + }; + defer file.close(self.io); + const end = file.length(self.io) catch return ""; + file.writePositionalAll(self.io, bytes[0..len], end) catch |err| { + self.print("appendFile: {s}: {t}", .{ path, err }); + return ""; + }; + const kept = self.written.getOrPut(self.gpa, path) catch return ""; + if (!kept.found_existing) { + const copy = self.gpa.dupeSentinel(u8, path, 0) catch { + self.written.removeByPtr(kept.key_ptr); + return ""; + }; + kept.key_ptr.* = copy; + } + return @ptrCast(kept.key_ptr.*.ptr); +} fn apiClock(_: *anyopaque) u64 { return c.SDL_GetTicksNS(); } fn apiEnv(ctx: *anyopaque, name: []const u8) ?[]const u8 { return hostOf(ctx).environ.get(name); } + +fn runPostProcess(self: *Host, g: *Gpu, source: abi.Image, target: abi.Image, post: abi.gpu.PostProcess) !void { + const input = g.output(source) orelse return error.MissingImage; + const before = g.output(target); + const changed = if (before) |t| t.width != input.width or t.height != input.height else true; + if (changed) self.images.unwrap(target); + try g.runPostProcess(source, target, post); + if (g.output(target)) |t| try self.images.wrap(target, t.texture, t.width, t.height); +} + +fn runGpu(self: *Host, g: *Gpu, pass: abi.gpu.Pass) !void { + const before = g.output(pass.image); + const changed = if (before) |t| t.width != pass.width or t.height != pass.height else true; + if (changed) self.images.unwrap(pass.image); + try g.run(pass); + if (g.output(pass.image)) |t| try self.images.wrap(pass.image, t.texture, t.width, t.height); +} + +test "session setup runs for restored state and layout reset, not same-layout reload or state reset" { + const Fixture = struct { + const State = extern struct { value: u32 = 1 }; + const Session1 = extern struct { ready: u32 = 0 }; + const Session2 = extern struct { ready: u32 = 0, extra: u32 = 0 }; + var setup_calls: usize = 0; + var init_calls: usize = 0; + var fail_setup = false; + const description: abi.Info = .{ .abi = abi.hash, .name = "setup-test", .width = 1, .height = 1, .state = abi.layout.of(State, .state), .session = abi.layout.of(Session1, .session) }; + const changed: abi.Info = blk: { + var value = description; + value.session = abi.layout.of(Session2, .session); + break :blk value; + }; + fn info() callconv(.c) *const abi.Info { + return &description; + } + fn info2() callconv(.c) *const abi.Info { + return &changed; + } + fn defaults(kind: abi.Memory, ptr: [*]u8) callconv(.c) void { + switch (kind) { + .state => @as(*State, @ptrCast(@alignCast(ptr))).* = .{}, + .session => @as(*Session1, @ptrCast(@alignCast(ptr))).* = .{}, + } + } + fn defaults2(kind: abi.Memory, ptr: [*]u8) callconv(.c) void { + if (kind == .session) @as(*Session2, @ptrCast(@alignCast(ptr))).* = .{} else defaults(kind, ptr); + } + fn init(_: *const abi.Context, _: [*]u8) callconv(.c) void { + init_calls += 1; + } + fn setup(ctx: *const abi.Context) callconv(.c) bool { + setup_calls += 1; + @as(*u32, @ptrCast(@alignCast(ctx.session_root))).* += 1; + return !fail_setup; + } + fn update(_: *const abi.Context, _: [*]u8) callconv(.c) void {} + fn draw(_: *const abi.Context, _: [*]const u8) callconv(.c) void {} + fn command(_: *const abi.Context, _: [*]u8, _: *const []const u8) callconv(.c) abi.Outcome { + return .unknown; + } + const sketch: abi.Sketch = .{ .info = info, .defaults = defaults, .init = @This().init, .setup = setup, .update = @This().update, .draw = @This().draw, .command = command }; + }; + Fixture.setup_calls = 0; + Fixture.init_calls = 0; + Fixture.fail_setup = false; + const a = std.testing.allocator; + // Exercise adoption without opening SDL devices. + var host: Host = undefined; + host.gpa = a; + host.state = .init(a); + defer host.state.deinit(a); + host.session = .init(a); + defer host.session.deinit(a); + host.info_arena = .init(a); + defer host.info_arena.deinit(); + host.frame_arena = .init(a); + defer host.frame_arena.deinit(); + host.options = .{ .headless = true }; + host.console = .{}; + host.reloads = 0; + host.made = .{}; + host.passes = null; + host.input = .{}; + host.commands = .empty; + try host.adopt(Fixture.sketch); + // Defaults: the present or `init` comes after (openPresent). + try std.testing.expectEqual(@as(u32, 1), @as(*Fixture.State, @ptrCast(@alignCast(host.live().ptr))).value); + try std.testing.expectEqual(@as(usize, 0), Fixture.init_calls); + try std.testing.expectEqual(@as(usize, 1), Fixture.setup_calls); + const session_ptr = host.session.bytes.ptr; + try host.adopt(Fixture.sketch); + try std.testing.expectEqual(session_ptr, host.session.bytes.ptr); + try std.testing.expectEqual(@as(usize, 1), Fixture.setup_calls); + host.resetState(); + try std.testing.expectEqual(@as(usize, 1), Fixture.init_calls); + try std.testing.expectEqual(@as(usize, 1), Fixture.setup_calls); + var changed = Fixture.sketch; + changed.info = Fixture.info2; + changed.defaults = Fixture.defaults2; + try host.adopt(changed); + try std.testing.expectEqual(@as(usize, 2), Fixture.setup_calls); + try std.testing.expectEqual(@as(u32, 1), @as(*Fixture.Session2, @ptrCast(host.session.bytes.ptr)).ready); + try std.testing.expect(host.session_ready); + Fixture.fail_setup = true; + defer Fixture.fail_setup = false; + try host.adopt(Fixture.sketch); + try std.testing.expect(!host.session_ready); + try std.testing.expect(host.console.visible); + try std.testing.expectEqual(abi.Outcome.failed, host.forward(&host.console, "anything", 0)); + try host.adopt(Fixture.sketch); + try std.testing.expect(!host.session_ready); // A compatible reload cannot bless partial resources. + try std.testing.expectEqual(@as(usize, 3), Fixture.setup_calls); +} + +test "setup writeBuffer propagates unavailable, unknown and out-of-bounds upload errors" { + const a = std.testing.allocator; + var host: Host = undefined; + host.passes = null; + const client: abi.Host = .{ .ctx = &host, .vtable = &api }; + try std.testing.expectError(error.GpuUnavailable, client.writeBuffer(.none, 0, "data")); + host.passes = .{ .gpa = a, .device = undefined, .stencil_format = undefined }; + defer host.passes.?.buffers.deinit(a); + try std.testing.expectError(error.UnknownBuffer, client.writeBuffer(.none, 0, "data")); + try host.passes.?.buffers.append(a, .{ .buffer = undefined, .size = 4 }); + try std.testing.expectError(error.UploadOutOfBounds, client.writeBuffer(@fromBackingInt(@intCast(1)), 2, "data")); +} + +test "a state file from before imports into a new save: its State live, its snapshots marks, the file as it was" { + const State = extern struct { v: u32 = 0 }; + const D = struct { + fn f(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*State, @ptrCast(@alignCast(p))).* = .{}; + } + }; + 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 std.mem.print(&buf, ".zig-cache/tmp/{s}/old.state", .{&tmp.sub_path}); + var state: memory.State = .init(gpa); + defer state.deinit(gpa); + _ = try state.adopt(gpa, abi.layout.of(State, .state), D.f); + // An eight-slot SKETCH02 file, slots 0, 1 and 3 used, its header State's. + const stride = memory.page; + const old = try gpa.alloc(u8, memory.header_len + memory.old_slot_count * stride); + defer gpa.free(old); + @memset(old, 0); + @memcpy(old[0..state.header.len], state.header); + const h: *memory.Header = @ptrCast(@alignCast(old.ptr)); + h.magic = memory.file_magic_v2; + h.used = 0b1011; + for ([_][2]u32{ .{ 0, 5 }, .{ 1, 11 }, .{ 3, 33 } }) |slot| std.mem.writeInt(u32, old[memory.header_len + slot[0] * stride ..][0..4], slot[1], .little); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = old }); + + var sessions = try Sessions.open(gpa, io, .memory, "", .{ .build = 1, .source = "", .layout = state.header }, 0); + defer sessions.deinit(); + try std.testing.expectEqual(@as(usize, 2), try importOld(gpa, path, &state, &sessions, D.f)); + for ([_][2]u32{ .{ '1', 11 }, .{ '3', 33 } }) |want| { + const m = (try sessions.findMark(&.{@intCast(want[0])})).?; + const key = try sessions.load(m.session, m.key); + defer key.free(gpa); + try std.testing.expectEqual(Sessions.Why.import, key.why); + try std.testing.expectEqual(want[1], std.mem.readInt(u32, key.state[0..4], .little)); + } + try std.testing.expectEqual(@as(?Sessions.Entry, null), try sessions.findMark("2")); + try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, state.live()[0..4], .little)); + const after = try std.Io.Dir.cwd().readFileAlloc(io, path, gpa, .unlimited); + defer gpa.free(after); + try std.testing.expectEqualSlices(u8, old, after); +} + +test "appendFile appends to a file beside the save and names it for readFile" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + var host: Host = undefined; + host.gpa = std.testing.allocator; + host.io = io; + host.written = .empty; + host.files = .empty; + host.options = .{}; + host.stem = try std.mem.print(&buf, ".zig-cache/tmp/{s}/t", .{&tmp.sub_path}); + defer { + var written = host.written.keyIterator(); + while (written.next()) |path| host.gpa.free(path.ptr[0 .. path.len + 1]); + host.written.deinit(host.gpa); + } + const name = "take-1.f32"; + const path = std.mem.span(apiAppendFile(&host, name, name.len, "ab", 2, false)); + try std.testing.expect(std.mem.endsWith(u8, path, "/t.take-1.f32")); + try std.testing.expectEqual(path.ptr, apiAppendFile(&host, name, name.len, "cd", 2, false)); + const bytes = try tmp.dir.readFileAlloc(io, "t.take-1.f32", host.gpa, .unlimited); + defer host.gpa.free(bytes); + try std.testing.expectEqualStrings("abcd", bytes); + // writeFile: a new file, its old bytes gone. + _ = apiAppendFile(&host, name, name.len, "e", 1, true); + const again = try tmp.dir.readFileAlloc(io, "t.take-1.f32", host.gpa, .unlimited); + defer host.gpa.free(again); + try std.testing.expectEqualStrings("e", again); +} diff --git a/src/host/Presentation.zig b/src/host/Presentation.zig new file mode 100644 index 0000000..c191c79 --- /dev/null +++ b/src/host/Presentation.zig @@ -0,0 +1,331 @@ +//! Wayland feedback and display-clock pacing; other backends keep SDL's pacing. +const Presentation = @This(); +const std = @import("std"); +const c = @import("c"); +const linux = @import("builtin").os.tag == .linux; +const capacity = 8192; +const pending_capacity = 256; + +pub const Record = struct { + frame: u64 = 0, + sample_ms: u64 = 0, + commit_ns: u64 = 0, + presented_ns: u64 = 0, + refresh_ns: u64 = 0, + sequence: u64 = 0, + flags: u32 = 0, + result: enum { presented, discarded } = .discarded, +}; +status: []const u8 = if (linux) "not_wayland" else "unsupported_platform", +clock_id: u32 = 0, +have_clock: bool = false, +completed: u64 = 0, +exhausted: u64 = 0, +last_submit_ns: u64 = 0, +next_submit_ns: u64 = 0, +submit_period_ns: u64 = 0, +records: [capacity]Record = @splat(.{}), +wl: Wayland = .{}, + +const Wayland = if (linux) struct { + display: ?*c.wl_display = null, + surface: ?*c.wl_surface = null, + queue: ?*c.wl_event_queue = null, + registry: ?*c.wl_registry = null, + presentation: ?*c.wp_presentation = null, + global_name: u32 = 0, + pending: [pending_capacity]Pending = @splat(.{}), + + const Pending = struct { + owner: *Presentation = undefined, + feedback: ?*c.struct_wp_presentation_feedback = null, + record: Record = .{}, + }; + fn finish(pending: *Pending, result: @FieldType(Record, "result")) void { + const self = pending.owner; + pending.record.result = result; + self.records[self.completed % capacity] = pending.record; + self.completed += 1; + if (pending.feedback) |feedback| c.wp_presentation_feedback_destroy(feedback); + pending.feedback = null; + } + fn syncOutput(_: ?*anyopaque, _: ?*c.struct_wp_presentation_feedback, _: ?*c.wl_output) callconv(.c) void {} + fn presented(data: ?*anyopaque, _: ?*c.struct_wp_presentation_feedback, sec_hi: u32, sec_lo: u32, nsec: u32, refresh: u32, seq_hi: u32, seq_lo: u32, flags: u32) callconv(.c) void { + const pending: *Pending = @ptrCast(@alignCast(data.?)); + pending.record.presented_ns = ((@as(u64, sec_hi) << 32) | sec_lo) * std.time.ns_per_s + nsec; + pending.record.refresh_ns = refresh; + pending.record.sequence = (@as(u64, seq_hi) << 32) | seq_lo; + pending.record.flags = flags; + finish(pending, .presented); + } + fn discarded(data: ?*anyopaque, _: ?*c.struct_wp_presentation_feedback) callconv(.c) void { + finish(@ptrCast(@alignCast(data.?)), .discarded); + } + const feedback_listener: c.wp_presentation_feedback_listener = .{ .sync_output = syncOutput, .presented = presented, .discarded = discarded }; + fn clockId(data: ?*anyopaque, _: ?*c.wp_presentation, id: u32) callconv(.c) void { + const self: *Presentation = @ptrCast(@alignCast(data.?)); + self.clock_id = id; + var now: c.timespec = undefined; + self.have_clock = c.clock_gettime(@bitCast(id), &now) == 0; + self.status = if (self.have_clock) "ready" else "unsupported_presentation_clock"; + } + const presentation_listener: c.wp_presentation_listener = .{ .clock_id = clockId }; + fn global(data: ?*anyopaque, registry: ?*c.wl_registry, name: u32, interface: [*c]const u8, version: u32) callconv(.c) void { + const self: *Presentation = @ptrCast(@alignCast(data.?)); + if (!std.mem.eql(u8, std.mem.span(interface), "wp_presentation") or self.wl.presentation != null or version < 1) return; + self.wl.global_name = name; + self.wl.presentation = @ptrCast(c.wl_registry_bind(registry, name, &c.wp_presentation_interface, 1)); + if (self.wl.presentation) |presentation| _ = c.wp_presentation_add_listener(presentation, &presentation_listener, self); + } + fn globalRemove(data: ?*anyopaque, _: ?*c.wl_registry, name: u32) callconv(.c) void { + const self: *Presentation = @ptrCast(@alignCast(data.?)); + if (name == self.wl.global_name) { + self.have_clock = false; + self.status = "presentation_global_removed"; + } + } + const registry_listener: c.wl_registry_listener = .{ .global = global, .global_remove = globalRemove }; +} else struct {}; + +pub fn init(gpa: std.mem.Allocator, window: *c.SDL_Window) !*Presentation { + const self = try gpa.create(Presentation); + self.* = .{}; + if (!linux) return self; + const props = c.SDL_GetWindowProperties(window); + self.wl.display = @ptrCast(c.SDL_GetPointerProperty(props, c.SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, null)); + self.wl.surface = @ptrCast(c.SDL_GetPointerProperty(props, c.SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, null)); + if (self.wl.display == null or self.wl.surface == null) return self; + self.status = "wayland_queue_failed"; + self.wl.queue = c.wl_display_create_queue(self.wl.display) orelse return self; + const wrapper = c.wl_proxy_create_wrapper(self.wl.display) orelse return self; + c.wl_proxy_set_queue(@ptrCast(wrapper), self.wl.queue); + self.wl.registry = c.wl_display_get_registry(@ptrCast(wrapper)); + c.wl_proxy_wrapper_destroy(wrapper); + if (self.wl.registry == null) return self; + self.status = "wp_presentation_unavailable"; + _ = c.wl_registry_add_listener(self.wl.registry, &Wayland.registry_listener, self); + // Only initialization reads/waits on SDL's shared display connection. + if (c.wl_display_roundtrip_queue(self.wl.display, self.wl.queue) < 0 or + c.wl_display_roundtrip_queue(self.wl.display, self.wl.queue) < 0) + { + self.have_clock = false; + self.status = "wayland_roundtrip_failed"; + } + return self; +} + +pub fn deinit(self: *Presentation, gpa: std.mem.Allocator) void { + if (linux) { + for (&self.wl.pending) |*pending| if (pending.feedback) |feedback| c.wp_presentation_feedback_destroy(feedback); + if (self.wl.presentation) |presentation| c.wp_presentation_destroy(presentation); + if (self.wl.registry) |registry| c.wl_registry_destroy(registry); + if (self.wl.queue) |queue| c.wl_event_queue_destroy(queue); + } + gpa.destroy(self); +} +pub fn poll(self: *Presentation) void { + if (!linux) return; + // SDL_PollEvent reads the connection; dispatch only our already-queued events. + if (self.wl.queue) |queue| if (c.wl_display_dispatch_queue_pending(self.wl.display, queue) < 0) { + self.have_clock = false; + self.status = "wayland_dispatch_failed"; + }; +} +fn protocolNow(self: *Presentation) ?u64 { + if (!linux or !self.have_clock) return null; + var now: c.timespec = undefined; + if (c.clock_gettime(@bitCast(self.clock_id), &now) != 0) { + self.have_clock = false; + self.status = "presentation_clock_failed"; + return null; + } + return @as(u64, @intCast(now.tv_sec)) * std.time.ns_per_s + @as(u64, @intCast(now.tv_nsec)); +} +fn validPeriod(period: u64) bool { + return period >= std.time.ns_per_ms and period <= 100 * std.time.ns_per_ms; +} +fn nextDeadline(record: ?*const Record, now: u64) u64 { + const r = record orelse return 0; + if (r.flags & 1 == 0 or !validPeriod(r.refresh_ns) or r.presented_ns > now) return 0; + var remaining = r.refresh_ns - (now - r.presented_ns) % r.refresh_ns; + if (remaining < r.refresh_ns / 2) remaining += r.refresh_ns; + return std.math.add(u64, now, remaining) catch 0; +} +fn remainingWait(deadline: u64, submitted: u64, period: u64, now: u64) u64 { + if (!validPeriod(period) or now < submitted or deadline <= now or deadline - now > period + period / 2) return 0; + return deadline - now; +} +fn waitAt(self: *Presentation, now: u64) u64 { + if (!self.have_clock) return 0; + const r = self.latest(); + if (r == null or r.?.flags & 1 == 0 or r.?.refresh_ns != self.submit_period_ns or r.?.presented_ns > now or + now < self.last_submit_ns or (self.next_submit_ns > now and self.next_submit_ns - now > self.submit_period_ns + self.submit_period_ns / 2)) + { + self.next_submit_ns = 0; + return 0; + } + // Keep a reached deadline until request replaces it, for sampling this frame. + return remainingWait(self.next_submit_ns, self.last_submit_ns, self.submit_period_ns, now); +} +pub fn waitNs(self: *Presentation) u64 { + return self.waitAt(self.protocolNow() orelse return 0); +} +fn scheduledSample(deadline: u64, period: u64, protocol_now: u64, sdl_now: u64) u64 { + if (deadline == 0 or !validPeriod(period) or protocol_now < deadline) return sdl_now; + const age = protocol_now - deadline; + if (age > period / 2 or age > sdl_now) return sdl_now; + return sdl_now - age; +} +pub fn sampleNs(self: *Presentation, sdl_now: u64) u64 { + const now = self.protocolNow() orelse return sdl_now; + _ = self.waitAt(now); + return scheduledSample(self.next_submit_ns, self.submit_period_ns, now, sdl_now); +} +pub fn request(self: *Presentation, frame: u64, sample_ns: u64) void { + if (!linux) return; + const now = self.protocolNow() orelse return; + const r = self.latest(); + self.last_submit_ns = now; + self.submit_period_ns = if (r) |record| record.refresh_ns else 0; + self.next_submit_ns = nextDeadline(r, now); + for (&self.wl.pending) |*pending| { + if (pending.feedback != null) continue; + pending.owner = self; + pending.record = .{ .frame = frame, .sample_ms = sample_ns / std.time.ns_per_ms, .commit_ns = now }; + // The next surface commit consumes this request, normally Vulkan present. + pending.feedback = c.wp_presentation_feedback(self.wl.presentation, self.wl.surface) orelse { + self.exhausted += 1; + return; + }; + _ = c.wp_presentation_feedback_add_listener(pending.feedback, &Wayland.feedback_listener, pending); + return; + } + self.exhausted += 1; +} +pub fn latest(self: *const Presentation) ?*const Record { + for (0..@min(self.completed, capacity)) |i| { + const r = &self.records[(self.completed - i - 1) % capacity]; + if (r.result == .presented) return r; + } + return null; +} +pub fn csv(self: ?*const Presentation, writer: *std.Io.Writer) !void { + try writer.print("# status {s}\n", .{if (self) |s| s.status else "disabled_or_allocation_failed"}); + try writer.writeAll("# commit_ns is feedback-request time before SDL_RenderPresent, not a wl_surface.commit interception\n# sample_ms uses SDL elapsed time; commit_ns and presented_ns use the protocol clock\n"); + if (linux) { + var pending: usize = 0; + if (self) |s| for (s.wl.pending) |p| { + pending += @intFromBool(p.feedback != null); + }; + try writer.print("# clock_id {d}\n# completed {d}\n# pending {d}\n# exhausted {d}\n# retained_limit {d}\n", .{ if (self) |s| s.clock_id else 0, if (self) |s| s.completed else 0, pending, if (self) |s| s.exhausted else 0, capacity }); + } + try writer.writeAll("frame_id,sample_ms,commit_ns,result,presented_ns,refresh_ns,sequence,flags\n"); + if (self) |s| { + const start = s.completed -| capacity; + for (start..s.completed) |i| { + const r = s.records[i % capacity]; + try writer.print("{d},{d},{d},{t},{d},{d},{d},{d}\n", .{ r.frame, r.sample_ms, r.commit_ns, r.result, r.presented_ns, r.refresh_ns, r.sequence, r.flags }); + } + } +} + +test "presentation display deadlines reject invalid clocks and avoid phase-jitter bursts" { + const expectEqual = std.testing.expectEqual; + var r: Record = .{ .presented_ns = 100000000, .refresh_ns = 10000000, .flags = 1 }; + try expectEqual(@as(u64, 110000000), nextDeadline(&r, 100000000)); + try expectEqual(@as(u64, 150000000), nextDeadline(&r, 137000000)); + try expectEqual(@as(u64, 140000000), nextDeadline(&r, 135000000)); + try expectEqual(@as(u64, 150000000), nextDeadline(&r, 139900000)); + try expectEqual(@as(u64, 150000000), nextDeadline(&r, 140000000)); + try expectEqual(@as(u64, 0), nextDeadline(&r, 99000000)); + try expectEqual(@as(u64, 0), nextDeadline(null, 100000000)); + r.flags = 0; + try expectEqual(@as(u64, 0), nextDeadline(&r, 100000000)); + r.flags = 1; + r.refresh_ns = 999999; + try expectEqual(@as(u64, 0), nextDeadline(&r, 100000000)); + r.refresh_ns = 100000001; + try expectEqual(@as(u64, 0), nextDeadline(&r, 100000000)); + r.refresh_ns = 100000000; + try expectEqual(@as(u64, 200000000), nextDeadline(&r, 100000000)); + r.refresh_ns = 1000000; + try expectEqual(@as(u64, 101000000), nextDeadline(&r, 100000000)); + r.presented_ns = std.math.maxInt(u64) - 500; + try expectEqual(@as(u64, 0), nextDeadline(&r, std.math.maxInt(u64) - 100)); + try expectEqual(@as(u64, 3000000), remainingWait(140000000, 137000000, 10000000, 137000000)); + try expectEqual(@as(u64, 1), remainingWait(140000000, 137000000, 10000000, 139999999)); + try expectEqual(@as(u64, 0), remainingWait(140000000, 137000000, 10000000, 140000000)); + try expectEqual(@as(u64, 0), remainingWait(140000000, 137000000, 10000000, 136000000)); + try expectEqual(@as(u64, 0), remainingWait(std.math.maxInt(u64), 137000000, 10000000, 138000000)); +} + +test "presentation reached deadlines survive sampling but refresh changes invalidate them" { + const self = try std.testing.allocator.create(Presentation); + defer self.deinit(std.testing.allocator); + self.* = .{ .have_clock = true, .completed = 1, .last_submit_ns = 137000000, .submit_period_ns = 10000000, .next_submit_ns = 140000000 }; + self.records[0] = .{ .presented_ns = 100000000, .refresh_ns = 10000000, .flags = 1, .result = .presented }; + try std.testing.expectEqual(@as(u64, 2000000), self.waitAt(138000000)); + try std.testing.expectEqual(@as(u64, 0), self.waitAt(140000000)); + try std.testing.expectEqual(@as(u64, 140000000), self.next_submit_ns); + self.records[0].refresh_ns = 20000000; + try std.testing.expectEqual(@as(u64, 0), self.waitAt(138000000)); + try std.testing.expectEqual(@as(u64, 0), self.next_submit_ns); + self.have_clock = false; + try std.testing.expectEqual(@as(u64, 0), self.waitNs()); + try std.testing.expectEqual(@as(u64, 99), self.sampleNs(99)); +} + +test "presentation scheduled sample converts clocks without overflow or stale adjustment" { + const expectEqual = std.testing.expectEqual; + try expectEqual(@as(u64, 998000000), scheduledSample(140000000, 10000000, 142000000, 1000000000)); + try expectEqual(@as(u64, 995000000), scheduledSample(140000000, 10000000, 145000000, 1000000000)); + try expectEqual(@as(u64, 1000000000), scheduledSample(140000000, 10000000, 145000001, 1000000000)); + try expectEqual(@as(u64, 1000000000), scheduledSample(140000000, 10000000, 139999999, 1000000000)); + try expectEqual(@as(u64, 1), scheduledSample(140000000, 10000000, 142000000, 1)); + try expectEqual(std.math.maxInt(u64) - 1, scheduledSample(std.math.maxInt(u64) - 1, 10000000, std.math.maxInt(u64), std.math.maxInt(u64))); + try expectEqual(@as(u64, 99), scheduledSample(0, 10000000, 1, 99)); + try expectEqual(@as(u64, 99), scheduledSample(140000000, 0, 142000000, 99)); +} + +test "presentation feedback callbacks retain 64-bit clocks and sequences, ring CSV stays chronological" { + if (!linux) return error.SkipZigTest; + const self = try std.testing.allocator.create(Presentation); + defer self.deinit(std.testing.allocator); + self.* = .{ .status = "ready" }; + var pending: Wayland.Pending = .{ .owner = self, .record = .{ .frame = 7, .sample_ms = 9, .commit_ns = 11 } }; + Wayland.presented(&pending, null, 1, 2, 3, 16666666, 4, 5, 7); + try std.testing.expectEqual(@as(u64, 1), self.completed); + try std.testing.expectEqual((((@as(u64, 1) << 32) + 2) * std.time.ns_per_s) + 3, self.records[0].presented_ns); + try std.testing.expectEqual((@as(u64, 4) << 32) + 5, self.records[0].sequence); + try std.testing.expectEqual(@as(u32, 7), self.records[0].flags); + pending.record = .{ .frame = 8 }; + Wayland.discarded(&pending, null); + try std.testing.expectEqual(.discarded, self.records[1].result); + try std.testing.expectEqual(@as(u64, 0), self.records[1].presented_ns); + try std.testing.expectEqual(@as(u64, 7), self.latest().?.frame); + for (2..capacity + 3) |i| { + pending.record = .{ .frame = i }; + Wayland.finish(&pending, .presented); + } + var out: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer out.deinit(); + try csv(self, &out.writer); + try std.testing.expect(std.mem.indexOf(u8, out.written(), "\n3,0,0,presented,0,0,0,0\n") != null); + try std.testing.expect(std.mem.indexOf(u8, out.written(), "\n2,0,0,presented,0,0,0,0\n") == null); + try std.testing.expect(std.mem.indexOfScalar(u8, out.written(), 0) == null); + var small: [9]u8 = undefined; + var writer: std.Io.Writer = .fixed(&small); + try std.testing.expectError(error.WriteFailed, csv(self, &writer)); +} + +test "presentation disabled CSV is explicit and has a valid header" { + var out: std.Io.Writer.Allocating = .init(std.testing.allocator); + defer out.deinit(); + try csv(null, &out.writer); + try std.testing.expect(std.mem.startsWith(u8, out.written(), "# status disabled_or_allocation_failed\n")); + try std.testing.expect(std.mem.indexOf(u8, out.written(), "frame_id,sample_ms,commit_ns,result,presented_ns,refresh_ns,sequence,flags\n") != null); +} + +test "presentation runtime declarations compile for the target backend" { + std.testing.refAllDecls(Presentation); +} diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig new file mode 100644 index 0000000..d6caf9c --- /dev/null +++ b/src/host/Sessions.zig @@ -0,0 +1,1599 @@ +//! A sketch's save: one xitdb database holding every run of it, an +//! append-only log of the frames live `update` was fed, and the State at +//! the moments that matter (keyframes). The newest keyframe and the frames +//! after it are the present a run starts from (`present`). Nothing in it +//! is ever overwritten: marks and clips are names pointing into it. +//! +//! root = map { +//! version 1 +//! sessions list of session, one a run +//! marks list of { name, session, key }: a named keyframe (the newest of a name wins) +//! clips list of { name, session, key, start, end }: frames start..end of a +//! session, played from keyframe `key`; `end` 0 while recording +//! } +//! session = map { +//! started wall-clock ns at open ended ns at a clean exit, else 0 +//! from "" or where it came from ("import <file>") +//! stretches list of { frame, build, source, layout }: one a load (start, reloads); +//! `layout` is State's header (memory.State.header, read by memory.layoutOf) +//! batches list of bytes: a little-endian u64, the first frame's index, then +//! whole frames of raw `abi.Event`s, each frame ending with its tick +//! keys list of { frame, batch, stretch, why, time, size, pages, written, line }: +//! the State before frame `frame` is fed, in stretch `stretch`'s +//! layout; `batch` is where reading frames from it starts +//! frames frames committed +//! } +//! +//! A keyframe's State is `size` bytes in `pages`, a list of `page`-byte +//! blobs (the last one shorter), an all-zero page an empty slot. A keyframe +//! starts from the list of the one before it and writes only the pages +//! whose hash changed (`written` bytes), so every page another keyframe +//! holds unchanged is shared, never written twice. (Keyframes from before +//! pages hold their State whole, as `state` bytes; `load` reads both.) +//! +//! One transaction (`commit`) a second of session time, and at once for a +//! reload, a mark, a clip's start or end, and exit: a crash loses less than +//! a second of play; fsync only at exit and for a mark. A file a crash or +//! power loss tore opens at its last whole commit (`mend`). +//! +//! A save is at most `max_save`: a commit that leaves the file past it +//! drops history oldest first and compacts the file to three quarters of +//! it (`trim`); the present, the newest mark of each name and the newest +//! clip of each name are never dropped. A build that does not record +//! (`history` false) drops all it may at every keyframe, and compacts at +//! exit: what it keeps is the present and the marks. +const Sessions = @This(); +const std = @import("std"); +const xitdb = @import("xitdb"); +const abi = @import("abi"); + +pub const Disk = xitdb.Database(.buffered_file, u64); +pub const Memory = xitdb.Database(.memory, u64); + +/// `file` for every run but two: `memory` for a scripted run from fresh +/// state (time travel, nothing on disk), `off` for an export. +pub const Mode = enum { off, memory, file }; +pub const Why = enum(u64) { start, periodic, jump, before_reload, after_reload, mark, exit, import }; + +/// Pending frames are committed once they span a second of session time +/// (less a millisecond: 60 ticks of a 60 Hz clock that rounds down are one). +pub const commit_ns: u64 = std.time.ns_per_s - std.time.ns_per_ms; +/// A keyframe at least this often while the State changes. +pub const periodic_ns: u64 = 60 * std.time.ns_per_s; +/// Mark and clip names: no spaces, at most this long. +pub const max_name = 32; +/// A memory database (a scripted run's) stops recording at this size: ten +/// hours or more of play at 60 Hz. What it holds stays readable. +pub const memory_max: u64 = 256 << 20; +/// The most a save's file holds (State is at most half of it, +/// `abi.layout.max_state`, so history has room). +pub const max_save: u64 = 64 << 20; + +pub const K = enum(u64) { version = 1, sessions, marks, clips, started, ended, from, stretches, batches, keys, frames, frame, build, source, layout, batch, stretch, why, time, state, line, name, session, key, start, end, pages, size, written }; +fn key(k: K) u64 { + return @intFromEnum(k); +} + +pub const Stretch = struct { frame: u64 = 0, build: u64, source: []const u8, layout: []const u8 }; + +/// A keyframe's page: the OS page, the grain of sharing. +pub const page = 4096; + +/// A keyframe: `session`'s keyframe `key`. +const At = struct { session: u64, key: u64 }; + +/// A keyframe queued: the pages of its State that differ from keyframe +/// `from`'s (`indices`, ascending; their bytes one after the other in +/// `pages`, `page` apart). +const Keyframe = struct { frame: u64, batch: u64, stretch: u64, why: Why, time: u64, size: u64, from: ?At, indices: []u64, pages: []u8, line: []u8 }; + +gpa: std.mem.Allocator, +/// Files and the wall clock; a memory database runs on `std.Io.failing`. +io: std.Io, +db: union(Mode) { off, memory: Memory, file: Disk } = .off, +file: ?std.Io.File = null, +/// The database's write buffer, an allocation of its own: the database +/// points at it. +buffer: ?*std.Io.Writer.Allocating = null, +/// The database's path (file mode), owned. +path: []const u8 = "", +/// This run's index in `sessions`. +session: u64 = 0, +/// Frames logged, committed or not, and committed. +frames: u64 = 0, +committed: u64 = 0, +/// This session's committed batches, its stretches and its keyframes +/// (queued ones too). +batches: u64 = 0, +stretches: u64 = 0, +keys: u64 = 0, +/// The frames since the last commit, as the batch they become. +batch: std.ArrayList(u8) = .empty, +/// The first pending frame's tick. +batch_time: u64 = 0, +/// The first and latest event time logged: times never go back. +first_time: u64 = 0, +last_time: u64 = 0, +/// The last keyframe's time and stretch. +key_time: u64 = 0, +key_stretch: u64 = 0, +/// The keyframe the next one starts from (the last one, or the one a run +/// started from), and the hashes of its pages. +last: ?At = null, +hashes: []u64 = &.{}, +queued: std.ArrayList(Keyframe) = .empty, +/// The clip being recorded, an index into `clips`. +recording: ?u64 = null, +/// State may have changed other than by a logged live frame (a replay ran): +/// the next live frame starts with a keyframe. +away: bool = false, +/// A memory database reached `memory_max`: nothing more is logged. +full: bool = false, +/// The console line that last changed State, for the next keyframe. +line: [256]u8 = undefined, +line_len: usize = 0, +/// Keep history, up to `max_save`; else only the present and the marks. +history: bool = true, + +/// Opens the database at `path` (file mode: created with its directory when +/// missing; one a crash or power loss tore is opened at its last whole +/// commit (`mend`); one still refused is renamed aside, never deleted) and +/// starts this run's session in it. +pub fn open(gpa: std.mem.Allocator, io: std.Io, want: Mode, path: []const u8, first: Stretch, started: u64) !Sessions { + var s: Sessions = .{ .gpa = gpa, .io = io }; + if (want == .off) return s; + errdefer s.deinit(); + const buffer = try gpa.create(std.Io.Writer.Allocating); + buffer.* = .init(gpa); + s.buffer = buffer; + switch (want) { + .off => unreachable, + .memory => s.db = .{ .memory = try Memory.init(std.Io.failing, .{ .buffer = buffer, .file_id = 0, .max_size = memory_max }) }, + .file => { + s.path = try gpa.dupe(u8, path); + const cwd = std.Io.Dir.cwd(); + if (std.fs.path.dirname(path)) |dir| try cwd.createDirPath(io, dir); + var aside = false; + while (true) { + const file = try cwd.createFile(io, path, .{ .read = true, .truncate = false }); + if (Disk.init(io, .{ .file = file, .buffer = buffer, .fsync = false })) |db| { + s.db = .{ .file = db }; + s.file = file; + break; + } else |err| { + switch (err) { + error.InvalidDatabase, error.InvalidVersion, error.InvalidHashSize, error.TruncatedDatabase, error.InvalidEnumTag, error.EndOfStream => {}, + else => { + file.close(io); + return err; + }, + } + if (mend(io, file, buffer)) |db| { + s.db = .{ .file = db }; + s.file = file; + std.log.warn("{s} was torn after its last whole commit: opened at it", .{path}); + break; + } + file.close(io); + if (aside) return err; + aside = true; + var buf: [std.fs.max_path_bytes + 32]u8 = undefined; + const to = try std.fmt.bufPrint(&buf, "{s}.broken.{d}", .{ path, started / std.time.ns_per_s }); + try cwd.rename(path, cwd, to, io); + std.log.warn("{s} is no sessions database this host reads: moved to {s}", .{ path, to }); + } + } + }, + } + s.session = try s.count(.sessions); + try s.commit(.{ .open = .{ .started = started, .first = first } }); + s.stretches = 1; + return s; +} + +/// The database at `path`, read only and no session started in it: for +/// its `present` with no run of the sketch (`fs` with the sketch closed). +pub fn openRead(gpa: std.mem.Allocator, io: std.Io, path: []const u8) !Sessions { + var s: Sessions = .{ .gpa = gpa, .io = io }; + errdefer s.deinit(); + s.file = try std.Io.Dir.cwd().openFile(io, path, .{}); + s.buffer = try gpa.create(std.Io.Writer.Allocating); + s.buffer.?.* = .init(gpa); + s.db = .{ .file = try Disk.init(io, .{ .file = s.file.?, .buffer = s.buffer.?, .fsync = false }) }; + s.session = try s.count(.sessions); + return s; +} + +pub fn deinit(s: *Sessions) void { + s.dropQueued(); + s.queued.deinit(s.gpa); + s.batch.deinit(s.gpa); + s.gpa.free(s.hashes); + if (s.file) |f| f.close(s.io); + if (s.buffer) |b| { + b.deinit(); + s.gpa.destroy(b); + } + s.gpa.free(s.path); + s.* = .{ .gpa = s.gpa, .io = s.io }; +} + +/// The last frames, an exit keyframe of `state`, and the session's end, in +/// one transaction synced to disk; then closed. Without history the file is +/// compacted, so the frames it dropped are gone from the disk too. +pub fn close(s: *Sessions, state: []const u8, ended: u64) void { + defer s.deinit(); + if (s.db == .off) return; + s.stopClip() catch {}; + _ = s.keyframe(.exit, state) catch {}; + s.commitSynced(.{ .end = ended }) catch |err| std.log.warn("sessions: last commit failed: {t}", .{err}); + if (s.db == .file and !s.history) s.compact() catch |err| std.log.warn("sessions: not compacted: {t}", .{err}); +} + +/// Writes what the database holds now, without the nodes commits left +/// behind, to `<path>.new`, synced, renames it over the file, and goes on +/// in it. +fn compact(s: *Sessions) !void { + const cwd = std.Io.Dir.cwd(); + var buf: [std.fs.max_path_bytes + 8]u8 = undefined; + const new = try std.fmt.bufPrint(&buf, "{s}.new", .{s.path}); + const file = try cwd.createFile(s.io, new, .{ .read = true, .truncate = true }); + errdefer { + file.close(s.io); + cwd.deleteFile(s.io, new) catch {}; + } + const buffer = try s.gpa.create(std.Io.Writer.Allocating); + buffer.* = .init(s.gpa); + errdefer { + buffer.deinit(); + s.gpa.destroy(buffer); + } + var offsets: std.AutoHashMap(u64, u64) = .init(s.gpa); + defer offsets.deinit(); + var db = try s.db.file.compact(.buffered_file, .{ .file = file, .buffer = buffer, .fsync = true }, &offsets); + db.core.file.fsync = false; + try cwd.rename(new, cwd, s.path, s.io); + s.file.?.close(s.io); + s.buffer.?.deinit(); + s.gpa.destroy(s.buffer.?); + s.file = file; + s.buffer = buffer; + s.db = .{ .file = db }; +} + +/// Drops history oldest first, then compacts the file: until its keyframes' +/// pages and frames are under three quarters of `max_save`, or, without +/// history, all it may (compacted only past `max_save`; `close` does it +/// at exit). A few rounds at most: what a dropped keyframe wrote may still +/// be shared by one kept, so a round can free less than it counted. +fn trim(s: *Sessions) !void { + const goal = max_save / 4 * 3; + var keep: u64 = if (s.history) goal else 0; + for (0..4) |_| { + var dropped: u64 = 0; + var left: u64 = 0; + try s.db.file.runContext(Trim(Disk){ .s = s, .keep = keep, .dropped = &dropped, .left = &left }); + var size = try s.file.?.length(s.io); + if (!s.history and size <= max_save) return; + try s.compact(); + size = try s.file.?.length(s.io); + if (size <= goal or dropped == 0) return; + // What the count leaves out (lists, maps, stretches) is the rest. + keep = left -| (size - goal); + } +} + +/// A keyframe's pages or a batch of frames, for `trim`. +const Item = struct { session: u64, index: u64, frame: u64, bytes: u64, batch: bool, keep: bool }; + +/// The transaction of `trim`: drops a keyframe by removing its pages (its +/// fields stay, so keyframe numbers and searches by frame still work) and +/// a batch by emptying its slot, oldest first, until what is left counts at +/// most `keep` bytes; a session left with nothing loses its keyframes, +/// batches and stretches. Never the present (the newest keyframe and the +/// batches from its own on), the keyframe of the newest mark or clip of a +/// name, or a batch holding a frame of such a clip. +fn Trim(comptime DB: type) type { + return struct { + s: *Sessions, + keep: u64, + /// Bytes dropped, and counted after. + dropped: *u64, + left: *u64, + + const R = Read(DB); + const Map = DB.HashMap(.read_write); + const List = DB.ArrayList(.read_write); + + pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { + var arena: std.heap.ArenaAllocator = .init(ctx.s.gpa); + defer arena.deinit(); + const a = arena.allocator(); + const root = try Map.init(cursor.*); + const all = try R.list(root.readOnly(), .sessions) orelse return; + const n = try all.count(); + var pinned: std.AutoHashMapUnmanaged(At, void) = .empty; + const Clip = struct { session: u64, start: u64, end: u64 }; + var clips: std.ArrayList(Clip) = .empty; + for ([_]K{ .marks, .clips }) |which| { + const list = try R.list(root.readOnly(), which) orelse continue; + var names: std.StringHashMapUnmanaged(void) = .empty; + var i = try list.count(); + while (i > 0) { + i -= 1; + const e = try R.at(list, i); + var buf: [max_name]u8 = undefined; + const name = (try R.field(e, .name)).readBytes(&buf) catch return error.CorruptSession; + if ((try names.getOrPut(a, try a.dupe(u8, name))).found_existing) continue; + const at: At = .{ .session = try R.uint(e, .session), .key = try R.uint(e, .key) }; + try pinned.put(a, at, {}); + const end = if (which == .clips) try R.uint(e, .end) else continue; + try clips.append(a, .{ .session = at.session, .start = try R.uint(e, .start), .end = if (end == 0) std.math.maxInt(u64) else end }); + } + } + // The present: the newest keyframe, and the batches from its own on. + var now: At = undefined; + var now_batch: u64 = 0; + var i = n; + while (true) { + if (i == 0) return; + i -= 1; + const keys = try R.list(try R.at(all, i), .keys) orelse continue; + const n_keys = try keys.count(); + if (n_keys == 0) continue; + now = .{ .session = i, .key = n_keys - 1 }; + now_batch = try R.uint(try R.at(keys, n_keys - 1), .batch); + break; + } + try pinned.put(a, now, {}); + // What is not dropped yet, each session's in frame order. + var items: std.ArrayList(Item) = .empty; + var total: u64 = 0; + for (0..n) |si| { + const ses = try R.at(all, si); + const start = items.items.len; + if (try R.list(ses, .keys)) |keys| for (0..try keys.count()) |k| { + const e = try R.at(keys, k); + if (try e.getCursor(key(.pages)) == null) continue; + const written = if (try e.getCursor(key(.written))) |w| try w.readUint() else 0; + try items.append(a, .{ .session = si, .index = k, .frame = try R.uint(e, .frame), .bytes = written, .batch = false, .keep = pinned.contains(.{ .session = si, .key = k }) }); + }; + if (try R.list(ses, .batches)) |batches| for (0..try batches.count()) |b| { + var c = try batches.getCursor(@intCast(b)) orelse continue; + if ((try c.slot()).tag == .none) continue; + var buf: [8]u8 = undefined; + var reader = try c.reader(&buf); + var head: [8]u8 = undefined; + try reader.interface.readSliceAll(&head); + try items.append(a, .{ .session = si, .index = b, .frame = std.mem.readInt(u64, &head, .little), .bytes = reader.size, .batch = true, .keep = si == now.session and b >= now_batch }); + }; + const mine = items.items[start..]; + std.mem.sort(Item, mine, {}, struct { + fn lessThan(_: void, x: Item, y: Item) bool { + return x.frame < y.frame or (x.frame == y.frame and !x.batch and y.batch); + } + }.lessThan); + // A batch holds frames up to the next batch's first. + const frames = try R.uint(ses, .frames); + var end: u64 = std.math.maxInt(u64); + var j = mine.len; + while (j > 0) { + j -= 1; + const it = &mine[j]; + if (!it.batch) continue; + const last = @min(end, frames); + for (clips.items) |clip| if (clip.session == si and clip.start < last and it.frame < clip.end) { + it.keep = true; + }; + end = it.frame; + } + for (mine) |it| total += it.bytes; + } + // Dropped oldest first. + const sessions = try List.init(try root.putCursor(key(.sessions))); + var left: std.ArrayList(u64) = .empty; + try left.appendNTimes(a, 0, n); + for (items.items) |it| { + if (it.keep or total <= ctx.keep) { + left.items[it.session] += 1; + continue; + } + const ses = try Map.init(try sessions.putCursor(@intCast(it.session))); + if (it.batch) { + try (try List.init(try ses.putCursor(key(.batches)))).put(@intCast(it.index), .{ .slot = null }); + } else { + const k = try Map.init(try (try List.init(try ses.putCursor(key(.keys)))).putCursor(@intCast(it.index))); + _ = try k.remove(key(.pages)); + try k.put(key(.written), .{ .uint = 0 }); + } + total -= it.bytes; + ctx.dropped.* += it.bytes; + } + ctx.left.* = total; + for (left.items, 0..) |kept, si| { + if (kept > 0 or si >= now.session) continue; + var any = false; + var it = pinned.keyIterator(); + while (it.next()) |at| any = any or at.session == si; + if (any) continue; + const ses = try Map.init(try sessions.putCursor(@intCast(si))); + for ([_]K{ .keys, .batches, .stretches }) |k| _ = try ses.remove(key(k)); + } + } + }; +} + +/// The bytes of every session's batches and keyframes' pages (each written +/// once): what compaction keeps. +/// Reading them all is also what proves a mended file whole. +fn liveSize(db: *Disk) !u64 { + const R = Read(Disk); + const sessions = try R.list(try R.root(db), .sessions) orelse return 0; + var live: u64 = 0; + var it = try sessions.iterator(); + while (try it.next()) |c| { + const session = try R.Map.init(c); + if (try R.list(session, .batches)) |batches| { + var b = try batches.iterator(); + while (try b.next()) |blob| live += if ((try blob.slot()).tag == .none) 0 else try blob.count(); + } + if (try R.list(session, .keys)) |keys| { + var k = try keys.iterator(); + while (try k.next()) |key_cursor| { + const e = try R.Map.init(key_cursor); + live += if (try e.getCursor(key(.written))) |w| try w.readUint() else try (try R.field(e, .state)).count(); + } + } + } + return live; +} + +// xitdb's layout, as `mend` reads it (zig-pkg's xitdb src/lib.zig): the +// database header (its file id the last 8 of its 20 bytes, big endian), the +// root header (the root slot's 9 bytes, then the committed size's 8), and +// the trailer ending every commit (the root slot's 9 bytes, the commit id's +// 8, and their CRC-64/XZ's 8, with the file id and its own position). +const db_header_len = 20; +const root_header_end = db_header_len + 17; +const trailer_len = 25; +const hash_map_tag = 4; + +/// A file xitdb refused because a crash or power loss tore its last commit +/// (its root header written, the commit's bytes not all): opened at the last +/// commit before the tear whose trailer verifies and whose sessions read +/// whole, scanned back from the file's end, its root header put back at it. +/// Null, the file as it was, when none does. +fn mend(io: std.Io, file: std.Io.File, buffer: *std.Io.Writer.Allocating) ?Disk { + var head: [root_header_end]u8 = undefined; + if ((file.readPositionalAll(io, &head, 0) catch return null) != head.len or !std.mem.eql(u8, head[0..3], "xit")) return null; + const file_id = std.mem.readInt(u64, head[db_header_len - 8 .. db_header_len], .big); + var chunk: [64 << 10]u8 = undefined; + var end = file.length(io) catch return null; + while (end >= root_header_end + trailer_len) { + const start = @max(root_header_end, end - @min(end, chunk.len)); + const got = file.readPositionalAll(io, chunk[0 .. end - start], start) catch return null; + if (got != end - start) return null; + var i = got; + while (i >= trailer_len) : (i -= 1) { + const t = chunk[i - trailer_len ..][0..trailer_len]; + const at = start + i - trailer_len; + // The root is a map, at a slot written before its trailer. + if (t[0] & 0x7f != hash_map_tag or std.mem.readInt(u64, t[1..9], .big) >= at) continue; + var sum: [33]u8 = undefined; + std.mem.writeInt(u64, sum[0..8], file_id, .big); + std.mem.writeInt(u64, sum[8..16], at, .big); + @memcpy(sum[16..33], t[0..17]); + if (std.hash.crc.@"CRC-64/XZ".hash(&sum) != std.mem.readInt(u64, t[17..25], .big)) continue; + var root: [17]u8 = undefined; + @memcpy(root[0..9], t[0..9]); + std.mem.writeInt(u64, root[9..17], at + trailer_len, .big); + file.writePositionalAll(io, &root, db_header_len) catch return null; + var db = Disk.init(io, .{ .file = file, .buffer = buffer, .fsync = false }) catch continue; + _ = liveSize(&db) catch continue; + file.sync(io) catch {}; + return db; + } + if (start == root_header_end) break; + end = start + trailer_len - 1; + } + file.writePositionalAll(io, head[db_header_len..], db_header_len) catch {}; + return null; +} + +pub fn mode(s: *const Sessions) Mode { + return s.db; +} + +/// Remembers the console line that changed State, for the next keyframe. +pub fn noteLine(s: *Sessions, line: []const u8) void { + s.line_len = @min(line.len, s.line.len); + @memcpy(s.line[0..s.line_len], line[0..s.line_len]); +} + +/// One live frame, its tick last, before `update` is fed it: its times are +/// made never to go back (what `update` then sees is what a replay will), +/// and it is logged, after a keyframe of `state` when `why` says so or the +/// last is a minute old. +pub fn frame(s: *Sessions, one: []abi.Event, state: []const u8, why: ?Why) !void { + for (one) |*e| { + e.time = @max(e.time, s.last_time); + s.last_time = e.time; + } + if (s.db == .off or s.full) return; + const tick = one[one.len - 1].time; + if (s.batch.items.len > 0 and tick -| s.batch_time >= commit_ns) try s.commit(.none); + if (why) |w| { + _ = try s.keyframe(w, state); + } else if (tick -| s.key_time >= periodic_ns) _ = try s.keyframe(.periodic, state); + if (s.frames == 0) s.first_time = tick; + if (s.batch.items.len == 0) { + try s.batch.appendSlice(s.gpa, std.mem.asBytes(&std.mem.nativeToLittle(u64, s.frames))); + s.batch_time = tick; + } + try s.batch.appendSlice(s.gpa, std.mem.sliceAsBytes(one)); + s.frames += 1; +} + +/// Queues a keyframe of `state` before the next frame, and returns its +/// index; an unchanged State is the last keyframe again (but for a +/// `before_reload` one: the last word in the old layout). Only the pages +/// whose hash differs from the last keyframe's are copied, and written. +pub fn keyframe(s: *Sessions, why: Why, state: []const u8) !u64 { + if (s.db == .off) return error.NotRecording; + defer s.line_len = 0; + const hashes = try s.gpa.alloc(u64, std.math.divCeil(usize, state.len, page) catch unreachable); + errdefer s.gpa.free(hashes); + var changed: std.ArrayList(u64) = .empty; + defer changed.deinit(s.gpa); + for (hashes, 0..) |*h, i| { + h.* = std.hash.Wyhash.hash(0, state[i * page .. @min(state.len, (i + 1) * page)]); + if (i >= s.hashes.len or s.hashes[i] != h.*) try changed.append(s.gpa, i); + } + if (s.keys > 0 and why != .before_reload and changed.items.len == 0 and hashes.len == s.hashes.len and s.key_stretch == s.stretches - 1) { + s.gpa.free(hashes); + return s.keys - 1; + } + const pages = try s.gpa.alloc(u8, changed.items.len * page); + errdefer s.gpa.free(pages); + for (changed.items, 0..) |i, k| { + const bytes = state[i * page .. @min(state.len, (i + 1) * page)]; + @memcpy(pages[k * page ..][0..bytes.len], bytes); + } + const indices = try changed.toOwnedSlice(s.gpa); + errdefer s.gpa.free(indices); + const line_copy = try s.gpa.dupe(u8, s.line[0..s.line_len]); + errdefer s.gpa.free(line_copy); + try s.queued.append(s.gpa, .{ .frame = s.frames, .batch = s.batches, .stretch = s.stretches - 1, .why = why, .time = s.last_time, .size = state.len, .from = s.last, .indices = indices, .pages = pages, .line = line_copy }); + s.gpa.free(s.hashes); + s.hashes = hashes; + s.last = .{ .session = s.session, .key = s.keys }; + s.key_time = s.last_time; + s.key_stretch = s.stretches - 1; + s.keys += 1; + return s.keys - 1; +} + +/// A new stretch from the next frame: a reload's build, source and layout. +pub fn stretch(s: *Sessions, st: Stretch) !void { + if (s.db == .off) return; + var with = st; + with.frame = s.frames; + try s.commit(.{ .stretch = with }); + s.stretches += 1; +} + +/// Mark `name`: a keyframe of `state` (`why`: `.mark`, or `.import`), +/// synced to disk. Returns the keyframe's index. A mark of a new name needs +/// room for a whole State under `max_save`, history dropped to make it +/// (`trim`): without, it is refused (`error.SaveFull`). A mark over an +/// existing name lets the older one go with history. +pub fn mark(s: *Sessions, name: []const u8, why: Why, state: []const u8) !u64 { + if (s.db == .file and try s.file.?.length(s.io) + state.len > max_save and try s.findMark(name) == null) { + try s.trim(); + if (try s.file.?.length(s.io) + state.len > max_save) return error.SaveFull; + } + const k = try s.keyframe(why, state); + try s.commitSynced(.{ .mark = .{ .name = name, .key = k } }); + return k; +} + +/// Starts clip `name` at the next frame, played from keyframe `k`. +pub fn startClip(s: *Sessions, name: []const u8, k: u64) !void { + const index = try s.count(.clips); + try s.commit(.{ .clip = .{ .name = name, .key = k, .start = s.frames } }); + s.recording = index; +} + +/// Ends the clip being recorded after the frames logged so far. +pub fn stopClip(s: *Sessions) !void { + const index = s.recording orelse return; + try s.commit(.{ .clip_end = .{ .index = index, .end = s.frames } }); + s.recording = null; +} + +fn dropQueued(s: *Sessions) void { + for (s.queued.items) |k| { + s.gpa.free(k.indices); + s.gpa.free(k.pages); + s.gpa.free(k.line); + } + s.queued.clearRetainingCapacity(); +} + +const Extra = union(enum) { + none, + open: struct { started: u64, first: Stretch }, + stretch: Stretch, + mark: struct { name: []const u8, key: u64 }, + clip: struct { name: []const u8, key: u64, start: u64 }, + clip_end: struct { index: u64, end: u64 }, + end: u64, +}; + +fn commitSynced(s: *Sessions, extra: Extra) !void { + if (s.db == .file) s.db.file.core.file.fsync = true; + defer if (s.db == .file) { + s.db.file.core.file.fsync = false; + }; + try s.commit(extra); +} + +/// One transaction: the pending frames, the queued keyframes and `extra`. +pub fn commit(s: *Sessions, extra: Extra) !void { + switch (s.db) { + .off => return, + inline .memory, .file => |*db| db.runContext(Commit(@TypeOf(db.*)){ .s = s, .extra = extra }) catch |err| { + // A memory database that cannot grow (`memory_max`, or no + // memory left) stops recording, once. + if (s.db == .memory and !s.full) { + s.full = true; + s.batch.clearRetainingCapacity(); + s.dropQueued(); + std.log.warn("sessions: the memory database cannot grow ({t}; {d} MiB at most): no more frames recorded", .{ err, memory_max >> 20 }); + } + return err; + }, + } + if (s.batch.items.len > 0) { + s.batches += 1; + s.committed = s.frames; + s.batch.clearRetainingCapacity(); + } + const keyed = s.queued.items.len > 0; + s.dropQueued(); + if (s.db != .file) return; + if ((try s.file.?.length(s.io)) > max_save or (!s.history and keyed)) s.trim() catch |err| std.log.warn("sessions: history not trimmed: {t}", .{err}); +} + +fn Commit(comptime DB: type) type { + return struct { + s: *Sessions, + extra: Extra, + + const Map = DB.HashMap(.read_write); + const List = DB.ArrayList(.read_write); + + pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { + const s = ctx.s; + const root = try Map.init(cursor.*); + const sessions = try List.init(try root.putCursor(key(.sessions))); + if (ctx.extra == .open) { + const o = ctx.extra.open; + try root.put(key(.version), .{ .uint = 1 }); + const session = try Map.init(try sessions.appendCursor()); + try session.put(key(.started), .{ .uint = o.started }); + try session.put(key(.ended), .{ .uint = 0 }); + try session.put(key(.from), .{ .bytes = "" }); + try session.put(key(.frames), .{ .uint = 0 }); + try putStretch(try List.init(try session.putCursor(key(.stretches))), o.first); + return; + } + const session = try Map.init(try sessions.putCursor(@intCast(s.session))); + if (s.batch.items.len > 0) { + try (try List.init(try session.putCursor(key(.batches)))).append(.{ .bytes = s.batch.items }); + try session.put(key(.frames), .{ .uint = s.frames }); + } + if (s.queued.items.len > 0) { + const keys = try List.init(try session.putCursor(key(.keys))); + for (s.queued.items) |q| { + const k = try Map.init(try keys.appendCursor()); + try k.put(key(.frame), .{ .uint = q.frame }); + try k.put(key(.batch), .{ .uint = q.batch }); + try k.put(key(.stretch), .{ .uint = q.stretch }); + try k.put(key(.why), .{ .uint = @intFromEnum(q.why) }); + try k.put(key(.time), .{ .uint = q.time }); + try k.put(key(.size), .{ .uint = q.size }); + try k.put(key(.line), .{ .bytes = q.line }); + // From the keyframe before's pages, copied as the + // transaction writes on (it may be in it too). + var from = try k.putCursor(key(.pages)); + if (q.from) |at| if (try pagesSlot(root, at)) |slot| { + try from.write(.{ .slot = slot }); + try from.freeze(); + }; + const pages = try List.init(from); + const had = try pages.count(); + var written: u64 = 0; + for (q.indices, 0..) |i, j| { + const bytes = q.pages[j * page ..][0..@min(page, q.size - i * page)]; + const zero = std.mem.allEqual(u8, bytes, 0); + if (!zero) written += bytes.len; + const data: DB.WriteableData = if (zero) .{ .slot = null } else .{ .bytes = bytes }; + if (i < had) try pages.put(@intCast(i), data) else try pages.append(data); + } + const n = std.math.divCeil(u64, q.size, page) catch unreachable; + if (had > n) try pages.slice(n); + try k.put(key(.written), .{ .uint = written }); + } + } + switch (ctx.extra) { + .none, .open => {}, + .stretch => |st| try putStretch(try List.init(try session.putCursor(key(.stretches))), st), + .mark => |m| { + const item = try Map.init(try (try List.init(try root.putCursor(key(.marks)))).appendCursor()); + try item.put(key(.name), .{ .bytes = m.name }); + try item.put(key(.session), .{ .uint = s.session }); + try item.put(key(.key), .{ .uint = m.key }); + }, + .clip => |c| { + const item = try Map.init(try (try List.init(try root.putCursor(key(.clips)))).appendCursor()); + try item.put(key(.name), .{ .bytes = c.name }); + try item.put(key(.session), .{ .uint = s.session }); + try item.put(key(.key), .{ .uint = c.key }); + try item.put(key(.start), .{ .uint = c.start }); + try item.put(key(.end), .{ .uint = 0 }); + }, + .clip_end => |c| { + const item = try Map.init(try (try List.init(try root.putCursor(key(.clips)))).putCursor(@intCast(c.index))); + try item.put(key(.end), .{ .uint = c.end }); + }, + .end => |t| try session.put(key(.ended), .{ .uint = t }), + } + } + + /// The slot of keyframe `at`'s pages; null for one from before pages. + fn pagesSlot(root: Map, at: At) !?xitdb.Slot { + const R = Read(DB); + const ses = try R.at(try R.list(root.readOnly(), .sessions) orelse return null, at.session); + return (try R.at(try R.list(ses, .keys) orelse return null, at.key)).getSlot(key(.pages)); + } + + fn putStretch(list: List, st: Stretch) !void { + const item = try Map.init(try list.appendCursor()); + try item.put(key(.frame), .{ .uint = st.frame }); + try item.put(key(.build), .{ .uint = st.build }); + try item.put(key(.source), .{ .bytes = st.source }); + try item.put(key(.layout), .{ .bytes = st.layout }); + } + }; +} + +// --------------------------------------------------------------------------- +// Reading + +/// A mark (`start` and `end` 0) or a clip. +pub const Entry = struct { + session: u64, + key: u64, + start: u64 = 0, + end: u64 = 0, + bytes: [max_name]u8 = undefined, + len: usize = 0, + + pub fn name(e: *const Entry) []const u8 { + return e.bytes[0..e.len]; + } +}; +/// A keyframe, its bytes `gpa`'s (`free`). +pub const Loaded = struct { + /// Stored in pages (not whole, from before them). + paged: bool = true, + frame: u64, + batch: u64, + why: Why, + time: u64, + state: []const u8, + line: []const u8, + /// Its stretch: the layout of `state`, and the build that wrote it. + stretch: u64, + build: u64, + source: []const u8, + layout: []const u8, + + pub fn free(l: Loaded, gpa: std.mem.Allocator) void { + for ([_][]const u8{ l.state, l.line, l.source, l.layout }) |b| gpa.free(b); + } +}; + +pub fn markAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.marks, i); +} +pub fn clipAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.clips, i); +} +/// The newest mark named `name`. +pub fn findMark(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.marks, name); +} +/// The newest clip named `name`. +pub fn findClip(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.clips, name); +} + +fn findNamed(s: *Sessions, which: K, name: []const u8) !?Entry { + var i = try s.count(which); + while (i > 0) { + i -= 1; + const e = try s.entryAt(which, i); + if (std.mem.eql(u8, e.name(), name)) return e; + } + return null; +} + +fn entryAt(s: *Sessions, which: K, i: u64) !Entry { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + const e = try R.at(try R.list(try R.root(db), which) orelse return error.NoSuchEntry, i); + var out: Entry = .{ .session = try R.uint(e, .session), .key = try R.uint(e, .key) }; + out.len = ((try R.field(e, .name)).readBytes(&out.bytes) catch return error.CorruptSession).len; + if (which == .clips) { + out.start = try R.uint(e, .start); + out.end = try R.uint(e, .end); + } + return out; + }, + } +} + +/// Entries in the root list `which`, or sessions. +pub fn count(s: *Sessions, which: K) !u64 { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + return if (try R.list(try R.root(db), which)) |l| try l.count() else 0; + }, + } +} + +/// Keyframe `k` of `session`, with its stretch. +pub fn load(s: *Sessions, session: u64, k: u64) !Loaded { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + const ses = try R.session(db, session); + const e = try R.at(try R.list(ses, .keys) orelse return error.NoSuchKeyframe, k); + const st = try R.uint(e, .stretch); + const stretch_entry = try R.at(try R.list(ses, .stretches) orelse return error.CorruptSession, st); + const why = std.enums.fromInt(Why, try R.uint(e, .why)) orelse return error.CorruptSession; + const pages = try R.list(e, .pages); + const state = if (pages) |list| try R.pages(list, try R.uint(e, .size), s.gpa) else if (try e.getCursor(key(.state))) |whole| try whole.readBytesAlloc(s.gpa, null) else return error.HistoryDropped; + errdefer s.gpa.free(state); + const line = try R.bytes(e, .line, s.gpa); + errdefer s.gpa.free(line); + const source = try R.bytes(stretch_entry, .source, s.gpa); + errdefer s.gpa.free(source); + return .{ + .paged = pages != null, + .frame = try R.uint(e, .frame), + .batch = try R.uint(e, .batch), + .why = why, + .time = try R.uint(e, .time), + .state = state, + .line = line, + .stretch = st, + .build = try R.uint(stretch_entry, .build), + .source = source, + .layout = try R.bytes(stretch_entry, .layout, s.gpa), + }; + }, + } +} + +/// The State a run starts from, the present: the newest keyframe of the +/// sessions before this one, with the frames logged after it appended to +/// `out` (replayed, they bring it to where that run stopped). Null in a +/// new database. This run's first keyframe starts from its pages. +pub fn present(s: *Sessions, out: *std.ArrayList(abi.Event)) !?Loaded { + if (s.db == .off) return null; + var i = s.session; + while (i > 0) { + i -= 1; + const sum = try s.summary(i); + if (sum.keys == 0) continue; + const newest = try s.load(i, sum.keys - 1); + errdefer newest.free(s.gpa); + try s.events(i, newest.batch, newest.frame, sum.frames, out); + if (newest.paged) { + const hashes = try s.gpa.alloc(u64, std.math.divCeil(usize, newest.state.len, page) catch unreachable); + for (hashes, 0..) |*h, j| h.* = std.hash.Wyhash.hash(0, newest.state[j * page .. @min(newest.state.len, (j + 1) * page)]); + s.gpa.free(s.hashes); + s.hashes = hashes; + s.last = .{ .session = i, .key = sum.keys - 1 }; + } + return newest; + } + return null; +} + +/// Keyframe `k`'s frame, time and why, without its bytes. +pub fn keyAt(s: *Sessions, session: u64, k: u64) !struct { frame: u64, time: u64, why: Why } { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + const e = try R.at(try R.list(try R.session(db, session), .keys) orelse return error.NoSuchKeyframe, k); + return .{ .frame = try R.uint(e, .frame), .time = try R.uint(e, .time), .why = std.enums.fromInt(Why, try R.uint(e, .why)) orelse return error.CorruptSession }; + }, + } +} + +/// The newest keyframe of `session` whose `by` (`.frame` or `.time`) is at +/// most `value`: a binary search. +pub fn keyBefore(s: *Sessions, session: u64, by: K, value: u64) !?u64 { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + const keys = try R.list(try R.session(db, session), .keys) orelse return null; + var lo: u64 = 0; + var hi = try keys.count(); + while (lo < hi) { + const mid = lo + (hi - lo) / 2; + if (try R.uint(try R.at(keys, mid), by) <= value) lo = mid + 1 else hi = mid; + } + return if (lo == 0) null else lo - 1; + }, + } +} + +/// Appends to `out` the events of frames `start..end` of `session`, read +/// from batch `from` on. +pub fn events(s: *Sessions, session: u64, from: u64, start: u64, end: u64, out: *std.ArrayList(abi.Event)) !void { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + const batches = try R.list(try R.session(db, session), .batches) orelse return; + const n = try batches.count(); + var i = from; + while (i < n) : (i += 1) { + const cursor = try batches.getCursor(@intCast(i)) orelse return error.CorruptSession; + if ((try cursor.slot()).tag == .none) return error.HistoryDropped; + const blob = try cursor.readBytesAlloc(s.gpa, null); + defer s.gpa.free(blob); + if (blob.len < 8 or (blob.len - 8) % @sizeOf(abi.Event) != 0) return error.CorruptSession; + var f = std.mem.readInt(u64, blob[0..8], .little); + if (f >= end) return; + var at: usize = 8; + while (at < blob.len) : (at += @sizeOf(abi.Event)) { + var e: abi.Event = undefined; + @memcpy(std.mem.asBytes(&e), blob[at..][0..@sizeOf(abi.Event)]); + if (f >= start) try out.append(s.gpa, e); + if (e.kind == .tick) f += 1; + if (f >= end) return; + } + } + }, + } +} + +/// Session `i`'s summary. +pub const Summary = struct { + started: u64, + ended: u64, + frames: u64, + stretches: u64, + keys: u64, + /// Where it came from ("import <file name>"), cut short. + from_bytes: [48]u8 = undefined, + from_len: usize = 0, + + pub fn from(m: *const Summary) []const u8 { + return m.from_bytes[0..m.from_len]; + } +}; + +pub fn summary(s: *Sessions, i: u64) !Summary { + switch (s.db) { + .off => return error.NotRecording, + inline .memory, .file => |*db| { + const R = Read(@TypeOf(db.*)); + const ses = try R.session(db, i); + var m: Summary = .{ + .started = try R.uint(ses, .started), + .ended = try R.uint(ses, .ended), + .frames = try R.uint(ses, .frames), + .stretches = if (try R.list(ses, .stretches)) |l| try l.count() else 0, + .keys = if (try R.list(ses, .keys)) |l| try l.count() else 0, + }; + const from = try R.bytes(ses, .from, s.gpa); + defer s.gpa.free(from); + m.from_len = @min(from.len, m.from_bytes.len); + @memcpy(m.from_bytes[0..m.from_len], from[0..m.from_len]); + return m; + }, + } +} + +// --------------------------------------------------------------------------- +// Export and import + +/// Writes session `i`, with the marks and clips in it (now of session 0), to +/// a new database at `path` (written beside it, synced, then renamed): a +/// playtest's sessions to send, which `import` reads. +pub fn exportSession(s: *Sessions, i: u64, path: []const u8) !void { + if (s.db == .off) return error.NotRecording; + if (i == s.session) try s.commit(.none); + if (i >= try s.count(.sessions)) return error.NoSuchSession; + const cwd = std.Io.Dir.cwd(); + var buf: [std.fs.max_path_bytes + 8]u8 = undefined; + const tmp = try std.fmt.bufPrint(&buf, "{s}.tmp", .{path}); + const file = try cwd.createFile(s.io, tmp, .{ .read = true, .truncate = true }); + errdefer cwd.deleteFile(s.io, tmp) catch {}; + defer file.close(s.io); + var buffer: std.Io.Writer.Allocating = .init(s.gpa); + defer buffer.deinit(); + var out = try Disk.init(s.io, .{ .file = file, .buffer = &buffer, .fsync = true }); + switch (s.db) { + .off => unreachable, + inline .memory, .file => |*db| try out.runContext(Transfer(@TypeOf(db.*), Disk){ .gpa = s.gpa, .from = db, .only = i, .label = null }), + } + try cwd.rename(tmp, cwd, path, s.io); +} + +/// Appends the sessions of the database at `path` (an `exportSession`'s) to +/// this one, their `from` "import <file name>", with their marks and clips, +/// named `<name>@<file name>` (cut to `max_name`). Returns the first one's +/// index. +pub fn import(s: *Sessions, path: []const u8) !u64 { + if (s.db == .off) return error.NotRecording; + const file = try std.Io.Dir.cwd().openFile(s.io, path, .{}); + defer file.close(s.io); + if (try file.length(s.io) == 0) return error.NotASessionsDatabase; + var buffer: std.Io.Writer.Allocating = .init(s.gpa); + defer buffer.deinit(); + var in = try Disk.init(s.io, .{ .file = file, .buffer = &buffer, .fsync = false }); + const first = try s.count(.sessions); + switch (s.db) { + .off => unreachable, + inline .memory, .file => |*db| try db.runContext(Transfer(Disk, @TypeOf(db.*)){ .gpa = s.gpa, .from = &in, .only = null, .label = std.fs.path.basename(path) }), + } + return first; +} + +/// A transaction appending database `from`'s sessions (`only` one, else +/// all) to the one it runs in, with the marks and clips pointing into them; +/// `label`, an import's file name, goes in their `from` and names. +fn Transfer(comptime From: type, comptime To: type) type { + return struct { + gpa: std.mem.Allocator, + from: *From, + only: ?u64, + label: ?[]const u8, + + const R = Read(From); + const Map = To.HashMap(.read_write); + const List = To.ArrayList(.read_write); + + pub fn run(ctx: @This(), cursor: *To.Cursor(.read_write)) !void { + var copied: std.AutoHashMap(u64, xitdb.Slot) = .init(ctx.gpa); + defer copied.deinit(); + const src = try R.root(ctx.from); + if (try R.uint(src, .version) != 1) return error.NotASessionsDatabase; + const all = try R.list(src, .sessions) orelse return error.NotASessionsDatabase; + const root = try Map.init(cursor.*); + try root.put(key(.version), .{ .uint = 1 }); + const sessions = try List.init(try root.putCursor(key(.sessions))); + const base = try sessions.count(); + var buf: [std.fs.max_path_bytes + 8]u8 = undefined; + for (0..try all.count()) |i| { + if (ctx.only) |o| if (o != i) continue; + var to = try sessions.appendCursor(); + try copy(From, To, ctx.gpa, try all.getCursor(@intCast(i)) orelse return error.CorruptSession, &to, &copied); + if (ctx.label) |l| try (try Map.init(to)).put(key(.from), .{ .bytes = try std.fmt.bufPrint(&buf, "import {s}", .{l}) }); + } + for ([_]K{ .marks, .clips }) |which| { + const entries = try R.list(src, which) orelse continue; + const list = try List.init(try root.putCursor(key(which))); + var it = try entries.iterator(); + while (try it.next()) |e| { + const session = try R.uint(try R.Map.init(e), .session); + if (ctx.only) |o| if (o != session) continue; + var to = try list.appendCursor(); + try copy(From, To, ctx.gpa, e, &to, &copied); + const item = try Map.init(to); + try item.put(key(.session), .{ .uint = base + if (ctx.only == null) session else 0 }); + const l = ctx.label orelse continue; + var name: [max_name]u8 = undefined; + const old = (try R.field(try R.Map.init(e), .name)).readBytes(&name) catch return error.CorruptSession; + const new = std.fmt.bufPrint(&buf, "{s}@{s}", .{ old, l }) catch unreachable; + try item.put(key(.name), .{ .bytes = new[0..@min(new.len, max_name)] }); + } + } + } + }; +} + +/// Copies the value at `from` (a uint, bytes, or a list or map of them) to +/// `to`. Bytes `copied` already (a page keyframes share) are written once: +/// it maps their place in `from` to their slot in `to`. +fn copy(comptime From: type, comptime To: type, gpa: std.mem.Allocator, from: From.Cursor(.read_only), to: *To.Cursor(.read_write), copied: *std.AutoHashMap(u64, xitdb.Slot)) anyerror!void { + const slot = try from.slot(); + switch (slot.tag) { + .none => try to.write(.{ .slot = null }), + .uint => try to.write(.{ .uint = try from.readUint() }), + .bytes, .short_bytes => { + if (slot.tag == .bytes) if (copied.get(slot.value)) |done| return to.write(.{ .slot = done }); + const bytes = try from.readBytesAlloc(gpa, null); + defer gpa.free(bytes); + try to.write(.{ .bytes = bytes }); + if (slot.tag == .bytes) try copied.put(slot.value, try to.slot()); + }, + .array_list => { + const list = try To.ArrayList(.read_write).init(to.*); + var it = try from.iterator(); + while (try it.next()) |c| { + var next = try list.appendCursor(); + try copy(From, To, gpa, c, &next, copied); + } + }, + .hash_map => { + const map = try To.HashMap(.read_write).init(to.*); + var it = try from.iterator(); + while (try it.next()) |c| { + const kv = try c.readKeyValuePair(); + var next = try map.putCursor(kv.hash); + try copy(From, To, gpa, kv.value_cursor, &next, copied); + } + }, + else => return error.CorruptSession, + } +} + +fn Read(comptime DB: type) type { + return struct { + const Map = DB.HashMap(.read_only); + const List = DB.ArrayList(.read_only); + + fn root(db: *DB) !Map { + return Map.init(db.rootCursor().readOnly()); + } + fn session(db: *DB, i: u64) !Map { + return at(try list(try root(db), .sessions) orelse return error.NoSuchSession, i); + } + fn field(map: Map, k: K) !DB.Cursor(.read_only) { + return try map.getCursor(key(k)) orelse error.CorruptSession; + } + fn uint(map: Map, k: K) !u64 { + return (try field(map, k)).readUint(); + } + fn bytes(map: Map, k: K, gpa: std.mem.Allocator) ![]const u8 { + return (try field(map, k)).readBytesAlloc(gpa, null); + } + /// `size` bytes from a keyframe's pages, `gpa`'s. + fn pages(l: List, size: u64, gpa: std.mem.Allocator) ![]u8 { + const out = try gpa.alloc(u8, size); + errdefer gpa.free(out); + @memset(out, 0); + for (0..try l.count()) |i| { + const c = try l.getCursor(@intCast(i)) orelse continue; + if ((try c.slot()).tag == .none) continue; + const into = out[@min(size, i * page)..@min(size, (i + 1) * page)]; + if ((c.readBytes(into) catch return error.CorruptSession).len != into.len) return error.CorruptSession; + } + return out; + } + fn list(map: Map, k: K) !?List { + return try List.init(try map.getCursor(key(k)) orelse return null); + } + fn at(l: List, i: u64) !Map { + if (i >= try l.count()) return error.NoSuchEntry; + return Map.init(try l.getCursor(@intCast(i)) orelse return error.NoSuchEntry); + } + }; +} + +test "frames written to a session read back byte for byte, in memory and through a reopened file" { + const gpa = std.testing.allocator; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = try std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + const layout = "a layout"; + for ([_]Mode{ .memory, .file }) |m| { + var s = try open(gpa, std.testing.io, m, path, .{ .build = 7, .source = "jj:abc", .layout = layout }, 1000); + var live: std.ArrayList(abi.Event) = .empty; + defer live.deinit(gpa); + // Three seconds at 60 Hz, a move in every third frame; the state is + // the frame count, so keyframes differ. + var state: u64 = 0; + for (0..180) |f| { + const t = std.time.ns_per_s + f * std.time.ns_per_s / 60; + var frame_events: [2]abi.Event = .{ .{ .kind = .mouse_move, .time = t, .x = @floatFromInt(f) }, abi.Event.tick(t, std.time.ns_per_s / 60) }; + const one = if (f % 3 == 0) frame_events[0..] else frame_events[1..]; + try s.frame(one, std.mem.asBytes(&state), if (f == 0) .start else if (f == 90) .jump else null); + try live.appendSlice(gpa, one); + state += 1; + } + // Two commits of a second each; the third second is pending. + try std.testing.expectEqual(@as(u64, 2), s.batches); + try s.commit(.none); + try std.testing.expectEqual(@as(u64, 2), s.keys); + var back: std.ArrayList(abi.Event) = .empty; + defer back.deinit(gpa); + try s.events(s.session, 0, 0, 180, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items), std.mem.sliceAsBytes(back.items)); + // From the jump's keyframe: frame 90 on, its state 90. + const k = (try s.keyBefore(s.session, .frame, 100)).?; + try std.testing.expectEqual(@as(u64, 1), k); + const loaded = try s.load(s.session, k); + defer loaded.free(gpa); + try std.testing.expectEqual(@as(u64, 90), loaded.frame); + try std.testing.expectEqual(@as(u64, 90), std.mem.bytesToValue(u64, loaded.state)); + try std.testing.expectEqualStrings(layout, loaded.layout); + try std.testing.expectEqual(@as(u64, 7), loaded.build); + back.clearRetainingCapacity(); + try s.events(s.session, loaded.batch, 90, 92, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items[120..][0..3]), std.mem.sliceAsBytes(back.items)); + try std.testing.expectEqual(@as(?u64, 0), try s.keyBefore(s.session, .frame, 0)); + if (m == .memory) { + s.deinit(); + continue; + } + s.close(std.mem.asBytes(&state), 2000); + // Reopened: the first session ended, its frames all there; a new one. + var again = try open(gpa, std.testing.io, .file, path, .{ .build = 8, .source = "", .layout = layout }, 3000); + defer again.deinit(); + try std.testing.expectEqual(@as(u64, 1), again.session); + const first = try again.summary(0); + try std.testing.expectEqual(@as(u64, 180), first.frames); + try std.testing.expectEqual(@as(u64, 2000), first.ended); + back.clearRetainingCapacity(); + try again.events(0, 0, 0, 180, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items), std.mem.sliceAsBytes(back.items)); + } +} + +test "a session database torn inside its last commit opens at the one before, every earlier session and frame in it; one that cannot be mended is kept aside" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + const first: Stretch = .{ .build = 7, .source = "", .layout = "a layout" }; + var live: std.ArrayList(abi.Event) = .empty; + defer live.deinit(gpa); + var state: u64 = 0; + // Session 0: 120 frames, closed. Session 1: 180 frames in three commits + // of a second, then the process gone (no exit commit). + var before: u64 = 0; + var whole: u64 = 0; + for ([_]u64{ 120, 180 }, 0..) |frames, session| { + var s = try open(gpa, io, .file, path, first, 1000 + session); + for (0..frames) |f| { + const t = f * std.time.ns_per_s / 60; + var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)}; + try s.frame(&one, std.mem.asBytes(&state), if (f == 0) .start else null); + try live.appendSlice(gpa, &one); + state += 1; + } + if (session == 0) { + s.close(std.mem.asBytes(&state), 2000); + continue; + } + before = try s.file.?.length(io); + try s.commit(.none); + whole = try s.file.?.length(io); + s.deinit(); + } + const bytes = try tmp.dir.readFileAlloc(io, "t.xitdb", gpa, .unlimited); + defer gpa.free(bytes); + try std.testing.expectEqual(whole, bytes.len); + for ([_]u64{ before, before + 1, (before + whole) / 2, whole - trailer_len, whole - 10, whole - 1 }) |cut| { + errdefer std.debug.print("cut at {d} of {d}..{d}\n", .{ cut, before, whole }); + try tmp.dir.writeFile(io, .{ .sub_path = "t.xitdb", .data = bytes[0..cut] }); + var s = try open(gpa, io, .file, path, first, 3000); + defer s.deinit(); + try std.testing.expectEqual(@as(u64, 2), s.session); + const s0 = try s.summary(0); + try std.testing.expectEqual(@as(u64, 120), s0.frames); + try std.testing.expectEqual(@as(u64, 2000), s0.ended); + const s1 = try s.summary(1); + try std.testing.expectEqual(@as(u64, 120), s1.frames); + try std.testing.expectEqual(@as(u64, 0), s1.ended); + var back: std.ArrayList(abi.Event) = .empty; + defer back.deinit(gpa); + try s.events(0, 0, 0, 120, &back); + try s.events(1, 0, 0, 120, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items[0..240]), std.mem.sliceAsBytes(back.items)); + var dir = try tmp.dir.openDir(io, ".", .{ .iterate = true }); + defer dir.close(io); + var it = dir.iterate(); + while (try it.next(io)) |e| try std.testing.expectEqualStrings("t.xitdb", e.name); + } + // Nothing whole to open at: kept aside as it was, a new database begun. + var torn = try gpa.dupe(u8, bytes); + defer gpa.free(torn); + @memset(torn[root_header_end..], 0); + try tmp.dir.writeFile(io, .{ .sub_path = "t.xitdb", .data = torn }); + var s = try open(gpa, io, .file, path, first, 4 * std.time.ns_per_s); + defer s.deinit(); + try std.testing.expectEqual(@as(u64, 0), s.session); + const aside = try tmp.dir.readFileAlloc(io, "t.xitdb.broken.4", gpa, .unlimited); + defer gpa.free(aside); + try std.testing.expectEqualSlices(u8, torn, aside); +} + +test "a compacted session database is smaller and holds every session and frame" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + const first: Stretch = .{ .build = 7, .source = "", .layout = "a layout" }; + var live: std.ArrayList(abi.Event) = .empty; + defer live.deinit(gpa); + var s = try open(gpa, io, .file, path, first, 1000); + var state: u64 = 0; + for (0..600) |f| { + const t = f * std.time.ns_per_s / 60; + var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)}; + try s.frame(&one, std.mem.asBytes(&state), if (f == 0) .start else if (f % 100 == 0) .jump else null); + try live.appendSlice(gpa, &one); + state += 1; + } + try s.commit(.none); + const size = try s.file.?.length(io); + const live_size = try liveSize(&s.db.file); + // Six keyframes of 8 bytes, the first all zeros: an empty page. + try std.testing.expectEqual(@as(u64, 600 * @sizeOf(abi.Event) + 10 * 8 + 5 * 8), live_size); + try s.compact(); + s.deinit(); + const compacted = (try tmp.dir.statFile(io, "t.xitdb", .{})).size; + try std.testing.expect(compacted < size); + var again = try open(gpa, io, .file, path, first, 2000); + defer again.deinit(); + try std.testing.expectEqual(@as(u64, 600), (try again.summary(0)).frames); + try std.testing.expectEqual(@as(u64, 6), (try again.summary(0)).keys); + var back: std.ArrayList(abi.Event) = .empty; + defer back.deinit(gpa); + try again.events(0, 0, 0, 600, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items), std.mem.sliceAsBytes(back.items)); +} + +test "a memory session stops recording at its bound, and what it holds still reads" { + const gpa = std.testing.allocator; + var s = try open(gpa, std.testing.io, .memory, "", .{ .build = 7, .source = "", .layout = "a layout" }, 0); + defer s.deinit(); + s.db.memory.core.max_size = 64 << 10; + var live: std.ArrayList(abi.Event) = .empty; + defer live.deinit(gpa); + var state: u64 = 0; + var failed: usize = 0; + for (0..60 * 60) |f| { + const t = f * std.time.ns_per_s / 60; + var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)}; + s.frame(&one, std.mem.asBytes(&state), if (f == 0) .start else null) catch |err| { + try std.testing.expectEqual(error.WriteFailed, err); + failed += 1; + }; + try live.appendSlice(gpa, &one); + state += 1; + } + try std.testing.expectEqual(@as(usize, 1), failed); + const n = (try s.summary(s.session)).frames; + try std.testing.expect(n >= 60 and n < 60 * 60); + var back: std.ArrayList(abi.Event) = .empty; + defer back.deinit(gpa); + try s.events(s.session, 0, 0, n, &back); + try std.testing.expectEqualSlices(u8, std.mem.sliceAsBytes(live.items[0..n]), std.mem.sliceAsBytes(back.items)); +} + +test "a session's keyframes write only the pages that changed: a State of megabytes edited each minute, and one never changed" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + // 4 MiB, every page holding something, one u64 edited a minute. + const state = try gpa.alloc(u8, 4 << 20); + defer gpa.free(state); + for (state, 0..) |*b, i| b.* = @truncate(i *% 7 + 1); + var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000); + defer s.deinit(); + const minutes = 10; + var sizes: [minutes + 1]u64 = undefined; + for (0..minutes * 60 * 60) |f| { + const t = f * std.time.ns_per_s / 60; + var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)}; + if (f % 3600 == 1800) std.mem.writeInt(u64, state[(f / 3600) * 300_000 ..][0..8], f, .little); + try s.frame(&one, state, if (f == 0) .start else null); + if (f % 3600 == 0) { + try s.commit(.none); + sizes[f / 3600] = try s.file.?.length(io); + } + } + try s.commit(.none); + sizes[minutes] = try s.file.?.length(io); + // One keyframe a minute: at 0 s, 60 s, ... + try std.testing.expectEqual(@as(u64, minutes), s.keys); + // A minute is 216,000 bytes of ticks and a keyframe of one page with + // its copied path: under 230 KB a minute, where a whole State a + // keyframe was 4 MiB. + for (1..minutes) |m| try std.testing.expect(sizes[m + 1] - sizes[m] < 230_000); + // Every keyframe reads back whole: the edits made before it, and none after. + const want = try gpa.alloc(u8, state.len); + defer gpa.free(want); + for (0..minutes) |k| { + for (want, 0..) |*b, i| b.* = @truncate(i *% 7 + 1); + for (0..k) |m| std.mem.writeInt(u64, want[m * 300_000 ..][0..8], m * 3600 + 1800, .little); + const loaded = try s.load(s.session, k); + defer loaded.free(gpa); + try std.testing.expectEqual(@as(u64, k * 3600), loaded.frame); + try std.testing.expectEqualSlices(u8, want, loaded.state); + } + // A keyframe alone, in a commit of its own: one changed page is that + // page, its copied path and the keyframe's fields; no changed page (one + // kept before a reload) is the fields alone. + for ([_]bool{ true, false }) |edit| { + if (edit) state[123] +%= 1; + const before = try s.file.?.length(io); + _ = try s.keyframe(if (edit) .jump else .before_reload, state); + try s.commit(.none); + try std.testing.expect(try s.file.?.length(io) - before < @as(u64, if (edit) 12 << 10 else 8 << 10)); + } + const last = try s.load(s.session, s.keys - 1); + defer last.free(gpa); + try std.testing.expectEqualSlices(u8, state, last.state); +} + +test "an exported session keeps its keyframes' shared pages shared" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + var out_buf: [std.fs.max_path_bytes]u8 = undefined; + const out = try std.fmt.bufPrint(&out_buf, ".zig-cache/tmp/{s}/one.xitdb", .{&tmp.sub_path}); + const state = try gpa.alloc(u8, 1 << 20); + defer gpa.free(state); + for (state, 0..) |*b, i| b.* = @truncate(i *% 13 + 1); + var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000); + defer s.deinit(); + // Ten keyframes a byte apart. + for (0..600) |f| { + const t = f * std.time.ns_per_s / 60; + var one = [_]abi.Event{abi.Event.tick(t, std.time.ns_per_s / 60)}; + if (f % 60 == 0) state[f * 100] +%= 1; + try s.frame(&one, state, if (f == 0) .start else if (f % 60 == 0) .jump else null); + } + try s.exportSession(0, out); + const size = (try std.Io.Dir.cwd().statFile(io, out, .{})).size; + // One State and nine pages, not ten States. + try std.testing.expect(size < state.len + state.len / 8); + var again = try open(gpa, io, .file, out, .{ .build = 7, .source = "", .layout = "a layout" }, 2000); + defer again.deinit(); + const loaded = try again.load(0, 9); + defer loaded.free(gpa); + try std.testing.expectEqualSlices(u8, state, loaded.state); +} + +test "a session database past max_save drops its oldest history: the present and the marks stay, the file under it" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + // 2 MiB of State, its first half rewritten each second: a keyframe + // writes 1 MiB, so the cap is reached at about the 60th. + const state = try gpa.alloc(u8, 2 << 20); + defer gpa.free(state); + @memset(state, 1); + var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000); + defer s.deinit(); + for (0..100) |k| { + @memset(state[0 .. 1 << 20], @truncate(k + 2)); + var one = [_]abi.Event{abi.Event.tick(k * std.time.ns_per_s, std.time.ns_per_s)}; + try s.frame(&one, state, if (k == 0) .start else .jump); + if (k == 5) _ = try s.mark("a", .mark, state); + if (k == 20) _ = try s.mark("b", .mark, state); + try std.testing.expect(try s.file.?.length(io) <= max_save); + } + try s.commit(.none); + // The oldest keyframes and frames are gone; the marks and the newest + // keyframe read back whole. + try std.testing.expectError(error.HistoryDropped, s.load(0, 1)); + var log: std.ArrayList(abi.Event) = .empty; + defer log.deinit(gpa); + try std.testing.expectError(error.HistoryDropped, s.events(0, 0, 0, 2, &log)); + for ([_]struct { []const u8, u8 }{ .{ "a", 7 }, .{ "b", 22 } }) |want| { + const m = (try s.findMark(want[0])).?; + const loaded = try s.load(m.session, m.key); + defer loaded.free(gpa); + try std.testing.expectEqual(want[1], loaded.state[0]); + try std.testing.expectEqual(@as(u8, 1), loaded.state[1 << 20]); + } + const newest = try s.load(0, s.keys - 1); + defer newest.free(gpa); + try std.testing.expectEqualSlices(u8, state, newest.state); + try s.events(0, newest.batch, newest.frame, s.frames, &log); +} + +test "a session database without history keeps the present and the marks, and leaves nothing else at exit" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + var state: [64 << 10]u8 = @splat(1); + var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000); + s.history = false; + for (0..20) |k| { + @memset(state[0 .. 32 << 10], @truncate(k + 2)); + var one = [_]abi.Event{abi.Event.tick(k * std.time.ns_per_s, std.time.ns_per_s)}; + try s.frame(&one, &state, if (k == 0) .start else .jump); + if (k == 3) _ = try s.mark("a", .mark, &state); + } + try s.commit(.none); + try std.testing.expectError(error.HistoryDropped, s.load(0, 1)); + const m = (try s.findMark("a")).?; + const loaded = try s.load(m.session, m.key); + loaded.free(gpa); + s.close(&state, 2000); + // At exit: compacted to the present, the mark and the session's fields. + try std.testing.expect((try tmp.dir.statFile(io, "t.xitdb", .{})).size < 3 * state.len); + var again = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 3000); + defer again.deinit(); + var log: std.ArrayList(abi.Event) = .empty; + defer log.deinit(gpa); + const now = (try again.present(&log)).?; + defer now.free(gpa); + try std.testing.expectEqualSlices(u8, &state, now.state); +} + +test "a session database full of marks refuses a mark of a new name, and takes one over an old name" { + 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 std.fmt.bufPrint(&buf, ".zig-cache/tmp/{s}/t.xitdb", .{&tmp.sub_path}); + // 2 MiB of State, all of it changed for each mark. + const state = try gpa.alloc(u8, 2 << 20); + defer gpa.free(state); + var s = try open(gpa, io, .file, path, .{ .build = 7, .source = "", .layout = "a layout" }, 1000); + defer s.deinit(); + var name: [8]u8 = undefined; + var k: u8 = 0; + while (true) : (k += 1) { + @memset(state, k + 1); + _ = s.mark(try std.fmt.bufPrint(&name, "m{d}", .{k}), .mark, state) catch |err| { + try std.testing.expectEqual(error.SaveFull, err); + break; + }; + try std.testing.expect(k < 40 and try s.file.?.length(io) <= max_save); + } + try std.testing.expect(k > 20); + @memset(state, 99); + try std.testing.expectError(error.SaveFull, s.mark("new", .mark, state)); + _ = try s.mark("m3", .mark, state); + try s.commit(.none); + try std.testing.expect(try s.file.?.length(io) <= max_save); + // Every mark's newest keyframe is whole. + for (0..k) |i| { + const m = (try s.findMark(try std.fmt.bufPrint(&name, "m{d}", .{i}))).?; + const loaded = try s.load(m.session, m.key); + defer loaded.free(gpa); + try std.testing.expectEqual(@as(u8, if (i == 3) 99 else @intCast(i + 1)), loaded.state[state.len - 1]); + } +} diff --git a/src/host/Timing.zig b/src/host/Timing.zig new file mode 100644 index 0000000..1e5d991 --- /dev/null +++ b/src/host/Timing.zig @@ -0,0 +1,169 @@ +//! Wall-clock frame history, independent of the sketch's replay clock. +const Timing = @This(); +const std = @import("std"); + +pub const capacity = 240; +pub const frames_per_column = 8; +pub const Metric = enum { frame, work }; +pub const Sample = struct { frame: u64 = 0, work: u64 = 0 }; +pub const Stats = struct { + mean: f64 = 0, + p90: f64 = 0, + p99: f64 = 0, + max: f64 = 0, +}; + +visible: bool = false, +metric: Metric = .frame, +samples: [capacity]Sample = @splat(.{}), +count: usize = 0, +next: usize = 0, +dirty: bool = true, +cached: [2]Stats = @splat(.{}), +labels: [4][96]u8 = undefined, +label_lengths: [4]usize = @splat(0), +label_elapsed: u64 = 0, +ceiling: f64 = 1.25, +graph: [capacity]Sample = @splat(.{}), +graph_count: usize = 0, +graph_next: usize = 0, +group_count: usize = 0, +group_peak: Sample = .{}, + +pub fn add(self: *Timing, sample: Sample) void { + self.samples[self.next] = sample; + self.next = (self.next + 1) % capacity; + self.count = @min(self.count + 1, capacity); + self.label_elapsed +|= sample.frame; + // Refresh text at most four times a second; collection never allocates or sorts. + if (self.label_elapsed >= 250 * std.time.ns_per_ms) { + self.dirty = true; + self.label_elapsed = 0; + } + self.group_peak.frame = @max(self.group_peak.frame, sample.frame); + self.group_peak.work = @max(self.group_peak.work, sample.work); + self.group_count += 1; + if (self.group_count == frames_per_column) { + self.graph[self.graph_next] = self.group_peak; + self.graph_next = (self.graph_next + 1) % capacity; + self.graph_count = @min(self.graph_count + 1, capacity); + self.group_count = 0; + self.group_peak = .{}; + } +} + +pub fn graphAt(self: *const Timing, index: usize) Sample { + std.debug.assert(index < self.graph_count); + return self.graph[(self.graph_next + capacity - self.graph_count + index) % capacity]; +} + +pub fn label(self: *const Timing, index: usize) []const u8 { + return self.labels[index][0..self.label_lengths[index]]; +} + +pub fn setLabel(self: *Timing, index: usize, comptime fmt: []const u8, args: anytype) void { + const text = std.mem.print(&self.labels[index], fmt, args) catch unreachable; + self.label_lengths[index] = text.len; +} + +pub fn sampleAt(self: *const Timing, index: usize) Sample { + std.debug.assert(index < self.count); + return self.samples[(self.next + capacity - self.count + index) % capacity]; +} + +pub fn value(sample: Sample, metric: Metric) u64 { + return switch (metric) { + .frame => sample.frame, + .work => sample.work, + }; +} + +pub fn ms(ns: u64) f64 { + return @as(f64, @floatFromInt(ns)) / std.time.ns_per_ms; +} + +pub fn stats(self: *const Timing, metric: Metric) Stats { + if (self.count == 0) return .{}; + var sorted: [capacity]u64 = undefined; + var total: f64 = 0; + for (sorted[0..self.count], 0..) |*v, i| { + v.* = value(self.sampleAt(i), metric); + total += ms(v.*); + } + const values = sorted[0..self.count]; + std.mem.sort(u64, values, {}, std.sort.asc(u64)); + return .{ + .mean = total / @as(f64, @floatFromInt(values.len)), + .p90 = ms(values[(values.len * 90 + 99) / 100 - 1]), + .p99 = ms(values[(values.len * 99 + 99) / 100 - 1]), + .max = ms(values[values.len - 1]), + }; +} + +pub fn command(self: *Timing, arg: ?[]const u8) !void { + if (arg) |name| { + if (std.mem.eql(u8, name, "on")) { + self.visible = true; + } else if (std.mem.eql(u8, name, "off")) { + self.visible = false; + } else if (std.mem.eql(u8, name, "reset")) { + self.* = .{ .visible = self.visible, .metric = self.metric }; + } else if (std.meta.stringToEnum(Metric, name)) |metric| { + self.metric = metric; + self.visible = true; + } else return error.ExpectedTimingOnOffFrameWorkResetStats; + } else self.visible = !self.visible; + self.dirty = true; +} + +test "timing history percentiles, wrap, reset and commands" { + var t: Timing = .{}; + try std.testing.expectEqual(@as(f64, 0), t.stats(.frame).p99); + t.add(.{ .frame = 1000000, .work = 500000 }); + try std.testing.expectEqual(@as(f64, 0.5), t.stats(.work).p90); + try t.command("reset"); + for (1..401) |i| t.add(.{ .frame = i * 1000000, .work = i * 1000 }); + try std.testing.expectEqual(capacity, t.count); + try std.testing.expectEqual(@as(u64, 161000000), t.sampleAt(0).frame); + const s = t.stats(.frame); + try std.testing.expectEqual(@as(f64, 280.5), s.mean); + try std.testing.expectEqual(@as(f64, 376), s.p90); + try std.testing.expectEqual(@as(f64, 398), s.p99); + try std.testing.expectEqual(@as(f64, 400), s.max); + try t.command("work"); + try t.command("reset"); + try std.testing.expect(t.visible and t.metric == .work and t.count == 0); + try t.command(null); + try std.testing.expect(!t.visible); + try t.command("on"); + try std.testing.expect(t.visible); + try t.command("off"); + try std.testing.expect(!t.visible); + try std.testing.expectError(error.ExpectedTimingOnOffFrameWorkResetStats, t.command("bogus")); +} + +test "timing graph keeps peaks while scrolling eight times slower" { + var t: Timing = .{}; + for (0..7) |_| t.add(.{ .frame = 10, .work = 1 }); + try std.testing.expectEqual(@as(usize, 0), t.graph_count); + t.add(.{ .frame = 100, .work = 5 }); + try std.testing.expectEqual(@as(usize, 1), t.graph_count); + try std.testing.expectEqual(Sample{ .frame = 100, .work = 5 }, t.graphAt(0)); + for (0..capacity * frames_per_column) |_| t.add(.{ .frame = 20, .work = 2 }); + try std.testing.expectEqual(capacity, t.graph_count); + try std.testing.expectEqual(Sample{ .frame = 20, .work = 2 }, t.graphAt(0)); + try t.command("reset"); + try std.testing.expectEqual(@as(usize, 0), t.graph_count); + try std.testing.expectEqual(@as(usize, 0), t.group_count); +} + +test "timing readouts refresh quarterly and use fixed text storage" { + var t: Timing = .{}; + t.dirty = false; + for (0..24) |_| t.add(.{ .frame = 10 * std.time.ns_per_ms }); + try std.testing.expect(!t.dirty); + t.add(.{ .frame = 10 * std.time.ns_per_ms }); + try std.testing.expect(t.dirty); + t.setLabel(0, "p99 {d:.3}", .{@as(f64, 0.123)}); + try std.testing.expectEqualStrings("p99 0.123", t.label(0)); +} diff --git a/src/host/TimingAtlas.zig b/src/host/TimingAtlas.zig new file mode 100644 index 0000000..a00a87e --- /dev/null +++ b/src/host/TimingAtlas.zig @@ -0,0 +1,139 @@ +//! One immutable texture for the timing overlay's printable ASCII glyphs. +const Atlas = @This(); +const std = @import("std"); +const abi = @import("abi"); +const gfx = @import("gfx.zig"); +const c = @import("c"); + +image: abi.Image = .none, +advance: f32 = 0, +cell_width: f32 = 0, +cell_height: f32 = 0, + +const padding = 1; +const glyphs = blk: { + var bytes: [95]u8 = undefined; + for (&bytes, 0..) |*byte, i| byte.* = @intCast(i + 32); + break :blk bytes; +}; + +/// Called once with Painter on its canvas. Images owns the texture until shutdown. +pub fn init(images: *gfx.Images, painter: *gfx.Painter) !Atlas { + const run = gfx.textRun(.mono, 1, "0", &.{}); + const size: [2]f32 = .{ run.width, run.height }; + var atlas: Atlas = .{ + .advance = size[0], + .cell_width = @ceil(size[0]) + padding * 2, + .cell_height = @ceil(size[1]) + padding * 2, + }; + atlas.image = images.new(@intFromFloat(atlas.cell_width * glyphs.len), @intFromFloat(atlas.cell_height)); + errdefer images.free(atlas.image); + const entry = images.get(atlas.image) orelse return error.TimingAtlasCreationFailed; + if (entry.texture == null) return error.TimingAtlasCreationFailed; + // The atlas stores the native scale-one raster; do not supersample this cache. + const smooth = painter.smooth; + painter.smooth = false; + defer painter.smooth = smooth; + painter.draw(.{ .target = atlas.image }); + painter.draw(.{ .clear = .{ .r = 0, .g = 0, .b = 0, .a = 0 } }); + for (glyphs, 0..) |_, i| painter.draw(.{ .text = .{ + .x = @as(f32, @floatFromInt(i)) * atlas.cell_width + padding, + .y = padding, + .scale = 1, + .raster = 1, + .color = .white, + .bytes = glyphs[i..][0..1], + } }); + // The unused space cell also supplies an opaque texel for bars/backgrounds. + painter.draw(.{ .rect = .{ .dst = .{ .x = 0, .y = 0, .w = 1, .h = 1 }, .color = .white } }); + painter.draw(.{ .target = .none }); + return atlas; +} + +fn source(atlas: Atlas, byte: u8) abi.Rect { + const printable = if (byte >= 32 and byte <= 126) byte else '?'; + return .{ .x = @as(f32, @floatFromInt(printable - 32)) * atlas.cell_width, .y = 0, .w = atlas.cell_width, .h = atlas.cell_height }; +} + +/// The complete overlay fits one fixed vertex/index batch and one SDL draw call. +pub const Batch = struct { + const capacity = 4 * 96 + 48 + @import("Timing.zig").capacity + 2; + atlas: Atlas, + vertices: [capacity * 4]c.SDL_Vertex = undefined, + indices: [capacity * 6]u16 = undefined, + count: usize = 0, + + pub fn rect(batch: *Batch, dst: abi.Rect, color: abi.Color) !void { + try batch.quad(dst, .{ .x = 0.5, .y = 0.5, .w = 0, .h = 0 }, color); + } + + pub fn text(batch: *Batch, bytes: []const u8, x: f32, y: f32, scale: f32, color: abi.Color) !void { + const atlas = batch.atlas; + for (bytes, 0..) |byte, i| { + if (byte == ' ') continue; + try batch.quad(.{ + .x = x + (@as(f32, @floatFromInt(i)) * atlas.advance - padding) * scale, + .y = y - padding * scale, + .w = atlas.cell_width * scale, + .h = atlas.cell_height * scale, + }, atlas.source(byte), color); + } + } + + fn quad(batch: *Batch, dst: abi.Rect, src: abi.Rect, color: abi.Color) !void { + if (batch.count == capacity) return error.TimingBatchFull; + const width = batch.atlas.cell_width * glyphs.len; + const height = batch.atlas.cell_height; + const alpha = @as(f32, @floatFromInt(color.a)) / 255; + const tint: c.SDL_FColor = .{ + .r = @as(f32, @floatFromInt(color.r)) / 255 * alpha, + .g = @as(f32, @floatFromInt(color.g)) / 255 * alpha, + .b = @as(f32, @floatFromInt(color.b)) / 255 * alpha, + .a = alpha, + }; + const base = batch.count * 4; + const corners = [4][2]f32{ .{ 0, 0 }, .{ 1, 0 }, .{ 1, 1 }, .{ 0, 1 } }; + for (corners, 0..) |corner, i| batch.vertices[base + i] = .{ + .position = .{ .x = dst.x + corner[0] * dst.w, .y = dst.y + corner[1] * dst.h }, + .color = tint, + .tex_coord = .{ .x = (src.x + corner[0] * src.w) / width, .y = (src.y + corner[1] * src.h) / height }, + }; + for ([_]u16{ 0, 1, 2, 0, 2, 3 }, 0..) |index, i| batch.indices[batch.count * 6 + i] = @as(u16, @intCast(base)) + index; + batch.count += 1; + } + + pub fn draw(batch: *const Batch, painter: *gfx.Painter) void { + painter.texturedTriangles(batch.atlas.image, batch.vertices[0 .. batch.count * 4], batch.indices[0 .. batch.count * 6]); + } +}; + +test "timing atlas ASCII cells and scaled glyph positions share one image" { + const atlas: Atlas = .{ .image = @fromBackingInt(7), .advance = 7.5, .cell_width = 10, .cell_height = 16 }; + try std.testing.expectEqual(@as(f32, 0), atlas.source(' ').x); + try std.testing.expectEqual(@as(f32, 940), atlas.source('~').x); + try std.testing.expectEqual(atlas.source('?'), atlas.source(0xff)); + var batch: Batch = .{ .atlas = atlas }; + try batch.text("1 2", 10, 20, 0.5, .white); + try std.testing.expectEqual(@as(usize, 2), batch.count); + try std.testing.expectEqual(@as(f32, 9.5), batch.vertices[0].position.x); + try std.testing.expectEqual(@as(f32, 19.5), batch.vertices[0].position.y); + try std.testing.expectEqual(@as(f32, 14.5), batch.vertices[2].position.x); + try std.testing.expectEqual(@as(f32, 27.5), batch.vertices[2].position.y); + try std.testing.expectEqual(@as(f32, 17), batch.vertices[4].position.x); + try std.testing.expectEqual(@as(f32, 170.0 / 950.0), batch.vertices[0].tex_coord.x); + try std.testing.expectEqual(@as(f32, 180.0 / 950.0), batch.vertices[4].tex_coord.x); + try std.testing.expectEqualSlices(u16, &.{ 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7 }, batch.indices[0..12]); +} + +test "timing batch is bounded and solid quads sample one opaque atlas texel" { + var batch: Batch = .{ .atlas = .{ .image = @fromBackingInt(1), .advance = 8, .cell_width = 10, .cell_height = 16 } }; + const box: abi.Rect = .{ .x = 10, .y = 20, .w = 5, .h = 8 }; + for (0..Batch.capacity) |_| try batch.rect(box, .{ .r = 255, .g = 128, .b = 0, .a = 128 }); + try std.testing.expectError(error.TimingBatchFull, batch.rect(box, .white)); + for (batch.indices) |index| try std.testing.expect(index < batch.vertices.len); + for (batch.vertices[0..4]) |vertex| { + try std.testing.expectEqual(batch.vertices[0].tex_coord, vertex.tex_coord); + try std.testing.expectApproxEqAbs(@as(f32, 128.0 / 255.0), vertex.color.r, 0.00001); + try std.testing.expectApproxEqAbs(@as(f32, 128.0 / 255.0), vertex.color.a, 0.00001); + } +} diff --git a/src/host/audio.zig b/src/host/audio.zig index aebb909..880be72 100644 --- a/src/host/audio.zig +++ b/src/host/audio.zig @@ -11,6 +11,9 @@ pub const sample_rate = 48_000; const spec: c.SDL_AudioSpec = .{ .format = c.SDL_AUDIO_F32, .channels = 1, .freq = sample_rate }; /// A device block plus the next frame; 512 samples can underrun at 60 Hz. const queue_samples = 2048; +/// How far a group's gain moves a sample toward a new one: a full swing +/// takes 10 ms, so a gain set in a step does not click. +const gain_step = 1.0 / 480.0; pub const Audio = struct { gpa: std.mem.Allocator, @@ -20,8 +23,24 @@ pub const Audio = struct { clips: std.ArrayList([]f32) = .empty, paths: std.StringHashMapUnmanaged(abi.Audio) = .empty, voices: std.ArrayList(Playing) = .empty, + /// Each group's gain as declared this frame, and as the mix has got to. + gains: [8]f32 = @splat(1), + heard_gains: [8]f32 = @splat(1), streams: std.ArrayList(Stream) = .empty, status: std.ArrayList(abi.Stream) = .empty, + /// Samples the device takes from each stream at once. + pull: u32 = 1024, + /// The wall-clock time (ns) between the ends of the last frames' + /// commands, where pushes are done; and when the last ended. + intervals: [64]u64 = @splat(0), + frame: u64 = 0, + last_frame: u64 = 0, + /// Grown by underruns, shrinking back while there are none. + margin: u32 = 0, + /// A stream ran dry since the lead was last worked out. + dry: bool = false, + /// `Input.lead`: see `lead`. + lead: u32 = 4800, scope: Scope = .{}, /// The default recording device, bound to this stream while declared. microphone: ?*c.SDL_AudioStream = null, @@ -29,13 +48,38 @@ pub const Audio = struct { /// Set when opening failed, so it is tried once per declaration. deaf: bool = false, - const Playing = struct { voice: abi.Voice, cursor: usize = 0, done: bool = false, seen: bool = true }; - const Stream = struct { key: u32, out: *c.SDL_AudioStream, pushed: u64 = 0 }; + const Playing = struct { + voice: abi.Voice, + /// Where in the sound, in its samples: fractional when `rate` is. + cursor: f64 = 0, + done: bool = false, + seen: bool = true, + /// Mixed since a script last asked (`expect-audio`). + sounded: bool = false, + }; + const Stream = struct { + key: u32, + out: *c.SDL_AudioStream, + pushed: u64 = 0, + /// Pushed to this frame, and the last. + fed: bool = false, + was_fed: bool = false, + underruns: u32 = 0, + }; - pub fn open(self: *Audio) !void { + /// The default playback device, taking `pull` samples at once if asked + /// (`abi.Info.audio_pull`): a hint, so the environment's wins. + pub fn open(self: *Audio, pull: u32) !void { + var hint: [16]u8 = undefined; + if (pull > 0) _ = c.SDL_SetHint(c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, try std.fmt.bufPrintSentinel(&hint, "{d}", .{pull}, 0)); const device = c.SDL_OpenAudioDevice(c.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec); if (device == 0) return error.OpenAudio; self.device = device; + var format: c.SDL_AudioSpec = undefined; + var frames: c_int = 0; + if (c.SDL_GetAudioDeviceFormat(device, &format, &frames) and frames > 0 and format.freq > 0) { + self.pull = @intCast(@divFloor(@as(i64, frames) * sample_rate + format.freq - 1, format.freq)); + } self.voices_out = try self.bind(); self.scope.attach(device) catch {}; } @@ -81,6 +125,12 @@ pub const Audio = struct { return self.add(decodeBytes(self.gpa, wav) catch return .none); } + /// A copy of `samples`; `.none` if one is not finite. + pub fn new(self: *Audio, samples: []const f32) abi.Audio { + for (samples) |v| if (!std.math.isFinite(v)) return .none; + return self.add(self.gpa.dupe(f32, samples) catch return .none); + } + fn add(self: *Audio, samples: []f32) abi.Audio { self.clips.append(self.gpa, samples) catch { self.gpa.free(samples); @@ -97,6 +147,7 @@ pub const Audio = struct { pub fn beginFrame(self: *Audio) void { for (self.voices.items) |*v| v.seen = false; + self.gains = @splat(1); self.listening = false; } @@ -155,9 +206,18 @@ pub const Audio = struct { if (clear) { _ = c.SDL_ClearAudioStream(s.out); s.pushed = 0; + s.was_fed = false; } if (samples.len == 0) return; + // Fed last frame and empty now: the device came up short since + // (counted at the push, as a pull before it would hide it by the + // next frame). + if (s.was_fed and !s.fed and c.SDL_GetAudioStreamQueued(s.out) == 0) { + s.underruns += 1; + self.dry = true; + } if (c.SDL_PutAudioStreamData(s.out, samples.ptr, @intCast(samples.len * @sizeOf(f32)))) s.pushed += samples.len; + s.fed = true; } fn streamFor(self: *Audio, key: u32) ?*Stream { @@ -171,14 +231,22 @@ pub const Audio = struct { return &self.streams.items[self.streams.items.len - 1]; } - /// Drops undeclared voices, keeps the voice mix queued ahead, and opens - /// or closes the microphone. + /// Drops undeclared voices, works out the lead for the next frame, keeps + /// the voice mix queued ahead, and opens or closes the microphone. pub fn endFrame(self: *Audio) void { self.reconcileMicrophone(); var i: usize = 0; while (i < self.voices.items.len) { if (self.voices.items[i].seen) i += 1 else _ = self.voices.swapRemove(i); } + const now = c.SDL_GetTicksNS(); + if (self.last_frame != 0) { + self.intervals[self.frame % self.intervals.len] = now -| self.last_frame; + self.frame += 1; + } + self.last_frame = now; + self.lead = lead(self.intervals, self.pull, &self.margin, self.dry); + self.dry = false; const out = self.voices_out orelse return; const queued = c.SDL_GetAudioStreamQueued(out); if (queued < 0) return; @@ -189,45 +257,98 @@ pub const Audio = struct { } /// Mixes live voices into `output`; returns how many samples were written, - /// stopping early once every voice has finished. + /// stopping early once every voice has finished. A voice at another + /// rate is resampled by linear interpolation between its neighbours + /// (at rate 1 it is its samples, exactly). pub fn mix(self: *Audio, output: []f32) usize { var count: usize = 0; for (output) |*sample| { var sum: f32 = 0; var alive = false; + for (&self.heard_gains, self.gains) |*heard, gain| heard.* += std.math.clamp(gain - heard.*, -gain_step, gain_step); for (self.voices.items) |*playing| { if (playing.done) continue; const samples = self.samplesOf(playing.voice.audio); - if (playing.cursor >= samples.len) { + const len: f64 = @floatFromInt(samples.len); + // Also ends a cursor an infinite or NaN rate made. + if (!(playing.cursor < len)) { playing.done = true; continue; } alive = true; + playing.sounded = true; const v = playing.voice; - const edge = @min(playing.cursor + 1, samples.len - playing.cursor); + const i: usize = @intFromFloat(playing.cursor); + const t: f32 = @floatCast(playing.cursor - @floor(playing.cursor)); + const next = if (i + 1 < samples.len) samples[i + 1] else if (v.loop) samples[0] else 0; + const at = if (t == 0) samples[i] else samples[i] + (next - samples[i]) * t; + const edge = @min(i + 1, samples.len - i); const fade: f32 = if (v.fade == 0) 1 else @as(f32, @floatFromInt(@min(edge, v.fade))) / @as(f32, @floatFromInt(v.fade)); - sum += samples[playing.cursor] * v.volume * fade; - playing.cursor += 1; - if (playing.cursor == samples.len and v.loop) playing.cursor = 0; + sum += at * v.volume * self.heard_gains[v.group] * fade; + playing.cursor += @max(v.rate, 0); + if (playing.cursor >= len and v.loop) playing.cursor = @mod(playing.cursor, len); } if (!alive) break; - // Bounded however many voices sound at once. - sample.* = sum / (1 + @abs(sum)); + sample.* = limit(sum); count += 1; } return count; } + /// Whether voice `key` was mixed since the last call: for scripts. + pub fn sounded(self: *Audio, key: u32) bool { + for (self.voices.items) |*playing| if (playing.voice.key == key) { + defer playing.sounded = false; + return playing.sounded; + }; + return false; + } + + /// The streams as a frame starts. pub fn streamStatus(self: *Audio) []const abi.Stream { self.status.clearRetainingCapacity(); - for (self.streams.items) |s| { - const queued = c.SDL_GetAudioStreamQueued(s.out); - self.status.append(self.gpa, .{ .key = s.key, .queued = @intCast(@divFloor(@max(queued, 0), @sizeOf(f32))), .pushed = s.pushed }) catch {}; + for (self.streams.items) |*s| { + const queued: u32 = @intCast(@divFloor(@max(c.SDL_GetAudioStreamQueued(s.out), 0), @sizeOf(f32))); + s.was_fed = s.fed; + s.fed = false; + self.status.append(self.gpa, .{ .key = s.key, .queued = queued, .pushed = s.pushed, .underruns = s.underruns }) catch {}; } return self.status.items; } }; +/// How many samples to keep queued: the slowest of the last frames' worth +/// (pushes come once a frame, `intervals` apart), plus the device's pull +/// (it takes that much at once), plus `margin`: half a pull more at each +/// underrun (`dry`), back down by a sample a frame. +fn lead(intervals: [64]u64, pull: u32, margin: *u32, dry: bool) u32 { + var slowest: u64 = 0; + for (intervals) |ns| slowest = @max(slowest, ns); + margin.* = if (dry) @min(margin.* + pull / 2, max_lead) else margin.* -| 1; + const frame: u32 = @intCast(@min(slowest * sample_rate / std.time.ns_per_s, 1 << 20)); + return @min(frame + pull + margin.*, max_lead); +} +/// 170 ms: a frame slower than that is a stall, not a pace. +const max_lead = 8192; + +/// Where the voice mix stops being passed through: -2.5 dBFS. Quieter +/// mixes keep their true volume to the bit; a clip normalised to full scale +/// only has its peaks bent (1 comes out at 0.875). +pub const knee = 0.75; + +/// The voice mix, bounded however many voices sound at once: unity gain up +/// to the knee, then `knee + (1 - knee)·u/(1 + u)` with `u` how far past the +/// knee in units of the room left, which meets the line at the knee with +/// its slope (1) and tends to 1. (With a knee of 0 it is the old +/// `x/(1 + |x|)`.) Written `1 - 1/(1 + u)` so that every step rounds +/// monotonically and infinity comes out at 1. No state, no lookahead. +pub fn limit(x: f32) f32 { + const a = @abs(x); + if (a <= knee) return x; + const u = (a - knee) / (1 - knee); + return std.math.copysign(knee + (1 - knee) * (1 - 1 / (1 + u)), x); +} + /// WAV, FLAC or Ogg Vorbis, by content (see `vendor/codecs.h`). fn decodeBytes(gpa: std.mem.Allocator, bytes: []const u8) ![]f32 { var channels: u32 = 0; @@ -325,3 +446,116 @@ test "voices reconcile: one-shots finish and stay silent, new serials restart, u try std.testing.expectEqual(@as(usize, 1), audio.voices.items.len); try std.testing.expectEqual(@as(usize, 8), audio.mix(&out)); } + +test "voices play made sounds: copied, and refused when not finite" { + var audio: Audio = .{ .gpa = std.testing.allocator }; + defer audio.deinit(); + var made = [_]f32{ 0.25, -0.25 }; + const sound = audio.new(&made); + made[0] = 1; + try std.testing.expectEqualSlices(f32, &.{ 0.25, -0.25 }, audio.samplesOf(sound)); + try std.testing.expectEqual(abi.Audio.none, audio.new(&.{ 0, std.math.nan(f32) })); + try std.testing.expectEqual(abi.Audio.none, audio.new(&.{std.math.inf(f32)})); +} + +test "the mixer passes the mix through below the knee and limits it to 1 above, rising" { + // Identity to the bit: every 997th float from 0 to the knee, both signs. + const knee_bits: u32 = @bitCast(@as(f32, knee)); + var bits: u32 = 0; + while (bits <= knee_bits) : (bits += 997) { + const x: f32 = @bitCast(bits); + try std.testing.expectEqual(@as(u32, @bitCast(x)), @as(u32, @bitCast(limit(x)))); + try std.testing.expectEqual(@as(u32, @bitCast(-x)), @as(u32, @bitCast(limit(-x)))); + } + try std.testing.expectEqual(@as(f32, knee), limit(knee)); + // Rising and at most 1, from the knee's neighbourhood (every float) to + // infinity (every 101st), and odd. + var last = limit(@bitCast(knee_bits - 100_000)); + bits = knee_bits - 100_000; + const inf_bits: u32 = @bitCast(std.math.inf(f32)); + while (bits <= inf_bits) : (bits += if (bits < knee_bits + 100_000) 1 else 101) { + const x: f32 = @bitCast(bits); + const y = limit(x); + try std.testing.expect(y >= last and y <= 1); + try std.testing.expectEqual(-y, limit(-x)); + last = y; + } + try std.testing.expectEqual(@as(f32, 1), limit(std.math.inf(f32))); + // Slope 1 on both sides of the knee, and what a full-scale peak becomes. + const h = 1e-3; + try std.testing.expectApproxEqAbs(@as(f32, 1), (limit(knee + h) - limit(knee)) / h, 0.01); + try std.testing.expectEqual(@as(f32, 0.875), limit(1)); +} + +test "voices at another rate are resampled linearly, and a group's gain scales its voices smoothly" { + var audio: Audio = .{ .gpa = std.testing.allocator }; + defer audio.deinit(); + const clip = audio.new(&.{ 0, 0.1, 0.2, 0.3 }); + var out: [1024]f32 = undefined; + // Half speed: twice as long, the halves interpolated (the last toward + // silence); double speed: every other sample. + audio.declare(.{ .key = 1, .audio = clip, .rate = 0.5 }); + try std.testing.expectEqual(@as(usize, 8), audio.mix(&out)); + for ([_]f32{ 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.15 }, out[0..8]) |want, got| try std.testing.expectApproxEqAbs(want, got, 1e-6); + audio.declare(.{ .key = 1, .audio = clip, .rate = 2, .serial = 1 }); + try std.testing.expectEqual(@as(usize, 2), audio.mix(&out)); + try std.testing.expectEqualSlices(f32, &.{ 0, 0.2 }, out[0..2]); + // Rate 0 holds it; an infinite rate ends it. + audio.declare(.{ .key = 1, .audio = clip, .rate = 0, .serial = 2 }); + try std.testing.expectEqual(@as(usize, 3), audio.mix(out[0..3])); + audio.declare(.{ .key = 1, .audio = clip, .rate = std.math.inf(f32), .serial = 3 }); + try std.testing.expectEqual(@as(usize, 1), audio.mix(&out)); + + // A looping voice in group 1 and one in group 0, at 0.25 each: group 1 + // set to 0.5 glides there in 10 ms and stays; group 0 is untouched. + const level = audio.new(&.{0.25}); + audio.beginFrame(); + audio.declare(.{ .key = 1, .audio = level, .loop = true, .group = 1 }); + audio.declare(.{ .key = 2, .audio = level, .loop = true }); + audio.gains[1] = 0.5; + try std.testing.expectEqual(out.len, audio.mix(&out)); + try std.testing.expect(out[0] < 0.5 and out[0] > 0.49); + try std.testing.expect(out[100] > out[200] and out[200] > out[400]); + try std.testing.expectEqual(@as(f32, 0.375), out[480]); + try std.testing.expectEqual(@as(f32, 0.375), out[1023]); + // Undeclared next frame: back to 1, as smoothly. + audio.beginFrame(); + audio.declare(.{ .key = 1, .audio = level, .loop = true, .group = 1 }); + audio.declare(.{ .key = 2, .audio = level, .loop = true }); + try std.testing.expectEqual(out.len, audio.mix(&out)); + try std.testing.expect(out[0] > 0.375 and out[0] < 0.38); + try std.testing.expectEqual(@as(f32, 0.5), out[1023]); +} + +test "the lead the mixer gives streams outlasts the slowest recent frame and the device's pull, more after an underrun" { + const expectEqual = std.testing.expectEqual; + var intervals: [64]u64 = @splat(20 * std.time.ns_per_ms); + var margin: u32 = 0; + // 50 Hz and a pull of 1024: a frame's 960 samples and the pull. + try expectEqual(@as(u32, 960 + 1024), lead(intervals, 1024, &margin, false)); + // One 50 ms frame in the window sets it. + intervals[5] = 50 * std.time.ns_per_ms; + try expectEqual(@as(u32, 2400 + 1024), lead(intervals, 1024, &margin, false)); + // An underrun adds half a pull, which goes a sample a frame. + try expectEqual(@as(u32, 2400 + 1024 + 512), lead(intervals, 1024, &margin, true)); + try expectEqual(@as(u32, 2400 + 1024 + 511), lead(intervals, 1024, &margin, false)); + // A stall is not a pace: the lead stops at 170 ms. + intervals[6] = std.time.ns_per_s; + try expectEqual(@as(u32, max_lead), lead(intervals, 1024, &margin, false)); +} + +test "the mixer's device takes at once what the sketch asks, SDL's own when it asks nothing, and the environment's over both" { + _ = c.SDL_SetHintWithPriority(c.SDL_HINT_AUDIO_DRIVER, "dummy", c.SDL_HINT_OVERRIDE); + try std.testing.expect(c.SDL_Init(c.SDL_INIT_AUDIO)); + defer c.SDL_Quit(); + defer _ = c.SDL_ResetHint(c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES); + for ([_][3]u32{ .{ 0, 0, 1024 }, .{ 256, 0, 256 }, .{ 256, 512, 512 } }) |case| { + // SDL's copy of the environment, not the process's. + if (case[1] > 0) try std.testing.expect(c.SDL_SetEnvironmentVariable(c.SDL_GetEnvironment(), c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES, "512", true)); + defer _ = c.SDL_UnsetEnvironmentVariable(c.SDL_GetEnvironment(), c.SDL_HINT_AUDIO_DEVICE_SAMPLE_FRAMES); + var a: Audio = .{ .gpa = std.testing.allocator }; + try a.open(case[0]); + defer a.deinit(); + try std.testing.expectEqual(case[2], a.pull); + } +} diff --git a/src/host/control.zig b/src/host/control.zig new file mode 100644 index 0000000..943316c --- /dev/null +++ b/src/host/control.zig @@ -0,0 +1,707 @@ +//! Local 9P controls, serviced on the frame thread: no SDL or state races. +const std = @import("std"); +const c9 = @import("cloud9"); +const c = @import("c"); +const abi = @import("abi"); +const Host = @import("Host.zig"); +const Presentation = @import("Presentation.zig"); +const state_files = @import("state_files.zig"); +const script = @import("script.zig"); +const Console = @import("Console.zig"); +const fs = c9.fs; +const transport = c9.transport; +const Backend = struct { + pub const Req = fs.Req; + pub const Reply = fs.Reply; +}; +const Server = fs.Server(Backend, .{ .fid_capacity = 32, .slot_capacity = 4, .name_capacity = 64 }); +const msize = 16384; +/// The files, a row each: what lookup, readdir and getattr answer. A +/// file's node is its number here. +const File = enum(u64) { @"/" = 1, ctl, status, log, @"screen.png", profile, @"profile-trace", @"timing-trace", @"presentation-trace", README, commands, sketch = 1 << 63, state = @bitCast(state_files.root) }; +const files = std.enums.EnumArray(File, struct { mode: u16 = 0o400, doc: []const u8 }).init(.{ + .@"/" = .{ .mode = 0o700, .doc = "" }, + .ctl = .{ .mode = 0o600, .doc = "write a verb a line, of those below: ctl's own, the input a script plays (live, no frame of its own), the console's; any other line goes to the sketch (commands lists its words)" }, + .status = .{ .doc = "name, canvas size, playback mode, gpu, and the screenshot's state" }, + .log = .{ .doc = "the console's last five lines" }, + .@"screen.png" = .{ .doc = "the next frame, as PNG: an open waits for it to be drawn, and pins its own copy" }, + .profile = .{ .doc = "with --profile-gpu: label, count, mean, worst, p95 and p99 ms, tab-separated" }, + .@"profile-trace" = .{ .doc = "with --profile-gpu: label,sample,ns, the samples kept, as CSV" }, + .@"timing-trace" = .{ .doc = "index,frame_ns,work_ns,outside_work_ns, the frames kept, as CSV" }, + .@"presentation-trace" = .{ .doc = "on Wayland: presentation feedback per frame, as CSV" }, + .README = .{ .doc = "this: a line a file, ctl's verbs under it, a line a State file under state/" }, + .commands = .{ .doc = "every verb ctl takes, a line each, the sketch's words last" }, + .sketch = .{ .mode = 0o700, .doc = "the sketch's own files, which the host never serves: reserved, empty" }, + .state = .{ .mode = 0o700, .doc = "the sketch's State: a directory per struct and array, a file per value; a read is the value, a write what set does (a jump); an array of numbers is one file of its values, blank-separated" }, +}); + +/// The verbs `ctl` takes beside the console's lines. +const Verb = enum { screenshot, @"profile-reset" }; +const verbs = std.enums.EnumArray(Verb, []const u8).init(.{ + .screenshot = "captures the next frame (status says when); screen.png's open does it itself", + .@"profile-reset" = "clears profile's counters and frame count, keeping its labels", +}); + +const Connection = struct { + fd: c_int = -1, + server: Server = undefined, + input: [msize]u8 = undefined, + output: [msize * 2]u8 = undefined, + incoming: [msize]u8 = undefined, + used: usize = 0, + snapshots: [32]?[]u8 = @splat(null), + + fn close(self: *Connection, a: std.mem.Allocator) void { + if (self.fd < 0) return; + transport.close(self.fd); + for (&self.snapshots) |*snapshot| if (snapshot.*) |bytes| { + a.free(bytes); + snapshot.* = null; + }; + self.fd = -1; + } +}; + +pub const Control = struct { + allocator: std.mem.Allocator, + path: [:0]u8, + listener: c_int, + /// Serving with no sketch running (`fs` with it closed): the State to + /// show and where it came from; the host is not touched. + offline: ?Offline = null, + connections: [4]Connection = @splat(.{}), + png: []u8 = &.{}, + capture_pending: bool = false, + generation: u64 = 0, + capture_error: ?anyerror = null, + + pub fn init(a: std.mem.Allocator, path: []const u8) !*Control { + const self = try a.create(Control); + errdefer a.destroy(self); + const z = try a.dupeSentinel(u8, path, 0); + errdefer a.free(z); + // Refuse an existing path; never unlink another process's socket. + const fd = try transport.listenFd(.{ .unix = z }, 4); + errdefer { + transport.close(fd); + _ = std.c.unlink(z); + } + if (std.c.chmod(z, 0o600) != 0) return error.SocketPermissions; + self.* = .{ .allocator = a, .path = z, .listener = fd }; + return self; + } + + /// Listens at `where`: a socket path, or a name posted as + /// `$XDG_RUNTIME_DIR/9p/sketchbook/<where>` (a stale entry replaced, a + /// live one refused). + pub fn open(a: std.mem.Allocator, environ: *const std.process.Environ.Map, where: []const u8) !*Control { + if (std.mem.startsWith(u8, where, "/")) return init(a, where); + var buf: [c9.post.sun_path_len]u8 = undefined; + const path = try registered(environ, where, &buf); + const dir = path[0..std.mem.lastIndexOfScalar(u8, path, '/').?]; + var dir_buf: [c9.post.sun_path_len]u8 = undefined; + for ([_]usize{ dir.len - "/sketchbook".len, dir.len }) |n| _ = std.c.mkdir(std.fmt.bufPrintSentinel(&dir_buf, "{s}", .{dir[0..n]}, 0) catch unreachable, 0o700); + switch (c9.post.probe(path)) { + .live => return error.AlreadyPosted, + .stale => _ = std.c.unlink(path), + .none => {}, + } + return init(a, path); + } + + /// Serves one client on `fd` until it hangs up: `fs` with no sketch + /// running, on a thread of its own. + pub fn serveOffline(self: *Control, fd: c_int) void { + const conn = self.allocator.create(Connection) catch return transport.close(fd); + defer self.allocator.destroy(conn); + conn.* = .{ .fd = fd }; + conn.server = .init(.{ .in = &conn.input, .out = &conn.output, .root = 1 }); + var host: Host = undefined; // Offline, nothing reads it. + while (true) self.service(&host, conn) catch break; + conn.close(self.allocator); + } + + pub fn deinit(self: *Control) void { + for (&self.connections) |*conn| conn.close(self.allocator); + transport.close(self.listener); + _ = std.c.unlink(self.path); + self.allocator.free(self.path); + self.allocator.free(self.png); + self.allocator.destroy(self); + } + + pub fn poll(self: *Control, host: *Host) void { + if (transport.acceptFd(self.listener, false) catch null) |fd| { + var accepted = false; + for (&self.connections) |*conn| if (conn.fd < 0) { + conn.fd = fd; + conn.used = 0; + conn.server = .init(.{ .in = &conn.input, .out = &conn.output, .root = 1 }); + accepted = true; + break; + }; + if (!accepted) transport.close(fd); + } + for (&self.connections) |*conn| { + if (conn.fd < 0) continue; + self.service(host, conn) catch conn.close(self.allocator); + } + } + + fn service(self: *Control, host: *Host, conn: *Connection) !void { + // Bound work per frame, including clients which continually write. + for (0..32) |_| { + var progressed = false; + if (conn.used != 0) { + const consumed = conn.server.push(conn.incoming[0..conn.used]); + std.mem.copyForwards(u8, &conn.incoming, conn.incoming[consumed..conn.used]); + conn.used -= consumed; + progressed = consumed != 0; + } + while (conn.server.retry()) |req| self.answer(host, conn, req, true); + while (conn.server.next()) |req| self.answer(host, conn, req, false); + if (conn.server.protocol.dead) return error.Closed; + if (conn.server.output().len > 0) { + if (try transport.write(conn.fd, conn.server.output())) |n| { + if (n == 0) return error.Closed; + conn.server.wrote(n); + progressed = true; + } + } + if (conn.used < conn.incoming.len) { + if (try transport.read(conn.fd, conn.incoming[conn.used..])) |n| { + if (n == 0) return error.Closed; + conn.used += n; + progressed = true; + } + } + if (!progressed) break; + } + } + + /// Answers a request, or parks it: an open of screen.png asks for the + /// next frame and waits for it, asked again (`retried`) each frame + /// until the capture has landed. + fn answer(self: *Control, host: *Host, conn: *Connection, req: fs.Req, retried: bool) void { + var reply: fs.Reply = .{ .tag = req.tag }; + var buffer: [msize]u8 = undefined; + if (!retried and req.op == .open and req.node == @backingInt(File.@"screen.png")) { + self.capture_pending = true; + self.capture_error = null; + } + const bytes = self.respond(host, conn, req, &reply, &buffer) catch |err| blk: { + reply = if (err == error.CapturePending) .{ .tag = req.tag, .status = .again } else .{ .tag = req.tag, .status = .err, .errno = fs.E.INVAL, .ename = if (err == error.SketchNotRunning) self.offline.?.from else @errorName(err) }; + break :blk &.{}; + }; + conn.server.reply(&reply, bytes); + } + + fn respond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { + // Nodes from 2^63 are the sketch's: none is served but sketch/. + if (req.node > @backingInt(File.sketch)) return error.UnknownFile; + if (req.node & @as(u64, @bitCast(state_files.root)) != 0) return stateRespond(self, host, conn, req, reply, buffer); + const file = std.enums.fromInt(File, req.node) orelse return error.UnknownFile; + if (self.offline != null and (req.op == .write or (req.op == .open or req.op == .read) and file != .README and file != .commands and file != .ctl and file != .@"/" and file != .sketch)) return error.SketchNotRunning; + switch (req.op) { + .lookup => { + if (file == .sketch) return error.UnknownFile; + if (file != .@"/") return error.NotDirectory; + const found = std.meta.stringToEnum(File, req.data) orelse return error.UnknownFile; + if (found == .@"/") return error.UnknownFile; + reply.attr = self.attr(@backingInt(found)); + }, + .getattr => reply.attr = self.attr(req.node), + .open => { + if (file == .@"screen.png" or file == .@"profile-trace" or file == .@"timing-trace" or file == .@"presentation-trace" or file == .README or file == .commands) { + if (file == .@"screen.png") { + if (self.capture_pending) return error.CapturePending; + if (self.capture_error) |err| return err; + if (self.png.len == 0) return error.CaptureScreenshotFirst; + } + for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) { + slot.* = switch (file) { + .@"screen.png" => try self.allocator.dupe(u8, self.png), + .@"profile-trace" => try self.trace(host), + .@"timing-trace" => try self.timingTrace(host), + .README, .commands => blk: { + var text: std.Io.Writer.Allocating = .init(self.allocator); + defer text.deinit(); + const layout = if (self.offline) |o| o.layout else host.state.layout; + if (file == .README) try readme(layout, &text.writer) else try verbLines(&text.writer, "", if (self.offline) |o| o.completions else host.completions); + break :blk try text.toOwnedSlice(); + }, + else => try self.presentationTrace(host), + }; + reply.handle = @intCast(handle); + return &.{}; + }; + return error.TooManySnapshots; + } + }, + .release => if (req.opened and req.handle > 0 and req.handle <= conn.snapshots.len) { + const slot = &conn.snapshots[req.handle - 1]; + if (slot.*) |bytes| self.allocator.free(bytes); + slot.* = null; + }, + .readdir => { + if (file == .sketch) return &.{}; + if (file != .@"/") return error.NotDirectory; + var used: usize = 0; + for (std.enums.values(File)[1..], 0..) |f, index| { + if (index < req.off) continue; + const name = @tagName(f); + std.mem.writeInt(u64, buffer[used..][0..8], @backingInt(f), .little); + buffer[used + 8] = @intFromBool(f == .state or f == .sketch); + buffer[used + 9] = @intCast(name.len); + @memcpy(buffer[used + 10 ..][0..name.len], name); + used += 10 + name.len; + } + return buffer[0..used]; + }, + .read => { + var writer: std.Io.Writer = .fixed(buffer); + const bytes: []const u8 = switch (file) { + .ctl => "Write a verb a line: commands lists them, README says what each does.\n", + .status => blk: { + try writer.print("name {s}\nsize {d} {d}\nmode {t}\ngpu {s}\nscreenshot_pending {}\nscreenshot_generation {d}\nscreenshot_error {s}\n", .{ host.name, host.canvas[0], host.canvas[1], host.timeline.mode, if (host.gpu != null) "yes" else "no", self.capture_pending, self.generation, if (self.capture_error) |err| @errorName(err) else "none" }); + break :blk writer.buffered(); + }, + .log => blk: { + for (host.console.lines) |line| { + try writer.writeAll(line.bytes[0..line.len]); + try writer.writeByte('\n'); + } + break :blk writer.buffered(); + }, + .@"screen.png", .@"profile-trace", .@"timing-trace", .@"presentation-trace", .README, .commands => if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle, + .profile => blk: { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + try writer.print("# CPU wall milliseconds including GPU submission and fence wait; not hardware timestamps\n# frames {d}\nlabel\tcount\tmean_ms\tworst_ms\tp95_ms\tp99_ms\n", .{gpu.frame - profile.start_frame}); + var entries = profile.entries.iterator(); + while (entries.next()) |entry| { + const value = entry.value_ptr; + const percentiles = value.percentiles(); + const count: f64 = @floatFromInt(@max(value.count, 1)); + try writer.print("{s}\t{d}\t{d:.6}\t{d:.6}\t{d:.6}\t{d:.6}\n", .{ entry.key_ptr.*, value.count, @as(f64, @floatFromInt(value.total)) / count / std.time.ns_per_ms, @as(f64, @floatFromInt(value.worst)) / std.time.ns_per_ms, @as(f64, @floatFromInt(percentiles[0])) / std.time.ns_per_ms, @as(f64, @floatFromInt(percentiles[1])) / std.time.ns_per_ms }); + } + break :blk writer.buffered(); + }, + .@"/", .state, .sketch => return error.NotFile, + }; + return window(bytes, req.off, req.size); + }, + .write => { + if (file != .ctl) return error.ReadOnly; + if (req.data.len > 4096 or std.mem.indexOfScalar(u8, req.data, 0) != null) return error.InvalidCommand; + var lines = std.mem.tokenizeScalar(u8, req.data, '\n'); + while (lines.next()) |raw| { + const line = std.mem.trim(u8, raw, " \t\r"); + const verb = std.meta.stringToEnum(Verb, line) orelse { + var words = std.mem.tokenizeAny(u8, line, " \t"); + const input = std.meta.stringToEnum(script.Verb, words.next() orelse ""); + if (input != null and script.verbs.get(input.?).input) { + try script.input(host, input.?, &words, c.SDL_GetTicksNS()); + } else try host.runLine(line, c.SDL_GetTicksNS()); + continue; + }; + switch (verb) { + .screenshot => { + self.capture_pending = true; + self.capture_error = null; + }, + .@"profile-reset" => { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + for (profile.entries.values()) |*entry| entry.* = .{}; + profile.start_frame = gpu.frame; + }, + } + } + reply.written = @intCast(req.data.len); + }, + .setattr => { + if (file != .ctl or !req.truncate) return error.ReadOnly; + reply.attr = self.attr(req.node); + }, + } + return &.{}; + } + + fn trace(self: *Control, host: *Host) ![]u8 { + const gpu = if (host.passes) |*gpu| gpu else return error.ProfilingDisabled; + const profile = if (gpu.profile) |*profile| profile else return error.ProfilingDisabled; + const buffer = try self.allocator.alloc(u8, 256 << 10); + defer self.allocator.free(buffer); + var writer: std.Io.Writer = .fixed(buffer); + try writer.writeAll("label,sample,ns\n"); + var entries = profile.entries.iterator(); + while (entries.next()) |entry| { + const value = entry.value_ptr; + for (0..value.retained()) |i| { + try writer.writeByte('"'); + for (entry.key_ptr.*) |ch| { + if (ch == '"') try writer.writeByte('"'); + try writer.writeByte(ch); + } + try writer.print("\",{d},{d}\n", .{ value.count - value.retained() + i + 1, value.sample(i) }); + } + } + return self.allocator.dupe(u8, writer.buffered()); + } + + fn timingTrace(self: *Control, host: *const Host) ![]u8 { + var csv: std.ArrayList(u8) = .empty; + errdefer csv.deinit(self.allocator); + try csv.appendSlice(self.allocator, "index,frame_ns,work_ns,outside_work_ns\n"); + for (0..host.timing.count) |i| { + const sample = host.timing.sampleAt(i); + try csv.print(self.allocator, "{d},{d},{d},{d}\n", .{ i, sample.frame, sample.work, sample.frame -| sample.work }); + } + return csv.toOwnedSlice(self.allocator); + } + + fn presentationTrace(self: *Control, host: *const Host) ![]u8 { + var csv: std.Io.Writer.Allocating = .init(self.allocator); + defer csv.deinit(); + try Presentation.csv(host.presentation, &csv.writer); + if (csv.written().len > 2 << 20) return error.TraceTooLarge; + return csv.toOwnedSlice(); + } + + fn attr(self: *Control, node: u64) fs.Attr { + const file: File = @fromBackingInt(@intCast(node)); + return .{ .name = @tagName(file), .node = node, .dir = file == .@"/" or file == .state or file == .sketch, .mode = files.get(file).mode, .size = if (file == .@"screen.png") self.png.len else 0, .version = if (file == .@"screen.png") @truncate(self.generation) else 0 }; + } + + pub fn capture(self: *Control, surface: *c.SDL_Surface) void { + self.capture_pending = false; + self.save(surface) catch |err| { + self.capture_error = err; + }; + } + + fn save(self: *Control, surface: *c.SDL_Surface) !void { + const stream = c.SDL_IOFromDynamicMem() orelse return error.CaptureStream; + defer _ = c.SDL_CloseIO(stream); + if (!c.SDL_SavePNG_IO(surface, stream, false)) return error.EncodePng; + const size = c.SDL_GetIOSize(stream); + if (size <= 0) return error.EncodePng; + const pointer = c.SDL_GetPointerProperty(c.SDL_GetIOProperties(stream), c.SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, null) orelse return error.EncodePng; + const png = try self.allocator.dupe(u8, @as([*]const u8, @ptrCast(pointer))[0..@intCast(size)]); + self.allocator.free(self.png); + self.png = png; + self.generation += 1; + } +}; + +/// The README: a line a file, ctl's verbs indented under it and the +/// State's files under state/, in the shape cloud9's `help` reads. +fn readme(layout: @import("memory.zig").Layout, w: *std.Io.Writer) !void { + for (std.enums.values(File)[1..]) |f| { + const name = @tagName(f); + const dir = f == .state or f == .sketch; + try w.print("{s}{s}", .{ name, if (dir) "/" else "" }); + try w.splatByteAll(' ', @max(1, 41 -| (name.len + @intFromBool(dir)))); + try w.print("{s}\n", .{files.get(f).doc}); + if (f == .ctl) try verbLines(w, " ", &.{}); + if (f == .state) try state_files.readme(layout, w); + } +} + +/// A line a verb ctl takes: ctl's own, the input verbs, the console's, +/// then `sketch`'s words. +fn verbLines(w: *std.Io.Writer, indent: []const u8, sketch: []const []const u8) !void { + for (std.enums.values(Verb)) |v| try verbLine(w, indent, @tagName(v), "", verbs.get(v)); + for (std.enums.values(script.Verb)) |v| if (script.verbs.get(v).input) try verbLine(w, indent, @tagName(v), script.verbs.get(v).args, script.verbs.get(v).doc); + for (std.enums.values(Console.Verb)) |v| try verbLine(w, indent, @tagName(v), Console.verbs.get(v).args, Console.verbs.get(v).doc); + for (sketch) |word| try w.print("{s}{s}\n", .{ indent, word }); +} + +fn verbLine(w: *std.Io.Writer, indent: []const u8, name: []const u8, args: []const u8, doc: []const u8) !void { + try w.print("{s}{s}{s}{s}", .{ indent, name, if (args.len > 0) " " else "", args }); + try w.splatByteAll(' ', @max(1, 37 -| (name.len + args.len + @intFromBool(args.len > 0)))); + try w.print("{s}\n", .{doc}); +} + +/// Under `state/`: the live State as files (state_files.zig), a write +/// run as the console's `set`. +fn stateRespond(self: *Control, host: *Host, conn: *Connection, req: fs.Req, reply: *fs.Reply, buffer: []u8) ![]const u8 { + const layout = if (self.offline) |o| o.layout else host.state.layout; + const node: state_files.Node = @bitCast(req.node); + if (!state_files.valid(layout, node)) return error.StateLayoutChangedWalkAgain; + const dir = state_files.isDir(layout, node); + const mode: u16 = if (dir) 0o700 else if (state_files.writable(layout, node)) 0o600 else 0o400; + switch (req.op) { + .lookup => { + if (!dir) return error.NotDirectory; + const child = state_files.lookup(layout, node, req.data) orelse return error.UnknownField; + const child_dir = state_files.isDir(layout, child); + reply.attr = .{ .node = @bitCast(child), .dir = child_dir, .mode = if (child_dir) 0o700 else if (state_files.writable(layout, child)) 0o600 else 0o400 }; + }, + .getattr => reply.attr = .{ .node = req.node, .dir = dir, .mode = mode }, + .setattr => { + if (mode != 0o600 or !req.truncate) return error.ReadOnly; + reply.attr = .{ .node = req.node, .mode = mode }; + }, + // A value is read as it was at the open: it changes every frame, + // and its length with it. + .open => if (!dir) for (&conn.snapshots, 1..) |*slot, handle| if (slot.* == null) { + var text: std.Io.Writer.Allocating = .init(self.allocator); + defer text.deinit(); + try state_files.read(layout, if (self.offline) |o| o.bytes else host.live(), node, &text.writer); + slot.* = try text.toOwnedSlice(); + reply.handle = @intCast(handle); + break; + } else return error.TooManySnapshots, + .release => if (req.opened and req.handle > 0 and req.handle <= conn.snapshots.len) { + const slot = &conn.snapshots[req.handle - 1]; + if (slot.*) |bytes| self.allocator.free(bytes); + slot.* = null; + }, + .readdir => { + if (!dir) return error.NotDirectory; + return buffer[0..state_files.readdir(layout, node, req.off, buffer)]; + }, + .read => { + if (dir) return error.NotFile; + const bytes = if (req.handle > 0 and req.handle <= conn.snapshots.len) conn.snapshots[req.handle - 1] orelse return error.InvalidHandle else return error.InvalidHandle; + return window(bytes, req.off, req.size); + }, + .write => { + if (mode != 0o600) return error.ReadOnly; + if (self.offline != null) return error.SketchNotRunning; + var w: std.Io.Writer = .fixed(buffer); + w.writeAll("set ") catch unreachable; + state_files.path(layout, node, &w) catch return error.ValueTooLong; + w.writeByte(' ') catch return error.ValueTooLong; + for (std.mem.trim(u8, req.data, " \t\r\n")) |byte| w.writeByte(if (byte == '\n') ' ' else byte) catch return error.ValueTooLong; + try host.runLine(w.buffered(), c.SDL_GetTicksNS()); + reply.written = @intCast(req.data.len); + }, + } + return &.{}; +} + +/// The State `fs` shows with no sketch running, and its words for what a +/// running one would answer. +pub const Offline = struct { + layout: @import("memory.zig").Layout, + bytes: []const u8, + completions: []const []const u8, + /// "drat-meout is not running: ..." + from: []const u8, +}; + +/// `$XDG_RUNTIME_DIR/9p/sketchbook/<name>`, where a sketch posts its tree. +pub fn registered(environ: *const std.process.Environ.Map, name: []const u8, buf: []u8) ![:0]u8 { + if (!c9.post.legalName(name)) return error.IllegalName; + const runtime = environ.get("XDG_RUNTIME_DIR") orelse return error.NotRuntimeDir; + return std.fmt.bufPrintSentinel(buf, "{s}/9p/sketchbook/{s}", .{ runtime, name }, 0) catch error.NameTooLong; +} + +fn window(bytes: []const u8, offset: u64, count: u32) []const u8 { + if (offset >= bytes.len) return &.{}; + const start: usize = @intCast(offset); + return bytes[start..][0..@min(count, bytes.len - start)]; +} + +test "9P reads respect offsets, counts and EOF" { + try std.testing.expectEqualStrings("cd", window("abcdef", 2, 2)); + try std.testing.expectEqualStrings("ef", window("abcdef", 4, 200)); + try std.testing.expectEqualStrings("", window("abcdef", std.math.maxInt(u64), 10)); +} + +test "control writes validate and screenshot opens pin an immutable snapshot" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; // These backend operations do not access the host. + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [256]u8 = undefined; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .setattr, .node = 2, .truncate = true }, &reply, &buf); + try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 3, .data = "screenshot" }, &reply, &buf)); + try std.testing.expectError(error.InvalidCommand, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "screenshot\x00" }, &reply, &buf)); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "screenshot\n" }, &reply, &buf); + try std.testing.expect(control.capture_pending); + try std.testing.expectError(error.CapturePending, control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 5 }, &reply, &buf)); + control.capture_pending = false; + control.png = try a.dupe(u8, "first image"); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 5 }, &reply, &buf); + const handle = reply.handle; + a.free(control.png); + control.png = try a.dupe(u8, "second image"); + defer a.free(control.png); + const bytes = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 5, .handle = handle, .size = 100 }, &reply, &buf); + try std.testing.expectEqualStrings("first image", bytes); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 5, .handle = handle, .opened = true }, &reply, &buf); + try std.testing.expect(conn.snapshots[handle - 1] == null); +} + +test "control writes reset profiling counters while retaining labels" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; + host.passes = null; + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [512]u8 = undefined; + const reset: fs.Req = .{ .tag = 1, .op = .write, .node = 2, .data = "profile-reset\n" }; + try std.testing.expectError(error.ProfilingDisabled, control.respond(&host, &conn, reset, &reply, &buf)); + host.passes = @as(@import("gpu"), undefined); + const gpu = &host.passes.?; + gpu.profile = .init(a); + defer gpu.profile.?.deinit(); + const profile = &gpu.profile.?; + try profile.entries.put(profile.arena.allocator(), "scene", .{}); + profile.entries.values()[0].add(400_000); + profile.entries.values()[0].add(600_000); + gpu.frame = 420; + const read: fs.Req = .{ .tag = 1, .op = .read, .node = 6, .size = buf.len }; + const before = try control.respond(&host, &conn, read, &reply, &buf); + try std.testing.expect(std.mem.indexOf(u8, before, "scene\t2\t0.500000\t0.600000") != null); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 7 }, &reply, &buf); + const trace_handle = reply.handle; + _ = try control.respond(&host, &conn, reset, &reply, &buf); + const trace = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 7, .handle = trace_handle, .size = 100 }, &reply, &buf); + try std.testing.expectEqualStrings("label,sample,ns\n\"scene\",1,400000\n\"scene\",2,600000\n", trace); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 7, .handle = trace_handle, .opened = true }, &reply, &buf); + try std.testing.expect(conn.snapshots[trace_handle - 1] == null); + try std.testing.expectEqual(@as(usize, 1), profile.entries.count()); + try std.testing.expectEqual(@as(u64, 0), profile.entries.values()[0].total); + try std.testing.expectEqual(@as(u64, 420), profile.start_frame); + const after = try control.respond(&host, &conn, read, &reply, &buf); + try std.testing.expect(std.mem.indexOf(u8, after, "# frames 0") != null); + try std.testing.expect(std.mem.indexOf(u8, after, "scene\t0\t0.000000\t0.000000") != null); +} + +test "control writes profiling ring retains latest samples and nearest rank percentiles" { + var entry: @import("gpu").Profile.Entry = .{}; + for (1..4100) |ns| entry.add(ns); + try std.testing.expectEqual(@as(u64, 4099), entry.count); + try std.testing.expectEqual(@as(usize, 2048), entry.retained()); + try std.testing.expectEqual(@as(u64, 2052), entry.sample(0)); + try std.testing.expectEqual(@as(u64, 4099), entry.sample(2047)); + try std.testing.expectEqual([2]u64{ 3997, 4079 }, entry.percentiles()); + entry = .{}; + try std.testing.expectEqual(@as(usize, 0), entry.retained()); + try std.testing.expectEqual([2]u64{ 0, 0 }, entry.percentiles()); + entry.add(20); + try std.testing.expectEqual([2]u64{ 20, 20 }, entry.percentiles()); +} + +test "control writes reject timing trace mutations and opens pin chronological samples" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; + host.timing = .{}; + for (0..@import("Timing.zig").capacity + 2) |i| host.timing.add(.{ .frame = i + 1, .work = i }); + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [256]u8 = undefined; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1, .data = "timing-trace" }, &reply, &buf); + try std.testing.expectEqual(@as(u64, 8), reply.attr.node); + try std.testing.expectEqual(@as(u16, 0o400), reply.attr.mode); + try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 8, .data = "reset" }, &reply, &buf)); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 8 }, &reply, &buf); + const first = reply.handle; + host.timing = .{}; + host.timing.add(.{ .frame = 4, .work = 7 }); + const original = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 8, .handle = first, .size = 100 }, &reply, &buf); + try std.testing.expect(std.mem.startsWith(u8, original, "index,frame_ns,work_ns,outside_work_ns\n0,3,2,1\n1,4,3,1\n")); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 8 }, &reply, &buf); + const second = reply.handle; + const latest = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 8, .handle = second, .size = 100 }, &reply, &buf); + try std.testing.expectEqualStrings("index,frame_ns,work_ns,outside_work_ns\n0,4,7,0\n", latest); + for ([_]u32{ first, second }) |handle| { + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 8, .handle = handle, .opened = true }, &reply, &buf); + try std.testing.expect(conn.snapshots[handle - 1] == null); + } +} + +test "control writes reject presentation trace mutations and opens own independent snapshots" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; + host.presentation = null; + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [512]u8 = undefined; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1, .data = "presentation-trace" }, &reply, &buf); + try std.testing.expectEqual(@as(u64, 9), reply.attr.node); + try std.testing.expectEqual(@as(u16, 0o400), reply.attr.mode); + try std.testing.expectError(error.ReadOnly, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 9, .data = "reset" }, &reply, &buf)); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 9 }, &reply, &buf); + const first = reply.handle; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = 9 }, &reply, &buf); + const second = reply.handle; + try std.testing.expect(conn.snapshots[first - 1].?.ptr != conn.snapshots[second - 1].?.ptr); + const original = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 9, .handle = first, .size = 4096 }, &reply, &buf); + try std.testing.expect(std.mem.startsWith(u8, original, "# status ")); + try std.testing.expect(std.mem.indexOf(u8, original, "disabled_or_allocation_failed") != null); + try std.testing.expect(std.mem.endsWith(u8, original, "frame_id,sample_ms,commit_ns,result,presented_ns,refresh_ns,sequence,flags\n")); + try std.testing.expect(std.mem.indexOfScalar(u8, original, 0) == null); + const expected = try a.dupe(u8, original); + defer a.free(expected); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 9, .handle = first, .opened = true }, &reply, &buf); + const retained = try control.respond(&host, &conn, .{ .tag = 1, .op = .read, .node = 9, .handle = second, .size = 4096 }, &reply, &buf); + try std.testing.expectEqualStrings(expected, retained); + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = 9, .handle = second, .opened = true }, &reply, &buf); + try std.testing.expect(conn.snapshots[first - 1] == null and conn.snapshots[second - 1] == null); +} + +test "control writes take input verbs live, and the README and commands read the tables" { + const a = std.testing.allocator; + var control: Control = .{ .allocator = a, .path = undefined, .listener = -1 }; + var host: Host = undefined; + host.gpa = a; + host.console = .{}; + host.input = .{}; + host.pending = .empty; + defer host.pending.deinit(a); + const S = struct { scene: enum(u8) { room, street } = .room, talk: struct { text: [3]u8 = @splat(0) } = .{} }; + var arena: std.heap.ArenaAllocator = .init(a); + defer arena.deinit(); + host.state.layout = try @import("memory.zig").Layout.copy(arena.allocator(), @import("abi").layout.of(S, .state)); + host.completions = &.{ "scene room", "scene street" }; + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [512]u8 = undefined; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "click 10 20\nhold left_shift\n" }, &reply, &buf); + try std.testing.expectEqual(@as(usize, 4), host.pending.items.len); + try std.testing.expectEqual(abi.Event.Kind.mouse_up, host.pending.items[2].kind); + try std.testing.expectEqual(@as(f32, 20), host.pending.items[2].y); + try std.testing.expectEqual(abi.Event.Kind.key_down, host.pending.items[3].kind); + try std.testing.expectError(error.ExpectedPoint, control.respond(&host, &conn, .{ .tag = 1, .op = .write, .node = 2, .data = "click 10" }, &reply, &buf)); + for ([_]File{ .README, .commands }) |f| { + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .open, .node = @backingInt(f) }, &reply, &buf); + const handle = reply.handle; + const text = conn.snapshots[handle - 1].?; + if (f == .README) { + try std.testing.expect(std.mem.startsWith(u8, text, "ctl write a verb a line")); + try std.testing.expect(std.mem.indexOf(u8, text, "\n screenshot captures the next frame") != null); + try std.testing.expect(std.mem.indexOf(u8, text, "\n click X Y move there") != null); + try std.testing.expect(std.mem.indexOf(u8, text, "\n set FIELD VALUE... a field to a value") != null); + try std.testing.expect(std.mem.indexOf(u8, text, "\nstate/ the sketch's State") != null); + try std.testing.expect(std.mem.indexOf(u8, text, "\nsketch/ the sketch's own files") != null); + try std.testing.expect(std.mem.endsWith(u8, text, "\nstate/scene .room .street\nstate/talk/text 3 u8, blank-separated\n")); + } else { + try std.testing.expect(std.mem.startsWith(u8, text, "screenshot captures")); + try std.testing.expect(std.mem.endsWith(u8, text, "\nscene room\nscene street\n")); + } + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .release, .node = @backingInt(f), .handle = handle, .opened = true }, &reply, &buf); + } +} + +test "control writes leave the sketch's directory and nodes alone" { + var control: Control = .{ .allocator = std.testing.allocator, .path = undefined, .listener = -1 }; + var host: Host = undefined; + var conn: Connection = .{}; + var reply: fs.Reply = .{ .tag = 1 }; + var buf: [256]u8 = undefined; + _ = try control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1, .data = "sketch" }, &reply, &buf); + try std.testing.expect(reply.attr.dir and reply.attr.node == 1 << 63); + try std.testing.expectEqual(@as(usize, 0), (try control.respond(&host, &conn, .{ .tag = 1, .op = .readdir, .node = 1 << 63 }, &reply, &buf)).len); + try std.testing.expectError(error.UnknownFile, control.respond(&host, &conn, .{ .tag = 1, .op = .lookup, .node = 1 << 63, .data = "buffer" }, &reply, &buf)); + for ([_]u64{ (1 << 63) + 1, (1 << 63) | (1 << 62), std.math.maxInt(u64) }) |node| try std.testing.expectError(error.UnknownFile, control.respond(&host, &conn, .{ .tag = 1, .op = .getattr, .node = node }, &reply, &buf)); +} diff --git a/src/host/fields.zig b/src/host/fields.zig index 2e70414..fc1a1e7 100644 --- a/src/host/fields.zig +++ b/src/host/fields.zig @@ -123,6 +123,25 @@ pub fn set(leaf: Leaf, memory: []u8, text: []const u8) !void { } } +/// Sets every value of the row of scalars `path` names (an array of +/// numbers, a vector, `grid[1]` of a `grid: [3][2]u16`), in memory order, +/// from `text`'s words, one each; nothing when one is not a value. +pub fn setRow(layout: Layout, memory: []u8, path: []const u8, text: []const u8) !void { + for (layout.fields) |leaf| { + if (leaf.whole() or !std.mem.endsWith(u8, leaf.path, "[]")) continue; + const at = abi.layout.match(leaf.path[0 .. leaf.path.len - 2], leaf.dims[0 .. leaf.dims.len - 1], path) orelse continue; + const d = leaf.dims[leaf.dims.len - 1]; + var scratch: [16]u8 = undefined; + var words = std.mem.tokenizeAny(u8, text, " \t\n"); + for (0..d.count) |_| try set(leaf.at(0), &scratch, words.next() orelse return error.TooFewValues); + if (words.next() != null) return error.TooManyValues; + words.reset(); + for (0..d.count) |i| try set(leaf.at(leaf.offset + at + i * d.stride), memory, words.next().?); + return; + } + return error.UnsupportedField; +} + /// True when the field's formatted value equals `text` (enum names compare /// with or without the leading dot). pub fn matches(leaf: Leaf, memory: []const u8, text: []const u8) bool { @@ -206,6 +225,24 @@ test "fields format, parse and compare by kind" { try std.testing.expectError(error.InvalidValue, set(layout.find("x").?, bytes, "banana")); } +test "a row of scalars is set from its words, all or none" { + const S = struct { grid: [3][2]i16 = @splat(@splat(0)), pos: @Vector(2, f32) = .{ 0, 0 } }; + 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 = .{}; + const bytes = std.mem.asBytes(&s); + try setRow(layout, bytes, "grid[1]", "4 -5"); + try std.testing.expectEqual([2]i16{ 4, -5 }, s.grid[1]); + try setRow(layout, bytes, "pos", "1.5\n2"); + try std.testing.expectEqual(@as(f32, 2), s.pos[1]); + try std.testing.expectError(error.TooFewValues, setRow(layout, bytes, "grid[2]", "7")); + try std.testing.expectError(error.TooManyValues, setRow(layout, bytes, "grid[2]", "7 8 9")); + try std.testing.expectError(error.InvalidValue, setRow(layout, bytes, "grid[2]", "7 x")); + try std.testing.expectEqual([2]i16{ 0, 0 }, s.grid[2]); + try std.testing.expectError(error.UnsupportedField, setRow(layout, bytes, "grid", "1 2")); +} + 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); diff --git a/src/host/gfx.zig b/src/host/gfx.zig index a7ebd05..d679067 100644 --- a/src/host/gfx.zig +++ b/src/host/gfx.zig @@ -2,6 +2,7 @@ const std = @import("std"); const c = @import("c"); const abi = @import("abi"); +const Glyphs = @import("Glyphs.zig"); pub const Images = struct { gpa: std.mem.Allocator, @@ -12,6 +13,7 @@ pub const Images = struct { const Entry = struct { texture: ?*c.SDL_Texture = null, + gpu_texture: ?*anyopaque = null, w: u32 = 0, h: u32 = 0, /// Holds premultiplied colour: a target drawn into, a GPU pass's @@ -79,6 +81,7 @@ pub const Images = struct { /// Makes `image` show a GPU render target (GPU renderer only). pub fn wrap(self: *Images, image: abi.Image, gpu_texture: *anyopaque, w: u32, h: u32) !void { const entry = self.get(image) orelse return error.UnknownImage; + if (entry.gpu_texture == gpu_texture) return; const props = c.SDL_CreateProperties(); defer c.SDL_DestroyProperties(props); _ = c.SDL_SetPointerProperty(props, c.SDL_PROP_TEXTURE_CREATE_GPU_TEXTURE_POINTER, gpu_texture); @@ -89,7 +92,7 @@ pub const Images = struct { // Rendered output: smooth scaling, and already premultiplied. _ = c.SDL_SetTextureScaleMode(texture, c.SDL_SCALEMODE_LINEAR); entry.release(); - entry.* = .{ .texture = texture, .w = w, .h = h, .premultiplied = true, .smooth = true }; + entry.* = .{ .texture = texture, .w = w, .h = h, .premultiplied = true, .smooth = true, .gpu_texture = gpu_texture }; } /// Stops showing a wrapped render target, before the target goes away. @@ -97,6 +100,7 @@ pub const Images = struct { const entry = self.get(image) orelse return; if (entry.texture) |t| c.SDL_DestroyTexture(t); entry.texture = null; + entry.gpu_texture = null; } pub fn free(self: *Images, image: abi.Image) void { @@ -169,6 +173,7 @@ pub const Painter = struct { backdrop_state: ?*c.SDL_GPURenderState = null, backdrop_shader: ?*c.SDL_GPUShader = null, backdrop_hash: u64 = 0, + glyphs: Glyphs, const Layer = struct { big: ?*c.SDL_Texture = null, @@ -196,6 +201,7 @@ pub const Painter = struct { } pub fn deinit(p: *Painter) void { + p.glyphs.deinit(); if (p.backdrop_texture) |t| c.SDL_DestroyTexture(t); if (p.backdrop_state) |state| c.SDL_DestroyGPURenderState(state); if (p.backdrop_shader) |shader| if (c.SDL_GetGPURendererDevice(p.renderer)) |device| c.SDL_ReleaseGPUShader(device, shader); @@ -217,6 +223,7 @@ pub const Painter = struct { /// Back to the canvas, blending alpha, unclipped: where overlays draw /// and screenshots read. pub fn end(p: *Painter) void { + p.glyphs.flush(p.renderer); p.flush(); if (p.target != .none) { p.target = .none; @@ -228,11 +235,11 @@ pub const Painter = struct { /// Draws one graphics command. Other commands are not graphics: ignored. pub fn draw(p: *Painter, command: abi.Command) void { + if (command != .text) p.glyphs.flush(p.renderer); 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)); @@ -246,10 +253,7 @@ pub const Painter = struct { .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), .backdrop => |b| p.backdrop(b.dst, b.fragment, b.uniforms), - .text => |text| { - _ = c.SDL_SetRenderDrawBlendMode(renderer, c.SDL_BLENDMODE_BLEND); - drawText(renderer, text.x, text.y, text.scale, text.bytes, text.color, text.face, if (text.raster > 0) text.raster else text.scale); - }, + .text => |text| p.glyphs.draw(renderer, text), .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) }; @@ -407,11 +411,19 @@ pub const Painter = struct { _ = c.SDL_SetGPURenderState(p.renderer, null); } + fn missing(p: *Painter, dst: abi.Rect) void { + _ = c.SDL_SetRenderDrawBlendMode(p.renderer, c.SDL_BLENDMODE_BLEND); + setColor(p.renderer, .{ .r = 110, .g = 30, .b = 70 }); + _ = c.SDL_RenderFillRect(p.renderer, &frect(dst)); + p.glyphs.draw(p.renderer, .{ .x = dst.x + 4, .y = dst.y + 4, .scale = 1, .bytes = "missing image", .color = .{ .r = 255, .g = 210, .b = 240 } }); + p.glyphs.flush(p.renderer); + } + 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 t = texture orelse return p.missing(dst); const e = entry.?; if (handle == p.target) return; // Drawing into itself: nothing. if (e.stale) p.resolve(e); @@ -431,6 +443,19 @@ pub const Painter = struct { } else _ = c.SDL_RenderTexture(renderer, t, srcp, &frect(dst)); } + /// One atlas batch; vertex colors are premultiplied when the image is. + pub fn texturedTriangles(p: *Painter, image: abi.Image, vertices: []const c.SDL_Vertex, indices: []const u16) void { + if (vertices.len == 0 or indices.len == 0 or vertices.len > std.math.maxInt(c_int) or indices.len > std.math.maxInt(c_int)) return; + const entry = p.images.get(image) orelse return; + const texture = entry.texture orelse return; + if (image == p.target) return; + p.flush(); + if (entry.stale) p.resolve(entry); + _ = c.SDL_SetTextureBlendMode(texture, textureBlend(p.blend, entry.premultiplied)); + const stride = @sizeOf(c.SDL_Vertex); + _ = c.SDL_RenderGeometryRaw(p.renderer, texture, @ptrCast(&vertices[0].position), stride, &vertices[0].color, stride, @ptrCast(&vertices[0].tex_coord), stride, @intCast(vertices.len), indices.ptr, @intCast(indices.len), @sizeOf(u16)); + } + /// `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. @@ -588,45 +613,14 @@ fn fcolor(color: abi.Color) c.SDL_FColor { }; } -/// Native shaped text on Linux, using a 13px monospace face at scale 1. -/// Other targets retain SDL's built-in 8x8 fallback. Coordinates are line-box tops. -pub fn drawText(renderer: *c.SDL_Renderer, x: f32, y: f32, scale: f32, bytes: []const u8, color: abi.Color, face: abi.Face, raster: f32) void { - if (@import("builtin").os.tag == .linux and c.sk_text_draw_scaled(renderer, @intFromEnum(face), x, y, scale, raster, bytes.ptr, bytes.len, color.r, color.g, color.b, color.a)) return; - var buf: [1024]u8 = undefined; - const len = @min(std.mem.indexOfScalar(u8, bytes, 0) orelse bytes.len, buf.len - 1); - @memcpy(buf[0..len], bytes[0..len]); - buf[len] = 0; - setColor(renderer, color); - var sx: f32 = 1; - var sy: f32 = 1; - _ = c.SDL_GetRenderScale(renderer, &sx, &sy); - const s = @max(scale, 1); - // The clip rect scales with the renderer: shrink it to match, to the - // whole pixels around it (so it may let up to `s - 1` pixels more through). - var clip: c.SDL_Rect = undefined; - const clipped = c.SDL_RenderClipEnabled(renderer) and c.SDL_GetRenderClipRect(renderer, &clip); - _ = c.SDL_SetRenderScale(renderer, sx * s, sy * s); - if (clipped and s != 1) { - const x0 = @floor(@as(f32, @floatFromInt(clip.x)) / s); - const y0 = @floor(@as(f32, @floatFromInt(clip.y)) / s); - const x1 = @ceil(@as(f32, @floatFromInt(clip.x + clip.w)) / s); - const y1 = @ceil(@as(f32, @floatFromInt(clip.y + clip.h)) / s); - const scaled: c.SDL_Rect = .{ .x = @intFromFloat(x0), .y = @intFromFloat(y0), .w = @intFromFloat(x1 - x0), .h = @intFromFloat(y1 - y0) }; - _ = c.SDL_SetRenderClipRect(renderer, &scaled); - } - _ = c.SDL_RenderDebugText(renderer, x / s, y / s, &buf); - _ = c.SDL_SetRenderScale(renderer, sx, sy); - if (clipped and s != 1) _ = c.SDL_SetRenderClipRect(renderer, &clip); -} - -/// What `drawText` gives `bytes` at `scale`: its width and the height of -/// its line, in canvas units (`Host.textSize`). -pub fn textSize(renderer: *c.SDL_Renderer, scale: f32, bytes: []const u8, face: abi.Face) [2]f32 { - var size: [2]f32 = .{ 0, 0 }; - if (@import("builtin").os.tag == .linux and c.sk_text_measure_face(renderer, @intFromEnum(face), scale, bytes.ptr, bytes.len, &size[0], &size[1])) return size; - // The debug font's 8 px cells. - const s = @max(scale, 1); - return .{ @as(f32, @floatFromInt(bytes.len)) * 8 * s, 8 * s }; +/// What `Command.text` gives `bytes` in `face` at `scale`, in canvas units: +/// `Host.VTable.textRun`. `xs` is empty, or a byte each. +pub fn textRun(face: abi.Face, scale: f32, bytes: []const u8, xs: []f32) abi.TextRun { + if (!(scale > 0) or !std.math.isFinite(scale) or (xs.len != 0 and xs.len != bytes.len)) return .{}; + var ascent: i32 = 0; + var height: i32 = 0; + const advance = c.sk_text_run(@backingInt(face), Glyphs.pixels(scale), bytes.ptr, bytes.len, if (xs.len == 0) null else xs.ptr, &ascent, &height); + return .{ .width = @floatFromInt(advance), .ascent = @floatFromInt(ascent), .height = @floatFromInt(height) }; } fn premultiply(color: abi.Color) abi.Color { @@ -634,12 +628,6 @@ fn premultiply(color: abi.Color) abi.Color { 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 }, .mono, 1); -} fn setColor(renderer: *c.SDL_Renderer, color: abi.Color) void { _ = c.SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); diff --git a/src/host/gpu.zig b/src/host/gpu.zig deleted file mode 100644 index 94b2e39..0000000 --- a/src/host/gpu.zig +++ /dev/null @@ -1,669 +0,0 @@ -//! 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; - // 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 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; - } - // 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; - } - } - 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); - // 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 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; - 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); -} - -/// 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/library.zig b/src/host/library.zig index 6958eec..6619d5b 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -1,9 +1,13 @@ //! Loading sketch libraries, and noticing when a build replaces one. //! //! `dlopen` returns the handle it already has for a path it loaded before, so -//! every load goes through a private copy with a fresh name. The copy is -//! deleted right after loading: the mapping keeps the code alive, and nothing -//! is left behind if the host dies. +//! every load goes through a symlink with a fresh name to an fd open on the +//! library: what loads is the file that was opened, whatever a build renames +//! over it meanwhile. Link and fd live as long as the library is loaded: the +//! name dlopen was given is where a panic's trace reads the sketch's debug +//! info, so a host that dies leaves its dangling links behind. A library +//! rewritten in place instead (same inode) would not load again, and would +//! change the code under the running host. const std = @import("std"); const linux = std.os.linux; const c = @import("c"); @@ -12,8 +16,14 @@ const abi = @import("abi"); pub const Library = struct { object: *c.SDL_SharedObject, sketch: abi.Sketch, + /// What the link names (the library, or its fixed copy) and where the + /// link is, for `openFresh`'s libraries; -1 for `open`'s. + fd: i32 = -1, + scratch_dir: []const u8 = "", + serial: usize = 0, - pub fn open(path: [:0]const u8) !Library { + /// Loads the library at `path`; `source` names it in messages. + pub fn open(path: [:0]const u8, source: []const u8) !Library { const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary; errdefer c.SDL_UnloadObject(object); var sketch: abi.Sketch = undefined; @@ -22,25 +32,109 @@ pub const Library = struct { const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint; @field(sketch, name) = @ptrCast(address); } - if (sketch.info().abi != abi.hash) return error.AbiMismatch; + if (sketch.info().abi != abi.hash) { + const address = c.SDL_LoadFunction(object, "sketch_build") orelse { + std.log.err("{s}: built by an older sketchbook: rebuild it", .{source}); + return error.AbiMismatch; + }; + const build = std.mem.span(@as([*:0]const u8, @ptrFromInt(@intFromPtr(address)))); + if (std.mem.eql(u8, build, abi.build)) { + std.log.err("{s}: built against other abi types: restart the host to load it", .{source}); + } else { + const mode = build[std.mem.lastIndexOfScalar(u8, build, ' ').? + 1 ..]; + std.log.err("{s}: built by zig {s}, this host by zig {s}; Zig calls cross the boundary, so both need the same compiler, backend and mode: build the host like the library (`zig build run -Doptimize={s}`) or the library like the host", .{ source, build, abi.build, mode }); + } + return error.AbiMismatch; + } return .{ .object = object, .sketch = sketch }; } - /// Loads `source` through a uniquely named copy in `scratch_dir`. - pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const copy = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); + /// Loads `source` through a uniquely named symlink in `scratch_dir`. + pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { const cwd = std.Io.Dir.cwd(); - try cwd.copyFile(source, cwd, copy, io, .{}); - defer cwd.deleteFile(io, copy) catch {}; - return open(copy); + const file = try cwd.openFile(io, source, .{}); + const fixed = withoutVersym(file.handle) catch |err| { + file.close(io); + return err; + }; + if (fixed != null) file.close(io); + const fd = fixed orelse file.handle; + errdefer _ = linux.close(fd); + var target_buf: [32]u8 = undefined; + const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fd}); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const link = try linkName(&buf, scratch_dir, serial); + try cwd.symLink(io, target, link, .{}); + errdefer cwd.deleteFile(io, link) catch {}; + var library = try open(link, source); + library.fd = fd; + library.scratch_dir = scratch_dir; + library.serial = serial; + return library; } pub fn close(self: *Library) void { c.SDL_UnloadObject(self.object); + if (self.fd < 0) return; + var buf: [std.fs.max_path_bytes]u8 = undefined; + _ = linux.unlink(linkName(&buf, self.scratch_dir, self.serial) catch unreachable); + _ = linux.close(self.fd); + } + + fn linkName(buf: []u8, scratch_dir: []const u8, serial: usize) ![:0]u8 { + return std.mem.printSentinel(buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); } }; +// ponytail: Zig 0.17.1's new ELF linker (which `-fincremental` selects) +// writes DT_VERSYM without DT_VERDEF or DT_VERNEED when nothing is +// versioned, and glibc's dlopen then reads a null version table and +// faults. Such a library has nothing versioned, so a copy without +// DT_VERSYM, in an anonymous memory file, loads instead (the build's own +// file is never written: the watch would see the write). Never fires on a +// correct library; dead once Zig has commit 6d27011edb (0.18): delete it. +fn withoutVersym(fd: i32) !?i32 { + const elf = std.elf; + var ehdr: elf.Elf64.Ehdr = undefined; + try pread(fd, std.mem.asBytes(&ehdr), 0); + if (!std.mem.eql(u8, &ehdr.ident.magic, elf.MAGIC) or ehdr.ident.class != .@"64" or ehdr.phnum > 32) return null; + var phdrs: [32]elf.Elf64.Phdr = undefined; + try pread(fd, std.mem.sliceAsBytes(phdrs[0..ehdr.phnum]), ehdr.phoff); + for (phdrs[0..ehdr.phnum]) |ph| { + if (ph.type != .DYNAMIC) continue; + var dyn: [64]elf.Elf64_Dyn = undefined; + const len = @min(dyn.len, ph.filesz / @sizeOf(elf.Elf64_Dyn)); + try pread(fd, std.mem.sliceAsBytes(dyn[0..len]), ph.offset); + var versym: ?usize = null; + var end: usize = 0; + while (end < len and dyn[end].d_tag != elf.DT_NULL) : (end += 1) switch (dyn[end].d_tag) { + elf.DT_VERSYM => versym = end, + elf.DT_VERDEF, elf.DT_VERNEED => return null, + else => {}, + }; + const at = versym orelse return null; + if (end == len) return null; + const copy: i32 = @intCast(@as(isize, @bitCast(linux.memfd_create("sketchbook", linux.MFD.CLOEXEC)))); + if (copy < 0) return error.MemfdFailed; + errdefer _ = linux.close(copy); + var offset: i64 = 0; + while (true) { + const rc = linux.sendfile(copy, fd, &offset, 1 << 30); + if (linux.errno(rc) != .SUCCESS) return error.CopyFailed; + if (rc == 0) break; + } + std.mem.copyForwards(elf.Elf64_Dyn, dyn[at..end], dyn[at + 1 .. end + 1]); + const bytes = std.mem.sliceAsBytes(dyn[at..end]); + if (linux.pwrite(copy, bytes.ptr, bytes.len, @intCast(ph.offset + at * @sizeOf(elf.Elf64_Dyn))) != bytes.len) return error.WriteFailed; + return copy; + } + return null; +} + +fn pread(fd: i32, buf: []u8, offset: u64) !void { + if (linux.pread(fd, buf.ptr, buf.len, @intCast(offset)) != buf.len) return error.ShortRead; +} + /// inotify on a library's directory, filtered to its file name. Builds /// replace files by renaming (`MOVED_TO`) or rewrite them (`CLOSE_WRITE`). pub const Watch = struct { diff --git a/src/host/memory.zig b/src/host/memory.zig index 5fbb88f..2749ea1 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -1,13 +1,12 @@ //! Sketch memory by lifetime. //! -//! `StateFile`: one `MAP_SHARED` file holding the live state and its snapshot -//! slots, plus a header describing the layout, so the state outlives restarts, -//! a probe can read it from another process, and a reload with a changed -//! layout can carry the old values over field by field. +//! `State`: process memory in the layout the sketch declares; a reload +//! with a changed layout carries the old values over field by field. It +//! outlives the process in the sessions database (Sessions.zig), whose +//! stretches keep its layout in the header format below. //! -//! `StoreFile`: a heap in a file, mapped at the same address in every run, -//! so what it holds may point into it: the sketch's `Store` and everything -//! allocated for it outlive restarts. +//! `OldState`: a state file from before the database held the present, +//! read once to import it. //! //! `Session`: this process's memory, kept across reloads. const std = @import("std"); @@ -17,13 +16,16 @@ const abi = @import("abi"); const field_value = @import("fields.zig"); 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; -/// Layouts with element leaves (`list[].x`, `StoredField` with `dims`). -pub const file_magic = "SKETCH02".*; +/// A layout header, and an old file's one slot, the live state. +pub const file_magic = "SKETCH03".*; +/// Before sessions: slot 0 live and 1..7 snapshots (`old_slot_count`), which +/// the host imports as marks and then drops (`dropSnapshots`). +pub const file_magic_v2 = "SKETCH02".*; +pub const old_slot_count = 8; /// What hosts before element leaves wrote: arrays are whole leaves -/// (`StoredFieldV1`). Read, and rewritten as `file_magic` on adopt. +/// (`StoredFieldV1`), and eight slots. Read, and rewritten as +/// `file_magic_v2` on adopt. pub const file_magic_v1 = "SKETCH01".*; pub const Header = extern struct { @@ -31,7 +33,8 @@ pub const Header = extern struct { size: u64, alignment: u64, hash: u64, - /// Distance between slots, page aligned. + /// The slot's size, page aligned (and the distance between an old + /// file's slots). stride: u64, /// Bit `i`: slot `i` holds state. used: u32, @@ -260,9 +263,14 @@ pub const Diff = struct { /// 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 { + return compareKept(layout, layout, a, b); +} + +/// `compare` over the leaves `migrate` from `from` keeps (`keeps`). +pub fn compareKept(from: Layout, layout: Layout, a: []const u8, b: []const u8) Diff { var diff: Diff = .{}; for (layout.fields) |leaf| { - if (leaf.whole()) continue; + if (leaf.whole() or (from.hash != layout.hash and !keeps(from, leaf))) 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; @@ -282,141 +290,120 @@ fn remapEnum(from: Layout.Leaf, old: []const u8, to: Layout.Leaf, new: []u8) boo return true; } -pub const StateFile = struct { - fd: posix.fd_t, - map: []align(page) u8, - /// Layout of the bytes in the file, owned by `arena`. - layout: Layout, +/// Live State: page aligned, in the running sketch's layout; `header` is +/// that layout as `layoutOf` reads it back, for the sessions' stretches. +pub const State = struct { + bytes: []align(page) u8 = &.{}, + layout: Layout = .{}, + header: []const u8 = "", + /// Owns `layout` and `header`. arena: std.heap.ArenaAllocator, - /// 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 > 0) { - if (len < header_len) return error.NotAStateFile; - self.map = try mapFile(fd, @intCast(len), .{ .READ = true, .WRITE = true }); - 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 }; - return self; + pub fn init(gpa: std.mem.Allocator) State { + return .{ .arena = .init(gpa) }; } - pub fn close(self: *StateFile) void { - if (self.map.len != 0) posix.munmap(self.map); - _ = posix.system.close(self.fd); + pub fn deinit(self: *State, gpa: std.mem.Allocator) void { + gpa.free(self.bytes); self.arena.deinit(); } - pub fn header(self: *StateFile) *Header { - return @ptrCast(self.map.ptr); + pub fn live(self: *State) []align(page) u8 { + return self.bytes[0..self.layout.size]; } - pub fn slot(self: *StateFile, index: usize) []align(page) u8 { - std.debug.assert(index < slot_count); - const h = self.header(); - const start = header_len + index * h.stride; - return @alignCast(self.map[start..][0..h.size]); + /// Makes State hold `new`'s layout: a new allocation of its defaults + /// (`defaults`), then every leaf `migrate` keeps of the old State. Null + /// when the layout is unchanged, or there was none. + pub fn adopt(self: *State, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !?Migration { + const first = self.bytes.len == 0; + if (!first and self.layout.hash == new.hash and self.layout.size == new.size) return null; + var arena: std.heap.ArenaAllocator = .init(gpa); + errdefer arena.deinit(); + const layout = try Layout.copy(arena.allocator(), new); + const region = try arena.allocator().alignedAlloc(u8, .of(Header), header_len); + @memset(region, 0); + const h: *Header = @ptrCast(region.ptr); + h.* = .{ .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = std.mem.alignForward(usize, @max(new.size, 1), page), .used = 1, .field_count = 0, .strings_len = 0 }; + try writeLayout(region, layout); + const bytes = try gpa.alignedAlloc(u8, .fromByteUnits(page), @max(new.size, 1)); + @memset(bytes, 0); + defaults(.state, bytes.ptr); + const m = if (first) null else migrate(self.layout, self.live(), layout, bytes); + self.deinit(gpa); + self.* = .{ .bytes = bytes, .layout = layout, .header = region[0 .. @sizeOf(Header) + @as(usize, h.field_count) * @sizeOf(StoredField) + h.strings_len], .arena = arena }; + return m; } +}; - pub fn used(self: *StateFile, index: usize) bool { - return self.header().used & (@as(u32, 1) << @intCast(index)) != 0; - } +/// A state file from before the sessions database held the present: a +/// layout header, then one slot (`file_magic`), or eight (`file_magic_v2`, +/// `file_magic_v1`), the first live State and the others snapshots. Mapped +/// read-only: imported once, never changed. +pub const OldState = struct { + map: []align(page) const u8, + slots: usize, - pub fn markUsed(self: *StateFile, index: usize, value: bool) void { - const bit = @as(u32, 1) << @intCast(index); - if (value) self.header().used |= bit else self.header().used &= ~bit; + /// error.FileNotFound when `path` is missing or empty; a file that is + /// no state file this host reads is refused. + pub fn open(path: []const u8) !OldState { + const map = mapReadOnly(path); + if (map.len == 0) return error.FileNotFound; + errdefer posix.munmap(map); + if (map.len < header_len) return error.NotAStateFile; + const h: *const Header = @ptrCast(map.ptr); + const slots: usize = if (std.mem.eql(u8, &h.magic, &file_magic)) 1 else if (std.mem.eql(u8, &h.magic, &file_magic_v2) or std.mem.eql(u8, &h.magic, &file_magic_v1)) old_slot_count else return error.NotAStateFile; + if (map.len < header_len + slots * h.stride or h.size > h.stride) return error.CorruptStateFile; + return .{ .map = map, .slots = slots }; } - pub const Adoption = union(enum) { - unchanged, - created, - migrated: Migration, - }; - - /// 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) { - // 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. - var old_arena: std.heap.ArenaAllocator = .init(gpa); - defer old_arena.deinit(); - const old_layout = self.layout; - var old_slots: [slot_count]?[]u8 = @splat(null); - for (&old_slots, 0..) |*copy, i| { - if (self.used(i)) copy.* = try old_arena.allocator().dupe(u8, self.slot(i)); - } - const old_used = self.header().used; - // `old_layout` points into `self.arena`: move it aside before resetting. - var kept = self.arena; - defer kept.deinit(); - self.arena = .init(gpa); - - self.layout = try Layout.copy(self.arena.allocator(), new); - const stride = std.mem.alignForward(usize, @max(new.size, 1), page); - try self.resize(stride); - const h = self.header(); - 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 = .{}; - for (old_slots, 0..) |maybe_old, i| { - const old = maybe_old orelse continue; - const bytes = self.slot(i); - @memset(bytes, 0); - defaults(.state, bytes.ptr); - const m = migrate(old_layout, old, self.layout, bytes); - total.kept += m.kept; - if (i == 0) total.reset = m.reset; - } - if (!had_state) { - self.markUsed(0, false); - return .created; - } - return .{ .migrated = total }; + pub fn close(self: OldState) void { + posix.munmap(self.map); } - fn check(self: *StateFile) !void { - const h = self.header(); - 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; + /// The header and layout, as `restore` and `layoutOf` take them. + pub fn layoutBytes(self: OldState) []const u8 { + return self.map[0..header_len]; } - fn resize(self: *StateFile, stride: usize) !void { - const len = header_len + slot_count * stride; - if (self.map.len != 0) posix.munmap(self.map); - self.map = &.{}; - switch (posix.errno(posix.system.ftruncate(self.fd, @intCast(len)))) { - .SUCCESS => {}, - else => |err| return posix.unexpectedErrno(err), - } - self.map = try mapFile(self.fd, len, .{ .READ = true, .WRITE = true }); + /// Slot `i`'s State, null when the slot is unused. + pub fn slot(self: OldState, i: usize) ?[]const u8 { + const h: *const Header = @ptrCast(self.map.ptr); + if (h.used & (@as(u32, 1) << @intCast(i)) == 0) return null; + return self.map[header_len + i * h.stride ..][0..h.size]; } }; +/// Fills `live` (laid out as `to`) from `image`, state kept in another +/// layout, described by `layout_bytes` (a header as `State.header` holds +/// it): `defaults`, then every leaf `migrate` keeps. Returns the +/// image's layout, `arena`'s. +pub fn restore(arena: std.mem.Allocator, layout_bytes: []const u8, image: []const u8, to: Layout, live: []u8, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Layout { + const from = try layoutOf(arena, layout_bytes); + if (image.len != from.size) return error.CorruptKeyframe; + @memset(live, 0); + defaults(.state, live.ptr); + _ = migrate(from, image, to, live); + return from; +} + +/// The layout in a header as `State.header` holds it, `arena`'s. +pub fn layoutOf(arena: std.mem.Allocator, layout_bytes: []const u8) !Layout { + if (layout_bytes.len < @sizeOf(Header) or layout_bytes.len > header_len) return error.CorruptKeyframe; + const region = try arena.alignedAlloc(u8, .of(Header), header_len); + @memset(region, 0); + @memcpy(region[0..layout_bytes.len], layout_bytes); + return try readLayout(arena, region) orelse .{}; +} + +/// Whether `migrate` from `from` keeps `leaf`'s value: compared on these +/// only, state restored across a layout change differs nowhere it was reset. +pub fn keeps(from: Layout, leaf: Layout.Leaf) bool { + const before = from.pattern(leaf.path) orelse return false; + if (before.dims.len != leaf.dims.len) return false; + return (before.hash == leaf.hash and before.size == leaf.size) or (before.kind == .@"enum" and leaf.kind == .@"enum"); +} + /// 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 { @@ -453,14 +440,14 @@ fn writeLayout(region: []u8, layout: Layout) !void { h.strings_len = @intCast(strings); } -/// 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. +/// The layout stored in a state file's header, or null if it has none. +/// Reads both formats, by magic: a v1 header (`file_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 v1 = std.mem.eql(u8, &h.magic, &file_magic_v1) or std.mem.eql(u8, &h.magic, &store_layout_magic_v1); + const v1 = std.mem.eql(u8, &h.magic, &file_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; @@ -498,10 +485,6 @@ pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout { return .{ .size = h.size, .alignment = h.alignment, .hash = h.hash, .fields = leaves }; } -pub fn mapFile(fd: posix.fd_t, len: usize, prot: posix.PROT) ![]align(page) u8 { - return posix.mmap(null, len, prot, .{ .TYPE = .SHARED }, fd, 0); -} - pub fn fileSize(fd: posix.fd_t) !u64 { var stat: linux.Statx = undefined; switch (linux.errno(linux.statx(fd, "", linux.AT.EMPTY_PATH, .{ .SIZE = true }, &stat))) { @@ -520,208 +503,6 @@ pub fn mapReadOnly(path: []const u8) []align(page) const u8 { return posix.mmap(null, @intCast(len), .{ .READ = true }, .{ .TYPE = .SHARED }, fd, 0) catch &.{}; } -/// Where every store is mapped, in every process: far from where Linux puts -/// executables (0x55...) and mappings (0x7f...). -pub const store_base: usize = 0x2000_0000_0000; -/// The most a store can grow to. Only reserved: pages exist as they are used. -pub const store_reserve: usize = 64 << 30; -/// 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; -const heap_offset = 2 * header_len; - -/// A heap in a file, with the sketch's `Store` at its root. Mapped at -/// `store_base` in every run, so pointers it holds into itself stay valid -/// across restarts. Not crash-safe: a crash in the middle of an allocation -/// can leave it inconsistent (`--fresh` starts over). -/// -/// Blocks come in power-of-two sizes, each class with a free list threaded -/// through the free blocks, so growing a list by doubling reuses what it -/// frees. The file grows by mapping more of it in place: nothing moves. -pub const StoreFile = struct { - fd: posix.fd_t, - /// Bytes of the file mapped at `store_base`. - len: usize, - /// The root's layout, owned by `arena`. - layout: Layout, - arena: std.heap.ArenaAllocator, - - const Meta = extern struct { - magic: [8]u8 = store_magic, - base: u64 = store_base, - /// Offset of the first byte never handed out. - top: u64 = heap_offset, - /// Offset of the sketch's Store, or 0 before the first adopt. - root: u64 = 0, - /// Offsets of the first free block of each size class (2^i bytes), 0 - /// when none; a free block's first 8 bytes hold the next one's. - free: [64]u64 = @splat(0), - }; - - pub fn open(gpa: std.mem.Allocator, path: []const u8, fresh: bool) !StoreFile { - const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); - errdefer _ = posix.system.close(fd); - // The same file mapped by two processes would be two heaps writing - // over each other. - if (linux.errno(linux.flock(fd, posix.LOCK.EX | posix.LOCK.NB)) != .SUCCESS) return error.StoreInUse; - const reserved = posix.mmap(@ptrFromInt(store_base), store_reserve, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED_NOREPLACE = true }, -1, 0) catch return error.StoreAddressTaken; - // Old kernels treat FIXED_NOREPLACE as a hint. - if (@intFromPtr(reserved.ptr) != store_base) { - posix.munmap(reserved); - return error.StoreAddressTaken; - } - errdefer posix.munmap(reserved); - var self: StoreFile = .{ .fd = fd, .len = 0, .layout = .{}, .arena = .init(gpa) }; - errdefer self.arena.deinit(); - const size: usize = @intCast(try fileSize(fd)); - if (!fresh and size > 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); - self.layout = try readLayout(self.arena.allocator(), self.region()) orelse .{}; - return self; - } - try self.wipe(); - return self; - } - - pub fn close(self: *StoreFile) void { - posix.munmap(@as([*]align(page) u8, @ptrFromInt(store_base))[0..store_reserve]); - _ = posix.system.close(self.fd); - self.arena.deinit(); - } - - /// Empties the store: no root, nothing allocated. - fn wipe(self: *StoreFile) !void { - // Mapped pages past the new end would fault: map the reserve back. - if (self.len > heap_offset) { - _ = try posix.mmap(@ptrFromInt(store_base + heap_offset), self.len - heap_offset, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED = true }, -1, 0); - self.len = heap_offset; - } - try truncate(self.fd, heap_offset); - try self.grow(heap_offset); - @memset(self.region(), 0); - self.header().* = .{ .magic = store_layout_magic, .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 }; - self.meta().* = .{}; - self.layout = .{}; - } - - /// Maps the file up to `need` bytes, growing it when it is shorter. - fn grow(self: *StoreFile, need: usize) !void { - if (need <= self.len) return; - const len = std.mem.alignForward(usize, @max(need, self.len * 2, 1 << 20), page); - if (len > store_reserve) return error.StoreFull; - if (try fileSize(self.fd) < len) try truncate(self.fd, len); - _ = try posix.mmap(@ptrFromInt(store_base + self.len), len - self.len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED, .FIXED = true }, self.fd, self.len); - self.len = len; - } - - fn region(_: *StoreFile) []u8 { - return @as([*]u8, @ptrFromInt(store_base))[0..header_len]; - } - fn header(_: *StoreFile) *Header { - return @ptrFromInt(store_base); - } - fn meta(_: *StoreFile) *Meta { - return @ptrFromInt(store_base + meta_offset); - } - - /// The sketch's Store. - pub fn root(self: *StoreFile) [*]u8 { - const offset = self.meta().root; - return @ptrFromInt(store_base + if (offset == 0) heap_offset else offset); - } - - pub const Adoption = StateFile.Adoption; - - /// Makes the root hold `new`'s layout: the new defaults, then every field - /// whose path and type are unchanged, copied over. Pointer fields keep - /// pointing where they did, since the store never moves. - pub fn adopt(self: *StoreFile, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Adoption { - const m = self.meta(); - if (m.root != 0 and self.layout.hash == new.hash and self.layout.size == new.size) return .unchanged; - const a = self.allocator(); - const alignment: std.mem.Alignment = .fromByteUnits(@max(new.alignment, 1)); - const fresh = a.rawAlloc(@max(new.size, 1), alignment, @returnAddress()) orelse return error.StoreFull; - @memset(fresh[0..@max(new.size, 1)], 0); - defaults(.store, fresh); - - var kept = self.arena; - defer kept.deinit(); - self.arena = .init(gpa); - const old_layout = self.layout; - self.layout = try Layout.copy(self.arena.allocator(), new); - var result: Adoption = .created; - if (m.root != 0) { - const old: [*]u8 = @ptrFromInt(store_base + m.root); - result = .{ .migrated = migrate(old_layout, old[0..old_layout.size], self.layout, fresh[0..new.size]) }; - a.rawFree(old[0..@max(old_layout.size, 1)], .fromByteUnits(@max(old_layout.alignment, 1)), @returnAddress()); - } - self.header().* = .{ .magic = store_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; - } - - pub fn allocator(self: *StoreFile) std.mem.Allocator { - return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; - } - - fn class(len: usize, alignment: std.mem.Alignment) u6 { - return @intCast(std.math.log2_int_ceil(usize, @max(len, alignment.toByteUnits(), 16))); - } - - fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 { - const self: *StoreFile = @ptrCast(@alignCast(ctx)); - const m = self.meta(); - const c = class(len, alignment); - const size = @as(usize, 1) << c; - if (m.free[c] != 0) { - const block: [*]u8 = @ptrFromInt(store_base + m.free[c]); - if (alignment.check(@intFromPtr(block))) { - m.free[c] = @as(*const u64, @ptrCast(@alignCast(block))).*; - return block; - } - } - const offset = std.mem.alignForward(usize, m.top, @max(@min(size, page), alignment.toByteUnits())); - self.grow(offset + size) catch return null; - m.top = offset + size; - return @ptrFromInt(store_base + offset); - } - - /// In place within the block's size class. - fn resize(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, _: usize) bool { - return class(memory.len, alignment) == class(new_len, alignment); - } - - fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) ?[*]u8 { - return if (resize(ctx, memory, alignment, new_len, ret)) memory.ptr else null; - } - - fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, _: usize) void { - const self: *StoreFile = @ptrCast(@alignCast(ctx)); - const m = self.meta(); - const c = class(memory.len, alignment); - @as(*u64, @ptrCast(@alignCast(memory.ptr))).* = m.free[c]; - m.free[c] = @intFromPtr(memory.ptr) - store_base; - } -}; - -fn truncate(fd: posix.fd_t, len: usize) !void { - switch (posix.errno(posix.system.ftruncate(fd, @intCast(len)))) { - .SUCCESS => {}, - else => |err| return posix.unexpectedErrno(err), - } -} - /// Session memory: survives reloads while its layout hash is unchanged. /// Everything allocated from `heap` is released with it. pub const Session = struct { @@ -859,7 +640,7 @@ test "migration keeps unchanged fields and defaults the rest" { try std.testing.expectEqual(@as(usize, 1), m.reset); } -test "state file migrates live state and snapshots, and a reopen reads the layout back" { +test "State migrates to a changed layout, and keeps itself when it is unchanged" { const V1 = extern struct { score: u32 = 0, name: [4]u8 = "none".* }; const V2 = extern struct { level: u16 = 1, score: u32 = 0 }; const D = struct { @@ -870,71 +651,20 @@ test "state file migrates live state and snapshots, and a reopen reads the layou @as(*V2, @ptrCast(@alignCast(p))).* = .{}; } }; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.state", .{&tmp.sub_path}); - const gpa = std.testing.allocator; - { - var file = try StateFile.open(gpa, path); - defer file.close(); - try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .state), D.v1)); - file.markUsed(0, true); - @as(*V1, @ptrCast(file.slot(0).ptr)).score = 99; - @memcpy(file.slot(3), file.slot(0)); - file.markUsed(3, true); - } - var file = try StateFile.open(gpa, path); - defer file.close(); - try std.testing.expectEqual(abi.layout.hash(V1), file.layout.hash); - const adoption = try file.adopt(gpa, abi.layout.of(V2, .state), D.v2); - try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); - for ([_]usize{ 0, 3 }) |i| { - const s: *V2 = @ptrCast(file.slot(i).ptr); - try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, s.*); - } - try std.testing.expect(!file.used(1)); -} - -test "a store keeps its root and what it points to across a reopen, and migrates by field" { - const V1 = struct { list: std.ArrayList(u32) = .empty, count: u32 = 0 }; - const V2 = struct { added: u8 = 7, list: std.ArrayList(u32) = .empty }; - const D = struct { - fn v1(_: abi.Memory, p: [*]u8) callconv(.c) void { - @as(*V1, @ptrCast(@alignCast(p))).* = .{}; - } - fn v2(_: abi.Memory, p: [*]u8) callconv(.c) void { - @as(*V2, @ptrCast(@alignCast(p))).* = .{}; - } - }; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.store", .{&tmp.sub_path}); const gpa = std.testing.allocator; - { - var store = try StoreFile.open(gpa, path, false); - defer store.close(); - try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); - const v: *V1 = @ptrCast(@alignCast(store.root())); - for (0..10_000) |i| try v.list.append(store.allocator(), @intCast(i)); - v.count = 3; - try std.testing.expectError(error.StoreInUse, StoreFile.open(gpa, path, false)); - } - { - var store = try StoreFile.open(gpa, path, false); - defer store.close(); - try std.testing.expectEqual(StoreFile.Adoption.unchanged, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); - const adoption = try store.adopt(gpa, abi.layout.of(V2, .store), D.v2); - try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); - const v: *V2 = @ptrCast(@alignCast(store.root())); - try std.testing.expectEqual(@as(u8, 7), v.added); - try std.testing.expectEqual(@as(u32, 9999), v.list.items[9999]); - v.list.deinit(store.allocator()); - } - var store = try StoreFile.open(gpa, path, true); - defer store.close(); - try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V2, .store), D.v2)); + var state: State = .init(gpa); + defer state.deinit(gpa); + try std.testing.expectEqual(@as(?Migration, null), try state.adopt(gpa, abi.layout.of(V1, .state), D.v1)); + @as(*V1, @ptrCast(state.live().ptr)).score = 99; + try std.testing.expectEqual(@as(?Migration, null), try state.adopt(gpa, abi.layout.of(V1, .state), D.v1)); + try std.testing.expectEqual(@as(u32, 99), @as(*V1, @ptrCast(state.live().ptr)).score); + const m = (try state.adopt(gpa, abi.layout.of(V2, .state), D.v2)).?; + try std.testing.expectEqual(@as(usize, 1), m.reset); + try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, @as(*V2, @ptrCast(@alignCast(state.live().ptr))).*); + // The header reads back as the layout. + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + try std.testing.expectEqual(abi.layout.hash(V2), (try layoutOf(arena.allocator(), state.header)).hash); } test "arrays migrate element by element: fields, enums by name, shrink and growth" { @@ -1049,14 +779,15 @@ test "a file that is not a state file is refused, untouched" { 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)); + try std.testing.expectError(error.NotAStateFile, OldState.open(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); } + try std.testing.expectError(error.FileNotFound, OldState.open(try testPath(&buf, tmp, "none.state"))); } -test "a state file from before element leaves (drat-meout's) keeps every value" { +test "a state file from before element leaves (drat-meout's) imports with every value" { const gpa = std.testing.allocator; const io = std.testing.io; var tmp = std.testing.tmpDir(.{}); @@ -1076,30 +807,29 @@ test "a state file from before element leaves (drat-meout's) keeps every value" 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); + const file = try OldState.open(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(usize, old_slot_count), file.slots); + // Into the layout the game had when it wrote the file, as a sketch would + // describe it: every byte; then into a changed one (one field gone, one + // new): every other leaf keeps its value, the songs included (an + // unchanged array type, so whole). + for ([_][]const u8{ "", "scene" }) |drop| { + var state: State = .init(gpa); + defer state.deinit(gpa); + _ = try state.adopt(gpa, try asSketchLayout(a, old, drop), NoDefaults.f); + @memset(state.live(), 0); + _ = try restore(a, file.layoutBytes(), file.slot(0).?, state.layout, state.live(), NoDefaults.f); + if (drop.len == 0) try std.testing.expectEqualSlices(u8, live, state.live()); + for (old.fields) |leaf| { + if (drop.len > 0 and std.mem.eql(u8, leaf.path, drop)) continue; + try std.testing.expectEqualSlices(u8, leaf.bytes(live), leaf.bytes(state.live())); + } } - try std.testing.expectEqual(@as(u8, 0), file.slot(0)[h.size]); + // The file is as it was. + const after = try tmp.dir.readFileAlloc(io, "game.state", gpa, .unlimited); + defer gpa.free(after); + try std.testing.expectEqualSlices(u8, original, after); } const NoDefaults = struct { diff --git a/src/host/overlay.zig b/src/host/overlay.zig index 5219a83..ec040e7 100644 --- a/src/host/overlay.zig +++ b/src/host/overlay.zig @@ -4,10 +4,58 @@ const std = @import("std"); const abi = @import("abi"); const memory = @import("memory.zig"); const fields = @import("fields.zig"); +const Timing = @import("Timing.zig"); +const gfx = @import("gfx.zig"); +pub const TimingAtlas = @import("TimingAtlas.zig"); const List = std.ArrayList(abi.Command); const shown_bytes = 0x100; +pub fn drawTiming(painter: *gfx.Painter, timing: *Timing, canvas: [2]u32, atlas: TimingAtlas) !void { + var batch: TimingAtlas.Batch = .{ .atlas = atlas }; + const reference: f64 = if (timing.metric == .frame) 1000.0 / 60.0 else 1; + var ceiling = reference * 1.25; + for (0..timing.graph_count) |i| ceiling = @max(ceiling, Timing.ms(Timing.value(timing.graphAt(i), timing.metric)) * 1.1); + if (timing.dirty or ceiling != timing.ceiling) { + timing.ceiling = ceiling; + timing.setLabel(3, "8 frames/peak / 0..{d:.2} ms / line {d:.2}", .{ ceiling, reference }); + } + if (timing.dirty) { + timing.cached = .{ timing.stats(.frame), timing.stats(.work) }; + timing.setLabel(0, "TIMING / {t} / stats: {d} frames / ms", .{ timing.metric, timing.count }); + for (timing.cached, 0..) |s, i| timing.setLabel(i + 1, "{s} avg {d:.3} p90 {d:.3} p99 {d:.3}", .{ if (i == 0) "Frame" else "Work ", s.mean, s.p90, s.p99 }); + timing.dirty = false; + } + const width = @min(@as(f32, @floatFromInt(canvas[0])), 480); + const scale = width / 480; + const x = @as(f32, @floatFromInt(canvas[0])) - width; + const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 }; + const muted: abi.Color = .{ .r = 165, .g = 180, .b = 195 }; + try batch.rect(.{ .x = x, .y = 0, .w = width, .h = 200 * scale }, .{ .r = 20, .g = 25, .b = 32 }); + for (0..4) |i| { + const y: f32 = switch (i) { + 0 => 8, + 1 => 30, + 2 => 48, + else => 70, + }; + try batch.text(timing.label(i), x + 12 * scale, y * scale, scale, if (i == 0) green else if (i == 3) muted else .white); + } + const left = x + 12 * scale; + const bottom = 170 * scale; + const height = 78 * scale; + const step = 456 * scale / Timing.capacity; + const line_y = bottom - height * @as(f32, @floatCast(reference / ceiling)); + try batch.rect(.{ .x = left, .y = line_y, .w = 456 * scale, .h = 1 }, muted); + for (0..timing.graph_count) |i| { + const value = Timing.ms(Timing.value(timing.graphAt(i), timing.metric)); + const h = @max(1, height * @as(f32, @floatCast(value / ceiling))); + try batch.rect(.{ .x = left + @as(f32, @floatFromInt(Timing.capacity - timing.graph_count + i)) * step, .y = bottom - h, .w = step, .h = h }, green); + } + try batch.text("Frame includes present / Work stops before it", left, 180 * scale, scale, muted); + batch.draw(painter); +} + /// Each field hashes its path into a dark shade; bytes it owns wear the same /// shade in the hex view below, and padding stays gray. fn shade(path: []const u8) abi.Color { @@ -63,7 +111,7 @@ pub fn drawMemory(out: *List, arena: std.mem.Allocator, layout: memory.Layout, s } } -pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, canvas: [2]u32) !void { +pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rate: u32, pull: u32, canvas: [2]u32) !void { const green: abi.Color = .{ .r = 105, .g = 245, .b = 160 }; const x = @as(f32, @floatFromInt(canvas[0])) - 526; const top = @as(f32, @floatFromInt(canvas[1])) - 130; @@ -71,7 +119,7 @@ pub fn drawScope(out: *List, arena: std.mem.Allocator, samples: []const f32, rat try out.append(arena, .{ .rect = .{ .dst = .{ .x = x, .y = top, .w = 514, .h = 118 }, .color = .{ .r = 24, .g = 24, .b = 24 } } }); try out.append(arena, .{ .rect = .{ .dst = .{ .x = x + 1, .y = mid, .w = 512, .h = 1 }, .color = .{ .r = 60, .g = 85, .b = 65 } } }); const ms = @as(f32, @floatFromInt(samples.len * 1000)) / @as(f32, @floatFromInt(@max(rate, 1))); - try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / overlay sound to hide", .{ms}) } }); + try out.append(arena, .{ .text = .{ .x = x + 8, .y = top + 10, .scale = 1, .color = green, .bytes = try std.fmt.allocPrint(arena, "OUTPUT ch1 / {d:.1}ms / pull {d} / overlay sound to hide", .{ ms, pull }) } }); for (samples, 0..) |sample, i| { const a = std.math.clamp(if (std.math.isFinite(sample)) sample else 0, -1, 1); const previous = samples[i -| 1]; diff --git a/src/host/probe.zig b/src/host/probe.zig deleted file mode 100644 index d8ed166..0000000 --- a/src/host/probe.zig +++ /dev/null @@ -1,86 +0,0 @@ -//! sketchbook-probe: prints the live state of any running sketch, from its -//! state file alone. The file header carries the layout, so the probe needs -//! no knowledge of the sketch: it maps the same pages read-only. -const std = @import("std"); -const memory = @import("memory.zig"); -const fields = @import("fields.zig"); - -const usage = - \\usage: sketchbook-probe [--watch] [--interval MS] STATE_FILE - \\Prints every field of the live state; --watch prints again on change. - \\ -; - -pub fn main(init: std.process.Init) !void { - var path: ?[]const u8 = null; - var watch = false; - var interval_ms: i64 = 100; - var args = init.minimal.args.iterate(); - _ = args.skip(); - while (args.next()) |arg| { - if (std.mem.eql(u8, arg, "--watch")) { - watch = true; - } else if (std.mem.eql(u8, arg, "--interval")) { - interval_ms = std.fmt.parseInt(i64, args.next() orelse "", 10) catch fatal("--interval needs milliseconds"); - } else if (std.mem.startsWith(u8, arg, "-")) { - std.debug.print("{s}", .{usage}); - return; - } else path = arg; - } - 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(); - const layout = (try memory.readLayout(arena.allocator(), map)) orelse fatal("state file has no layout yet"); - const header: *const memory.Header = @ptrCast(@alignCast(map.ptr)); - const live = map[memory.header_len..][0..header.size]; - - const shown = try arena.allocator().dupe(u8, live); - show(file, layout, live); - while (watch) { - try std.Io.sleep(init.io, .fromMilliseconds(interval_ms), .awake); - if (std.mem.eql(u8, shown, live)) continue; - @memcpy(shown, live); - show(file, layout, live); - } -} - -fn show(path: []const u8, layout: memory.Layout, live: []const u8) void { - var buf: [4096]u8 = undefined; - 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; - 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() }); - } -} - -fn fatal(message: []const u8) noreturn { - std.debug.print("sketchbook-probe: {s}\n{s}", .{ message, usage }); - std.process.exit(2); -} diff --git a/src/host/root.zig b/src/host/root.zig index d01a63f..73a2a1b 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -1,19 +1,6 @@ //! The sketchbook host: runs a sketch linked in, or loaded from a library -//! that it hot-reloads whenever a build replaces it. -//! -//! sketchbook [options] [LIBRARY] -//! --state FILE persistent state (default: <name>.state) -//! --headless offscreen video, silent audio -//! --script FILE play a script instead of reading devices -//! --fresh start from default state: no snapshots or recordings -//! --profile-gpu time each GPU command alone, print the times at exit -//! --console LINE run a console line once the sketch is loaded (repeatable) -//! --define NAME=VALUE substitute {NAME} in the script -//! --export DIR|- headless, from fresh state: every frame as DIR/000000.png, ... -//! or for - as raw RGBA on stdout, until the sketch quits -//! --fps N export: frames a second (60); each tick is exactly 1/N s -//! --frames N export: stop after N frames -//! --size WxH the first canvas and, headless, the room for it +//! that it hot-reloads whenever a build replaces it. Its options are +//! `flags`, which parse the command line and print `--help`. const std = @import("std"); const c = @import("c"); const abi = @import("abi"); @@ -22,6 +9,10 @@ pub const memory = @import("memory.zig"); pub const fields = @import("fields.zig"); pub const script = @import("script.zig"); pub const timeline = @import("timeline.zig"); +const c9 = @import("cloud9"); +const control = @import("control.zig"); +const Sessions = @import("Sessions.zig"); +const Library = @import("library.zig").Library; /// Entry point for both the generic host (`linked == null`) and apps that /// link their sketch in. @@ -37,72 +28,63 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { var args = init.minimal.args.iterate(); _ = args.skip(); while (args.next()) |arg| { - if (std.mem.eql(u8, arg, "--state")) { - options.state = args.next() orelse fatal("--state needs a file"); - } else if (std.mem.eql(u8, arg, "--fresh")) { - options.fresh = true; - } else if (std.mem.eql(u8, arg, "--headless")) { - options.headless = true; - } else if (std.mem.eql(u8, arg, "--profile-gpu")) { - options.profile_gpu = true; - } else if (std.mem.eql(u8, arg, "--console")) { - try lines.append(gpa, args.next() orelse fatal("--console needs a line")); - } else if (std.mem.eql(u8, arg, "--script")) { - script_path = args.next() orelse fatal("--script needs a file"); - } else if (std.mem.eql(u8, arg, "--define")) { - const pair = args.next() orelse fatal("--define needs NAME=VALUE"); - const eq = std.mem.indexOfScalar(u8, pair, '=') orelse fatal("--define needs NAME=VALUE"); - try defines.append(gpa, .{ pair[0..eq], pair[eq + 1 ..] }); - } else if (std.mem.eql(u8, arg, "--export")) { - options.export_to = args.next() orelse fatal("--export needs a directory, or - for stdout"); - options.headless = true; - options.fresh = true; - } else if (std.mem.eql(u8, arg, "--fps")) { - options.fps = std.fmt.parseInt(u32, args.next() orelse "", 10) catch 0; - if (options.fps == 0) fatal("--fps needs a whole number of frames a second"); - } else if (std.mem.eql(u8, arg, "--frames")) { - options.frames = std.fmt.parseInt(u64, args.next() orelse "", 10) catch fatal("--frames needs a number"); - } else if (std.mem.eql(u8, arg, "--size")) { - const size = args.next() orelse ""; - const x = std.mem.indexOfScalar(u8, size, 'x') orelse fatal("--size needs WxH"); - const w = std.fmt.parseInt(u32, size[0..x], 10) catch 0; - const h = std.fmt.parseInt(u32, size[x + 1 ..], 10) catch 0; - if (w == 0 or h == 0) fatal("--size needs WxH"); - options.size = .{ w, h }; - } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { - std.debug.print("{s}", .{usage}); - return; - } else if (std.mem.startsWith(u8, arg, "-")) { - fatal("unknown option (see --help)"); - } else { + if (std.mem.eql(u8, arg, "fs")) { + var words: std.ArrayList([]const u8) = .empty; + defer words.deinit(gpa); + while (args.next()) |word| try words.append(gpa, word); + std.process.exit(try fs(init, linked, options, words.items)); + } + if (!std.mem.startsWith(u8, arg, "-")) { options.library = arg; + continue; + } + const flag: Flag = if (std.mem.eql(u8, arg, "-h")) .help else (if (std.mem.startsWith(u8, arg, "--")) std.meta.stringToEnum(Flag, arg[2..]) else null) orelse fatal("{s} is no option (see --help)", .{arg}); + const v = if (flags.get(flag).arg.len == 0) "" else args.next() orelse fatal("{s} needs {s}", .{ arg, flags.get(flag).arg }); + switch (flag) { + .@"9p" => options.ninep = v, + .state => options.state = v, + .headless => options.headless = true, + .script => { + script_path = v; + options.script = true; + }, + .fresh => options.fresh = true, + .@"timing-marker" => options.timing_marker = true, + .@"profile-gpu" => options.profile_gpu = true, + .console => try lines.append(gpa, v), + .define => { + const eq = std.mem.indexOfScalar(u8, v, '=') orelse fatal("{s} needs {s}", .{ arg, "NAME=VALUE" }); + try defines.append(gpa, .{ v[0..eq], v[eq + 1 ..] }); + }, + .@"export" => { + options.export_to = v; + options.headless = true; + options.fresh = true; + }, + .fps => options.fps = std.fmt.parseInt(u32, v, 10) catch 0, + .frames => options.frames = std.fmt.parseInt(u64, v, 10) catch fatal("{s} needs {s}", .{ arg, "a number" }), + .size => { + const x = std.mem.indexOfScalar(u8, v, 'x') orelse 0; + const w = std.fmt.parseInt(u32, v[0..x], 10) catch 0; + const h = std.fmt.parseInt(u32, v[x + 1 ..], 10) catch 0; + if (w == 0 or h == 0) fatal("{s} needs {s}", .{ arg, "WxH" }); + options.size = .{ w, h }; + }, + .help => return std.debug.print("{s}", .{usage}), } + if (options.fps == 0) fatal("{s} needs {s}", .{ arg, "a whole number of frames a second" }); } - if (linked == null and options.library == null) fatal("no sketch library given (see --help)"); + if (linked == null and options.library == null) fatal("no sketch library given (see --help)", .{}); if (options.export_to) |to| { - if (script_path != null) fatal("--export plays no script: it runs the sketch on its own"); + if (script_path != null) fatal("--export plays no script: it runs the sketch on its own", .{}); if (!std.mem.eql(u8, to, "-")) { try std.Io.Dir.cwd().createDirPath(init.io, to); - } else if (try std.Io.File.stdout().isTty(init.io)) fatal("--export -: stdout is a terminal; pipe it to ffmpeg"); + } else if (try std.Io.File.stdout().isTty(init.io)) fatal("--export -: stdout is a terminal; pipe it to ffmpeg", .{}); } - // 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. An export's are - // removed instead: nothing reads them, and a player may have the given - // ones open. - var private_buf: [std.fs.max_path_bytes]u8 = undefined; - const given = options.state; - const private = (script_path != null or options.export_to != 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; - _ = if (options.export_to != null) std.os.linux.unlink(a.ptr) else std.os.linux.rename(a.ptr, b.ptr); - }; + if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.xitdb"; + // A scripted run from fresh state, or an export, touches no save. + if (!((script_path != null or options.export_to != null) and options.fresh)) sketchbookDir(init.io, options.state); sdl_heap.install(); { @@ -119,25 +101,144 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { if (sdl_heap.leaked()) return error.LeakedSdlMemory; } -const usage = - \\usage: sketchbook [options] [LIBRARY] - \\ --state FILE persistent state (default: sketch.state) - \\ --headless offscreen video, silent audio, no vsync - \\ --script FILE play a script instead of reading devices - \\ --fresh start from default state: no snapshots or recordings - \\ --profile-gpu time each GPU command alone, print the times at exit - \\ --console LINE run a console line once the sketch is loaded (repeatable) - \\ --define NAME=VALUE substitute {NAME} in the script - \\ --export DIR|- headless, from fresh state: every frame as DIR/000000.png, ... - \\ or for - as raw RGBA on stdout, until the sketch quits - \\ --fps N export: frames a second (60); each tick is exactly 1/N s - \\ --frames N export: stop after N frames - \\ --size WxH the first canvas and, headless, the room for it - \\ -; +/// The options: a row each, what parses the command line (the switch in +/// `main` sets what each one means) and prints `--help`. +const Flag = enum { @"9p", state, headless, script, fresh, @"timing-marker", @"profile-gpu", console, define, @"export", fps, frames, size, help }; +const flags = std.enums.EnumArray(Flag, struct { arg: []const u8 = "", doc: []const u8 }).init(.{ + .@"9p" = .{ .arg = "NAME|/SOCKET", .doc = "the 9P tree (README, ctl, state/, screen.png...), posted as sketchbook/NAME\n(a run in a window posts its own name), or at a socket path" }, + .state = .{ .arg = "FILE", .doc = "the save, one file: FILE, or FILE.xitdb for a FILE.state (where a\nstate file from before is imported from, once); default:\n$XDG_DATA_HOME/<name>/<name>.xitdb, sketch.xitdb for --script, --export" }, + .headless = .{ .doc = "offscreen video, silent audio, no vsync" }, + .script = .{ .arg = "FILE", .doc = "play a script instead of reading devices" }, + .fresh = .{ .doc = "start from default state (the save's history is kept)" }, + .@"timing-marker" = .{ .doc = "encode frame ID and draw sample time for video analysis" }, + .@"profile-gpu" = .{ .doc = "time each GPU command alone, print the times at exit" }, + .console = .{ .arg = "LINE", .doc = "run a console line once the sketch is loaded (repeatable)" }, + .define = .{ .arg = "NAME=VALUE", .doc = "substitute {NAME} in the script" }, + .@"export" = .{ .arg = "DIR|-", .doc = "headless, from fresh state: every frame as DIR/000000.png, ...\nor for - as raw RGBA on stdout, until the sketch quits" }, + .fps = .{ .arg = "N", .doc = "export: frames a second (60); each tick is exactly 1/N s" }, + .frames = .{ .arg = "N", .doc = "export: stop after N frames" }, + .size = .{ .arg = "WxH", .doc = "the first canvas and, headless, the room for it" }, + .help = .{ .doc = "this, also -h" }, +}); + +/// `--help`: a line per flag, its doc's lines under one another. +const usage = blk: { + @setEvalBranchQuota(100_000); + const pad: [23]u8 = @splat(' '); + var text: []const u8 = "usage: sketchbook [options] [LIBRARY] [fs VERB [PATH [WORDS...]]]\n"; + for (std.enums.values(Flag)) |flag| { + const f = flags.get(flag); + const head = " --" ++ @tagName(flag) ++ (if (f.arg.len > 0) " " ++ f.arg else ""); + text = text ++ head ++ pad[@min(head.len, pad.len)..]; + var lines = std.mem.splitScalar(u8, f.doc, '\n'); + text = text ++ lines.first() ++ "\n"; + while (lines.next()) |more| text = text ++ pad ++ more ++ "\n"; + } + break :blk text; +}; + +/// `fs VERB [PATH [WORDS]]`: cloud9's verbs on the sketch's tree. The +/// running sketch's, posted as sketchbook/<name> (or at --9p); with none, +/// the same tree served here from the save's newest State (--state's, or +/// the default save), else from the State's defaults: README, commands, +/// state/ answer, and the rest say the sketch is not running. No window. +fn fs(init: std.process.Init, linked: ?abi.Sketch, options: Host.Options, words: []const []const u8) !u8 { + const gpa = init.gpa; + const io = init.io; + const env = init.environ_map; + var in_buf: [4096]u8 = undefined; + var out_buf: [4096]u8 = undefined; + var err_buf: [1024]u8 = undefined; + var in = std.Io.File.stdin().reader(io, &in_buf); + var out = std.Io.File.stdout().writer(io, &out_buf); + var err = std.Io.File.stderr().writer(io, &err_buf); + const user = env.get("USER") orelse "sketchbook"; + var library: ?Library = null; + defer if (library) |*l| l.close(); + const sketch = linked orelse blk: { + library = try Library.openFresh(io, options.library orelse fatal("fs needs the sketch library first: sketchbook LIBRARY fs ...", .{}), Host.scratchDir(env), 0); + break :blk library.?.sketch; + }; + const info = sketch.info(); + const no_env = [_:null]?[*:0]const u8{}; + var f: c9.Fsys = undefined; + var path_buf: [c9.post.sun_path_len]u8 = undefined; + const where = options.ninep orelse info.name; + const path = if (std.mem.startsWith(u8, where, "/")) where else control.registered(env, where, &path_buf) catch ""; + if (f.dial(io, &no_env, path, user)) |_| { + defer f.close(); + return c9.verbs.run(&f, words, &in.interface, &out.interface, &err.interface); + } else |_| {} + + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const layout = try memory.Layout.copy(arena.allocator(), info.state); + const bytes = try arena.allocator().alignedAlloc(u8, .fromByteUnits(memory.page), layout.size); + @memset(bytes, 0); + sketch.defaults(.state, bytes.ptr); + var from = try std.fmt.allocPrint(arena.allocator(), "{s} is not running: this is its State's defaults (no save)", .{info.name}); + var data_buf: [std.fs.max_path_bytes]u8 = undefined; + const save = if (options.state.len > 0) + try std.fmt.allocPrint(arena.allocator(), "{s}.xitdb", .{Host.stemOf(options.state)}) + else + try std.fmt.allocPrint(arena.allocator(), "{s}/{s}/{s}.xitdb", .{ env.get("XDG_DATA_HOME") orelse try std.fmt.bufPrint(&data_buf, "{s}/.local/share", .{env.get("HOME") orelse "."}), info.name, info.name }); + if (Sessions.openRead(gpa, io, save)) |opened| { + var s = opened; + defer s.deinit(); + var events: std.ArrayList(abi.Event) = .empty; + defer events.deinit(gpa); + if (try s.present(&events)) |key| { + defer key.free(gpa); + _ = try memory.restore(arena.allocator(), key.layout, key.state, layout, bytes, sketch.defaults); + from = try std.fmt.allocPrint(arena.allocator(), "{s} is not running: this is its save's newest State, frame {d} of {s}", .{ info.name, key.frame, save }); + } + } else |_| {} + const commands = try arena.allocator().alloc([]const u8, info.commands.len); + for (commands, info.commands) |*to, word| to.* = word; + var server: control.Control = .{ .allocator = gpa, .path = undefined, .listener = -1, .offline = .{ .layout = layout, .bytes = bytes, .completions = commands, .from = from } }; + var fds: [2]i32 = undefined; + if (std.c.socketpair(std.c.AF.UNIX, std.c.SOCK.STREAM | std.c.SOCK.CLOEXEC, 0, &fds) != 0) return error.SocketPair; + const thread = try std.Thread.spawn(.{}, control.Control.serveOffline, .{ &server, fds[0] }); + defer thread.join(); + try f.init(io, .{ .socket = .{ .handle = fds[1], .address = undefined } }, user); + defer f.close(); + // A read answered here is not the running sketch's: said on stderr, so + // a script that reads before the sketch is up does not take it for one. + if (words.len > 0 and std.mem.eql(u8, words[0], "read")) { + try err.interface.print("{s}\n", .{from}); + try err.interface.flush(); + } + return c9.verbs.run(&f, words, &in.interface, &out.interface, &err.interface); +} + +/// A save in a directory named `.sketchbook` (`zig build run`'s): the +/// directory is made, with a `.gitignore` of `*` (the save grows, and a jj +/// snapshot would commit it), and the state file an older build kept beside +/// it (`<stem>.state`) is moved in, once, to be imported. A file is never +/// replaced: when both exist, nothing moves. +fn sketchbookDir(io: std.Io, state: []const u8) void { + const dir = std.fs.path.dirname(state) orelse return; + if (!std.mem.eql(u8, std.fs.path.basename(dir), ".sketchbook")) return; + const cwd = std.Io.Dir.cwd(); + cwd.createDirPath(io, dir) catch return; + var buf: [std.fs.max_path_bytes]u8 = undefined; + const ignore = std.fmt.bufPrint(&buf, "{s}/.gitignore", .{dir}) catch return; + cwd.writeFile(io, .{ .sub_path = ignore, .data = "*\n", .flags = .{ .exclusive = true } }) catch {}; + var from_buf: [std.fs.max_path_bytes]u8 = undefined; + var to_buf: [std.fs.max_path_bytes]u8 = undefined; + const parent = std.fs.path.dirname(dir) orelse "."; + const stem = Host.stemOf(std.fs.path.basename(state)); + const from = std.fmt.bufPrintSentinel(&from_buf, "{s}/{s}.state", .{ parent, stem }, 0) catch return; + const to = std.fmt.bufPrintSentinel(&to_buf, "{s}/{s}.state", .{ dir, stem }, 0) catch return; + switch (std.os.linux.errno(std.os.linux.renameat2(std.os.linux.AT.FDCWD, from.ptr, std.os.linux.AT.FDCWD, to.ptr, .{ .NOREPLACE = true }))) { + .SUCCESS => std.debug.print("sketchbook: moved {s} to {s}\n", .{ from, to }), + .EXIST => std.debug.print("sketchbook: {s} and {s} both exist: nothing moved, {s} is used\n", .{ from, to, to }), + else => {}, + } +} -fn fatal(message: []const u8) noreturn { - std.debug.print("sketchbook: {s}\n", .{message}); +fn fatal(comptime fmt: []const u8, args: anytype) noreturn { + std.debug.print("sketchbook: " ++ fmt ++ "\n", args); std.process.exit(2); } @@ -191,9 +292,15 @@ const sdl_heap = struct { }; test { + _ = Host; _ = memory; _ = fields; _ = timeline; + _ = @import("Sessions.zig"); _ = @import("audio.zig"); _ = @import("Console.zig"); + _ = @import("control.zig"); + _ = @import("state_files.zig"); + _ = @import("timing_marker.zig"); + _ = @import("Glyphs.zig"); } diff --git a/src/host/script.zig b/src/host/script.zig index 7818c6b..817462b 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -1,58 +1,60 @@ //! End-to-end scripts: plain text that plays a sketch like a person would, -//! through the same routing and frames, on a fixed 60 Hz clock. -//! -//! # comment -//! click 710 320 move there, press and release the left button -//! move|down|up X Y one pointer event, then a frame -//! wheel|wheel-x X Y DELTA -//! cancel what losing focus sends -//! key NAME press and release a key (`sketch.Key` names) -//! type TEXT text input, as typed into a focused field -//! 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); -//! 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. +//! through the same routing and frames, on a fixed 60 Hz clock. A line is +//! a verb of `verbs` and its words; `# comment`; `{name}` is replaced by +//! `--define name=value` from the command line. `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"); +const Console = @import("Console.zig"); +const Sessions = @import("Sessions.zig"); pub const frame_ns: u64 = std.time.ns_per_s / 60; /// The most frames `await` plays: a minute. const await_limit = 60 * 60; +/// The script's verbs, a row each. A verb with `frame` is followed by one +/// frame; the `input` verbs are also what a 9P `ctl` takes live (`input`). +pub const Verb = enum { click, move, down, up, wheel, @"wheel-x", cancel, key, hold, release, type, console, frames, expect, await, print, @"expect-snapshot", @"expect-key", @"expect-log", @"expect-no-log", @"expect-audio", @"expect-no-audio", screenshot, @"expect-same-screenshot", @"expect-different-screenshot", @"expect-pixel", load, reload, @"await-reload", kill, quit }; +pub const Row = struct { args: []const u8 = "", doc: []const u8, frame: bool = false, input: bool = false }; +pub const verbs = std.enums.EnumArray(Verb, Row).init(.{ + .click = .{ .args = "X Y", .doc = "move there, press and release the left button", .frame = true, .input = true }, + .move = .{ .args = "X Y", .doc = "a pointer move", .frame = true, .input = true }, + .down = .{ .args = "X Y", .doc = "the left button down there", .frame = true, .input = true }, + .up = .{ .args = "X Y", .doc = "the left button up there", .frame = true, .input = true }, + .wheel = .{ .args = "X Y DELTA", .doc = "a wheel turn", .frame = true, .input = true }, + .@"wheel-x" = .{ .args = "X Y DELTA", .doc = "a sideways wheel turn", .frame = true, .input = true }, + .cancel = .{ .doc = "what losing focus sends", .frame = true, .input = true }, + .key = .{ .args = "NAME", .doc = "press and release a key (`sketch.Key` names)", .frame = true, .input = true }, + .hold = .{ .args = "NAME", .doc = "a key down until released, for modifiers", .frame = true, .input = true }, + .release = .{ .args = "NAME", .doc = "a held key up", .frame = true, .input = true }, + .type = .{ .args = "TEXT", .doc = "text input, as typed into a focused field", .frame = true, .input = true }, + .console = .{ .args = "LINE", .doc = "a console line (`console get` takes no frame: it only reads); one that fails fails the script", .frame = true }, + .frames = .{ .args = "N", .doc = "N frames without input" }, + .expect = .{ .args = "FIELD VALUE [~T]", .doc = "a State field holds VALUE (enums by name or number; numbers within T); FIELD may index arrays and vector lanes, `boids.buffer[3].pos[0]`" }, + .await = .{ .args = "FIELD VALUE [~T]", .doc = "frames until expect holds, a minute at most" }, + .print = .{ .args = "FIELD", .doc = "a field's value to stderr (a vector, array or struct: every value in it)" }, + .@"expect-snapshot" = .{ .args = "NAME", .doc = "live State equals the newest mark NAME, leaf by leaf (padding, and leaves a layout change reset, ignored)" }, + .@"expect-key" = .{ .args = "WHY FIELD VALUE", .doc = "the newest keyframe of this session taken for WHY (`Sessions.Why`: before_reload, mark...) holds VALUE, read in its own layout" }, + .@"expect-log" = .{ .args = "TEXT", .doc = "a recent console line contains TEXT" }, + .@"expect-no-log" = .{ .args = "TEXT", .doc = "no recent console line does" }, + .@"expect-audio" = .{ .args = "KEY", .doc = "stream KEY has received samples, or voice KEY sounded since the last check of it" }, + .@"expect-no-audio" = .{ .args = "KEY", .doc = "voice KEY did not sound since the last check" }, + .screenshot = .{ .args = "FILE", .doc = "save the next frame as PNG", .frame = true }, + .@"expect-same-screenshot" = .{ .args = "A B [MAX [X Y W H]]", .doc = "two saved PNGs match, every channel within MAX (0), in the rect if given (pixels)" }, + .@"expect-different-screenshot" = .{ .args = "A B [X Y W H]", .doc = "two saved PNGs differ somewhere (in the rect)" }, + .@"expect-pixel" = .{ .args = "FILE X Y R G B A [TOL]", .doc = "a saved PNG's pixel, every channel within TOL" }, + .load = .{ .args = "LIBRARY", .doc = "swap in another build of the sketch", .frame = true }, + .reload = .{ .doc = "load the watched library again", .frame = true }, + .@"await-reload" = .{ .args = "SECONDS", .doc = "frames until a build replaces the library" }, + .kill = .{ .doc = "die by SIGKILL, as a crash would" }, + .quit = .{ .doc = "end the run" }, +}); + pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !void { var arena: std.heap.ArenaAllocator = .init(host.gpa); defer arena.deinit(); @@ -65,9 +67,9 @@ pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !voi const line = try substitute(arena.allocator(), std.mem.trim(u8, raw, " \t\r"), defines); if (line.len == 0 or line[0] == '#') continue; var words = std.mem.tokenizeAny(u8, line, " \t"); - const command = words.next().?; + const verb = std.meta.stringToEnum(Verb, words.next().?); const rest = std.mem.trim(u8, words.rest(), " \t"); - step(host, arena.allocator(), command, rest, &words, &now) catch |err| { + (if (verb) |v| step(host, arena.allocator(), v, rest, &words, &now) else error.UnknownScriptCommand) catch |err| { failures += 1; std.debug.print("script:{d}: {s}: {t}\n", .{ number, line, err }); if (err != error.ExpectationFailed) break; @@ -77,170 +79,226 @@ pub fn run(host: *Host, source: []const u8, defines: []const [2][]const u8) !voi if (failures > 0) return error.ScriptFailed; } -fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []const u8, words: anytype, now: *u64) !void { - const eq = std.mem.eql; - if (eq(u8, command, "click")) { - const x, const y = try point(words); - try host.route(.{ .kind = .mouse_move, .time = now.*, .x = x, .y = y, .flags = held(host) }); - try host.route(.{ .kind = .mouse_down, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); - try host.route(.{ .kind = .mouse_up, .time = now.*, .x = x, .y = y, .code = 1, .flags = held(host) }); - } else if (eq(u8, command, "move") or eq(u8, command, "down") or eq(u8, command, "up")) { - const x, const y = try point(words); - const kind: abi.Event.Kind = if (eq(u8, command, "move")) .mouse_move else if (eq(u8, command, "down")) .mouse_down else .mouse_up; - try host.route(.{ .kind = kind, .time = now.*, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); - } else if (eq(u8, command, "wheel") or eq(u8, command, "wheel-x")) { - const x, const y = try point(words); - const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); - try host.route(.{ .kind = if (eq(u8, command, "wheel-x")) .wheel_horizontal else .wheel, .time = now.*, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); - } else if (eq(u8, command, "cancel")) { - try host.route(.{ .kind = .cancel, .time = now.* }); - } else if (eq(u8, command, "key")) { - const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = .key_down, .time = now.*, .code = @backingInt(key) }); - try host.route(.{ .kind = .key_up, .time = now.*, .code = @backingInt(key) }); - } else if (eq(u8, command, "hold") or eq(u8, command, "release")) { - const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; - try host.route(.{ .kind = if (eq(u8, command, "hold")) .key_down else .key_up, .time = now.*, .code = @backingInt(key) }); - } else if (eq(u8, command, "type")) { - for (rest) |byte| try host.route(.{ .kind = .text, .time = now.*, .value = byte }); - } else if (eq(u8, command, "console")) { - // A failing console line fails the script, like a failed expect. - host.runLine(rest, now.*) catch |err| { - 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, 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 (!holds(host, path, want, tolerance) and waited < await_limit) : (waited += 1) try advance(host, now); - } - if (!holds(host, path, want, tolerance)) { - var buf: [128]u8 = undefined; +/// Routes an input verb's events at `now`, no frame after: a script's +/// line, or a 9P `ctl` line live. +pub fn input(host: *Host, verb: Verb, words: *std.mem.TokenIterator(u8, .any), now: u64) !void { + const rest = std.mem.trim(u8, words.rest(), " \t"); + switch (verb) { + .click => { + const x, const y = try point(words); + try host.route(.{ .kind = .mouse_move, .time = now, .x = x, .y = y, .flags = held(host) }); + try host.route(.{ .kind = .mouse_down, .time = now, .x = x, .y = y, .code = 1, .flags = held(host) }); + try host.route(.{ .kind = .mouse_up, .time = now, .x = x, .y = y, .code = 1, .flags = held(host) }); + }, + .move, .down, .up => { + const x, const y = try point(words); + const kind: abi.Event.Kind = switch (verb) { + .move => .mouse_move, + .down => .mouse_down, + else => .mouse_up, + }; + try host.route(.{ .kind = kind, .time = now, .x = x, .y = y, .code = if (kind == .mouse_move) 0 else 1, .flags = held(host) }); + }, + .wheel, .@"wheel-x" => { + const x, const y = try point(words); + const delta = try std.fmt.parseFloat(f32, words.next() orelse return error.ExpectedDelta); + try host.route(.{ .kind = if (verb == .@"wheel-x") .wheel_horizontal else .wheel, .time = now, .x = x, .y = y, .value = @bitCast(delta), .flags = held(host) }); + }, + .cancel => try host.route(.{ .kind = .cancel, .time = now }), + .key => { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + // with the modifiers `hold` has down, as a window's key carries them + try host.route(.{ .kind = .key_down, .time = now, .code = @backingInt(key), .value = mods(host) }); + try host.route(.{ .kind = .key_up, .time = now, .code = @backingInt(key), .value = mods(host) }); + }, + .hold, .release => { + const key = std.meta.stringToEnum(abi.Key, rest) orelse return error.UnknownKey; + try host.route(.{ .kind = if (verb == .hold) .key_down else .key_up, .time = now, .code = @backingInt(key) }); + }, + .type => for (rest) |byte| try host.route(.{ .kind = .text, .time = now, .value = byte }), + else => return error.NotInput, + } +} + +fn step(host: *Host, arena: std.mem.Allocator, verb: Verb, rest: []const u8, words: *std.mem.TokenIterator(u8, .any), now: *u64) !void { + switch (verb) { + .click, .move, .down, .up, .wheel, .@"wheel-x", .cancel, .key, .hold, .release, .type => try input(host, verb, words, now.*), + .console => { + // A failing console line fails the script, like a failed expect. + host.runLine(rest, now.*) catch |err| { + std.debug.print(" console {s}: {t}\n", .{ rest, err }); + return err; + }; + if (std.mem.startsWith(u8, rest, "get ")) return; + }, + .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 => w.writeAll("no such field") catch {}, + error.UnknownField => return error.UnknownField, 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]}); + std.debug.print("{s} = {s}\n", .{ path, w.buffered() }); + }, + .frames => { + const count = try std.fmt.parseInt(usize, rest, 10); + for (0..count) |_| try advance(host, now); + }, + .expect, .await => { + const path = words.next() orelse return error.ExpectedField; + 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 (verb == .await) { + var waited: usize = 0; + while (!holds(host, path, want, tolerance) and waited < await_limit) : (waited += 1) try advance(host, now); + } + if (!holds(host, path, want, tolerance)) { + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + 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; + } + }, + .@"expect-snapshot" => { + const saved = try Console.markState(host, arena, rest); + if (!same(host, saved.layout, saved.bytes, rest)) return error.ExpectationFailed; + }, + .@"expect-key" => { + const why = std.meta.stringToEnum(Sessions.Why, words.next() orelse "") orelse return error.UnknownWhy; + const path = words.next() orelse return error.ExpectedField; + const want = std.mem.trim(u8, words.rest(), " \t"); + const s = &host.sessions; + try s.commit(.none); + var k = s.keys; + while (k > 0) { + k -= 1; + const key = try s.load(s.session, k); + defer key.free(host.gpa); + if (key.why != why) continue; + const leaf = (try memory.layoutOf(arena, key.layout)).find(path) orelse return error.UnknownField; + if (fields.matches(leaf, key.state, want)) return; + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + fields.format(leaf, key.state, &w) catch {}; + std.debug.print(" {s} keyframe {d}: {s} expected {s}, found {s}\n", .{ @tagName(why), k, path, want, w.buffered() }); + return error.ExpectationFailed; + } + std.debug.print(" no {s} keyframe in this session\n", .{@tagName(why)}); 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; - return; - } else if (eq(u8, command, "screenshot")) { - if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); - host.screenshot = try arena.dupeSentinel(u8, rest, 0); - } else if (eq(u8, command, "load")) { - try host.load(rest); - } else if (eq(u8, command, "reload")) { - try host.reload(); - } else if (eq(u8, command, "await-reload")) { - const seconds = try std.fmt.parseInt(u64, rest, 10); - const start = host.reloads; - const deadline = std.Io.Timestamp.now(host.io, .awake).nanoseconds + @as(i96, seconds) * std.time.ns_per_s; - while (host.reloads == start) { - if (std.Io.Timestamp.now(host.io, .awake).nanoseconds > deadline) return error.NoReload; - try advance(host, now); - try std.Io.sleep(host.io, .fromMilliseconds(10), .awake); - } - return; - } else if (eq(u8, command, "quit")) { - host.quit = true; - return; - } else return error.UnknownScriptCommand; - try advance(host, now); + }, + .@"expect-log", .@"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 != (verb == .@"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; + } + }, + .@"expect-same-screenshot", .@"expect-different-screenshot" => { + const a = words.next() orelse return error.ExpectedFile; + const b = words.next() orelse return error.ExpectedFile; + const want_same = verb == .@"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; + } + }, + .@"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; + }; + }, + .@"expect-audio" => { + const key = try std.fmt.parseInt(u32, rest, 10); + if (host.input.stream(key).pushed == 0 and !host.audio.sounded(key)) return error.ExpectationFailed; + }, + .@"expect-no-audio" => { + if (host.audio.sounded(try std.fmt.parseInt(u32, rest, 10))) return error.ExpectationFailed; + }, + .screenshot => { + if (std.fs.path.dirname(rest)) |dir| try std.Io.Dir.cwd().createDirPath(host.io, dir); + host.screenshot = try arena.dupeSentinel(u8, rest, 0); + }, + .load => { + try host.load(rest); + }, + .reload => { + try host.reload(); + }, + .@"await-reload" => { + const seconds = try std.fmt.parseInt(u64, rest, 10); + const start = host.reloads; + const deadline = std.Io.Timestamp.now(host.io, .awake).nanoseconds + @as(i96, seconds) * std.time.ns_per_s; + while (host.reloads == start) { + if (std.Io.Timestamp.now(host.io, .awake).nanoseconds > deadline) return error.NoReload; + try advance(host, now); + try std.Io.sleep(host.io, .fromMilliseconds(10), .awake); + } + }, + .kill => { + // A crash, for tests of what survives one: SIGKILL, no cleanup. + _ = std.os.linux.kill(std.os.linux.getpid(), .KILL); + }, + .quit => { + host.quit = true; + }, + } + if (verbs.get(verb).frame) try advance(host, now); +} + +/// The modifiers `hold` has down, as `Key.Mod` bits: a key event's `value`. +fn mods(host: *const Host) u32 { + var m: u32 = 0; + if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) m |= abi.Key.Mod.shift; + if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) m |= abi.Key.Mod.ctrl; + if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) m |= abi.Key.Mod.alt; + if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) m |= abi.Key.Mod.gui; + return m; } -/// The modifiers `hold` has down, for a pointer event's `flags`. +/// The same, for a pointer event's `flags`. fn held(host: *const Host) u8 { - var mods: u32 = 0; - if (host.input.held(abi.Key.left_shift) or host.input.held(abi.Key.right_shift)) mods |= abi.Key.Mod.shift; - if (host.input.held(abi.Key.left_ctrl) or host.input.held(abi.Key.right_ctrl)) mods |= abi.Key.Mod.ctrl; - if (host.input.held(abi.Key.left_alt) or host.input.held(abi.Key.right_alt)) mods |= abi.Key.Mod.alt; - if (host.input.held(abi.Key.left_gui) or host.input.held(abi.Key.right_gui)) mods |= abi.Key.Mod.gui; - return abi.Event.withModifiers(mods); + return abi.Event.withModifiers(mods(host)); } /// "VALUE" or "VALUE ~TOLERANCE". @@ -258,15 +316,14 @@ fn holds(host: *Host, path: []const u8, want: []const u8, tolerance: ?f64) bool 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 { +/// Live state against mark `name`'s (`saved`, taken in layout `from`), leaf +/// by leaf over the leaves migration kept; prints the first few differences. +fn same(host: *Host, from: memory.Layout, saved: []const u8, name: []const u8) 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; + if (leaf.whole() or (from.hash != layout.hash and !memory.keeps(from, leaf))) 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; @@ -277,7 +334,7 @@ fn same(host: *Host, index: usize) bool { leaf.name(offset, &w) catch {}; w.writeAll(": live ") catch {}; fields.format(leaf.at(offset), live, &w) catch {}; - w.print(", snapshot {d} ", .{index}) catch {}; + w.print(", snapshot {s} ", .{name}) catch {}; fields.format(leaf.at(offset), saved, &w) catch {}; std.debug.print(" {s}\n", .{w.buffered()}); } diff --git a/src/host/state_files.zig b/src/host/state_files.zig new file mode 100644 index 0000000..ed796c9 --- /dev/null +++ b/src/host/state_files.zig @@ -0,0 +1,303 @@ +//! The State as files: a directory per struct and per array, a file per +//! value, `state/tracker/songs/3/bpm`. An array of scalars (a vector's +//! lanes, `talk.text`) is one file, its values blank-separated; an array +//! of arrays is a directory of such rows, so a file is always one value or +//! one row, the run of values one `set` names. Everything here reads a +//! `(Layout, bytes)` alone: the running host's, the save's newest State, +//! or a sketch's defaults. +//! +//! A node names a leaf of the layout (its first under a directory), how +//! many names deep it is, and the offset of the element it stands for: +//! the indices on its way, every later one zero. Nodes have bit 62 set; +//! bit 63 is the sketch's (control.zig). +const std = @import("std"); +const memory = @import("memory.zig"); +const fields = @import("fields.zig"); +const Layout = memory.Layout; +const Leaf = Layout.Leaf; + +pub const Node = packed struct(u64) { + offset: u40 = 0, + depth: u6 = 0, + leaf: u16 = 0, + state: u1 = 1, + sketch: u1 = 0, +}; + +/// `state/` itself. +pub const root: Node = .{}; + +/// The names of a leaf's pattern: a field's name, or `[]` for an index. +const Pieces = struct { + rest: []const u8, + + fn next(p: *Pieces) ?[]const u8 { + if (p.rest.len > 0 and p.rest[0] == '.') p.rest = p.rest[1..]; + if (p.rest.len == 0) return null; + const end = if (std.mem.startsWith(u8, p.rest, "[]")) 2 else std.mem.indexOfAny(u8, p.rest, ".[") orelse p.rest.len; + defer p.rest = p.rest[end..]; + return p.rest[0..end]; + } +}; + +/// How deep a leaf's file is: its names, an innermost index not counted +/// (a row of scalars is one file). +fn fileDepth(leaf: Leaf) usize { + var p: Pieces = .{ .rest = leaf.path }; + var n: usize = 0; + while (p.next()) |_| n += 1; + return if (std.mem.endsWith(u8, leaf.path, "[]")) n - 1 else n; +} + +/// Where `leaf` is under `dir`: its offset at the directory's indices (the +/// rest zero), and how many of its dimensions they used; null when it is +/// not under it. +fn under(layout: Layout, dir: Node, leaf: Leaf) ?struct { offset: usize, dim: usize } { + if (leaf.kind == .array or fileDepth(leaf) < dir.depth) return null; + if (dir.depth == 0) return .{ .offset = leaf.offset, .dim = 0 }; + const own = layout.fields[dir.leaf]; + var a: Pieces = .{ .rest = own.path }; + var b: Pieces = .{ .rest = leaf.path }; + var rest: usize = @intCast(dir.offset -| own.offset); + var dim: usize = 0; + var offset: usize = leaf.offset; + for (0..dir.depth) |_| { + const x = a.next() orelse return null; + if (!std.mem.eql(u8, x, b.next() orelse return null)) return null; + if (x[0] != '[') continue; + const index = rest / own.dims[dim].stride; + rest %= own.dims[dim].stride; + if (index >= leaf.dims[dim].count) return null; + offset += index * leaf.dims[dim].stride; + dim += 1; + } + return .{ .offset = offset, .dim = dim }; +} + +/// The name a leaf has `depth` names down. +fn pieceAt(leaf: Leaf, depth: usize) []const u8 { + var p: Pieces = .{ .rest = leaf.path }; + for (0..depth) |_| _ = p.next(); + return p.next().?; +} + +/// Whether `node` still names something in `layout` (a reload may have +/// changed it under an open fid). +pub fn valid(layout: Layout, node: Node) bool { + if (node.depth == 0) return true; + if (node.leaf >= layout.fields.len) return false; + const leaf = layout.fields[node.leaf]; + return leaf.kind != .array and node.depth <= fileDepth(leaf) and node.offset >= leaf.offset and node.offset < layout.size; +} + +pub fn isDir(layout: Layout, node: Node) bool { + return node.depth == 0 or node.depth < fileDepth(layout.fields[node.leaf]); +} + +/// The child of directory `dir` called `child`. +pub fn lookup(layout: Layout, dir: Node, child: []const u8) ?Node { + for (layout.fields, 0..) |leaf, j| { + const at = under(layout, dir, leaf) orelse continue; + if (fileDepth(leaf) == dir.depth) continue; + const piece = pieceAt(leaf, dir.depth); + var offset = at.offset; + if (piece[0] == '[') { + if (child.len == 0 or !std.ascii.isDigit(child[0])) return null; + const index = std.fmt.parseInt(u32, child, 10) catch return null; + if (index >= leaf.dims[at.dim].count) return null; + offset += index * leaf.dims[at.dim].stride; + } else if (!std.mem.eql(u8, piece, child)) continue; + return .{ .leaf = @intCast(j), .depth = dir.depth + 1, .offset = @intCast(offset) }; + } + return null; +} + +/// The entries of directory `dir` from the `from`-th, as cloud9's readdir +/// records (`node:u64le dir:u8 len:u8 name`), as many as `out` holds: the +/// bytes written. +pub fn readdir(layout: Layout, dir: Node, from: u64, out: []u8) usize { + var used: usize = 0; + var index: u64 = 0; + var last: []const u8 = ""; + for (layout.fields, 0..) |leaf, j| { + const at = under(layout, dir, leaf) orelse continue; + if (fileDepth(leaf) == dir.depth) continue; + const piece = pieceAt(leaf, dir.depth); + if (std.mem.eql(u8, piece, last)) continue; + last = piece; + const is_dir = fileDepth(leaf) > dir.depth + 1; + const count = if (piece[0] == '[') leaf.dims[at.dim].count else 1; + for (0..count) |i| { + defer index += 1; + if (index < from) continue; + var buf: [16]u8 = undefined; + const text = if (piece[0] == '[') std.fmt.bufPrint(&buf, "{d}", .{i}) catch unreachable else piece; + if (used + 10 + text.len > out.len) return used; + const offset = at.offset + if (piece[0] == '[') i * leaf.dims[at.dim].stride else 0; + const node: Node = .{ .leaf = @intCast(j), .depth = dir.depth + 1, .offset = @intCast(offset) }; + std.mem.writeInt(u64, out[used..][0..8], @bitCast(node), .little); + out[used + 8] = @intFromBool(is_dir); + out[used + 9] = @intCast(text.len); + @memcpy(out[used + 10 ..][0..text.len], text); + used += 10 + text.len; + } + } + return used; +} + +/// The row of scalars at a file node: its first element and its length +/// and stride; null for a file of one value. +fn rowOf(layout: Layout, node: Node) ?struct { first: Leaf, count: usize, stride: usize } { + const leaf = layout.fields[node.leaf]; + if (!std.mem.endsWith(u8, leaf.path, "[]") or node.depth != fileDepth(leaf)) return null; + const d = leaf.dims[leaf.dims.len - 1]; + return .{ .first = leaf.at(node.offset), .count = d.count, .stride = d.stride }; +} + +/// A file's value, as `get` shows one value, a row's blank-separated, +/// and a newline. +pub fn read(layout: Layout, bytes: []const u8, node: Node, w: *std.Io.Writer) std.Io.Writer.Error!void { + if (rowOf(layout, node)) |row| { + for (0..row.count) |i| { + if (i > 0) try w.writeByte(' '); + try fields.format(row.first.at(row.first.offset + i * row.stride), bytes, w); + } + } else try fields.format(layout.fields[node.leaf].at(node.offset), bytes, w); + try w.writeByte('\n'); +} + +/// The path `get` and `set` take for a node: `tracker.songs[3].bpm`, and +/// for a row the array's (`talk.text`). +pub fn path(layout: Layout, node: Node, w: *std.Io.Writer) std.Io.Writer.Error!void { + const leaf = layout.fields[node.leaf]; + var p: Pieces = .{ .rest = leaf.path }; + var rest: usize = @intCast(node.offset - leaf.offset); + var dim: usize = 0; + for (0..node.depth) |k| { + const piece = p.next().?; + if (piece[0] == '[') { + try w.print("[{d}]", .{rest / leaf.dims[dim].stride}); + rest %= leaf.dims[dim].stride; + dim += 1; + } else try w.print("{s}{s}", .{ if (k == 0) "" else ".", piece }); + } +} + +/// Whether `set` takes the file: numbers, bools and enums. +pub fn writable(layout: Layout, node: Node) bool { + return switch (layout.fields[node.leaf].kind) { + .int, .uint, .float, .bool, .@"enum" => true, + else => false, + }; +} + +/// A README line for every file of the layout: its path, `<n>` for an +/// index, and what it holds. +pub fn readme(layout: Layout, w: *std.Io.Writer) std.Io.Writer.Error!void { + for (layout.fields) |leaf| { + if (leaf.kind == .array) continue; + var line: [256]u8 = undefined; + var lw: std.Io.Writer = .fixed(&line); + var p: Pieces = .{ .rest = leaf.path }; + const depth = fileDepth(leaf); + lw.writeAll("state") catch {}; + for (0..depth) |_| { + const piece = p.next().?; + lw.print("/{s}", .{if (piece[0] == '[') "<n>" else piece}) catch {}; + } + const row = std.mem.endsWith(u8, leaf.path, "[]"); + try w.print("{s}", .{lw.buffered()}); + try w.splatByteAll(' ', @max(1, 41 -| lw.buffered().len)); + if (row) try w.print("{d} ", .{leaf.dims[leaf.dims.len - 1].count}); + switch (leaf.kind) { + .int, .uint, .float => try w.print("{s}{d}", .{ switch (leaf.kind) { + .int => "i", + .uint => "u", + else => "f", + }, leaf.size * 8 }), + .bool => try w.writeAll("bool"), + .@"enum" => { + var tags = std.mem.tokenizeScalar(u8, leaf.tags, ','); + var first = true; + while (tags.next()) |pair| : (first = false) try w.print("{s}.{s}", .{ if (first) "" else " ", pair[0 .. std.mem.indexOfScalar(u8, pair, '=') orelse pair.len] }); + }, + else => try w.print("{d} bytes, read-only", .{leaf.size}), + } + if (row) try w.writeAll(", blank-separated"); + try w.writeByte('\n'); + } +} + +const testing = std.testing; + +fn walk(layout: Layout, to: []const u8) ?Node { + var node = root; + var names = std.mem.tokenizeScalar(u8, to, '/'); + while (names.next()) |n| node = lookup(layout, node, n) orelse return null; + return node; +} + +/// The names in a directory, a blank after each, a slash after a directory's. +fn ls(layout: Layout, dir: Node, buf: []u8) []const u8 { + var records: [4096]u8 = undefined; + const bytes = records[0..readdir(layout, dir, 0, &records)]; + var w: std.Io.Writer = .fixed(buf); + var i: usize = 0; + while (i < bytes.len) : (i += 10 + bytes[i + 9]) { + const node: Node = @bitCast(std.mem.readInt(u64, bytes[i..][0..8], .little)); + // Each entry is what a lookup of its name answers. + const text = bytes[i + 10 ..][0..bytes[i + 9]]; + std.debug.assert(lookup(layout, dir, text).? == node); + w.print("{s}{s} ", .{ text, if (bytes[i + 8] != 0) "/" else "" }) catch {}; + } + return w.buffered(); +} + +test "the State as files: a directory per struct and array, a file per value or row" { + const S = struct { + scene: enum(u8) { room, street } = .street, + talk: struct { text: [4]u8 = .{ 104, 105, 0, 0 }, len: u8 = 2 } = .{}, + songs: [2]struct { bpm: u16 = 120, patterns: [2][3]struct { pitch: u8 = 0, velocity: u8 = 0 } = @splat(@splat(.{})) } = @splat(.{}), + grid: [2][3]i16 = @splat(@splat(0)), + pos: @Vector(2, f32) = .{ 250, 3 }, + }; + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const layout = try Layout.copy(arena.allocator(), @import("abi").layout.of(S, .state)); + var s: S = .{}; + s.songs[1].patterns[1][2].pitch = 64; + s.grid[1] = .{ 1, -2, 3 }; + const bytes = std.mem.asBytes(&s); + var buf: [256]u8 = undefined; + try testing.expectEqualStrings("scene talk/ songs/ grid/ pos ", ls(layout, root, &buf)); + try testing.expectEqualStrings("text len ", ls(layout, walk(layout, "talk").?, &buf)); + try testing.expectEqualStrings("0/ 1/ ", ls(layout, walk(layout, "songs").?, &buf)); + try testing.expectEqualStrings("bpm patterns/ ", ls(layout, walk(layout, "songs/1").?, &buf)); + try testing.expectEqualStrings("0/ 1/ 2/ ", ls(layout, walk(layout, "songs/1/patterns/1").?, &buf)); + try testing.expectEqualStrings("0 1 ", ls(layout, walk(layout, "grid").?, &buf)); + const cases = [_][3][]const u8{ + .{ "scene", "scene", ".street\n" }, + .{ "talk/text", "talk.text", "104 105 0 0\n" }, + .{ "songs/1/bpm", "songs[1].bpm", "120\n" }, + .{ "songs/1/patterns/1/2/pitch", "songs[1].patterns[1][2].pitch", "64\n" }, + .{ "grid/1", "grid[1]", "1 -2 3\n" }, + .{ "pos", "pos", "250 3\n" }, + }; + for (cases) |case| { + const node = walk(layout, case[0]).?; + try testing.expect(!isDir(layout, node) and valid(layout, node) and writable(layout, node)); + var w: std.Io.Writer = .fixed(&buf); + try path(layout, node, &w); + try testing.expectEqualStrings(case[1], w.buffered()); + w = .fixed(&buf); + try read(layout, bytes, node, &w); + try testing.expectEqualStrings(case[2], w.buffered()); + } + for ([_][]const u8{ "nope", "songs/2", "songs/x", "songs/01x", "grid/1/0", "scene/x", "talk/text/0" }) |bad| try testing.expect(walk(layout, bad) == null); + var out: std.Io.Writer.Allocating = .init(testing.allocator); + defer out.deinit(); + try readme(layout, &out.writer); + try testing.expect(std.mem.indexOf(u8, out.written(), "state/scene .room .street\n") != null); + try testing.expect(std.mem.indexOf(u8, out.written(), "state/songs/<n>/patterns/<n>/<n>/pitch u8\n") != null); + try testing.expect(std.mem.indexOf(u8, out.written(), "state/grid/<n> 3 i16, blank-separated\n") != null); +} diff --git a/src/host/timeline.zig b/src/host/timeline.zig index 8a5448a..e755433 100644 --- a/src/host/timeline.zig +++ b/src/host/timeline.zig @@ -1,47 +1,18 @@ -//! Snapshots, input recordings and replay: generic over state bytes and -//! `abi.Event`s, so any sketch gets time travel for free. +//! Clips and replay: generic over state bytes and `abi.Event`s, so any +//! sketch gets time travel for free. //! //! 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. +//! A clip is whole frames of a session (Sessions.zig) and the keyframe they +//! start from: 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, +//! at the times it saw. const std = @import("std"); -const posix = std.posix; const abi = @import("abi"); const memory = @import("memory.zig"); - -pub const recording_len = 4 * 1024 * 1024; -pub const recording_count = 16; -pub const recordings_len = recording_len * recording_count; -const recording_magic = 0x52454305; -pub const capacity = (recording_len - 32) / @sizeOf(abi.Event); - -pub const Recording = extern struct { - duration: u64, - count: u32, - snapshot: u32, - magic: 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); - } -}; +const Sessions = @import("Sessions.zig"); /// The largest frame boundary at or before `index`: 0, or just after a tick. pub fn frameStart(events: []const abi.Event, index: usize) usize { @@ -72,119 +43,92 @@ pub const Frames = struct { } }; -comptime { - std.debug.assert(@sizeOf(Recording) == recording_len); -} - -/// Snapshot slots of a state file, and the recordings that start from them. -pub const Bank = struct { - state: *memory.StateFile, - inputs: []align(memory.page) u8, - - pub fn slot(self: Bank, index: usize) []u8 { - return self.state.slot(index); - } - pub fn recording(self: Bank, index: usize) *Recording { - std.debug.assert(index < recording_count); - return @ptrCast(@alignCast(self.inputs[index * recording_len ..].ptr)); - } - pub fn hasSnapshot(self: Bank, index: usize) bool { - return index > 0 and index < memory.slot_count and self.state.used(index); - } - pub fn save(self: Bank, index: usize) void { - std.debug.assert(index > 0 and index < memory.slot_count); - for (0..recording_count) |i| { - const r = self.recording(i); - if (r.snapshot == index) r.magic = 0; - } - @memcpy(self.slot(index), self.slot(0)); - self.state.markUsed(index, true); - } - pub fn restore(self: Bank, index: usize) !void { - if (!self.hasSnapshot(index)) return error.EmptySnapshot; - @memcpy(self.slot(0), self.slot(index)); - } - pub fn get(self: Bank, index: usize) !*Recording { - const r = self.recording(index); - if (r.magic != recording_magic or r.count > capacity or !self.hasSnapshot(r.snapshot)) return error.EmptyRecording; - return r; - } - /// 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)); - } -}; - pub const Timeline = struct { - bank: Bank, + gpa: std.mem.Allocator, + /// Live State. + state: *memory.State, + /// The running sketch's: a restore starts from its defaults. + defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void, mode: abi.Mode = .live, - selected: usize = 0, + /// The clip loaded: its start keyframe and its frames, `gpa`'s. + key: ?Sessions.Loaded = null, + events: std.ArrayList(abi.Event) = .empty, + /// The clip's first tick: positions count from it. + base: u64 = 0, cursor: usize = 0, position: u64 = 0, clock: u64 = 0, - /// Set whenever live state is restored from a snapshot, for the host + /// Set whenever live state is restored from a keyframe, for the host /// to reset the input state that went with the old bytes. restored: bool = false, + /// Frames fed since the keyframe was restored. + fed: u64 = 0, + /// A past session played (`play session`), not a clip: its index, and + /// its keyframes after the one played from, checked against the replay + /// as their frames are reached (Host.resync). + session: ?u64 = null, + checks: std.ArrayList(Check) = .empty, + next_check: usize = 0, - /// Restores a snapshot into live state. - pub fn restore(self: *Timeline, index: usize) !void { - try self.bank.restore(index); + pub const Check = struct { + frame: u64, + key: u64, + /// Taken of what `update` made (periodic, a mark, before a reload, + /// at exit): a replay must match it. Other keyframes follow a jump + /// the log does not hold, and are restored without a word. + observed: bool, + }; + + pub fn deinit(self: *Timeline) void { + if (self.key) |k| k.free(self.gpa); + self.key = null; + self.events.deinit(self.gpa); + self.checks.deinit(self.gpa); + } + + /// Puts a keyframe, in whatever layout it was taken, into live state. + pub fn restore(self: *Timeline, key: Sessions.Loaded) !void { + var arena: std.heap.ArenaAllocator = .init(self.gpa); + defer arena.deinit(); + _ = try memory.restore(arena.allocator(), key.layout, key.state, self.state.layout, self.state.live(), self.defaults); self.restored = true; + self.fed = 0; + self.next_check = 0; + } + + /// Makes a clip, its start keyframe and its frames, the one played; both + /// become the timeline's. + pub fn load(self: *Timeline, key: Sessions.Loaded, events: std.ArrayList(abi.Event)) void { + self.deinit(); + self.key = key; + self.events = events; + self.session = null; + self.checks = .empty; + var it = frames(events.items); + const first = it.next(); + self.base = if (first) |f| f[f.len - 1].time else 0; + } + + fn at(self: *const Timeline, i: usize) u64 { + return self.events.items[i].time -| self.base; + } + + /// The last whole frame's tick, from the first. + fn duration(self: *const Timeline) u64 { + const end = frameStart(self.events.items, self.events.items.len); + return if (end == 0) 0 else self.at(end - 1); } - /// `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.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; + pub fn startRecording(self: *Timeline) void { self.cursor = 0; self.position = 0; - self.clock = now; self.mode = .recording; } - /// 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 (frame.len > capacity - r.count) { - self.mode = .live; - return error.RecordingFull; - } - 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 { - if (self.mode == .recording) { - const r = self.bank.recording(self.selected); - r.duration = @max(r.duration, now -| self.clock); - self.position = r.duration; - } + pub fn stop(self: *Timeline) void { self.mode = .live; } - pub fn play(self: *Timeline, index: usize, now: u64) !void { - const r = try self.bank.get(index); - try self.restore(r.snapshot); - self.selected = index; + pub fn play(self: *Timeline, now: u64) !void { + try self.restore(self.key orelse return error.EmptyRecording); self.cursor = 0; self.position = 0; self.clock = now; @@ -203,35 +147,33 @@ pub const Timeline = struct { /// 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)); + const end = self.duration(); + self.position = @min(end, self.position +| (now -| self.clock)); self.clock = now; const start = self.cursor; 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; + while (i < self.events.items.len and self.at(i) <= self.position) : (i += 1) { + if (self.events.items[i].kind == .tick) self.cursor = i + 1; } - if (self.position == r.duration) self.mode = .paused; - return r.events[start..self.cursor]; + if (self.position == end) self.mode = .paused; + return self.events.items[start..self.cursor]; } - /// 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. + /// Restores the start keyframe 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.restore(r.snapshot); - self.cursor = frameStart(r.slice(), count); - self.position = if (self.cursor == 0) 0 else r.events[self.cursor - 1].time; + try self.restore(self.key orelse return error.EmptyRecording); + self.cursor = frameStart(self.events.items, count); + self.position = if (self.cursor == 0) 0 else self.at(self.cursor - 1); self.mode = .paused; - return r.events[0..self.cursor]; + return self.events.items[0..self.cursor]; } - /// To the last frame whose tick is at or before `time`. + /// To the last frame whose tick is at or before `time` into the clip. pub fn seekTime(self: *Timeline, time: u64) ![]const abi.Event { - const r = try self.bank.get(self.selected); - const position = @min(time, r.duration); + const position = @min(time, self.duration()); var count: usize = 0; - for (r.slice(), 0..) |e, i| { - if (e.time > position) break; + for (self.events.items, 0..) |e, i| { + if (self.at(i) > position) break; if (e.kind == .tick) count = i + 1; } const events = try self.seekCount(count); @@ -240,13 +182,12 @@ pub const Timeline = struct { } /// 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); - const events = r.slice(); + const events = self.events.items; var cursor = frameStart(events, self.cursor); var remaining = @abs(steps); + const end = frameStart(events, events.len); while (remaining > 0) : (remaining -= 1) { if (steps > 0) { - const end = r.whole(); if (cursor == end) break; cursor += 1; while (cursor < end and events[cursor - 1].kind != .tick) cursor += 1; @@ -259,60 +200,42 @@ pub const Timeline = struct { } }; -/// Maps the recordings file next to a state file, creating it when missing. -pub fn mapRecordings(path: []const u8) ![]align(memory.page) u8 { - const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); - defer _ = posix.system.close(fd); - if (try memory.fileSize(fd) != recordings_len) switch (posix.errno(posix.system.ftruncate(fd, recordings_len))) { - .SUCCESS => {}, - else => |err| return posix.unexpectedErrno(err), +test "frames are whole: input waits for its tick, in playback, seek and step" { + const gpa = std.testing.allocator; + // Live State: 8 bytes, no fields. + const D = struct { + fn f(_: abi.Memory, _: [*]u8) callconv(.c) void {} }; - 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); + var state: memory.State = .init(gpa); + defer state.deinit(gpa); + _ = try state.adopt(gpa, .{ .size = 8, .alignment = 1, .hash = 1, .fields = &.{} }, D.f); + var t: Timeline = .{ .gpa = gpa, .state = &state, .defaults = D.f }; + defer t.deinit(); + // Frame k: a move at 10k ms, its tick 5 ms later; then a move with no + // tick (a frame cut off). + var events: std.ArrayList(abi.Event) = .empty; 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); + try events.append(gpa, .{ .kind = .mouse_move, .time = 1000 + k * 10_000_000, .x = @floatFromInt(k) }); + try events.append(gpa, abi.Event.tick(1000 + k * 10_000_000 + 5_000_000, 16_666_667)); } - 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 events.append(gpa, .{ .kind = .mouse_move, .time = 40_000_000 }); + const key: Sessions.Loaded = .{ .frame = 0, .batch = 0, .why = .mark, .time = 0, .stretch = 0, .build = 7, .state = try gpa.dupe(u8, &@as([8]u8, @splat(0))), .line = try gpa.dupe(u8, ""), .source = try gpa.dupe(u8, ""), .layout = try gpa.dupe(u8, state.header) }; + t.load(key, events); - try t.play(0, 0); + try t.play(0); + try std.testing.expect(t.restored); 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); + const due = t.tick(now); + if (due.len == 0) continue; + try std.testing.expectEqual(abi.Event.Kind.tick, due[due.len - 1].kind); + var it = frames(due); while (it.next()) |frame| { try std.testing.expectEqual(@as(usize, 2), frame.len); + // Fed at the times it was recorded at. + try std.testing.expectEqual(1000 + delivered * 10_000_000 + 5_000_000, frame[1].time); delivered += 1; } } @@ -320,13 +243,13 @@ test "frames are whole: input waits for its tick, live and in playback" { 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.seekTime(27_000_000)).len); + try std.testing.expectEqual(@as(usize, 8), (try t.seekCount(t.events.items.len)).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 it = frames(t.events.items); var n: usize = 0; while (it.next()) |_| n += 1; try std.testing.expectEqual(@as(usize, 4), n); diff --git a/src/host/timing_marker.zig b/src/host/timing_marker.zig new file mode 100644 index 0000000..2d641c9 --- /dev/null +++ b/src/host/timing_marker.zig @@ -0,0 +1,45 @@ +//! Opt-in video marker: submitted frame ID and draw sample time, LSB-first. +const std = @import("std"); +const abi = @import("abi"); + +pub fn draw(out: *std.ArrayList(abi.Command), arena: std.mem.Allocator, canvas: [2]u32, frame: u64, now: u64) !void { + if (canvas[0] < 320 or canvas[1] < 8) return; + const y: f32 = @floatFromInt(canvas[1] - 8); + const milliseconds: u32 = @truncate(now / std.time.ns_per_ms); + const bits: u80 = 0xD3A7 | (@as(u80, @as(u32, @truncate(frame))) << 16) | (@as(u80, milliseconds) << 48); + try out.append(arena, .{ .clip = null }); + try out.append(arena, .{ .rect = .{ .dst = .{ .x = 0, .y = y, .w = 320, .h = 8 }, .color = .black } }); + for (0..80) |i| { + if ((bits >> @intCast(i)) & 1 == 0) continue; + try out.append(arena, .{ .rect = .{ .dst = .{ .x = @floatFromInt(i * 4), .y = y, .w = 4, .h = 8 }, .color = .white } }); + } +} + +test "timing marker encodes LSB-first fields, wraps counters and fits the canvas" { + const a = std.testing.allocator; + var commands: std.ArrayList(abi.Command) = .empty; + defer commands.deinit(a); + try draw(&commands, a, .{ 960, 600 }, 0x1_0000_1234, 0x1_0000_5678 * std.time.ns_per_ms + 999999); + try std.testing.expect(commands.items[0] == .clip and commands.items[0].clip == null); + const background = commands.items[1].rect; + try std.testing.expectEqual(abi.Rect{ .x = 0, .y = 592, .w = 320, .h = 8 }, background.dst); + try std.testing.expectEqual(abi.Color.black, background.color); + var decoded: u80 = 0; + for (commands.items[2..]) |command| { + const rect = command.rect; + try std.testing.expectEqual(abi.Color.white, rect.color); + try std.testing.expectEqual(@as(f32, 592), rect.dst.y); + try std.testing.expectEqual(@as(f32, 4), rect.dst.w); + try std.testing.expectEqual(@as(f32, 8), rect.dst.h); + try std.testing.expect(rect.dst.x >= 0 and rect.dst.x < 320 and @mod(rect.dst.x, 4) == 0); + const bit: u7 = @intFromFloat(rect.dst.x / 4); + decoded |= @as(u80, 1) << bit; + } + try std.testing.expectEqual(@as(u16, 0xD3A7), @as(u16, @truncate(decoded))); + try std.testing.expectEqual(@as(u32, 0x1234), @as(u32, @truncate(decoded >> 16))); + try std.testing.expectEqual(@as(u32, 0x5678), @as(u32, @truncate(decoded >> 48))); + commands.clearRetainingCapacity(); + try draw(&commands, a, .{ 319, 8 }, 0, 0); + try draw(&commands, a, .{ 320, 7 }, 0, 0); + try std.testing.expectEqual(@as(usize, 0), commands.items.len); +} |
