Ask Your Question

Revision history [back]

click to hide/show revision 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:

  1. Convert the float value to an unsigned int using a union:
union FloatUint {
    float f;
    uint u;
};
FloatUint floatUint;
floatUint.f = floatValue;
uint uintValue = floatUint.u;
  1. Use atomicCAS to perform the min/max operation on the unsigned int value:
uint oldValue, newValue;
do {
    oldValue = uintVariable;
    newValue = min(maxValue, oldValue); // or max(minValue, oldValue) for atomicMax
} while (atomicCAS(uintVariable, oldValue, newValue) != oldValue);
  1. If the atomicCAS operation was successful, convert the unsigned int value back to a float:
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.