Managing Multiple Shader Programs

Hey all,

I’m getting to the point where I’m starting to have several shaders (so far up to five) and am wondering how some of you start managing these. So far my system is less than elegant and I’m sure there is a cleaner, more OO way of doing this. Here are a few of my ideas:

  1. My current system is to have one Shader class which takes source code Strings as inputs in the constructor. I then have the main game class take care of compiling, binding, passing uniforms, and everything else. This is working fine for a small number of shaders but I can see it’ll be way too disorganized later.

  2. Have the Drawable objects (Drawable is an interface) themselves keep track of which shader to use when being drawn. This sounds like a nice solution but it’ll mean that each drawable object will need references to things like the current camera and projective matrix which doesn’t seem like good object oriented practice.

  3. Have a ShaderManager object which takes care of uniforms and binding and have a public static method getShader() or something which drawable gives drawable objects access. Again, doesn’t seem like very good OO practice since only Drawable classes actually need access to these shaders. I could pass a reference to the ShaderManager to each Drawable but I don’t really like that either.

  4. Something else???

Any advice would be greatly appreciated! Looking forward to a good discussion. :slight_smile:

-D

The easiest thing to do is simply to use the first one and reduce the number of shaders. For example, my meshes require 4 textures: diffuse, normal, emissive and specular+gloss. These 4 are all optional. In addition to this, I also have both skinned (skeleton animated) meshes and static unanimated meshes, which need different vertex shaders. In total I have 2^5 = 32 unique combinations of fragment and vertex shaders (shader programs) for my meshes. Managing them would be horrific and performance would probably also suffer from to the frequent shader switching. Instead, you can simply do some minor unnecessary work in your shader and avoid the permutations. I only have one fragment shader which assumes that my mesh has all 4 textures. If I don’t have a diffuse map, I simply bind a 1x1 white texture. No normal map? Just bind a 1x1 “pass-through” normal map and do the bump mapping calculations anyway since it hardly makes a difference. Skinning however is quite expensive, so I do create two shaders, one for skinned meshes and one for static meshes, for a grand total of 2 shader programs.

Thanks for your advice. I see what you mean about juts sending in pass through data for info which isn’t being used. That could help out. Right now I have the following shader programs: One for general static meshes. One for drawing billboards, the creation of which was chronicled in my previous thread. One for drawing the ground which gets special treatment since it can have shadows drawn on it. One for skinned meshes (which I am working on now). And one specifically for drawing text and the HUD info, which I think could combine with the billboard shader if I wanted to. I’m not sure how many shaders I’m going to want to use so far, but I am planning on using them for some other specialized effects. All in all it seems like the first option is a little too messy once you get more than 2 or 3 shaders going, but perhaps there are ways I can simplify things and use fewer…

Thanks a bunch for your quick response! ;D