Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a clamp function in glsl and opencl work? does it use create branches? and should I avoid using it?

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?

like image 513
Hasan Al-Baghdadi Avatar asked Jan 28 '23 08:01

Hasan Al-Baghdadi


1 Answers

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…

like image 76
Michael Kenzel Avatar answered Feb 03 '23 13:02

Michael Kenzel