Hello,
Looking for a bit of clarity on compute shaders. I have a compute shader that I used for calculating movement of an object. It works great when I have numthreads set to (1,1,1) and I dispatch like Dispatch(0,numberOfTasks,1,1);, but doing so doesn’t take full advantage of the GPU, so I bump the numthreads up to (64,1,1) and I dispatch like Dispatch(0,Mathf.CeilToInt(numberOfTasks/64),1,1); and some of my objects didn’t move. In this particular scenario there were 150 objects to move (150 = numberOfTasks). So I drilled down and found that exactly 22 objects weren’t moving out of the 150, so that looked like only 2 full thread groups (64 * 2 = 128) were working as expected. I then added a test buffer to see what the IDs were looking like Numbers[id.x] = id.x and the output is sometimes:
0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50; 51; 52; 53; 54; 55; 56; 57; 58; 59; 60; 61; 62; 63; 64; 65; 66; 67; 68; 69; 70; 71; 72; 73; 74; 75; 76; 77; 78; 79; 80; 81; 82; 83; 84; 85; 86; 87; 88; 89; 90; 91; 92; 93; 94; 95; 96; 97; 98; 99; 100; 101; 102; 103; 104; 105; 106; 107; 108; 109; 110; 111; 112; 113; 114; 115; 116; 117; 118; 119; 120; 121; 122; 123; 124; 125; 126; 127; 1082258336; 0; 1103246933; -1041713592; 0; -1081800488; -1048460300; 0; 1106062283; -1038535056; 0; -1065126173; -1039185940; 0; 1107541462; 1108340868; 0; 1104324530; -1039507570; 0; 1107771709; -1058268496
Looking at the output tells me that just the first 2 thread groups had sequential numbering and worked as normal which correlates to the correct data for 128 tasks and bad/no data for the remaining 22 tasks.
The way I’m accessing the buffers inside the compute shader is like myBuffer[id.x] where id is a uint3 SV_DispatchThreadID. Since it is just a 1D problem I understood that id.x should always give me the correct index, but it appears to not be the case.
Dispatch:
movementShader.Dispatch(0, Mathf.CeilToInt(numberOfTasks / 64), 1, 1);
Test compute:
[numthreads(64,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
Numbers[id.x] = id.x;
}
What am I missing here?