summaryrefslogtreecommitdiff
path: root/examples/live/src/probe.zig
diff options
context:
space:
mode:
Diffstat (limited to 'examples/live/src/probe.zig')
-rw-r--r--examples/live/src/probe.zig133
1 files changed, 133 insertions, 0 deletions
diff --git a/examples/live/src/probe.zig b/examples/live/src/probe.zig
new file mode 100644
index 0000000..4f816b2
--- /dev/null
+++ b/examples/live/src/probe.zig
@@ -0,0 +1,133 @@
+//! 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,
+ tapes: 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] = .{ @backingInt(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, .readonly_buffers = 1, .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.tapes = f.host.newBuffer(2 * Lang.max_tape * 16);
+ }
+ 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| {
+ const both = f.scratch.create([2][Lang.max_tape][4]u32) catch return;
+ both.* = .{ tape(k.len), tape(k.len) };
+ f.upload(ss.tapes, 0, std.mem.asBytes(both));
+ f.compute(.{
+ .label = k.label,
+ .kernel = kernel,
+ .readonly = &.{ss.tapes},
+ .readwrite = &.{ss.block},
+ .uniforms = gpu.uniform(f.scratch, Lang.Synth{
+ .len = k.len,
+ .old_len = k.len,
+ .count = k.count,
+ .fade = if (k.fade) k.count else 0,
+ .start = ss.time,
+ .old_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 }, .{});
+}