1 | initial version |
GLSL atomicMin/Max for float data types can be achieved by using atomicCAS (compare-and-swap) operations in combination with int bit manipulation. Here's an example of how this can be accomplished:
union FloatUint {
float f;
uint u;
};
FloatUint floatUint;
floatUint.f = floatValue;
uint uintValue = floatUint.u;
uint oldValue, newValue;
do {
oldValue = uintVariable;
newValue = min(maxValue, oldValue); // or max(minValue, oldValue) for atomicMax
} while (atomicCAS(uintVariable, oldValue, newValue) != oldValue);
floatUint.u = uintVariable;
float floatValue = floatUint.f;
Note that this method assumes that the float values being compared are finite and not NaN or infinity, and may not be as efficient as native atomicMin/Max operations on some hardware.