I'm trying to a write a simple compute shader which will take in an array of points as well as two points which define a line. I would like to invoke the shader for each point in the array, have all the distances calculated in parallel and then have each invocation wait it's turn in a busy loop to update the farthest point data. My compute shader looks like this
#version 430 core
layout(std430, binding = 0) buffer BufferObjectIn {
vec3 data[];
} pointData;
layout(std430, binding = 1) buffer BufferObjectOut{
uint inLeftIndex;
uint inRightIndex;
uint inPointCount;
int outFarthestIndex;
float outFarthestDist;
int mutex;
} ssbo;
layout(local_size_x = 64) in;
float signedDistance(vec3 linePoint1, vec3 linePoint2, vec3 p) {
return (linePoint2.x - linePoint1.x) * (linePoint1.y - p.y) - (linePoint2.y - linePoint1.y) * (linePoint1.x - p.x);
}
void main(void) {
uint threadIndex = gl_GlobalInvocationID.x;
if (threadIndex >= ssbo.inPointCount) {
return;
}
vec3 leftPoint = pointData.data[ssbo.inLeftIndex];
vec3 rightPoint = pointData.data[ssbo.inRightIndex];
vec3 thisPoint = pointData.data[threadIndex];
float dist = signedDistance(leftPoint, rightPoint, thisPoint);
if (dist < 0){
return;
}
do {
lock = atomicCompSwap(ssbo.mutex, 0, 1);
} while (lock == 0);
barrier();
if (dist > ssbo.outFarthestDist) {
ssbo.outFarthestDist = dist;
ssbo.outFarthestIndex = int(threadIndex);
}
ssbo.mutex = 0;
}
What I'm finding is that my application will show different results even though I am running it on the same set of data. Have I done something wrong?