diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:15:15 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 9bd77e54943d954291b5faef287ccca5dcebd7a4 (patch) | |
| tree | 3f2d628781441568620b8108fab920713f26f649 /examples/micscope/shaders/waveform.comp | |
| parent | e997fc3e6b312b30f5af90012fc22365bab701d4 (diff) | |
| download | sketchbook-9bd77e54943d954291b5faef287ccca5dcebd7a4.tar.gz sketchbook-9bd77e54943d954291b5faef287ccca5dcebd7a4.zip | |
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 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'examples/micscope/shaders/waveform.comp')
| -rw-r--r-- | examples/micscope/shaders/waveform.comp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/examples/micscope/shaders/waveform.comp b/examples/micscope/shaders/waveform.comp new file mode 100644 index 0000000..d1d64e5 --- /dev/null +++ b/examples/micscope/shaders/waveform.comp @@ -0,0 +1,72 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// One workgroup per waveform column: the lowest and highest the signal goes +// in it, and its RMS. Each column also includes the linearly interpolated +// signal at its own edges, which it shares with its neighbours, so that +// zoomed in past one sample per column the columns still join into one +// continuous line. +layout(local_size_x = 64) in; + +#include "WaveParams.glsl" + +layout(set = 0, binding = 0) readonly buffer Tape { float tape[]; }; +// x: min, y: max, z: RMS, w: 1 where the column has tape under it. +layout(set = 1, binding = 0) buffer Columns { vec4 columns[]; }; + +const uint THREADS = 64; +shared float lows[THREADS]; +shared float highs[THREADS]; +shared float squares[THREADS]; + +float sample_at(int i) { + return i >= 0 && i < int(u.samples) ? tape[u.offset + uint(i)] : 0.0; +} + +// The signal at `t` samples after `u.first`, between samples linearly. +float signal(float t) { + float whole = floor(t); + int i = int(u.first) + int(whole); + return mix(sample_at(i), sample_at(i + 1), t - whole); +} + +void main() { + uint c = gl_WorkGroupID.x; + uint t = gl_LocalInvocationID.x; + float start = float(c) * u.spp; + float end = start + u.spp; + int first = int(ceil(start)); + int last = min(int(floor(end)), int(u.samples) - int(u.first) - 1); + + float lo = 1e9; + float hi = -1e9; + float sq = 0.0; + for (int i = first + int(t); i <= last; i += int(THREADS)) { + float v = tape[u.offset + u.first + uint(i)]; + lo = min(lo, v); + hi = max(hi, v); + sq += v * v; + } + if (t == 0u) { + float a = signal(start); + float b = signal(min(end, float(u.samples - u.first - 1u))); + lo = min(lo, min(a, b)); + hi = max(hi, max(a, b)); + } + lows[t] = lo; + highs[t] = hi; + squares[t] = sq; + barrier(); + for (uint stride = THREADS / 2u; stride > 0u; stride >>= 1u) { + if (t < stride) { + lows[t] = min(lows[t], lows[t + stride]); + highs[t] = max(highs[t], highs[t + stride]); + squares[t] += squares[t + stride]; + } + barrier(); + } + if (t == 0u) { + float count = float(max(last - first + 1, 1)); + float inside = float(u.first) + start < float(u.samples) ? 1.0 : 0.0; + columns[(u.slot + c) % u.ring] = vec4(lows[0], highs[0], sqrt(squares[0] / count), inside); + } +} |
