Both GLSL and OpenCL have a clamp function that will clamp a number to the upper or lower bound inserted if the value exceeds the bounds. If I were to try and implement something like this in C++ it would look like the following code:
if(i < min){
i=min;
}else if(i > max){
i=max;
}
However, this has multiple branching paths, which as I understand can slow the GPU quite a bit as most of them will have to execute all branches.
So how does the GLSL/OpenCL clamp work and if it uses branches would you recommend avoiding it if possible?
If you check the relevant documentation of GPU instruction set architectures, e.g., here and here, you will find that GPUs generally have native support for min and max instructions. Even if they didn't have, conditionals on NVIDIA GPUs, for example, are based on predicated execution. Any reasonable compiler would turn your example above into conditional assignments rather than a fully-fledged branch (example here). Even on the CPU…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With