summaryrefslogtreecommitdiff
path: root/examples/live/src/Lang.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/Lang.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/Lang.zig')
-rw-r--r--examples/live/src/Lang.zig95
1 files changed, 95 insertions, 0 deletions
diff --git a/examples/live/src/Lang.zig b/examples/live/src/Lang.zig
new file mode 100644
index 0000000..644ff61
--- /dev/null
+++ b/examples/live/src/Lang.zig
@@ -0,0 +1,95 @@
+//! The language: lines of `name = expression` over `t`, `x` and `y`,
+//! compiled into two tapes, `out` (heard) and `color` (seen), that one
+//! interpreter (shaders/vm.glsl) runs per sample and per pixel.
+//!
+//! A tape is at most `max_tape` instructions; instruction i writes register
+//! i from earlier ones. Its GPU form is the uniform blocks at the end of
+//! this file, and build.zig writes `Op` out as ops.glsl for the shaders:
+//! this file is the one definition of both.
+const std = @import("std");
+
+pub const max_tape = 128;
+
+/// By how many registers an instruction reads: none, one, two, three.
+pub const Op = enum(u32) {
+ /// `a` is the value's bits.
+ constant,
+ t,
+ x,
+ y,
+ neg,
+ sin,
+ cos,
+ exp,
+ log,
+ sqrt,
+ abs,
+ floor,
+ fract,
+ saw,
+ sqr,
+ tri,
+ /// Register `a`, so that seq's values sit side by side.
+ copy,
+ /// Register `b + floor(a) mod c`: `b` and `c` are numbers, not
+ /// registers it reads, and registers b.. hold copies.
+ seq,
+ add,
+ sub,
+ mul,
+ div,
+ pow,
+ min,
+ max,
+ step,
+ atan2,
+ clamp,
+ mix,
+ smoothstep,
+ noise,
+ /// A colour, only as the color tape's last instruction.
+ hsv,
+ rgb,
+};
+
+pub const Instruction = extern struct {
+ op: Op = .constant,
+ a: u32 = 0,
+ b: u32 = 0,
+ c: u32 = 0,
+};
+
+pub const Tape = struct {
+ len: u32 = 0,
+ code: [max_tape]Instruction = @splat(.{}),
+};
+
+/// picture.frag's uniforms (SDL_GPU's fragment set).
+pub const Picture = extern struct {
+ tape: [max_tape][4]u32,
+ len: u32,
+ /// Seconds.
+ time: f32,
+ /// The target, pixels.
+ size: [2]f32,
+
+ pub const glsl = .{ .uniform = .{ .set = 3, .instance = "u" } };
+};
+
+/// synth.comp's uniforms (SDL_GPU's compute set): the tape, and the one
+/// it replaces, which the first `fade` samples cross from.
+pub const Synth = extern struct {
+ /// The new tape, then the old one.
+ tape: [2 * max_tape][4]u32,
+ len: u32,
+ old_len: u32,
+ /// Samples in the block.
+ count: u32,
+ fade: u32,
+ /// The block's first sample, seconds.
+ start: f32,
+ rate: f32,
+ pad: [2]f32 = .{ 0, 0 },
+
+ pub const glsl = .{ .uniform = .{ .set = 2, .instance = "u" } };
+};