# D3D/OpenGL: Question about shaders in larger projects

I'm a game dev student and last semester we had a '3D-programming' course, in which we did some basic stuff such as rendering a textured quad spinning around in the "scene". We used quite basic vertex-, geometry-, and fragment-shaders to do this.

My question is, how are shaders "managed" in a larger projects? For example, I might have a sphere in the scene that uses a shader to generate some effect on it, some VS or FS to generate water ripples or waves on a water 'model', etc. How would all these different shaders be combined in the same project? Do we just create new shader programs for the different shaders(as you can do in OpenGL)? Do you use some kind of condition-statement in the shader and use only one program? Some other approach? I've heard you really want to minimize API-calls, especially for realtime apps (Multiple shader programs would need at least 2 calls to set the shader programs, EVERY frame.) I'm really confused about this part, and our next course involves making a small game so this will be quite important I'd imagine. (also im personally very paranoid when it comes to performance; I want to do things right from the start when possible)

Sorry if the question is poorly formulated, hope it makes some sense. ^^'

An example would be great, HLSL/D3D or GLSL/OpenGL doesnt matter, I've fiddled with both.

## 1 Answer

It depends on your needs.

Most of the time you just have separate shaders for separate effects, like reflections water, post-processing or simple shading. This allows the graphics programmers to work in a much more organized way. How you create the individual shaders is however very inconsistent. A post-processing shader (or shaders) usually have a single purpose (e.g. blur), but stuff like lighting is usually just a very big shader that does things differently based on the values passed to it, so the same shader can be used to create metal and dirt.

The other approach is the uber shader. This is a very broad term, and you won't find a lot about it on the internet either. Usually, uber shaders are massive shader programs meant to deal with a variety of rendering situations (however, post-processing is usually not one of them). They either use compile time (#define, #if) or runtime (if (...)) switches to handle different scenarios. Sometimes even water and other reflecting or refracting surfaces are included in the mix. If you want, you can literally include every shader program in a single file, but it's not advised.

The third and in my opinion the most flexible approach are the modular shaders. These are constructed on the fly, usually when the game starts. Instead of having a single pre-defined shaders, you have a lot of self-contained pieces of code, which you put together to form a shader. This has multiple benefits, first it makes creating shaders easy and fast by sacrificing some load time, and second, it makes shaders only do what they need to do, thus avoiding wasting time on things like specularity on a matt object.

• Great answer, really cleared some stuff up for me. Thanks alot! I was looking around a lot during the last course but it didn't seem like this was discussed anywhere. Do you perhaps have some tips where I can learn more about this? As a Technical Artist i think deeper knowledge on this particular subject could be incredibly useful! – Elias Finoli Mar 27 '17 at 23:35
• @Elias if you do a lot of graphics programming, you'll eventually learn this. If you want to learn how you make shaders of specific effects, try looking into GPU gems – Bálint Mar 28 '17 at 5:51