'How do I synchronize access to variables in compute shader?
So i seem to be having an issue with assigning a value to a buffer within a compute shader.
Essentially, I want to check if a calculated value is greater than one of the values of my structured buffer, and if it is, change the buffer values. The pseudo code bellow hopefully should represent what I want to occur:
structuredBuffer<uint2> buffer;
CSMain {
int a = calculatedValue;
if(a > buffer[index].x) {
buffer[index].x = a;
buffer[index].y = 1;
}
}
I'm updating the buffer because i want this to effect the check on other threads (so that the other threads are now comparing to the higher value). This doesn't work as the access to the variable is not guaranteed to be synchronized.
I figured i could use the interlock functions to solve this problem and ended up with something like this:
structuredBuffer<uint2> buffer;
CSMain {
int a = calculatedValue;
int out;
InterlockMax(buffer[index].x, a, out)
if(out < a) {
InterlockExchange(buffer[index].y, 1);
}
}
This gave me much better results but I'm still ending up with some of the data being incorrectly assigned.
Overall, my questions are:
- Am i correct in that my issue is related to synchronization?
- If so, is there some way i can make sure that the data is sync'd before i check (with memory barriers maybe)?
- Or, is what im trying to do just not achievable? Am i better off doing a separate dispatch?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|