0

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;
            }
        }
    }
}
Emil Kabirov
  • 559
  • 4
  • 14
  • How do you declare an call the compute-shader? – Ripi2 May 19 '19 at 17:54
  • @Ripi2: It really doesn't matter. – Nicol Bolas May 19 '19 at 17:57
  • 2
    @Emil: I don't understand what was confusing about [your last question](https://stackoverflow.com/q/56203877/734069). It wasn't closed because it was under-described. It was closed because it was already answered. And the answer is, and remains, "you can't do that." – Nicol Bolas May 19 '19 at 17:58

0 Answers0