diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/live/build.zig | 43 | ||||
| -rw-r--r-- | examples/live/build.zig.zon | 10 | ||||
| -rw-r--r-- | examples/live/shaders/fullscreen.vert | 6 | ||||
| -rw-r--r-- | examples/live/shaders/picture.frag | 13 | ||||
| -rw-r--r-- | examples/live/shaders/synth.comp | 26 | ||||
| -rw-r--r-- | examples/live/shaders/vm.glsl | 92 | ||||
| -rw-r--r-- | examples/live/src/Lang.zig | 95 | ||||
| -rw-r--r-- | examples/live/src/probe.zig | 130 | ||||
| -rw-r--r-- | examples/live/test/probe.script | 4 |
9 files changed, 419 insertions, 0 deletions
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); +} diff --git a/examples/live/build.zig.zon b/examples/live/build.zig.zon new file mode 100644 index 0000000..27bd831 --- /dev/null +++ b/examples/live/build.zig.zon @@ -0,0 +1,10 @@ +.{ + .name = .live, + .version = "0.0.1", + .dependencies = .{ + .sketchbook = .{ .path = "../.." }, + }, + .minimum_zig_version = "0.17.0", + .paths = .{""}, + .fingerprint = 0x530f2caf5c8f14ab, +} diff --git a/examples/live/shaders/fullscreen.vert b/examples/live/shaders/fullscreen.vert new file mode 100644 index 0000000..f45ee09 --- /dev/null +++ b/examples/live/shaders/fullscreen.vert @@ -0,0 +1,6 @@ +#version 450 +// One triangle covering the target. +void main() { + vec2 p = vec2(float((gl_VertexIndex << 1) & 2), float(gl_VertexIndex & 2)); + gl_Position = vec4(p * 2.0 - 1.0, 0.0, 1.0); +} diff --git a/examples/live/shaders/picture.frag b/examples/live/shaders/picture.frag new file mode 100644 index 0000000..ec1293e --- /dev/null +++ b/examples/live/shaders/picture.frag @@ -0,0 +1,13 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The color tape at every pixel; x and y run 0..1, y up. +#include "Picture.glsl" +#define TAPE(i) u.tape[i] +#include "vm.glsl" + +layout(location = 0) out vec4 out_color; + +void main() { + vec2 p = vec2(gl_FragCoord.x, u.size.y - gl_FragCoord.y) / u.size; + out_color = vec4(clamp(vm(0u, u.len, u.time, p.x, p.y), 0.0, 1.0), 1.0); +} diff --git a/examples/live/shaders/synth.comp b/examples/live/shaders/synth.comp new file mode 100644 index 0000000..9a066a6 --- /dev/null +++ b/examples/live/shaders/synth.comp @@ -0,0 +1,26 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// One audio block, one thread per sample: the out tape at each sample's +// time, crossing over the first `fade` samples from the tape it replaces, +// so an edit does not click. x and y are 0. +layout(local_size_x = 64) in; + +#include "Synth.glsl" +#define TAPE(i) u.tape[i] +#include "vm.glsl" + +layout(set = 1, binding = 0) buffer Block { float block[]; }; + +// A sample the device can take: no NaN or infinity, -1..1. +float heard(float v) { + return isnan(v) || isinf(v) ? 0.0 : clamp(v, -1.0, 1.0); +} + +void main() { + uint n = gl_GlobalInvocationID.x; + if (n >= u.count) return; + float t = u.start + float(n) / u.rate; + float v = heard(vm(0u, u.len, t, 0.0, 0.0).x); + if (n < u.fade) v = mix(heard(vm(MAX_TAPE, u.old_len, t, 0.0, 0.0).x), v, (float(n) + 0.5) / float(u.fade)); + block[n] = v; +} diff --git a/examples/live/shaders/vm.glsl b/examples/live/shaders/vm.glsl new file mode 100644 index 0000000..185d23a --- /dev/null +++ b/examples/live/shaders/vm.glsl @@ -0,0 +1,92 @@ +// The interpreter of Lang.zig's tapes, included by picture.frag (a pixel) +// and synth.comp (a sample), which define TAPE(i), instruction i as a uvec4 +// (op, a, b, c). Instruction i writes register r[i] from earlier registers +// a, b and c; a constant's a is its value's bits, seq's b and c a first +// register and a count. Every thread runs the same instructions, so the +// switch never diverges. The registers are a local array (in the GPU's +// local memory): an array of 16 kept in registers measured twice as slow, +// its indexing turned into selects (the probe, in the package's README). +#include "ops.glsl" + +const uint MAX_TAPE = 128u; + +// An integer hash (no tables), to -1..1. +float lattice(vec3 p) { + uvec3 q = uvec3(ivec3(p)); + uint h = (q.x * 0x8da6b343u) ^ (q.y * 0xd8163841u) ^ (q.z * 0xcb1ab31fu); + h ^= h >> 16; + h *= 0x7feb352du; + h ^= h >> 15; + h *= 0x846ca68bu; + h ^= h >> 16; + return float(h) * (2.0 / 4294967295.0) - 1.0; +} + +// Value noise, smooth, -1..1. +float noise(vec3 p) { + vec3 i = floor(p); + vec3 f = p - i; + vec3 w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0); + return mix( + mix(mix(lattice(i), lattice(i + vec3(1, 0, 0)), w.x), mix(lattice(i + vec3(0, 1, 0)), lattice(i + vec3(1, 1, 0)), w.x), w.y), + mix(mix(lattice(i + vec3(0, 0, 1)), lattice(i + vec3(1, 0, 1)), w.x), mix(lattice(i + vec3(0, 1, 1)), lattice(i + vec3(1, 1, 1)), w.x), w.y), + w.z); +} + +vec3 hsv(float h, float s, float v) { + vec3 k = clamp(abs(fract(h + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - 3.0) - 1.0, 0.0, 1.0); + return v * mix(vec3(1.0), k, s); +} + +// The tape at TAPE(base) .. TAPE(base + len - 1) at one point: a colour if +// it ends in one, else its last register, grey. +vec3 vm(uint base, uint len, float t, float x, float y) { + float r[MAX_TAPE]; + vec3 color = vec3(0.0); + for (uint i = 0u; i < len; i++) { + uvec4 k = TAPE(base + i); + float a = r[k.y & 127u]; + float b = r[k.z & 127u]; + float c = r[k.w & 127u]; + float v = 0.0; + switch (k.x) { + case OP_constant: v = uintBitsToFloat(k.y); break; + case OP_t: v = t; break; + case OP_x: v = x; break; + case OP_y: v = y; break; + case OP_add: v = a + b; break; + case OP_sub: v = a - b; break; + case OP_mul: v = a * b; break; + case OP_div: v = a / b; break; + case OP_neg: v = -a; break; + case OP_copy: v = a; break; + case OP_seq: v = r[(k.z + uint(mod(floor(a), float(k.w)))) & 127u]; break; + case OP_sin: v = sin(a); break; + case OP_cos: v = cos(a); break; + case OP_exp: v = exp(a); break; + case OP_log: v = log(a); break; + case OP_pow: v = pow(a, b); break; + case OP_sqrt: v = sqrt(a); break; + case OP_abs: v = abs(a); break; + case OP_floor: v = floor(a); break; + case OP_fract: v = fract(a); break; + case OP_min: v = min(a, b); break; + case OP_max: v = max(a, b); break; + case OP_clamp: v = clamp(a, b, c); break; + case OP_mix: v = mix(a, b, c); break; + case OP_step: v = step(a, b); break; + case OP_smoothstep: v = smoothstep(a, b, c); break; + case OP_atan2: v = atan(a, b); break; + case OP_noise: v = noise(vec3(a, b, c)); break; + case OP_saw: v = 2.0 * fract(a + 0.5) - 1.0; break; + case OP_sqr: v = fract(a) < 0.5 ? 1.0 : -1.0; break; + case OP_tri: v = 4.0 * abs(fract(a - 0.25) - 0.5) - 1.0; break; + case OP_hsv: color = hsv(a, b, c); break; + case OP_rgb: color = vec3(a, b, c); break; + } + r[i] = v; + } + if (len == 0u) return vec3(0.0); + uint last = TAPE(base + len - 1u).x; + return last == OP_hsv || last == OP_rgb ? color : vec3(r[len - 1u]); +} 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 }, .{}); +} diff --git a/examples/live/test/probe.script b/examples/live/test/probe.script new file mode 100644 index 0000000..fc2e162 --- /dev/null +++ b/examples/live/test/probe.script @@ -0,0 +1,4 @@ +# The probe (src/probe.zig): frames enough for steady means, and what the +# 60-instruction tape draws, to see that the interpreter runs. +frames 600 +screenshot .zig-cache/live/probe.png |
