PDA

View Full Version : High number of shader program switches?



insane_ivan
02-22-2013, 10:35 AM
Hey guys!

So as this is my first post, I would like to say hello to everybody ...

... and dive directly into my question.
After playing around for a while with OpenGL, I finally managed to setup my own little graphics engine. Until now everything runs well. But - texturering is the next big thing to come.
I really don't like the engine to have plain images as textures so I am really going into all the special material stuff (or at least I plan to do so ;-) ).

So I am really concerned about how to organize this stuff. How do you guys handle huge shaders (as they sure will be)? Is it better to develop shader's that get some state-variables (for example b_MaterialHasSpecularMap = true) and compute all the stuff depending on these variables or is it no problem if I have shaders for every single material type?
I don't worry much about the code but as the title says, I am not sure about GL's beheaviour on an increasing number of shaderprogramm switches.

Any hint on this topic anyone?
I would appreciate any help!

thanks!
Ivan

Dark Photon
02-22-2013, 06:16 PM
How do you guys handle huge shaders (as they sure will be)? Is it better to develop shader's that get some state-variables (for example b_MaterialHasSpecularMap = true) and compute all the stuff depending on these variables or is it no problem if I have shaders for every single material type?

You have lots of choices, and what works best for someone else might not work best for your application, but...

I personally like the ubershader method alot for handling shader permutations. In particular, see:

* link1 (http://www.opengl.org/discussion_boards/showthread.php/171801-varying-float-accuracy?p=1206124&viewfull=1#post1206124), link2 (http://www.opengl.org/discussion_boards/showthread.php/171910-Shaders-organizing?p=1206638&viewfull=1#post1206638)

This works well for managing shader permutations where the shader code switches based on a boolean or enumerated value, particularly when the inputs provided to different shader permutations change (think light model, color material, texenv, etc.). Depends on your needs, but you'll probably still want to sort your batches by shader permutation for max perf.

All that said, branches in shaders nowadays are fairly cheap "if" you don't have divergence between neighboring pixels (e.g. the branch chosen is constant within every batch). So you could do that. Though you'll probably still want different shaders if your permutations cause the shader to take different inputs.

insane_ivan
02-23-2013, 02:49 AM
Thanks for all the information and your help!
Guess I will go for the "ubershader"-method :D