diff options
Diffstat (limited to 'examples/live/src')
| -rw-r--r-- | examples/live/src/Lang.zig | 95 | ||||
| -rw-r--r-- | examples/live/src/probe.zig | 130 |
2 files changed, 225 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" } }; +}; 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 }, .{}); +} |
