summaryrefslogtreecommitdiff
path: root/examples/micscope/shaders/waveform.comp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/micscope/shaders/waveform.comp')
-rw-r--r--examples/micscope/shaders/waveform.comp72
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);
+ }
+}