I'm trying to move a 262,144 [2^18] points (stars) in a compute shader and am struggling to reliably address the data in a buffer.
I understand that there's a 3-dimensional array SV_DispatchThreadID
that provides an index for the thread in the thread group.
My hardware can run 65,536 threads per thread group (assuming I've understood the terminology correctly). As my total number of stars exceeds that number (and they're rendered), unity must be making multiple calls under the covers(?)
In any case, I have:
[numthreads(64, 64, 16)]
void BuildStars(uint3 id3 : SV_DispatchThreadID, uint id1 : SV_GroupIndex) {
int id = id3.x
+ id3.y * 64
+ id3.z * 4096;
...
[To simplify things, I'm only attempting to use 32,768 stars to begin with, meaning I'm ignoring id1
for the moment. I'd hoped to be able to add id += id1 * 65536
, but I haven't got that far yet]
The movement of the stars is inconsistent. Some move far more rapidly than others, some don't move at all.
As all I'm doing is adding a constant offset every frame, the only explanation I can come up with is that I'm generating the ID incorrectly, wrapping around and effectively updating random stars
mapComputeShader.Dispatch(Kernel("MoveStars"), 64, 64, 8);
With an eye to calling the following in future
mapComputeShader.Dispatch(Kernel("MoveStars"), 64, 64, 64);
What's the correct way from within the compute shader to get an id that varies from 0
to numDispatchedThreads - 1
, where numDispatchedThreads
exceeds 65,536?