diff options
Diffstat (limited to 'examples/spheres/shaders')
| -rw-r--r-- | examples/spheres/shaders/advance.comp | 32 | ||||
| -rw-r--r-- | examples/spheres/shaders/fade.frag | 11 | ||||
| -rw-r--r-- | examples/spheres/shaders/fullscreen.vert | 6 | ||||
| -rw-r--r-- | examples/spheres/shaders/link.frag | 12 | ||||
| -rw-r--r-- | examples/spheres/shaders/link.vert | 47 | ||||
| -rw-r--r-- | examples/spheres/shaders/lookahead.comp | 52 | ||||
| -rw-r--r-- | examples/spheres/shaders/physics.glsl | 36 | ||||
| -rw-r--r-- | examples/spheres/shaders/rechord.comp | 27 | ||||
| -rw-r--r-- | examples/spheres/shaders/sim.comp | 86 | ||||
| -rw-r--r-- | examples/spheres/shaders/sprite.frag | 11 | ||||
| -rw-r--r-- | examples/spheres/shaders/sprite.vert | 23 | ||||
| -rw-r--r-- | examples/spheres/shaders/synth.comp | 112 | ||||
| -rw-r--r-- | examples/spheres/shaders/trail.frag | 7 | ||||
| -rw-r--r-- | examples/spheres/shaders/trail.vert | 48 | ||||
| -rw-r--r-- | examples/spheres/shaders/voices.glsl | 101 |
15 files changed, 611 insertions, 0 deletions
diff --git a/examples/spheres/shaders/advance.comp b/examples/spheres/shaders/advance.comp new file mode 100644 index 0000000..e1e8789 --- /dev/null +++ b/examples/spheres/shaders/advance.comp @@ -0,0 +1,32 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// After synth.comp: where the block's phases ended, wrapped, for the next +// block to start from; and the voices and coupling it ended on, to glide +// from. (The coupling pushes phases without moving them on, so the phases +// need nothing from it.) +layout(local_size_x = 1) in; + +#include "SynthParams.glsl" + +layout(set = 1, binding = 0) buffer Voices { vec4 voices[]; }; +layout(set = 1, binding = 1) buffer Phases { vec4 phases[]; }; + +const float TAU = 6.28318530718; + +void main() { + float count = float(u.count); + for (int i = 0; i < 3; i++) { + vec4 from = voices[i]; + vec4 to = voices[3 + i]; + vec4 phase = phases[i]; + float wc0 = TAU * from.x / u.rate; + float wc1 = TAU * to.x / u.rate; + // As synth.comp's glide at n = count. + float c = phase.x + 0.5 * (wc0 + wc1) * count; + float m = phase.y + 0.5 * (wc0 * from.y + wc1 * to.y) * count; + float fm = phase.z + (c - phase.x) + to.z / max(to.y, 1e-3) * (sin(m) - sin(phase.y)); + phases[i] = vec4(mod(c, TAU), mod(m, TAU), mod(fm, TAU), 0.0); + voices[i] = to; + voices[6 + i] = voices[9 + i]; + } +} diff --git a/examples/spheres/shaders/fade.frag b/examples/spheres/shaders/fade.frag new file mode 100644 index 0000000..71b02f2 --- /dev/null +++ b/examples/spheres/shaders/fade.frag @@ -0,0 +1,11 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// Blended over the sky (premultiplied), it moves every pixel `amount` of +// the way to the background: what was drawn before fades, a long exposure. +layout(location = 0) out vec4 out_color; + +#include "Fade.glsl" + +void main() { + out_color = vec4(u.background * u.amount, u.amount); +} diff --git a/examples/spheres/shaders/fullscreen.vert b/examples/spheres/shaders/fullscreen.vert new file mode 100644 index 0000000..f45ee09 --- /dev/null +++ b/examples/spheres/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/spheres/shaders/link.frag b/examples/spheres/shaders/link.frag new file mode 100644 index 0000000..53da3cf --- /dev/null +++ b/examples/spheres/shaders/link.frag @@ -0,0 +1,12 @@ +#version 450 +// Pulses running from the modulator to the carrier, soft at the edges. +layout(location = 0) in vec4 color; +layout(location = 1) in vec2 along; +layout(location = 2) flat in float time; +layout(location = 0) out vec4 out_color; + +void main() { + float pulse = 0.5 + 0.5 * sin(along.x * 0.25 - time * 9.0); + float edge = 1.0 - along.y * along.y; + out_color = color * edge * (0.3 + 0.7 * pulse * pulse); +} diff --git a/examples/spheres/shaders/link.vert b/examples/spheres/shaders/link.vert new file mode 100644 index 0000000..d4582e4 --- /dev/null +++ b/examples/spheres/shaders/link.vert @@ -0,0 +1,47 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The FM pair, drawn: from the modulating body to its carrier, a ribbon +// as wide and bright as the index is high, shading from the +// modulator's colour to the carrier's. Six vertices for each of the six +// ordered pairs; uncoupled pairs collapse to nothing. +layout(location = 0) out vec4 color; +layout(location = 1) out vec2 along; +layout(location = 2) flat out float time; + +#include "SkyView.glsl" + +layout(set = 0, binding = 0) readonly buffer Bodies { vec4 bodies[]; }; +// layouts.voices_*: coupling rows now at 9..11. +layout(set = 0, binding = 1) readonly buffer Voices { vec4 voices[]; }; + +// Match main.zig's palette. +const vec3 palette[3] = vec3[](vec3(0.40, 0.85, 1.0), vec3(1.0, 0.45, 0.82), vec3(1.0, 0.80, 0.35)); +// Pairs (carrier, modulator). +const ivec2 pairs[6] = ivec2[](ivec2(0, 1), ivec2(0, 2), ivec2(1, 0), ivec2(1, 2), ivec2(2, 0), ivec2(2, 1)); + +void main() { + ivec2 pair = pairs[uint(gl_VertexIndex) / 6u]; + uint corner = uint(gl_VertexIndex) % 6u; + float amount = voices[9 + pair.x][pair.y]; + time = u.time; + if (amount < 0.02) { + gl_Position = vec4(0.0, 0.0, 2.0, 1.0); + color = vec4(0.0); + along = vec2(0.0); + return; + } + // Corners 0, 3, 5 at the modulator, 1, 2, 4 at the carrier; 0, 1, 3 on + // one side. + bool far_end = corner == 1u || corner == 2u || corner == 4u; + bool side = corner == 2u || corner == 4u || corner == 5u; + vec2 from = (bodies[pair.y].xy - u.center) / u.per_pixel; + vec2 to = (bodies[pair.x].xy - u.center) / u.per_pixel; + vec2 d = to - from; + float len = max(length(d), 1e-3); + vec2 normal = vec2(-d.y, d.x) / len; + float width = u.radius * (0.4 + min(amount, 1.6)); + vec2 at = (far_end ? to : from) + normal * (side ? width : -width); + gl_Position = vec4(at * 2.0 / u.size, 0.0, 1.0); + along = vec2(far_end ? len : 0.0, side ? 1.0 : -1.0); + color = vec4(mix(palette[pair.y], palette[pair.x], far_end ? 1.0 : 0.0), 1.0) * u.alpha * clamp(amount, 0.0, 1.0); +} diff --git a/examples/spheres/shaders/lookahead.comp b/examples/spheres/shaders/lookahead.comp new file mode 100644 index 0000000..75902d7 --- /dev/null +++ b/examples/spheres/shaders/lookahead.comp @@ -0,0 +1,52 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// Futures of system 0, one per thread, for spheres as an instrument: from +// the preset's start, alternative 0 exactly as system 0 began and the +// others nudged by growing amounts, so where the system is chaotic their +// futures fan out. Each is integrated ahead `count` samples a frame (with +// physics.glsl, as the live system is), leaving a snapshot every +// `sample_dt` and the chord its bodies would sing there (voices.glsl). A +// chord chosen then finds the nearest time it plays, in any of them. +layout(local_size_x = 32) in; + +#include "LookParams.glsl" +#include "physics.glsl" +#include "voices.glsl" + +// Where each alternative has got to: 3 vec4s each (position xy, velocity zw). +layout(set = 1, binding = 0) buffer State { vec4 state[]; }; +// Snapshots, sample-major: sample t of alternative a at 3 (t * K + a). +layout(set = 1, binding = 1) buffer Snapshots { vec4 snapshots[]; }; +// Chords, sample-major: t * K + a. +layout(set = 1, binding = 2) buffer Chords { uint chords[]; }; + +// Match main.zig's leapfrog step. +const float BASE_STEP = 0.004; + +void main() { + uint alt = gl_GlobalInvocationID.x; + if (alt >= u.alternatives) return; + vec3 m = u.masses.xyz; + vec2 p[3]; + vec2 v[3]; + for (int i = 0; i < 3; i++) { + vec4 b = state[3u * alt + uint(i)]; + p[i] = b.xy; + v[i] = b.zw; + } + vec2 a[3]; + accelerations(p, m, u.softening, a); + Mapping map = Mapping(u.base, u.speed_ref, u.depth_scale, u.couple_scale, u.quantize); + uint steps = uint(ceil(u.sample_dt / BASE_STEP)); + float dt = u.sample_dt / float(steps); + for (uint t = u.first; t < min(u.first + u.count, u.horizon); t++) { + uint at = t * u.alternatives + alt; + for (int i = 0; i < 3; i++) snapshots[3u * at + uint(i)] = vec4(p[i], v[i]); + vec4 sung[3]; + vec4 coupled[3]; + sing(p, v, map, sung, coupled); + chords[at] = chordOf(sung) | voicing(sung, coupled); + for (uint k = 0u; k < steps; k++) leapfrog(p, v, a, dt, m, u.softening, 256.0); + } + for (int i = 0; i < 3; i++) state[3u * alt + uint(i)] = vec4(p[i], v[i]); +} diff --git a/examples/spheres/shaders/physics.glsl b/examples/spheres/shaders/physics.glsl new file mode 100644 index 0000000..5d74bee --- /dev/null +++ b/examples/spheres/shaders/physics.glsl @@ -0,0 +1,36 @@ +// The three-body physics, shared by sim.comp and lookahead.comp so the live +// system and its futures move alike: gravity with softening, and leapfrog +// (kick, drift, kick), symplectic and time-reversible, split finer through +// close passes, about 2 % of the local dynamical time a part: too coarse +// and a close pass gains energy and flings a body out. + +void accelerations(vec2 p[3], vec3 m, float softening, out vec2 a[3]) { + float eps2 = softening * softening; + a[0] = vec2(0.0); + a[1] = vec2(0.0); + a[2] = vec2(0.0); + for (int i = 0; i < 3; i++) { + for (int j = i + 1; j < 3; j++) { + vec2 d = p[j] - p[i]; + float inv = inversesqrt(dot(d, d) + eps2); + vec2 f = d * (inv * inv * inv); + a[i] += m[j] * f; + a[j] -= m[i] * f; + } + } +} + +// One step of `dt`, in up to `most` parts; `a` holds the accelerations at +// `p`, before and after. +void leapfrog(inout vec2 p[3], inout vec2 v[3], inout vec2 a[3], float dt, vec3 m, float softening, float most) { + float near = min(min(distance(p[0], p[1]), distance(p[0], p[2])), distance(p[1], p[2])) + softening; + float local = sqrt(near * near * near / (m.x + m.y + m.z)); + uint k = uint(clamp(ceil(abs(dt) / (0.02 * local)), 1.0, most)); + float h = dt / float(k); + for (uint j = 0u; j < k; j++) { + for (int i = 0; i < 3; i++) v[i] += 0.5 * h * a[i]; + for (int i = 0; i < 3; i++) p[i] += h * v[i]; + accelerations(p, m, softening, a); + for (int i = 0; i < 3; i++) v[i] += 0.5 * h * a[i]; + } +} diff --git a/examples/spheres/shaders/rechord.comp b/examples/spheres/shaders/rechord.comp new file mode 100644 index 0000000..6119120 --- /dev/null +++ b/examples/spheres/shaders/rechord.comp @@ -0,0 +1,27 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The look-ahead's chords again, for another scale setting: one thread per +// snapshot, the first `count` of them. +layout(local_size_x = 64) in; + +#include "LookParams.glsl" +#include "voices.glsl" + +layout(set = 0, binding = 0) readonly buffer Snapshots { vec4 snapshots[]; }; +layout(set = 1, binding = 0) buffer Chords { uint chords[]; }; + +void main() { + uint at = gl_GlobalInvocationID.x; + if (at >= u.count) return; + vec2 p[3]; + vec2 v[3]; + for (int i = 0; i < 3; i++) { + vec4 b = snapshots[3u * at + uint(i)]; + p[i] = b.xy; + v[i] = b.zw; + } + vec4 sung[3]; + vec4 coupled[3]; + sing(p, v, Mapping(u.base, u.speed_ref, u.depth_scale, u.couple_scale, u.quantize), sung, coupled); + chords[at] = chordOf(sung) | voicing(sung, coupled); +} diff --git a/examples/spheres/shaders/sim.comp b/examples/spheres/shaders/sim.comp new file mode 100644 index 0000000..4a79ce1 --- /dev/null +++ b/examples/spheres/shaders/sim.comp @@ -0,0 +1,86 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// The three-body problem, one system per thread: every copy of the system +// at once, each a hair apart from system 0, so the cloud they make shows +// how far apart chaos takes them (a stable orbit keeps it a thin line). +// The physics and the voices are physics.glsl's and voices.glsl's, which +// the look-ahead shares. +// +// System 0 also leaves its trail, reports its state, and sings: from its +// own motion, or, while a chord is held, from the look-ahead's snapshot +// that sings it. It can also adopt a snapshot, stepping into that future. +layout(local_size_x = 64) in; + +#include "SimParams.glsl" +#include "physics.glsl" +#include "voices.glsl" + +// The look-ahead's snapshots: 3 vec4s each (position xy, velocity zw). +layout(set = 0, binding = 0) readonly buffer Ahead { vec4 ahead[]; }; +// Position xy, velocity zw: system s's bodies are 3s, 3s + 1, 3s + 2. +layout(set = 1, binding = 0) buffer Bodies { vec4 bodies[]; }; +// System 0's positions: body b's ring at b * TRAIL. +layout(set = 1, binding = 1) buffer Trail { vec2 trail[]; }; +// layouts.voices_*: voices 0..2 as the synthesiser last used them, 3..5 +// now; coupling rows 6..8 last used, 9..11 now. +layout(set = 1, binding = 2) buffer Voices { vec4 voices[]; }; +// layouts.Lead: bodies 0..2, voices 3..5, coupling 6..8, (energy, time) 9. +layout(set = 1, binding = 3) buffer Lead { vec4 lead[]; }; + +// Match layouts.zig. +const uint TRAIL = 1024; + +void main() { + uint s = gl_GlobalInvocationID.x; + if (s >= u.systems) return; + vec3 m = u.masses.xyz; + vec2 p[3]; + vec2 v[3]; + for (int i = 0; i < 3; i++) { + vec4 b = s == 0u && u.adopt != 0u ? ahead[3u * (u.adopt - 1u) + uint(i)] : bodies[3u * s + uint(i)]; + p[i] = b.xy; + v[i] = b.zw; + } + vec2 a[3]; + accelerations(p, m, u.softening, a); + // System 0 leaves trail points as it goes, by simulated time, so a fast + // travel draws as smooth a path as running does. + uint written = 0u; + for (uint step = 0u; step < u.steps; step++) { + leapfrog(p, v, a, u.dt, m, u.softening, 512.0); + if (s == 0u && written < u.trail_points && step + 1u >= u.trail_first && (step + 1u - u.trail_first) % u.trail_every == 0u) { + for (int i = 0; i < 3; i++) trail[uint(i) * TRAIL + (u.trail_head + written) % TRAIL] = p[i]; + written++; + } + } + for (int i = 0; i < 3; i++) bodies[3u * s + uint(i)] = vec4(p[i], v[i]); + if (s != 0u) return; + + float kinetic = 0.0; + float potential = 0.0; + for (int i = 0; i < 3; i++) { + kinetic += 0.5 * m[i] * dot(v[i], v[i]); + for (int j = i + 1; j < 3; j++) potential -= m[i] * m[j] * inversesqrt(dot(p[j] - p[i], p[j] - p[i]) + u.softening * u.softening); + lead[i] = vec4(p[i], v[i]); + } + + vec2 sp[3]; + vec2 sv[3]; + for (int i = 0; i < 3; i++) { + vec4 b = u.voice_source != 0u ? ahead[3u * (u.voice_source - 1u) + uint(i)] : vec4(p[i], v[i]); + sp[i] = b.xy; + sv[i] = b.zw; + } + vec4 sung[3]; + vec4 coupled[3]; + sing(sp, sv, Mapping(u.base, u.speed_ref, u.depth_scale, u.couple_scale, u.quantize), sung, coupled); + for (int i = 0; i < 3; i++) { + voices[3 + i] = sung[i]; + lead[3 + i] = sung[i]; + // A quarter of the way each frame: pairs hand over smoothly. + vec4 row = mix(voices[9 + i], coupled[i], 0.25); + voices[9 + i] = row; + lead[6 + i] = row; + } + lead[9] = vec4(kinetic + potential, lead[9].y + float(u.steps) * u.dt, 0.0, 0.0); +} diff --git a/examples/spheres/shaders/sprite.frag b/examples/spheres/shaders/sprite.frag new file mode 100644 index 0000000..d39e7e4 --- /dev/null +++ b/examples/spheres/shaders/sprite.frag @@ -0,0 +1,11 @@ +#version 450 +// A Gaussian glow, premultiplied, for additive blending. +layout(location = 0) in vec2 corner; +layout(location = 1) in vec4 color; +layout(location = 0) out vec4 out_color; + +void main() { + float r2 = dot(corner, corner); + float glow = exp(-5.0 * r2) * (1.0 - smoothstep(0.8, 1.0, r2)); + out_color = color * glow; +} diff --git a/examples/spheres/shaders/sprite.vert b/examples/spheres/shaders/sprite.vert new file mode 100644 index 0000000..13e1c73 --- /dev/null +++ b/examples/spheres/shaders/sprite.vert @@ -0,0 +1,23 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// A soft disc per body: of every system from `first` for `count` systems, +// at `radius` pixels, coloured by body. Six vertices make each quad. +layout(location = 0) out vec2 corner; +layout(location = 1) out vec4 color; + +#include "SkyView.glsl" + +layout(set = 0, binding = 0) readonly buffer Bodies { vec4 bodies[]; }; + +const vec2 corners[6] = vec2[](vec2(-1, -1), vec2(1, -1), vec2(1, 1), vec2(-1, -1), vec2(1, 1), vec2(-1, 1)); +// Match main.zig's palette. +const vec3 palette[3] = vec3[](vec3(0.40, 0.85, 1.0), vec3(1.0, 0.45, 0.82), vec3(1.0, 0.80, 0.35)); + +void main() { + uint sprite = uint(gl_VertexIndex) / 6u; + uint index = 3u * u.first + sprite; + vec2 at = (bodies[index].xy - u.center) / u.per_pixel + corners[uint(gl_VertexIndex) % 6u] * u.radius; + gl_Position = vec4(at * 2.0 / u.size, 0.0, 1.0); + corner = corners[uint(gl_VertexIndex) % 6u]; + color = vec4(palette[index % 3u], 1.0) * u.alpha; +} diff --git a/examples/spheres/shaders/synth.comp b/examples/spheres/shaders/synth.comp new file mode 100644 index 0000000..3a6c40c --- /dev/null +++ b/examples/spheres/shaders/synth.comp @@ -0,0 +1,112 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// One audio block, one thread per sample: three voices from the bodies' +// parameters, gliding from what the last block ended on to what sim.comp +// set now, so blocks join without clicks. +// +// Each phase has a closed form, so every sample is computed alone: a +// frequency gliding linearly from w0 to w1 (radians per sample) over N +// samples has phase p0 + w0 n + (w1 - w0) n^2 / 2N after n of them. +// +// The modes, with carrier phase c, modulator phase m (at ratio times the +// carrier's frequency) and depth d: +// sine sin c +// additive sum over harmonics k of r^(k-1) sin kc, r = d / (d + 2), +// those under Nyquist, normalised +// AM sin c (1 + a sin m) / (1 + a), a = min(d / 4, 1) +// ring sin c sin m +// FM sin of the integral of the carrier's frequency, pushed +// d times the carrier's frequency either way by cos m: so the +// index, deviation over modulator frequency, is d / ratio and +// changes as the modulator does +// PM sin(c + d sin m): the index is d, whatever the modulator +// FM and PM sound alike for a steady modulator, and part when it moves. +// +// The closest pair (sim.comp) is one FM voice: the modulating body is +// silent, and a sine at its frequency, times the index, pushes the other +// body's carrier phase. Two operators, both moved by the bodies. The push +// moves every harmonic of the additive voice alike, so its timbre bends +// without turning harsh. +layout(local_size_x = 64) in; + +#include "SynthParams.glsl" + +layout(set = 0, binding = 0) readonly buffer Voices { vec4 voices[]; }; +// Per voice: carrier phase, modulator phase, FM's phase. +layout(set = 0, binding = 1) readonly buffer Phases { vec4 phases[]; }; +layout(set = 1, binding = 0) buffer Block { float block[]; }; + +const float TAU = 6.28318530718; +// Match layouts.Mode. +const uint SINE = 0u; +const uint ADDITIVE = 1u; +const uint AM = 2u; +const uint RING = 3u; +const uint FM = 4u; + +float glide(float phase, float w0, float w1, float n, float count) { + return phase + w0 * n + (w1 - w0) * n * n / (2.0 * count); +} + +// Voice i's signal, its carrier phase pushed by `push` radians. +float voice(int i, float n, float count, float push) { + vec4 from = voices[i]; + vec4 to = voices[3 + i]; + vec4 phase = phases[i]; + float t = n / count; + float wc0 = TAU * from.x / u.rate; + float wc1 = TAU * to.x / u.rate; + float c = glide(phase.x, wc0, wc1, n, count); + float m = glide(phase.y, wc0 * from.y, wc1 * to.y, n, count); + float depth = mix(from.z, to.z, t); + float ratio = mix(from.y, to.y, t); + float y; + if (u.mode == SINE) { + y = sin(c + push); + } else if (u.mode == ADDITIVE) { + float r = depth / (depth + 2.0); + float carrier = mix(from.x, to.x, t); + float sum = 0.0; + float norm = 0.0; + float weight = 1.0; + for (int k = 1; k <= 16; k++) { + if (float(k) * carrier >= 0.5 * u.rate) break; + sum += weight * sin(float(k) * c + push); + norm += weight; + weight *= r; + } + y = sum / max(norm, 1e-6); + } else if (u.mode == AM) { + float a = min(depth / 4.0, 1.0); + y = sin(c + push) * (1.0 + a * sin(m)) / (1.0 + a); + } else if (u.mode == RING) { + y = sin(c + push) * sin(m); + } else if (u.mode == FM) { + y = sin(phase.z + push + (c - phase.x) + depth / max(ratio, 1e-3) * (sin(m) - sin(phase.y))); + } else { + y = sin(c + push + depth * sin(m)); + } + return y; +} + +void main() { + uint n = gl_GlobalInvocationID.x; + if (n >= u.count) return; + float count = float(u.count); + float t = float(n) / count; + // Each body as a modulator: a sine at its carrier frequency. + vec3 sines; + for (int i = 0; i < 3; i++) { + float w0 = TAU * voices[i].x / u.rate; + float w1 = TAU * voices[3 + i].x / u.rate; + sines[i] = sin(glide(phases[i].x, w0, w1, float(n), count)); + } + float sum = 0.0; + for (int i = 0; i < 3; i++) { + vec4 row = mix(voices[6 + i], voices[9 + i], t); + float audible = (1.0 - row.w) * mix(voices[i].w, voices[3 + i].w, t); + if (audible < 1e-4) continue; + sum += audible * voice(i, float(n), count, dot(row.xyz, sines)); + } + block[n] = tanh(u.gain * sum); +} diff --git a/examples/spheres/shaders/trail.frag b/examples/spheres/shaders/trail.frag new file mode 100644 index 0000000..8a82628 --- /dev/null +++ b/examples/spheres/shaders/trail.frag @@ -0,0 +1,7 @@ +#version 450 +layout(location = 0) in vec4 color; +layout(location = 0) out vec4 out_color; + +void main() { + out_color = color; +} diff --git a/examples/spheres/shaders/trail.vert b/examples/spheres/shaders/trail.vert new file mode 100644 index 0000000..7f9f35c --- /dev/null +++ b/examples/spheres/shaders/trail.vert @@ -0,0 +1,48 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// System 0's trails: per body, a ribbon through its last positions, +// `radius` pixels wide at the newest and tapering to nothing at the oldest, +// fading as it goes. Six vertices per segment; segments past what has been +// filled, or across a jump, collapse to nothing. +layout(location = 0) out vec4 color; + +#include "SkyView.glsl" + +layout(set = 0, binding = 0) readonly buffer Trail { vec2 trail[]; }; + +// Match layouts.zig and main.zig's palette. +const uint TRAIL = 1024u; +const vec3 palette[3] = vec3[](vec3(0.40, 0.85, 1.0), vec3(1.0, 0.45, 0.82), vec3(1.0, 0.80, 0.35)); + +void main() { + uint segment = uint(gl_VertexIndex) / 6u; + uint corner = uint(gl_VertexIndex) % 6u; + uint body = segment / (TRAIL - 1u); + uint age = segment % (TRAIL - 1u); + if (age + 1u >= u.filled) { + gl_Position = vec4(0.0, 0.0, 2.0, 1.0); + color = vec4(0.0); + return; + } + // Corners 0, 3, 5 at the newer end, 1, 2, 4 at the older; 0, 1, 3 on + // one side, 2, 4, 5 on the other. + bool older = corner == 1u || corner == 2u || corner == 4u; + bool side = corner == 2u || corner == 4u || corner == 5u; + uint a = (u.head + TRAIL - age) % TRAIL; + uint b = (u.head + TRAIL - age - 1u) % TRAIL; + // A jump (system 0 stepping into a snapshot) breaks the trail. + if (distance(trail[body * TRAIL + a], trail[body * TRAIL + b]) > u.gap) { + gl_Position = vec4(0.0, 0.0, 2.0, 1.0); + color = vec4(0.0); + return; + } + vec2 pa = (trail[body * TRAIL + a] - u.center) / u.per_pixel; + vec2 pb = (trail[body * TRAIL + b] - u.center) / u.per_pixel; + vec2 along = pb - pa; + float len = length(along); + vec2 normal = len > 1e-4 ? vec2(-along.y, along.x) / len : vec2(0.0, 1.0); + float fade = 1.0 - float(older ? age + 1u : age) / float(u.filled); + vec2 at = (older ? pb : pa) + normal * (side ? 1.0 : -1.0) * u.radius * fade; + gl_Position = vec4(at * 2.0 / u.size, 0.0, 1.0); + color = vec4(palette[body], 1.0) * u.alpha * fade * fade; +} diff --git a/examples/spheres/shaders/voices.glsl b/examples/spheres/shaders/voices.glsl new file mode 100644 index 0000000..c2fd56c --- /dev/null +++ b/examples/spheres/shaders/voices.glsl @@ -0,0 +1,101 @@ +// From motion to sound, shared by sim.comp (what sounds) and lookahead.comp +// (what would): each body a voice whose pitch follows its speed, whose +// modulator ratio follows how lopsided its two distances are, and whose +// depth follows how near its nearest neighbour is; and the closest pair one +// FM voice, the faster body silent, modulating the other. + +struct Mapping { + float base; + float speed_ref; + float depth_scale; + float couple_scale; + // Match layouts.Scale: free, pentatonic, diatonic, chromatic. + uint scale; +}; + +// The nearest note of the scale, as a MIDI number: A minor pentatonic (A C +// D E G), A minor / C major (the white keys), every semitone, or none. +float snap(float midi, uint scale) { + if (scale == 0u) return midi; + if (scale == 3u) return round(midi); + float m = midi - 69.0; + float octave = floor(m / 12.0); + float degree = m - 12.0 * octave; + const float pentatonic[6] = float[](0.0, 3.0, 5.0, 7.0, 10.0, 12.0); + const float diatonic[8] = float[](0.0, 2.0, 3.0, 5.0, 7.0, 8.0, 10.0, 12.0); + float best = 0.0; + if (scale == 1u) { + for (int i = 1; i < 6; i++) if (abs(pentatonic[i] - degree) < abs(best - degree)) best = pentatonic[i]; + } else { + for (int i = 1; i < 8; i++) if (abs(diatonic[i] - degree) < abs(best - degree)) best = diatonic[i]; + } + return 69.0 + 12.0 * octave + best; +} + +// Voices: (carrier Hz, ratio, depth, amplitude). Coupling rows: xyz the FM +// index each voice gets from each other, w whether it is the silent +// modulator. +void sing(vec2 p[3], vec2 v[3], Mapping map, out vec4 voices[3], out vec4 coupling[3]) { + for (int i = 0; i < 3; i++) { + float speed = length(v[i]); + float octaves = clamp(log2(max(speed, 1e-4) / map.speed_ref), -2.0, 2.5); + float midi = 12.0 * log2(map.base / 440.0) + 69.0 + 12.0 * octaves; + midi = snap(midi, map.scale); + float d1 = distance(p[i], p[(i + 1) % 3]); + float d2 = distance(p[i], p[(i + 2) % 3]); + float near = min(d1, d2); + float far = max(d1, d2); + voices[i] = vec4( + 440.0 * exp2((midi - 69.0) / 12.0), + 0.5 + 4.0 * near / max(near + far, 1e-6), + clamp(map.depth_scale / max(near, 1e-3), 0.0, 12.0), + 0.2 + 0.15 * clamp(speed / (2.0 * map.speed_ref), 0.0, 1.0)); + coupling[i] = vec4(0.0); + } + int first = 0; + int second = 1; + float closest = distance(p[0], p[1]); + if (distance(p[0], p[2]) < closest) { first = 0; second = 2; closest = distance(p[0], p[2]); } + if (distance(p[1], p[2]) < closest) { first = 1; second = 2; closest = distance(p[1], p[2]); } + int modulator = length(v[first]) > length(v[second]) ? first : second; + int carrier = modulator == first ? second : first; + coupling[carrier][modulator] = clamp(map.couple_scale / max(closest, 1e-3), 0.0, 4.0); + coupling[modulator].w = 1.0; +} + +// The chord with its voicing, packed: bits 0-11 the pitch classes (the +// chord), bit 12 set if it is playable, bits 16-19 the lowest note's pitch +// class (for root position), bits 24-31 how bad it sounds, 0 to 255. +// Playable: every note from E2 to A5, an octave and a fifth at most from +// lowest to highest, and the FM pair's index at most 3; past those, the +// same pitch classes are another sound, shrill, sprawling or a cloud of +// sidebands. Within them, wider, higher and harder modulated cost more. +uint voicing(vec4 voices[3], vec4 coupling[3]) { + int low = 1000; + int high = -1000; + int lowest_class = 0; + for (int i = 0; i < 3; i++) { + int midi = int(round(69.0 + 12.0 * log2(voices[i].x / 440.0))); + if (midi < low) { + low = midi; + lowest_class = ((midi % 12) + 12) % 12; + } + high = max(high, midi); + } + float index = 0.0; + for (int i = 0; i < 3; i++) index = max(index, max(coupling[i].x, max(coupling[i].y, coupling[i].z))); + int spread = high - low; + bool playable = low >= 40 && high <= 81 && spread <= 19 && index <= 3.0; + float bad = 6.0 * float(max(spread - 12, 0)) + 10.0 * float(max(high - 76, 0)) + 15.0 * index; + return (playable ? 1u << 12 : 0u) | (uint(lowest_class) << 16) | (uint(clamp(bad, 0.0, 255.0)) << 24); +} + +// The pitch classes the three voices sing, as bits (C is bit 0): the chord. +uint chordOf(vec4 voices[3]) { + uint mask = 0u; + for (int i = 0; i < 3; i++) { + int midi = int(round(69.0 + 12.0 * log2(voices[i].x / 440.0))); + mask |= 1u << uint(((midi % 12) + 12) % 12); + } + return mask; +} |
