0

I'm attempting to generate a kind of octree from a triangle mesh and I'm using a spin-lock mechanism to lock a node before subdividing it. I'm familiar with this kind of thing and I already made it work using HLSL but I'm a bit puzzled that the same code hangs the gpu with GLSL and Vulkan.

Here's the relevant code

struct Node
{
    int nodeAddr;
    int dataAddr;
    int level;
    int mutex;
};

layout(binding = 1, std430) coherent buffer buf1
{
    Node        Nodes[];
};

int GetNodeAddr(uint parent, ivec3 c, int level)
{
    int addr = Nodes[parent].nodeAddr;

    if (addr < 0)
    {
        int value;

        do
        {
            value = atomicCompSwap(Nodes[parent].mutex, 0, 1);

            if (value == 0)
            {
                addr = Nodes[parent].nodeAddr;
                if (addr < 0)
                {
                    //addr = AllocateNodes(level);
                    addr = 0;
                    Nodes[parent].nodeAddr = addr;
                }

                atomicExchange(Nodes[parent].mutex, 0);
            }

        }while(value != 0);
    }

    return addr + (c.z * 16 + c.y * 4 + c.x);
}

Am I missing something ?

Martin
  • 13
  • 2
  • TL;DR: spinlocks like this don't work. – Nicol Bolas Nov 22 '20 at 17:03
  • Actually they work with directx and the playstation api so I'm not sure why they wouldn't work on vulkan, I'm not making the same mistake as the other post. After further investigation, it seems that I can not get out of a loop if the condition depends of a buffer read, HLSL has [allow_uav_condition] but it doesn't seem to translate to SPIR-V – Martin Nov 22 '20 at 22:06
  • Undefined behavior is undefined. You may get away with them, but there are no guarantees that your code won't suddenly fail for seemingly no reason. Or that arbitrary changes might not make it fail. "*I'm not making the same mistake as the other post.*" If more than one shader invocation in the same draw command can get the same `parent` value, then you are making the same mistake. It may just be that D3D and PS-API happen to be executing your work groups in an order so that your `parent` generation pattern just happens to work out. – Nicol Bolas Nov 23 '20 at 00:54

0 Answers0