There are different questions about shader spinlocks and their implementations, but I can't make work this simple code below.
I launch two compute shader invocations, first writes 1 over 0, second waits 1 and writes 2 over 1.
But my application just crashes, because the 2nd shader invocation doesn't get the updated value 1 from the 1st invocation and loops infinitely.
Is that even possible? Seems that some spinlocks work on some GPUs for some people, but not in general case as it is not guaranteed by the specification. Or my driver does buggy optimizations, though I've tried to get around with the keepWaiting flag.
#version 440
// more qualifiers for sure...
layout(binding = 0, std430) coherent volatile buffer ssboBlock
{
coherent volatile uint ssbo[];
};
void main()
{
if (gl_GlobalInvocationID.x >= 2) return;
if (gl_GlobalInvocationID.x == 0)
{
atomicCompSwap(ssbo[0], 0, 1); // initially ssbo is filled with zeroes
memoryBarrier(); // more memory barriers for sure
}
else
{
bool keepWaiting = true;
while (keepWaiting)
{
memoryBarrier(); // more memory barriers for sure
if (atomicCompSwap(ssbo[0], 1, 2) == 1) // wait for 1
{
keepWaiting = false;
}
}
}
}