switch() inside fragment program

Is there an optimized way to use a switch() inside a fragment program? Compilers (for common processor architectures) are known to generate jump tables when switch values use an integer range (say from 0 to 5), avoiding branches in this way. I was wondering if, with modern GL (3.2, 3.3+) there are similar optimizations. For example:

int a = clamp(somevalfromsomewhere,0,2);

switch(a) {
case 0: { somecode } break;
case 1: { someothercode } break;
case 2: { someothercode } break;

A C compiler is often smart enough to detect this and generate a jump table. Does this optimization commonly happen in GLSL compilers?

On Nvidia hardware you can use nvemulate (http://developer.nvidia.com/object/nvemulate.html) to see what assembly code is generated from your GLSL. I suggest experimenting and see if you can find any clues…

Also Cg:

cgc -oglsl -strict -profile gp4vp v.glsl

Thanks a lot! I tried giving cgc a go, although it doesnt seem to generate a GLSL new enough that understands switch() seems switch is unrecognized :frowning: