I'm new to shaders and I have no idea how to increment numbers in compute shader (HLSL).
groupshared uint i = 0;
#pragma kernel CSMain
[numthreads(8,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
if(some_condition)
{
i++;
}
if(i == 10) do_something();
}
It's obvious the above code doesn't work. i++ causes race conditions (btw I learned about race condition few hours ago).
I had partial success with RWStructuredBuffer.
RWStructuredBuffer<int> i;
#pragma kernel CSMain
[numthreads(8,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
if(some_condition)
{
int value_before_increment = i.IncrementCounter();
}
// if(i == 10) do_something();
// Can't access the value of i without continuous increment
}
I Read some MS docs and either their docs sucks or my braincells sucks. It would be really helpful if you could provide some beginner friendly blogs or articles or help me out in the answers. I know asking article recommendation at Stack overflow is not allowed but experienced people can guide the path.