Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am doing some 3D demos using OpenGL and I noticed that GLSL is somewhat "limited" (or is it just me?). Anyway I have many different types of materials. Some materials have ambient and diffuse color, some materials have ambient occlusion map, some have specular map and bump map etc.

Is it better to support everything in one vertex/fragment shader pair or is it better to create many vertex/fragment shaders and select them based on currently selected material? What is the usual shader strategy in OpenGL or D3D?

share|improve this question
2  
see stackoverflow.com/questions/4649801/… –  Devdalus Aug 23 '12 at 14:22
    
This is a false dichotomy, that you must choose one or the other approach to managing shaders. In fifteen years in the industry, I've never worked on a project where we adhered to either extreme, but always struck a balance between the two approaches to managing shaders, using each where it made sense to do so. –  Trevor Powell Sep 22 '12 at 2:04

1 Answer 1

up vote 6 down vote accepted

The answer is, it kinda depends.

Basically there are two schools of thought; small, optimal shaders for everything and the uber-shader camp.

The small shaders are just that; do one thing, and do it well. Uber-shaders either control their functionality at runtime through uniforms, or compile into a bunch of different shaders through preprocessor macros (and/or generated shader sources).

The optimal solution is probably neither, but some kind of hybrid. Perhaps a few uber-shaders or uber-shaders combined with specialized shaders for some cases.

Small shader

Pros:

  • Likely to be faster/more optimal
  • Does just what you want it to do
  • Can do weird, custom stuff easily

Cons:

  • Easily gets out of hand, especially when the number of features grows
  • Probably more work, unless you have a very small number of shaders

Uber-shader

Pros:

  • Everything in one place
  • More artist-friendly (using feature A with feature X for the first time doesn't necessarily require coder time)

Cons:

  • Longer shader compile times
  • Less optimal results (esp. if uniforms are used to switch features on/off)
  • May be more difficult to debug/optimize
share|improve this answer
    
Uber-shader in this representation is not a good thing :) I would propose to write good shaders manager based on predefines - so we will have uber-shader code with many #ifdefs which will be toggled for various materials. –  brigadir Sep 21 '12 at 15:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.