summaryrefslogtreecommitdiff
path: root/examples/live/src/Lang.zig
blob: 644ff61b0fb8bf6588f8bcba6a77e863117daf61 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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" } };
};