Are GLSL switch-case statements optimized as jump tables?

Discussion created by aqnuep on Dec 1, 2010
Latest reply on Dec 2, 2010 by aqnuep


I would like to implement an OpenGL based deferred renderer that uses multiple materials and the material indices come from one of the textures of the G-buffer.

The classical way to solve this is to use a switch-case construct like the following:

switch (materialID) {

    case 1: // do material #1 stuff

    case 2: // do material #2 stuff



Now, with OpenGL 4.0 we are in a better situation as we can solve the same thing with subroutine uniforms by putting all possible subroutines in an array and then simply index it:

subroutine uniform RoutineType DoMaterial[100];



The problem with this solution is that each time you change shaders you have to respecify a potentially large number of subroutine indices (and they are always the same). This is a kind of silly API limitation (I don't think that this has anything to do with HW support but maybe I'm wrong).

Because I'm worried about the overhead of respecifying all the subroutines (maybe several subroutine arrays: one for material, one for light and so on), I was thinking about maybe the switch-case construct may also work with constant time at least on HD5000 series cards by optimizing the switch-case into a jump table in the same style like usual compiler like GCC do with CPU code.

So my questions are the following:

Are GLSL switch-case statements optimized as jump tables on Evergreen GPUs in order to be executed in constant time? If yes, are they also optimized on earlier GPU generations?

Are glUniformSubroutines calls expensive operations if I have to pass hundreds of subroutines?

Is there any other possibility to solve the same issue in constant time?