diff options
Diffstat (limited to 'examples/live/src/Lang.zig')
| -rw-r--r-- | examples/live/src/Lang.zig | 95 |
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" } }; +}; |
