Creating an ubershader

at the moment i create my shaders the way ive seen everyone else do, ie a seperate whole shader text for each shader. now this is all good and well but often i wanna change things quickly eg turn off shadows or parallax mapping.
now to do this is a PITA with #ifdef etc or if statements.
a post by jwatte a little while ago had the bright idea of from multiple strings adding them together and then sending the uberstring to the card for compilation. sure u cant see the program all at one onscreen (but it can be easily dumped to text with glProgramString)
does anyone see any caveats with doing it this way. ie why is noone (from all the examples ive seen) doing it this way

zed

I do it that way, and it works fine. Except #line in the GLSL definition is broken; it will only give you the source string index, not a file name. Except NVIDIA goes ahead and accepts a source string anyway, so error messages are more useful on NVIDIA drivers. Non-conformance: the good, the bad, and the indifferent :wink:

Note that compiling a shader may impact your frame rate slightly, so the more you can pre-compile on load, the better. Of course, if you’re hitting the disk for the shader text, that’s even worse than compiling.

@jwatte: do you compile and link your ueber-shaders for each frame? what framerates can one expect with this approach?

cheers i think i might do it that way as well, my main app has already over 15 shaders (+ ive been trying as much as possible to use as few as possible!),
i see the mainabiltity being better eg i just read in these forums that writing the gl_position as soon as possible is a goood way to go, now in all my shaders its the last line of the vertex shaders, hence i would have to make a lot of mundane changes to my existing shdaers.
the only problem i see is im gonna have to be more uniform in my variable names.

I think 15 shaders isn’t too much. NVIDIA keeps saying how we’ll be “cinematic” and a movie like The Incredibles probably uses many thousands of shaders :slight_smile:

I don’t compile shaders every frame. I compile a new combination when I run into it, which may cause a very small stutter in frame rate. Then I cache the fact that I’ve seen and compiled this combination (and the result), and re-use.

If your game has levels, you could probably clear out all compiled shaders and start over on level load.

true 15 aint much, but i could see if getting out of hand really quickly (cause in many cases i was just using a similar shader to what i wanted instead of writing a new one)

i did start working on this and got a few up and running (already seen some benifits by me rewriting the shaders with my increased understanding of them) but its quite a bit more complicated than i originally thought. and there are some shaders that cant really be ‘assembled’, gotta add though it is a lot harder than i thought.

I don’t compile shaders every frame. I compile a new combination when I run into it, which may cause a very small stutter in frame rate. Then I cache the fact that I’ve seen and compiled this combination (and the result), and re-use.
sounds like how i handle my materials (contains shaders/textures shading state) eg a new particle systems is created + if it hasnt been used befoire then the particl systems material is loaded (and all shaders/textures) resulting in a very small stutter (i assume from the texture) hardly noticable, i could get rid of it by preloading but as im the only one who sees my demo, i can live with it for now.

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