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
|
#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);
}
}
|