PDA

View Full Version : switch() inside fragment program



Jose Goruka
05-18-2010, 04:57 PM
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?

Don't Disturb
05-20-2010, 07:10 AM
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...

Dark Photon
05-20-2010, 07:53 AM
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

Jose Goruka
05-20-2010, 11:02 PM
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 :(