From 9bd77e54943d954291b5faef287ccca5dcebd7a4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:15:15 -0300 Subject: examples: micscope and spheres come home, as they were, building Both lived outside any repository, beside sketchbook, with manifests that pointed at ../01-drat-meout/lib/sketchbook, which no longer exists. They are copied into examples/micscope and examples/spheres (their sources, shaders, tests, test material and web icons; not their caches, builds, packages, state files or .gitignore, which the root's covers), their manifests depend on "../.." as the other examples do, and their READMEs link sketchbook there. Nothing else changed: both build against today's library and pass their own zig build test (micscope 62/62 steps, 11/11 tests, its transport, record, render and tuner scripts; spheres 75/75 steps, 3/3 tests, its play script). The originals are untouched. micscope's test/reference.flac (3.7 MiB, CC BY-SA 3.0, credited in its test/README.md) is the rendering reference its render script opens. The README lists both examples. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- examples/spheres/shaders/sim.comp | 86 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 examples/spheres/shaders/sim.comp (limited to 'examples/spheres/shaders/sim.comp') 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); +} -- cgit v1.3