Segmentation fault in vulkan compute shader

I am trying to understand a segmentation fault in my compute shader.
I reduced the shader code to a minimum while still producing the seg fault:

#version 460

layout(binding = 0) buffer Layers {
    uint offsets[];
};

layout(binding = 1) buffer Data {
    float data[];
};

layout (local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

void main()
{
    for (uint i=0; i>=0; i--) {
        data[offsets[i]] = 1;
    }
}

I don’t understand why the code executes without seg fault if I change the for loop to:

for (uint i=0; i<=0; i++)

Both versions should produce a single iteration with i == 0.

Your CS code has an infinite loop. All unsigned integers are greater than or equal to zero, because that’s what “unsigned” means. Integers (signed and unsigned) in GLSL are defined to wrap around when you over/underflow them. So 0-1 is just MAX_UINT. The loop will never end, so it will eventually read from memory it’s not allowed to and cause the GPU to fail and either reset or lose your context or other unpleasantness.

And the reason why the other doesn’t exhibit this problem is obvious: it only executes once. There is exactly one unsigned integer that is less than or equal to zero. Once you’re past that, the condition is false and the loop terminates.

Thank you Alfonse. Obviously I failed to see the obvious.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.