From 1637cf5efa338fee7797ba83f264dcab5bde4800 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 05:50:47 -0300 Subject: examples/live: the interpreter of a tape, and its probe Demo 1's route: a program becomes a flat tape of at most 128 instructions (src/Lang.zig: the ops, the instruction, the tape and the two uniform blocks that carry it; build.zig writes Lang.Op out as ops.glsl, so the op numbers have one definition), and ONE interpreter, shaders/vm.glsl, runs it per pixel (picture.frag) and per sample (synth.comp, crossing from the tape it replaces over the first `fade` samples). Instruction i writes register i; the tape is pushed as uniforms. The probe measures it before anything else is built: `zig build probe` plays made-up tapes (every instruction reaching the last, a noise in ten) under --profile-gpu. RTX 3050 (driver 620.70.06), Debug host, means of 601 frames, three runs agreeing within 0.07 ms: picture, empty tape 720p 0.14-0.23 1080p 0.19-0.28 (a pass's cost) picture, 60 720p 1.38-1.45 1080p 2.89-2.96 picture, 128 720p 2.71-2.81 1080p 5.86-5.95 picture, 128 at 640x360 0.85-0.94 The budget, under 2 ms at 1280x720: 60 instructions meet it, 128 do not. The cheap fixes, on 128 at 720p: the tape in a storage buffer 2.97-3.03 (worse); the registers as an array of 16 (kept in registers, indexing turned into selects) 5.82-5.93 (twice as slow); the 128-array with only 16 slots used 2.11-2.34 (cache footprint: a register allocator would buy about a sixth); half resolution 0.85-0.94. So the sketch will draw a tape longer than 64 instructions at half resolution, scaled up. The variants are kept outside the tree. The synth's numbers taken here were of a kernel that wrote nothing (its uniform block was past SDL's 4 KiB): the sound's change measures it again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/live/build.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/live/build.zig (limited to 'examples/live/build.zig') diff --git a/examples/live/build.zig b/examples/live/build.zig new file mode 100644 index 0000000..cc93612 --- /dev/null +++ b/examples/live/build.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const sketchbook = @import("sketchbook"); +const Lang = @import("src/Lang.zig"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const sb = sketchbook.init(b, .{ .target = target, .optimize = optimize }); + + // The interpreter's op numbers, from Lang.Op: one definition. + const ops = b.addWriteFiles(); + _ = ops.add("ops.glsl", comptime blk: { + var glsl: []const u8 = "// Generated from Lang.Op by build.zig; do not edit.\n"; + for (std.enums.values(Lang.Op)) |op| glsl = glsl ++ std.fmt.comptimePrint("const uint OP_{t} = {d}u;\n", .{ op, @intFromEnum(op) }); + break :blk glsl; + }); + const lang = b.createModule(.{ .root_source_file = b.path("src/Lang.zig") }); + const shaders = sb.addShaders(.{ + .layouts = .{ .module = lang, .types = &.{ "Picture", "Synth" } }, + .include_dirs = &.{ops.getDirectory()}, + .sources = &.{ + b.path("shaders/fullscreen.vert"), + b.path("shaders/picture.frag"), + b.path("shaders/synth.comp"), + }, + }); + const imports: []const std.Build.Module.Import = &.{ + .{ .name = "Lang", .module = lang }, + .{ .name = "shaders", .module = shaders.module }, + }; + + // The interpreter's GPU time over made-up tapes (`--profile-gpu`). + const probe = sb.app(.{ .name = "live-probe", .root_source_file = b.path("src/probe.zig"), .imports = imports }); + const run_probe = b.addRunArtifact(probe.addExecutable()); + run_probe.addArgs(&.{ "--headless", "--fresh", "--profile-gpu", "--script" }); + run_probe.addFileArg(b.path("test/probe.script")); + run_probe.addArg("--state"); + _ = run_probe.addOutputFileArg("probe.xitdb"); + run_probe.setCwd(b.path(".")); + run_probe.stdio = .inherit; + run_probe.has_side_effects = true; + b.step("probe", "Time the interpreter on the GPU over made-up tapes").dependOn(&run_probe.step); +} -- cgit v1.3