Ask Your Question
1

How can GLSL atomicMin/Max be achieved for float data types if SPV_EXT_shader_atomic_float_min_max is not available?

asked 2021-09-25 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-07-18 12:00:00 +0000

plato gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-25 11:00:00 +0000

Seen: 9 times

Last updated: Jul 18 '22