Shader usage is large games


I have been learning the GLSL language recently and all the tutorials always contain one shader program, one vertex shader and one fragment shader. Now as a somewhat experience game dev (not so much opengl) I know that in complex games there is a need for more complex systems. Systems were different objects need different shaders. For example one object may have normal mapping another one may not and have transparency mapping and so on.

I have worked with Unity and Unreal and both of them have a system were each material has a shader specific for it. In my mind things are getting more complicated because in a deferred shading situation there would be a need to have multiple shaders making each pass and then another combining all the passes. Then if post processing is required another shader needs to act upon the final renderbuffer.

Now my little brain is very confused as to how this should all work together in unison.

So my question, and I know its a bit broad, how are shaders used in large, complex games to render lots of different things lots of different ways? Are many different shaders used? Are many different programs used? I have been told that swapping shader programs at runtime is very costly so what other alternative is there?

Thank you!

Your right it is a complex question - if you solve it well your game will run fast - if not it will run slow. That is why Unreal charge so much money for their engine :slight_smile:

Yeah, it’s costly if you do it hundreds or thousands of times (cost varies by CPU+GPU combination). So you try to minimize the number of different shader programs you need, and you try to render the stuff on the screen that uses the same shader program (and textures, etc.) together. The latter is called “state sorting” (websearch on it in combo with OpenGL or Direct3D). In general, you try to sort or bin objects with the same or similar “GL state” together to avoid wasting cycles reconfiguring the GPU rather than blasting triangles.