summaryrefslogtreecommitdiff
path: root/examples/live/src/probe.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 05:50:47 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit1637cf5efa338fee7797ba83f264dcab5bde4800 (patch)
tree7d11506d62f4c9613747fb517952c0ca24a3ef3f /examples/live/src/probe.zig
parent85452cbb28ec78913d72017fdcb666c40e2fc153 (diff)
downloadsketchbook-1637cf5efa338fee7797ba83f264dcab5bde4800.tar.gz
sketchbook-1637cf5efa338fee7797ba83f264dcab5bde4800.zip
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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/live/src/probe.zig')
-rw-r--r--examples/live/src/probe.zig130
1 files changed, 130 insertions, 0 deletions
diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig
new file mode 100644
index 0000000..663bed3
--- /dev/null
+++ b/examples/live/src/probe.zig
@@ -0,0 +1,130 @@
+//! The probe: the interpreter alone over made-up tapes of 60 and 128
+//! instructions, each pass at 1280x720 and 1920x1080 (and 128 at half of
+//! 1280x720, as the sketch draws a long tape), and the synth kernel over
+//! blocks of 256 and 1024 samples. `zig build probe` plays
+//! test/probe.script with `--profile-gpu`, which prints each label's time
+//! at exit; the empty tape's is the cost of a pass doing nothing.
+const std = @import("std");
+const sk = @import("sketch");
+const gpu = sk.abi.gpu;
+const Lang = @import("Lang");
+const shaders = @import("shaders");
+
+pub const name = "live-probe";
+pub const width = 640;
+pub const height = 360;
+
+pub const Session = struct {
+ images: [3]sk.Image = .{ .none, .none, .none },
+ block: sk.Buffer = .none,
+ time: f32 = 0,
+};
+
+const sizes = [3][2]u32{ .{ 1280, 720 }, .{ 1920, 1080 }, .{ 640, 360 } };
+
+const Picture = struct { label: []const u8, len: u32, size: u2 };
+const pictures = [_]Picture{
+ .{ .label = "picture 0 720p", .len = 0, .size = 0 },
+ .{ .label = "picture 60 720p", .len = 60, .size = 0 },
+ .{ .label = "picture 128 720p", .len = 128, .size = 0 },
+ .{ .label = "picture 0 1080p", .len = 0, .size = 1 },
+ .{ .label = "picture 60 1080p", .len = 60, .size = 1 },
+ .{ .label = "picture 128 1080p", .len = 128, .size = 1 },
+ .{ .label = "picture 128 360p", .len = 128, .size = 2 },
+};
+
+const Block = struct { label: []const u8, len: u32, count: u32, fade: bool = false };
+const blocks = [_]Block{
+ .{ .label = "synth 60 x256", .len = 60, .count = 256 },
+ .{ .label = "synth 128 x256", .len = 128, .count = 256 },
+ .{ .label = "synth 60 x1024", .len = 60, .count = 1024 },
+ .{ .label = "synth 128 x1024", .len = 128, .count = 1024 },
+ .{ .label = "synth 128 x1024 fading", .len = 128, .count = 1024, .fade = true },
+};
+
+/// `len` instructions, every one reaching the last: x, y, t and a
+/// constant, then a cycle of ops over the three before, a noise every
+/// tenth, and hsv.
+fn madeUp(comptime len: u32) [Lang.max_tape][4]u32 {
+ @setEvalBranchQuota(10_000);
+ var code: [Lang.max_tape][4]u32 = @splat(.{ 0, 0, 0, 0 });
+ const cycle = [_]Lang.Op{ .mul, .add, .sin, .fract, .sub, .smoothstep, .abs, .mix, .cos };
+ for (0..len) |i| {
+ const op: Lang.Op = switch (i) {
+ 0 => .x,
+ 1 => .y,
+ 2 => .t,
+ 3 => .constant,
+ else => if (i == len - 1) .hsv else if (i % 10 == 9) .noise else cycle[i % cycle.len],
+ };
+ const a: u32 = if (op == .constant) @bitCast(@as(f32, 6)) else @intCast(i -| 1);
+ code[i] = .{ @intFromEnum(op), a, @intCast(i -| 2), @intCast(i -| 3) };
+ }
+ return code;
+}
+
+const tapes = .{ madeUp(0), madeUp(60), madeUp(128) };
+
+fn tape(len: u32) [Lang.max_tape][4]u32 {
+ return switch (len) {
+ 0 => tapes[0],
+ 60 => tapes[1],
+ else => tapes[2],
+ };
+}
+
+const pipeline: gpu.Pipeline = .{
+ .vertex = .{ .code = shaders.fullscreen_vert },
+ .fragment = .{ .code = shaders.picture_frag, .uniform_buffers = 1 },
+ .stride = 0,
+ .attributes = &.{},
+ .blend = .none,
+};
+const kernel: gpu.Kernel = .{ .code = shaders.synth_comp, .readwrite_buffers = 1, .uniform_buffers = 1 };
+
+pub fn draw(ss: *Session, f: *sk.Frame) void {
+ f.clear(0x000000);
+ if (!f.host.gpu()) return f.text(8, 8, 2, "no GPU here", 0xffffff);
+ if (ss.block == .none) {
+ for (&ss.images, sizes) |*image, size| image.* = f.host.newImage(size[0], size[1]);
+ ss.block = f.host.newBuffer(1024 * @sizeOf(f32));
+ }
+ ss.time += @floatCast(@as(f64, @floatFromInt(f.in.dt)) / std.time.ns_per_s);
+ for (pictures) |p| {
+ const size = sizes[p.size];
+ f.gpu(.{
+ .label = p.label,
+ .image = ss.images[p.size],
+ .width = size[0],
+ .height = size[1],
+ .pipelines = &.{pipeline},
+ .draws = &.{.{ .pipeline = 0, .vertex_count = 3, .fragment_uniforms = gpu.uniform(f.scratch, Lang.Picture{
+ .tape = tape(p.len),
+ .len = p.len,
+ .time = ss.time,
+ .size = .{ @floatFromInt(size[0]), @floatFromInt(size[1]) },
+ }) }},
+ });
+ }
+ for (blocks) |k| {
+ var both: [2 * Lang.max_tape][4]u32 = undefined;
+ both[0..Lang.max_tape].* = tape(k.len);
+ both[Lang.max_tape..].* = tape(k.len);
+ f.compute(.{
+ .label = k.label,
+ .kernel = kernel,
+ .readwrite = &.{ss.block},
+ .uniforms = gpu.uniform(f.scratch, Lang.Synth{
+ .tape = both,
+ .len = k.len,
+ .old_len = k.len,
+ .count = k.count,
+ .fade = if (k.fade) k.count else 0,
+ .start = ss.time,
+ .rate = 48000,
+ }),
+ .groups = .{ (k.count + 63) / 64, 1, 1 },
+ });
+ }
+ f.image(ss.images[0], .{ .x = 0, .y = 0, .w = f.in.width, .h = f.in.height }, .{});
+}