summaryrefslogtreecommitdiff
path: root/examples/spheres/shaders/sim.comp
blob: 4a79ce1136cc84517b9279fcea98ce2a61cd101e (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
#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);
}