From afc60e735274b0de5423ac375a9ea582f64b516f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 22:56:35 -0300 Subject: test: the GPU move's gates, before a line of the renderer moves: window.zig's frame corpus, run when PARDES_GUI_CORPUS names a directory (zig build process-test -Dplatform=gui -- 'window: its frame corpus', the snake addon's library in PARDES_GUI_CORPUS_SNAKE): the move's twelve scenes on the hidden window under the virtual clock, one input a step and then idle and idle again once a worker had time to land (a PDF page, a highlighted file), every frame of a motion (a wheel's glide, a click's caret, a jump's trail) and the one that stays of the rest, the snake's first ten in a window of its own (its picture breathes on the clock, and a long session's clock has seen the machine's timing), each kept as frame-NNNNN.ppm with its sha256 and its scene; a build against a build on one machine, never a golden; gui.zig: the atlas's bytes, a fixed key list (ASCII plain, bold, in the five underlines and struck, taglines, an underlined space, the eleven rules, centred grips, Nerd icons, ligature strips shaped as a frame shapes them) at 27 px and at 54 px at 2x, its stage and its sorted slots hashed to constants recorded now, the same on any machine (the repo's faces, the build's FreeType and HarfBuzz) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/process/window.zig | 381 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 381 insertions(+) (limited to 'test/process') diff --git a/test/process/window.zig b/test/process/window.zig index 8d1afed8..e39e0963 100644 --- a/test/process/window.zig +++ b/test/process/window.zig @@ -7,6 +7,7 @@ const process = @import("../process.zig"); pub const cases = .{ .{ "window: a monkey over the effects: random clicks, drags, wheels, keys, columns, looks and themes, then it settles, alive, with no crash record", monkey }, + .{ "window: its frame corpus, every kept frame and its sha256 into PARDES_GUI_CORPUS, to compare two builds on one machine", corpus }, }; fn envOr(name: [*:0]const u8, default: []const u8) []const u8 { @@ -157,3 +158,383 @@ fn firstPane(f: *process.Files) !?u32 { const end = std.mem.indexOfScalar(u8, index, ' ') orelse return null; return std.fmt.parseInt(u32, index[0..end], 10) catch null; } + +/// The serial of the first pane whose tag holds `word`, waited for. +fn paneWith(f: *process.Files, word: []const u8) !u32 { + const deadline = process.nowMs() + 10_000; + while (process.nowMs() < deadline) : (process.nap(50)) { + var rows = std.mem.tokenizeScalar(u8, try f.read("/index"), '\n'); + while (rows.next()) |row| { + const serial = std.fmt.parseInt(u32, row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse continue], 10) catch continue; + if (process.has(try f.read(f.at("/pane/{d}/tag", .{serial})), word)) return serial; + } + } + std.debug.print("no pane's tag holds {s}\n", .{word}); + return error.NoSuchPane; +} + +/// The serial of a pane /index did not list in `before`, waited for. +fn paneSince(f: *process.Files, before: []const u8) !u32 { + const deadline = process.nowMs() + 10_000; + while (process.nowMs() < deadline) : (process.nap(50)) { + var rows = std.mem.tokenizeScalar(u8, try f.read("/index"), '\n'); + while (rows.next()) |row| { + const head = row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse continue]; + if (std.mem.indexOf(u8, before, f.at("{s} ", .{head})) == null) return std.fmt.parseInt(u32, head, 10); + } + } + return error.NoNewPane; +} + +/// Until the body of pane `serial` holds `word`. +fn bodyHas(f: *process.Files, serial: u32, word: []const u8) !void { + const deadline = process.nowMs() + 10_000; + while (!process.has(try f.read(f.at("/pane/{d}/body", .{serial})), word)) : (process.nap(50)) + if (process.nowMs() > deadline) return error.NoOutput; +} + +extern "c" fn system(cmd: [*:0]const u8) c_int; +extern "c" fn ioctl(fd: c_int, request: c_ulong, ...) c_int; + +/// The frames a corpus keeps (`corpus`): numbered in the order kept, each +/// with its sha256 (sha256sum's lines) and its scene. +const Corpus = struct { + gpa: std.mem.Allocator, + a: std.mem.Allocator, + out: []const u8, + n: u32 = 0, + sums: std.ArrayList(u8) = .empty, + scenes: std.ArrayList(u8) = .empty, + /// The window's own series below this are read and gone: a frame is + /// megabytes, and /tmp is small. + pruned: u32 = 0, + + /// The frames the window has captured, read from its status. + fn captured(k: *Corpus, d: *process.Detached) !u32 { + const text = try process.readWhole(k.a, k.at("{s}/status", .{d.captures()})); + var words = std.mem.tokenizeAny(u8, text, " \n"); + _ = words.next(); + return std.fmt.parseInt(u32, words.next() orelse return error.NoStatus, 10); + } + + fn at(k: *Corpus, comptime fmt: []const u8, args: anytype) []const u8 { + return std.fmt.allocPrint(k.a, fmt, args) catch @panic("out of memory"); + } + + /// Once the window is idle, the frames it has captured: where a step's + /// own begin. What came before is let go. + fn mark(k: *Corpus, d: *process.Detached) !u32 { + try d.settle(k.a); + const to = try k.captured(d); + k.prune(d, to); + return to; + } + + /// The step begun at `from`, once the window is idle again: every frame + /// it drew (a motion, one input on the virtual clock), or only the one + /// that stays (what more than one input or a worker's landing makes, + /// whose frames in between are the machine's timing). + fn keep(k: *Corpus, d: *process.Detached, from: u32, label: []const u8, every: bool) !void { + const to = try k.rest(d); + if (to == 0) return error.NoFrame; + try k.take(d, if (every and to > from) from else to - 1, to, label); + } + + /// Idle, and still idle once a worker had time to land what it owes (a + /// PDF page, a highlighted file: their jobs are not the loop's, and the + /// window is idle while they run): the frames captured then. + fn rest(k: *Corpus, d: *process.Detached) !u32 { + try d.settle(k.a); + var last = try k.captured(d); + for (0..50) |_| { + process.nap(400); + try d.settle(k.a); + const now = try k.captured(d); + if (now == last) return now; + last = now; + } + return error.NeverRests; + } + + /// The first `n` frames since `from` of a scene that never rests (the + /// pet, the snake: they animate while shown), as the virtual clock + /// steps them. + fn first(k: *Corpus, d: *process.Detached, from: u32, n: u32, label: []const u8) !void { + const deadline = process.nowMs() + 30_000; + while (try k.captured(d) < from + n) : (process.nap(5)) + if (process.nowMs() > deadline) return error.NoFrames; + try k.take(d, from, from + n, label); + } + + fn take(k: *Corpus, d: *process.Detached, lo: u32, hi: u32, label: []const u8) !void { + for (lo..hi) |i| { + const bytes = try process.readWhole(k.gpa, k.at("{s}/frame-{d:0>5}.ppm", .{ d.captures(), i })); + defer k.gpa.free(bytes); + var digest: [32]u8 = undefined; + std.crypto.hash.sha2.Sha256.hash(bytes, &digest, .{}); + const name = k.at("frame-{d:0>5}.ppm", .{k.n}); + try process.writeFile(k.a, k.at("{s}/{s}", .{ k.out, name }), bytes); + try k.sums.print(k.a, "{x} {s}\n", .{ digest, name }); + try k.scenes.print(k.a, "{d:0>5} {s}\n", .{ k.n, label }); + std.debug.print("{d:0>5} {x} {s}\n", .{ k.n, digest[0..6], label }); + k.n += 1; + } + k.prune(d, hi); + } + + fn prune(k: *Corpus, d: *process.Detached, below: u32) void { + while (k.pruned < below) : (k.pruned += 1) { + const path = std.fmt.allocPrintSentinel(k.a, "{s}/frame-{d:0>5}.ppm", .{ d.captures(), k.pruned }, 0) catch return; + _ = std.c.unlink(path); + } + } + + fn finish(k: *Corpus) !void { + try process.writeFile(k.a, k.at("{s}/frames.sha256", .{k.out}), k.sums.items); + try process.writeFile(k.a, k.at("{s}/scenes.txt", .{k.out}), k.scenes.items); + } +}; + +/// The window's frame size in pixels, from the frame it last captured. +fn frameSize(a: std.mem.Allocator, d: *process.Detached) ![2]u32 { + const bytes = try d.frame(a); + var words = std.mem.tokenizeAny(u8, bytes[0..@min(bytes.len, 32)], " \n"); + _ = words.next(); + return .{ try std.fmt.parseInt(u32, words.next().?, 10), try std.fmt.parseInt(u32, words.next().?, 10) }; +} + +/// The corpus's files, in a root of their own (fixed: its paths are drawn +/// in the tags), made anew for each window. +const root = "/tmp/pardes-gui-corpus"; +fn corpusRoot(a: std.mem.Allocator) !void { + _ = system("rm -rf -- /tmp/pardes-gui-corpus"); + if (std.c.mkdir(root, 0o700) != 0) return error.NoRoot; + var zig: std.ArrayList(u8) = .empty; + try zig.appendSlice(a, + \\const std = @import("std"); + \\ + \\pub const Shape = union(enum) { + \\ circle: f32, + \\ square: f32, + \\ + \\ pub fn area(shape: Shape) f32 { + \\ return switch (shape) { + \\ .circle => |r| std.math.pi * r * r, + \\ .square => |s| s * s, + \\ }; + \\ } + \\}; + \\ + \\pub fn main() void { + \\ var total: f32 = 0; + \\ const shapes = [_]Shape{ .{ .circle = 1 }, .{ .square = 2 } }; + \\ for (shapes) |shape| { + \\ if (shape == .circle) { + \\ + ); + for (0..80) |n| try zig.print(a, " total += shape.area() * {d}; // line {d}\n", .{ n, n }); + try zig.appendSlice(a, " }\n }\n std.debug.print(\"{d}\\n\", .{total});\n}\n"); + try process.writeFile(a, root ++ "/shapes.zig", zig.items); + try process.writeFile(a, root ++ "/notes.md", + \\# Fallback faces and rules + \\ + \\| name | glyphs | + \\|------|--------| + \\| cjk | 界面字 | + \\| nerd | + ++ "\u{e5ff} \u{f07b} \u{e0b0}" ++ + \\ | + \\| sym | ∮ ≠ ★ ☃ ⚡ ⌘ | + \\ + \\┌──┬──┐ + \\│ a│ b│ + \\├──┼──┤ + \\│ c│ d│ + \\└──┴──┘ + \\ + ); + try process.writeFile(a, root ++ "/ligatures.zig", + \\const a = b -> c; // => != <= >= == === !== |> <| :: ++ -- + \\const fi = "office flow staff"; // f|i f|l s|t stay apart + \\ + ); + var ppm: std.ArrayList(u8) = .empty; + try ppm.print(a, "P6\n{d} {d}\n255\n", .{ 96, 64 }); + for (0..64) |yy| for (0..96) |xx| try ppm.appendSlice(a, &.{ @intCast(xx * 255 / 96), @intCast(yy * 255 / 64), 128 }); + try process.writeFile(a, root ++ "/ramp.ppm", ppm.items); + try process.writeFile(a, root ++ "/doc.pdf", try process.readWhole(a, "docs/9p.pdf")); + // A face of the repo's with ligatures, where `Font` finds it by name. + for ([_][:0]const u8{ root ++ "/.local", root ++ "/.local/share", root ++ "/.local/share/fonts" }) |dir| _ = std.c.mkdir(dir, 0o755); + try process.writeFile(a, root ++ "/.local/share/fonts/MapleMono-NF-Regular.ttf", try process.readWhole(a, "assets/MapleMono-NF-Regular.ttf")); + try process.writeFile(a, root ++ "/sgr.sh", + \\printf '\033[1mbold\033[0m \033[2mdim\033[0m \033[7mreverse\033[0m [\033[8minvisible\033[0m]\n' + \\printf '\033[4:1msingle\033[0m \033[4:2mdouble\033[0m \033[4:3mcurly\033[0m \033[4:4mdotted\033[0m \033[4:5mdashed\033[0m \033[9mstrike\033[0m\n' + \\printf '\033[1;4:3;9mall\033[0m \033[38;2;255;128;0mrgb\033[0m \033[44mbg\033[0m\nEND\n' + \\ + ); + // A kitty image: 8x8 RGB, drawn 10 cells by 4. + var rgb: [8 * 8 * 3]u8 = undefined; + for (0..64) |i| rgb[i * 3 ..][0..3].* = .{ @intCast(i * 4), @intCast(255 - i * 4), @intCast((i % 8) * 32) }; + var b64: [std.base64.standard.Encoder.calcSize(rgb.len)]u8 = undefined; + _ = std.base64.standard.Encoder.encode(&b64, &rgb); + try process.writeFile(a, root ++ "/kitty.sh", try std.fmt.allocPrint(a, "printf '\\033_Ga=T,f=24,s=8,v=8,c=10,r=4;{s}\\033\\\\'\nprintf '\\nKITTY\\n'\n", .{b64})); +} + +/// S0 of the GPU move (.scratch/sketch/briefs/gpu-s0.md): the renderer's +/// frame corpus. Twelve scenes on the hidden window under the virtual +/// clock, each step one input (a 9P write, or one event on its terminal) +/// and then idle; the frames a step keeps go to PARDES_GUI_CORPUS as +/// frame-NNNNN.ppm, their sha256 in frames.sha256 (sha256sum's lines), +/// their scenes in scenes.txt. Two builds' corpora on one machine are the +/// same bytes, or the renderer changed: a GPU and its driver draw their own +/// pixels, so it gates a build against a build, never a machine. +/// PARDES_GUI_CORPUS_SNAKE is the snake_shader addon (`zig build +/// snake-shader`). Skipped without PARDES_GUI_CORPUS. +fn corpus(gpa: std.mem.Allocator, exe: [:0]const u8) !void { + try process.only(.gui); + const out = std.mem.span(std.c.getenv("PARDES_GUI_CORPUS") orelse return error.SkipZigTest); + const snake = std.mem.span(std.c.getenv("PARDES_GUI_CORPUS_SNAKE") orelse { + std.debug.print("PARDES_GUI_CORPUS_SNAKE: the snake_shader addon's library\n", .{}); + return error.NoSnakeAddon; + }); + var arena = std.heap.ArenaAllocator.init(gpa); + defer arena.deinit(); + const a = arena.allocator(); + defer _ = system("rm -rf -- /tmp/pardes-gui-corpus"); + const init = "Shell /bin/sh\nTheme forge\nMessageLinger 0\nCursorBlink off\n"; + var k: Corpus = .{ .gpa = gpa, .a = a, .out = out }; + { + try corpusRoot(a); + var d = try process.Detached.start(gpa, exe, "corpus", .{ .root = root, .file = root ++ "/shapes.zig", .init = init, .grid = .{ 100, 22 }, .env = &.{ + .{ "PARDES_TEST_CLOCK", "1" }, .{ "PARDES_TEST_SERIES", "1" }, .{ "PARDES_TEST_PAD", "7" }, + } }); + defer d.deinit(); + const f = &d.files; + const first = try paneWith(f, "shapes.zig"); + // 1. A Zig file: tags, the block cursor, the scrollbar. + try k.keep(&d, 0, "01 zig file: tags, block cursor, scrollbar", false); + const size = try frameSize(a, &d); + const cell_w = (size[0] - 7) / 100; + const cell_h = (size[1] - 7) / 22; + var from = try k.mark(&d); + try f.write(f.at("/pane/{d}/dot", .{first}), "45\n"); + try k.keep(&d, from, "01 zig file: the cursor at line 45, the thumb mid-track", false); + // 11. Motion on the virtual clock: a wheel's glide (rows shifted, + // the scroll scissor), a jump's trail and the caret's glide quad. + from = try k.mark(&d); + try d.mouse("wheel", -3, cell_w * 40, cell_h * 12); + try k.keep(&d, from, "11 a wheel's glide, every frame", true); + from = try k.mark(&d); + try d.mouse("down", 1, cell_w * 20, cell_h * 10); + try d.mouse("up", 1, cell_w * 20, cell_h * 10); + try k.keep(&d, from, "11 a click moves the caret, every frame", true); + from = try k.mark(&d); + try d.typeKeys("gg"); + try k.keep(&d, from, "11 a jump's trail and the caret's glide, every frame", true); + from = try k.mark(&d); + try f.write("/ctl", "Pet cat\n"); + try k.keep(&d, from, "11 the pet, every frame", true); + try f.write("/ctl", "Pet off\n"); + // 6. Taglines at 80% and the tree-sitter context rows. + from = try k.mark(&d); + try f.write("/ctl", "TaglineSize 80\n"); + try k.keep(&d, from, "06 TaglineSize 80", false); + from = try k.mark(&d); + try f.write(f.at("/pane/{d}/ctl", .{first}), "TreeContext\n"); + try k.keep(&d, from, "06 TreeContext on", false); + from = try k.mark(&d); + try f.write(f.at("/pane/{d}/dot", .{first}), "70\n"); + try k.keep(&d, from, "06 context rows over a deep line", false); + // 3. A terminal's SGR: bold, dim, reverse, invisible, the five + // underlines, strike; 10. a kitty image in it. + const before = try f.read("/index"); + try f.write(f.at("/pane/{d}/exec", .{first}), "Term\n"); + const term = try paneSince(f, before); + try f.write(f.at("/pane/{d}/pty/data", .{term}), "PS1='$ '; clear; sh " ++ root ++ "/sgr.sh\r"); + try bodyHas(f, term, "END"); + try k.keep(&d, 0, "03 a terminal's SGR styles", false); + try f.write(f.at("/pane/{d}/pty/data", .{term}), "clear; sh " ++ root ++ "/kitty.sh\r"); + try bodyHas(f, term, "KITTY"); + try k.keep(&d, 0, "10 a kitty image in a terminal pane", false); + try f.write(f.at("/pane/{d}/exec", .{term}), "Del\n"); + // 5. Fallback faces (CJK, Nerd icons), a markdown table, box rules. + try f.write(f.at("/pane/{d}/look", .{first}), root ++ "/notes.md\n"); + const notes = try paneWith(f, "notes.md"); + try k.keep(&d, 0, "05 fallback glyphs, a markdown table, box rules", false); + // 9. An image pane; a PDF pane and the pointer's word highlighted + // (its dirty rows uploaded). + try f.write(f.at("/pane/{d}/look", .{notes}), root ++ "/ramp.ppm\n"); + const image = try paneWith(f, "ramp.ppm"); + try k.keep(&d, 0, "09 an image pane", false); + try f.write(f.at("/pane/{d}/exec", .{image}), "Del\n"); + try f.write(f.at("/pane/{d}/exec", .{notes}), "Del\n"); + try f.write(f.at("/pane/{d}/look", .{first}), root ++ "/doc.pdf\n"); + _ = try paneWith(f, "doc.pdf"); + try k.keep(&d, 0, "09 a PDF pane", false); + _, const tag_row = try f.where("doc.pdf"); + for ([_][2]usize{ .{ 26, 8 }, .{ 26, 3 }, .{ 40, 8 } }) |off| { + from = try k.mark(&d); + try d.mouse("motion", 0, @intCast(cell_w * off[0] + cell_w / 2), @intCast(cell_h * (tag_row + off[1]) + cell_h / 2)); + try k.keep(&d, from, "09 the PDF's hovered word, its rows painted again", false); + } + // 7. Three columns, five panes, focus and dirty grips, InactiveDim. + try f.write("/ctl", "Newcol\n"); + try f.write("/ctl", "Newcol\n"); + const scratch = try f.newPane(); + try f.write(f.at("/pane/{d}/body", .{scratch}), "a dirty pane\n"); + try f.write("/ctl", "InactiveDim 40\n"); + try k.keep(&d, 0, "07 three columns, a dirty pane, InactiveDim 40", false); + // 8. WindowOpacity at 71 and 0. + try f.write("/ctl", "WindowOpacity 71\n"); + try k.keep(&d, 0, "08 WindowOpacity 71", false); + try f.write("/ctl", "WindowOpacity 0\n"); + try k.keep(&d, 0, "08 WindowOpacity 0", false); + try f.write("/ctl", "WindowOpacity 100\n"); + // 4. Ligatures in a face that has them; 12. the font changed (the + // atlas reset). + try f.write(f.at("/pane/{d}/look", .{first}), root ++ "/ligatures.zig\n"); + _ = try paneWith(f, "ligatures.zig"); + try k.keep(&d, 0, "04 ligatures.zig in the default face", false); + try f.write("/ctl", "Font MapleMono-NF-Regular:16\n"); + try k.keep(&d, 0, "04 12 Font MapleMono 16: ligatures, the atlas reset", false); + // The window resized: its terminal's size, which a test window follows. + var ws: std.posix.winsize = .{ .row = 20, .col = 70, .xpixel = 0, .ypixel = 0 }; + if (ioctl(d.child.master, 0x5414, &ws) != 0) return error.NoResize; // TIOCSWINSZ + try k.keep(&d, 0, "12 the window resized to 70x20", false); + } + // 12. The snake: an addon's GPU scene and its CRT post-process, + // through the premultiplied pipeline. A window of its own: its picture + // breathes on the clock, and a long session's clock has seen the + // machine's timing (a timer an input cancelled, or not yet). + k.pruned = 0; + { + try corpusRoot(a); + var d = try process.Detached.start(gpa, exe, "corpus-snake", .{ .root = root, .file = root ++ "/shapes.zig", .init = init, .grid = .{ 80, 24 }, .env = &.{ + .{ "PARDES_TEST_CLOCK", "1" }, .{ "PARDES_TEST_SERIES", "1" }, .{ "PARDES_TEST_PAD", "7" }, + } }); + defer d.deinit(); + const f = &d.files; + const first = try paneWith(f, "shapes.zig"); + _ = try k.rest(&d); + try f.write("/ctl", f.at("Addon load {s}\n", .{snake})); + try k.keep(&d, 0, "12 the snake addon loaded", false); + const from = try k.mark(&d); + try f.write(f.at("/pane/{d}/exec", .{first}), "SnakeShader\n"); + try k.first(&d, from, 10, "12 SnakeShader's CRT scene, its first frames"); + } + // 2. Scene 1 at a 2x display. + k.pruned = 0; + { + try corpusRoot(a); + var d = try process.Detached.start(gpa, exe, "corpus2x", .{ .root = root, .file = root ++ "/shapes.zig", .init = init, .grid = .{ 50, 14 }, .env = &.{ + .{ "PARDES_TEST_CLOCK", "1" }, .{ "PARDES_TEST_SERIES", "1" }, .{ "PARDES_TEST_PAD", "7" }, .{ "PARDES_TEST_SCALE", "2" }, + } }); + defer d.deinit(); + const f = &d.files; + const first = try paneWith(f, "shapes.zig"); + try k.keep(&d, 0, "02 the zig file at a 2x display", false); + try f.write(f.at("/pane/{d}/dot", .{first}), "20,22\n"); + try k.keep(&d, 0, "02 a selection at a 2x display", false); + } + try k.finish(); +} -- cgit v1.3 From 30a603341e3b23da6e5ab5995b060c8e4ec52242 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:01:02 -0300 Subject: test: the GPU move's gates, sharper: a window the harness starts runs under PARDES_TEST_WRAP when it names a command (perf stat, a script), given the window's argv, its binary made absolute; a test window's series keeps its frames as PAM (P7 RGB_ALPHA), so the corpus sees the alpha a blend writes (latest.ppm and every other capture stay RGB); the corpus adds a translucent addon picture, test/veil.zig (zig build veil: a GPU pass that clears to half alpha and draws nothing), for the addon pipeline's colour factor, which the snake's opaque picture never showed; the snake's window ends with its pane gone and at rest, so it ends having drawn as many frames every run --- build.zig | 4 ++++ src/gui/gui.zig | 27 ++++++++++++++++++--------- test/process.zig | 22 ++++++++++++++++------ test/process/window.zig | 24 ++++++++++++++++++------ test/veil.zig | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 21 deletions(-) create mode 100644 test/veil.zig (limited to 'test/process') diff --git a/build.zig b/build.zig index 0e67b61e..7e3c812d 100644 --- a/build.zig +++ b/build.zig @@ -447,6 +447,7 @@ pub fn build(b: *std.Build) void { const addon_mod = b.createModule(.{ .root_source_file = b.path("src/kinds/addon.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "pardes_addon", .module = kind_mod }} }); const snake_shader_step = b.step("snake-shader", "build the optional GPU snake addon into zig-out/lib"); var snake_shader_lib: ?*std.Build.Step.Compile = null; + const veil_step = b.step("veil", "build the frame corpus's translucent GPU test addon (test/veil.zig) into zig-out/lib"); if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| { const files = b.addWriteFiles(); for ([_][]const u8{ "vert", "frag", "post.frag" }) |stage| { @@ -461,6 +462,9 @@ pub fn build(b: *std.Build) void { snake_shader_lib = lib; snake_shader_step.dependOn(&b.addInstallArtifact(lib, .{}).step); snake_shader_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step); + // The frame corpus's translucent addon (test/veil.zig), into lib/ beside the snake. + const veil = b.addLibrary(.{ .name = "veil", .linkage = .dynamic, .root_module = b.createModule(.{ .root_source_file = b.path("test/veil.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "addon", .module = addon_mod }, .{ .name = "gpu_scene", .module = sb.module("gpu_scene") } } }) }); + veil_step.dependOn(&b.addInstallArtifact(veil, .{}).step); } // The PDF addon (addons/pdf/pdf.zig over MuPDF through pdf_bridge.c), built // and installed as the image addon is: compiled only for a step that diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a67602e8..cc43d8b6 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -5830,10 +5830,10 @@ fn captureFrame(g: *Gui, gpa: std.mem.Allocator, cmd: *c.SDL_GPUCommandBuffer, t const mapped = c.SDL_MapGPUTransferBuffer(g.device, xfer, false) orelse return error.GpuMap; defer c.SDL_UnmapGPUTransferBuffer(g.device, xfer); const pixels: [*]const u8 = @ptrCast(mapped); - try writeCapturePpm(g, gpa, pixels[0..size], sw, sh, "latest"); + try writeCapturePpm(g, gpa, pixels[0..size], sw, sh, "latest", false); var name: [24]u8 = undefined; // unreachable below: "frame-" and a u32 fit 24 - if (g.capture_series) try writeCapturePpm(g, gpa, pixels[0..size], sw, sh, std.fmt.bufPrint(&name, "frame-{d:0>5}", .{g.captured}) catch unreachable); + if (g.capture_series) try writeCapturePpm(g, gpa, pixels[0..size], sw, sh, std.fmt.bufPrint(&name, "frame-{d:0>5}", .{g.captured}) catch unreachable, true); g.captured +%= 1; } @@ -5863,7 +5863,10 @@ fn writeTestStatus(g: *const Gui, core: *const pardes.Pardes, presented: bool) v _ = libc.rename(tmp_path, final_path); } -fn writeCapturePpm(g: *Gui, gpa: std.mem.Allocator, pixels: []const u8, width: u32, height: u32, name: []const u8) !void { +/// `.ppm`, RGB; with `alpha`, `.pam` (P7 RGB_ALPHA) instead: a +/// series keeps the alpha a blend writes, which the window's transparency +/// shows and an RGB capture cannot. +fn writeCapturePpm(g: *Gui, gpa: std.mem.Allocator, pixels: []const u8, width: u32, height: u32, name: []const u8, alpha: bool) !void { // A latency trace times frames, and an 8 MB file write per frame would // stall the loop it is timing. if (g.capture_dir.len == 0 or latency_fd >= 0) return; @@ -5877,27 +5880,33 @@ fn writeCapturePpm(g: *Gui, gpa: std.mem.Allocator, pixels: []const u8, width: u var tmp_buf: [4096]u8 = undefined; var final_buf: [4096]u8 = undefined; - const tmp_path = std.fmt.bufPrintSentinel(&tmp_buf, "{s}/{s}.ppm.tmp", .{ g.capture_dir, name }, 0) catch return error.CapturePathTooLong; - const final_path = std.fmt.bufPrintSentinel(&final_buf, "{s}/{s}.ppm", .{ g.capture_dir, name }, 0) catch return error.CapturePathTooLong; + const ext = if (alpha) "pam" else "ppm"; + const tmp_path = std.fmt.bufPrintSentinel(&tmp_buf, "{s}/{s}.{s}.tmp", .{ g.capture_dir, name, ext }, 0) catch return error.CapturePathTooLong; + const final_path = std.fmt.bufPrintSentinel(&final_buf, "{s}/{s}.{s}", .{ g.capture_dir, name, ext }, 0) catch return error.CapturePathTooLong; const fd = libc.open(tmp_path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644)); if (fd < 0) return error.CaptureWriteFailed; defer _ = libc.close(fd); - var header: [64]u8 = undefined; - const hdr = std.fmt.bufPrint(&header, "P6\n{d} {d}\n255\n", .{ width, height }) catch unreachable; // two u32s fit 64 + var header: [96]u8 = undefined; + const hdr = if (alpha) + std.fmt.bufPrint(&header, "P7\nWIDTH {d}\nHEIGHT {d}\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n", .{ width, height }) catch unreachable // two u32s fit 96 + else + std.fmt.bufPrint(&header, "P6\n{d} {d}\n255\n", .{ width, height }) catch unreachable; // two u32s fit 96 _ = host_io.writeFd(fd, hdr); - const row_rgb = try gpa.alloc(u8, @as(usize, width) * 3); + const depth: usize = if (alpha) 4 else 3; + const row_rgb = try gpa.alloc(u8, @as(usize, width) * depth); defer gpa.free(row_rgb); for (0..height) |row| { const src = pixels[row * width * 4 ..][0 .. @as(usize, width) * 4]; for (0..width) |x| { const si = x * 4; - const di = x * 3; + const di = x * depth; row_rgb[di + 0] = src[si + if (bgr) @as(usize, 2) else 0]; row_rgb[di + 1] = src[si + 1]; row_rgb[di + 2] = src[si + if (bgr) @as(usize, 0) else 2]; + if (alpha) row_rgb[di + 3] = src[si + 3]; } _ = host_io.writeFd(fd, row_rgb); } diff --git a/test/process.zig b/test/process.zig index 67d883ca..126a7924 100644 --- a/test/process.zig +++ b/test/process.zig @@ -616,12 +616,22 @@ pub const Detached = struct { try std.fmt.bufPrintSentinel(&bufs[1], "--9p={s}", .{opts.socket orelse name}, 0) else try std.fmt.bufPrintSentinel(&bufs[1], "--detach={s}", .{name}, 0); + // PARDES_TEST_WRAP: a command a window runs under (`perf stat ...`, + // a script), given the window's own argv; the binary made absolute, + // as the child runs elsewhere. + const wrap = if (gui) std.c.getenv("PARDES_TEST_WRAP") else null; + var exe_buf: [4096]u8 = undefined; var argv: [24][:0]const u8 = undefined; - if (opts.args.len + 3 > argv.len) return error.TooManyArgs; - argv[0] = exe; - argv[1] = first; - for (opts.args, 0..) |a, k| argv[2 + k] = a; - argv[2 + opts.args.len] = fixture; + if (opts.args.len + 4 > argv.len) return error.TooManyArgs; + var at: usize = 0; + if (wrap) |w| { + argv[0] = std.mem.span(w); + at = 1; + } + argv[at] = if (wrap != null) std.mem.span(realpath(exe, &exe_buf) orelse return error.NoBinary) else exe; + argv[at + 1] = first; + for (opts.args, 0..) |a, k| argv[at + 2 + k] = a; + argv[at + 2 + opts.args.len] = fixture; var grid_buf: [2][8]u8 = undefined; var env: [16][2][:0]const u8 = undefined; var n: usize = 0; @@ -641,7 +651,7 @@ pub const Detached = struct { env[n] = kv; n += 1; } - d.child = try Child.spawn(argv[0 .. 3 + opts.args.len], .{ + d.child = try Child.spawn(argv[0 .. at + 3 + opts.args.len], .{ .root = dir, .env = env[0..n], // A window runs on a terminal of its own, as one started from a diff --git a/test/process/window.zig b/test/process/window.zig index e39e0963..687b9000 100644 --- a/test/process/window.zig +++ b/test/process/window.zig @@ -197,7 +197,8 @@ extern "c" fn system(cmd: [*:0]const u8) c_int; extern "c" fn ioctl(fd: c_int, request: c_ulong, ...) c_int; /// The frames a corpus keeps (`corpus`): numbered in the order kept, each -/// with its sha256 (sha256sum's lines) and its scene. +/// a PAM with its alpha (the window's series), with its sha256 (sha256sum's +/// lines) and its scene. const Corpus = struct { gpa: std.mem.Allocator, a: std.mem.Allocator, @@ -268,11 +269,11 @@ const Corpus = struct { fn take(k: *Corpus, d: *process.Detached, lo: u32, hi: u32, label: []const u8) !void { for (lo..hi) |i| { - const bytes = try process.readWhole(k.gpa, k.at("{s}/frame-{d:0>5}.ppm", .{ d.captures(), i })); + const bytes = try process.readWhole(k.gpa, k.at("{s}/frame-{d:0>5}.pam", .{ d.captures(), i })); defer k.gpa.free(bytes); var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(bytes, &digest, .{}); - const name = k.at("frame-{d:0>5}.ppm", .{k.n}); + const name = k.at("frame-{d:0>5}.pam", .{k.n}); try process.writeFile(k.a, k.at("{s}/{s}", .{ k.out, name }), bytes); try k.sums.print(k.a, "{x} {s}\n", .{ digest, name }); try k.scenes.print(k.a, "{d:0>5} {s}\n", .{ k.n, label }); @@ -284,7 +285,7 @@ const Corpus = struct { fn prune(k: *Corpus, d: *process.Detached, below: u32) void { while (k.pruned < below) : (k.pruned += 1) { - const path = std.fmt.allocPrintSentinel(k.a, "{s}/frame-{d:0>5}.ppm", .{ d.captures(), k.pruned }, 0) catch return; + const path = std.fmt.allocPrintSentinel(k.a, "{s}/frame-{d:0>5}.pam", .{ d.captures(), k.pruned }, 0) catch return; _ = std.c.unlink(path); } } @@ -384,8 +385,8 @@ fn corpusRoot(a: std.mem.Allocator) !void { /// frame corpus. Twelve scenes on the hidden window under the virtual /// clock, each step one input (a 9P write, or one event on its terminal) /// and then idle; the frames a step keeps go to PARDES_GUI_CORPUS as -/// frame-NNNNN.ppm, their sha256 in frames.sha256 (sha256sum's lines), -/// their scenes in scenes.txt. Two builds' corpora on one machine are the +/// frame-NNNNN.pam (RGBA: a blend's alpha too), their sha256 in +/// frames.sha256 (sha256sum's lines), their scenes in scenes.txt. Two builds' corpora on one machine are the /// same bytes, or the renderer changed: a GPU and its driver draw their own /// pixels, so it gates a build against a build, never a machine. /// PARDES_GUI_CORPUS_SNAKE is the snake_shader addon (`zig build @@ -501,6 +502,12 @@ fn corpus(gpa: std.mem.Allocator, exe: [:0]const u8) !void { var ws: std.posix.winsize = .{ .row = 20, .col = 70, .xpixel = 0, .ypixel = 0 }; if (ioctl(d.child.master, 0x5414, &ws) != 0) return error.NoResize; // TIOCSWINSZ try k.keep(&d, 0, "12 the window resized to 70x20", false); + // A translucent addon picture (test/veil.zig, beside the snake's + // library): the addon pipeline's colour factor, which the snake's + // opaque picture never shows. + try f.write("/ctl", f.at("Addon load {s}/libveil.so\n", .{std.fs.path.dirname(snake).?})); + try f.write(f.at("/pane/{d}/exec", .{first}), "Veil\n"); + try k.keep(&d, 0, "12 a translucent addon picture over its pane", false); } // 12. The snake: an addon's GPU scene and its CRT post-process, // through the premultiplied pipeline. A window of its own: its picture @@ -519,8 +526,13 @@ fn corpus(gpa: std.mem.Allocator, exe: [:0]const u8) !void { try f.write("/ctl", f.at("Addon load {s}\n", .{snake})); try k.keep(&d, 0, "12 the snake addon loaded", false); const from = try k.mark(&d); + const before = try f.read("/index"); try f.write(f.at("/pane/{d}/exec", .{first}), "SnakeShader\n"); try k.first(&d, from, 10, "12 SnakeShader's CRT scene, its first frames"); + // Then gone, and the window at rest: it ends having drawn as many + // frames every run (an instruction count's, perfwrap.sh). + try f.write(f.at("/pane/{d}/exec", .{try paneSince(f, before)}), "Del\n"); + _ = try k.rest(&d); } // 2. Scene 1 at a 2x display. k.pruned = 0; diff --git a/test/veil.zig b/test/veil.zig new file mode 100644 index 00000000..ed49e1c3 --- /dev/null +++ b/test/veil.zig @@ -0,0 +1,37 @@ +//! A test addon whose GPU picture is translucent: its pass draws nothing +//! and clears to a premultiplied half-alpha colour, so the window's addon +//! pipeline blends it over the pane (the frame corpus's case for the +//! pipeline's colour factor, which an opaque picture never shows). +const std = @import("std"); +const addon = @import("addon"); +const scene = @import("gpu_scene"); + +pub const Veil = extern struct { + painted: u32, + + pub const head: addon.Head = .{ .name = "veil", .version = 1, .words = &.{ + .{ .name = "Veil", .opens = true, .doc = "a translucent GPU picture over the pane (a test addon)" }, + } }; + + pub fn open(v: *Veil, _: ?[]const u8, _: ?[]const u8) void { + v.painted = 0; + } + + pub fn paint(_: *const Veil, w: addon.Window, out: addon.Paint) void { + w.put(0, 0, "veil over this", .{}); + const width: u32 = @min(4096, @max(1, @as(u32, w.cols) * w.cell_w)); + const height: u32 = @min(4096, @max(1, @as(u32, w.rows) * w.cell_h)); + var buffer: [256]u8 = undefined; + var writer: std.Io.Writer = .fixed(&buffer); + scene.encode(&writer, .{ .image = .none, .width = width, .height = height, .clear = .{ 0.30, 0.10, 0.20, 0.5 }, .pipelines = &.{}, .draws = &.{} }) catch return; + _ = out.gpu(writer.buffered()); + } + + pub const dump = addon.dumpBytes; + pub const load = addon.loadBytes; +}; + +pub const pardes_addon = addon.ops(Veil); +comptime { + addon.exportLib(&pardes_addon); +} -- cgit v1.3