Jump to content
  • Advertisement
Sebastian Docktor

Compute-Shader: InterlockedAdd: buggy?

This topic is 469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!