7

In a compute shader (with Unity) I have a raycast finding intersections with mesh triangles. At some point I would like to return how many intersections are found.

I can clearly see how many intersections there are by marking the pixels, however if I simply increment a global int for every intersection in the compute shader (and return via a buffer), the number I get back makes no sense. I assume this is because I'm creating a race condition.

I see that opengl has "atomic counters" : https://www.opengl.org/wiki/Atomic_Counter, which seem like what I need in this situation. I have had no luck finding such a feature in either the Unity nor the DirectCompute documentation. Is there a good way to do this?

I could create an appendBuffer, but it seems silly as I literally need to return just a single int.

4

1 回答 1

7

哈!那很简单。我会把这个留在这里,以防有人遇到同样的问题。HLSL 有一整套“联锁”功能,可防止此类事情发生:

https://msdn.microsoft.com/en-us/library/windows/desktop/ff476334(v=vs.85).aspx

就我而言,它是:

InterlockedAdd(collisionCount, 1);

取代

collisionCount++;

就是这样!

于 2015-02-20T22:09:26.877 回答