Multipurpose shader

I’ve read that to minimize shader program changes, it is useful to implement several materials in a single shader. My question is: how many? As many as possible? Does there exist some amount of conditionals in the shader, where the drawbacks of their use will outweigh the shader program switch overhead?


You should merge together materials in a single shader as much as possible to minimize program changes. Nowadays conditionals almost come for free. However, you should be aware that the selection of the branches in case of conditionals should be as consistent as possible. E.g. if in a 8x8 fragment group each fragment goes to different branches of conditionals, performance may suffer. This is because a group of shader units are actually executing the same code, so all the branches are executed if all of them are used at least in one shader unit instance of the group. This is because the shader units not taking a particular branch cannot always do something else but rather hardware often just masks the unit out (meaning that it does “nothing”).

Another option that you may consider is shader subroutines, however, they are available only on OpenGL 4.0 capable cards.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.