Compute-Shader: InterlockedAdd: buggy?

Recommended Posts

Hi,

can anybody tell me why the first code is not working in my ComputeShader,
and the second does?

This piece of code procdues broken meshes:
[numthreads(8,8,8)]
void MarchingCubes2(uint3 threadId : SV_DispatchThreadID)
{
...
      for (int i = 0; triTable[cubeIndex] != -1; i += 3)
        {
            // Seems not to work: ??? Why???
            InterlockedAdd(_cnt[0].trianglecount, 1, idxTriangle1);
            InterlockedAdd(_cnt[0].trianglecount, 1, idxTriangle2);
            InterlockedAdd(_cnt[0].trianglecount, 1, idxTriangle3);

            InterlockedAdd(_cnt[0].vertexcount, 1, idxVertex1);
            InterlockedAdd(_cnt[0].vertexcount, 1, idxVertex2);
            InterlockedAdd(_cnt[0].vertexcount, 1, idxVertex3);

            vertexRW[idxVertex1] = vertlist[triTable[cubeIndex]];
            normalRW[idxVertex1] = normalize(CalculateGradient(vertexRW[idxVertex1]));
            UV[idxVertex1]       = uvmap[voxeltype][0];
            

            vertexRW[idxVertex2] = vertlist[triTable[cubeIndex][i+1]];
            normalRW[idxVertex2] = normalize(CalculateGradient(vertexRW[idxVertex2]));
            UV[idxVertex2]       = uvmap[voxeltype][0];
            
            vertexRW[idxVertex3] = vertlist[triTable[cubeIndex][i+2]];
            normalRW[idxVertex3] = normalize(CalculateGradient(vertexRW[idxVertex3]));
            UV[idxVertex3]       = uvmap[voxeltype][0];

            triangleRW[idxTriangle1] = idxVertex1;
            triangleRW[idxTriangle2] = idxVertex2;
            triangleRW[idxTriangle3] = idxVertex3;
 ...

}

..

When I change the coding to this here, everythings is fine:

...
[numthreads(8,8,8)]
void MarchingCubes2(uint3 threadId : SV_DispatchThreadID)
{
...
  for (int i = 0; triTable[cubeIndex] != -1; i += 3)
        {
            
            //WTF ????
            // Seems to work:
            InterlockedAdd(_cnt[0].trianglecount, 3, idxTriangle1);
            InterlockedAdd(_cnt[0].vertexcount, 3, idxVertex1);
            idxTriangle2 = idxTriangle1 + 1;
            idxTriangle3 = idxTriangle2 + 1;
            idxVertex2 = idxVertex1 + 1;
            idxVertex3 = idxVertex2 + 1;

            vertexRW[idxVertex1] = vertlist[triTable[cubeIndex]];
            normalRW[idxVertex1] = normalize(CalculateGradient(vertexRW[idxVertex1]));
            UV[idxVertex1]       = uvmap[voxeltype][0];
            

            vertexRW[idxVertex2] = vertlist[triTable[cubeIndex][i+1]];
            normalRW[idxVertex2] = normalize(CalculateGradient(vertexRW[idxVertex2]));
            UV[idxVertex2]       = uvmap[voxeltype][0];
            
            vertexRW[idxVertex3] = vertlist[triTable[cubeIndex][i+2]];
            normalRW[idxVertex3] = normalize(CalculateGradient(vertexRW[idxVertex3]));
            UV[idxVertex3]       = uvmap[voxeltype][0];

            triangleRW[idxTriangle1] = idxVertex1;
            triangleRW[idxTriangle2] = idxVertex2;
            triangleRW[idxTriangle3] = idxVertex3;


...

}

..

But whats the difference? They should be the same???

 

 

 

MCshaderCode.txt

mesh_broken.PNG

mesh_ok.PNG

meshOutputTextNotWorking.txt

meshOutputTextWorking.txt

Share this post


Link to post
Share on other sites

If you you do 3  atomic operations in sequence:

            InterlockedAdd(_cnt[0].trianglecount, 1, idxTriangle1);
            InterlockedAdd(_cnt[0].trianglecount, 1, idxTriangle2);
            InterlockedAdd(_cnt[0].trianglecount, 1, idxTriangle3);

...then other threads running in parallel can do their instructions inbetween them, so idxTriangle variables will not be an ascending sequence like 4,5,6 but more likely something random like 4, 12, 40.

 

Your second attempt is also much better because you use less expensive atomic instructions.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now