dymokomi/luce-gpu
Source of release 0.5.5
0.5.5 / tests / gpu / shaders / compact.comp
#version 450
// Stream compaction with atomics: odd values are appended to `kept`, and the
// counters record how many, the smallest and largest, and a last writer.
layout(local_size_x = 64) in;
layout(set = 0, binding = 0, std430) readonly buffer Input { uint values[]; };
layout(set = 0, binding = 1, std430) writeonly buffer Kept { uint kept[]; };
layout(set = 0, binding = 2, std430) buffer Counters { uint count; int smallest; int largest; uint last; uint swapped; } counters;
layout(push_constant) uniform Params { uint n; } params;
void main() {
uint i = gl_GlobalInvocationID.x;
if (i >= params.n)
return;
uint value = values[i];
if ((value & 1u) == 1u) {
uint slot = atomicAdd(counters.count, 1u);
kept[slot] = value;
atomicMin(counters.smallest, int(value));
atomicMax(counters.largest, int(value));
atomicExchange(counters.last, value);
}
// Exactly one invocation swaps 0 for its index + 1.
atomicCompSwap(counters.swapped, 0u, i + 1u);
}