summaryrefslogtreecommitdiff
path: root/examples/live/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'examples/live/shaders')
-rw-r--r--examples/live/shaders/fullscreen.vert6
-rw-r--r--examples/live/shaders/picture.frag13
-rw-r--r--examples/live/shaders/synth.comp26
-rw-r--r--examples/live/shaders/vm.glsl92
4 files changed, 137 insertions, 0 deletions
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]);
+}