I have the following problem :
I use a shader, which has 3 functions which have identical arguments. I need to do 3 passes, with each pass using a different function, but in all generated fragments.
Instead of passing a uniform value (e.g. 0,1,2) and performing an if-statement, is there a better way to select the appropriate function?
I thought also of passing a texture encoding the appropriate function, which will be small, but the function performs about 12 integer arithmetic operations including 2-3 divisions and 2-3 modulos, so my guess is that texture encoding isn’t worth it.
Have I missed any GLSL feature that could help me in this case?
Thanks in advance,
Selecting function within a single shader may exhibit perf problems , depending on the hardware. But if it works for you, why not.
What about linking different programs, one with each function ?
That way each pass will use its own calcultations.
Switching shaders has a fixed cost, but if they have been already compiled and linked, it should be ok.
I was thinking that switching 3 shaders might be worse than using an if-statement with coherent results throughout the fragments.
But if I’m wrong…I’ll make different programs then, so thanks!
Of course, it is a good idea to minimize the number of shader changes. But in your case there are only three changes per frame. That’s no problem.
three changes for this part… Will be about 20 in total I guess, that’s why I’m a bit reluctant
20 Shader changes are nothing! Don’t be afraid about it.
Also changing the shaders has, as already mentioned, a FIXED cost. Your if-statement approach has a cost depending on the screen-resolution. If one can remove such a thing, it is always good to do it.
Nothing?? that’s good news I considered the if-statement approach because I’ve read that it performs relatively well in coherent branch selection, and all fragments selecting the same branch is pretty much coherent.
But yes, I forgot the difference of the fixed cost of shader switching vs the fragment number dependent cost of the if-statements.
Never be afraid to switch shaders. The cost is negligible compared to most other options (microseconds at most).
Now, if you were trying to make 10000 shader switches, that’s probably a design flaw.
This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.