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 have a question: how many shaders are usually active at the same time in one scene in modern games?

I mean: I know that multiple shaders are being used (switching between them in each frame). And it's common that you draw objects "by the shader":

  1. Draw all objects with shader1
  2. Change shader1 to shader2
  3. Draw all objects with shader2

And still, I know it's not as simple (glow effect for whole scene, render to texture etc.), but I guess we can assume it works that way most of the time, right?

That approach ("group by the shader") is good because the shader switching is expensive operation.

So, from one side you cannot have too many shaders, because you want to render the scene fast. But on the other hand you need many different shaders (or uber-shader with branches - quite similar) for skin, metal, water etc.

So - how many (and which) different shaders would the theoretical, modern, third person, 3d detective game for PC (DirectX 11 if it matters) use? It would be 5, 20 or more like 100 active shaders (I count only active, at some "frame X", shaders)?


In my sample game, I would use about 9-11 per frame (count it as different, small shaders or one uber-shader - doesn't matter now):

  1. Skin shader
  2. Eye shader (not too much? but they are different)
  3. Metal shader
  4. Ground shader
  5. Snow/rain shader (if required)
  6. Water shader (if the water exists in scene)
  7. Glow shader (only when some special effects are involved)
  8. Light emiter shader (street lamps etc.)
  9. Standard shader (for all other, just standard shading)
  10. Standard shader with normal maps
  11. 2D shader (for GUI etc.)

Is it "much" or "not many"? Did I forget about some important shaders that I would need?


P.s.

I know it's not one number ("always use 3 shaders!"), but I wonder what is the scale (5, 100?) and which factors are important (I consider the PC game only)?

share|improve this question

3 Answers 3

up vote 19 down vote accepted

It really depends on the technique. In a forward render you often have one shader for every different material type. In AAA games that use this technique shaders are often generated from a set of options on the material which an artist can set. This can lead to hundreds of shaders(1). But lets say generally the number of shaders is in the order of 40

In a deferred shading/rendering you often encode the material type in the G-buffer and later use one big shader to correctly render different material types differently. This leads to a lot less, but more complicated, shaders. Lets say the number of shaders here is in the order of 5.

(1) Source: I worked for a contractor of Crystal Dynamics where I built a tool to reduce the number of generated shaders for Tomb Raider since automatic generation was getting out of hand ;). The goal was to reduce the number of shaders to the order of 40 or something like that.

share|improve this answer
2  
Thank you for your answer - it was quite complex and rich. I've just up-voted it. Still, I will wait few days before marking is, because it's quite an "open" question - maybe somebody else will have something more to add from their own experience as well. Once again, thanks Roy :) –  PolGraphic May 9 at 11:09

You can't really put a number on how many shaders you should or should not have, as it is dependent on the limiting factors of your game, as well as the target platform. In practice, reducing the number of active shaders to be "only as many as you actually need" should be followed. Sometimes this means you have only a handful of shaders (10 or so) at once, and sometimes it means you have a few dozen.

One way to help limit the amount of permutations that pop up in your game is to implement the "ubershader" technique, by which almost every shader is derived from a singular shader, and the differences and special behaviors are swapped out via a preprocessor. There are exceptions of course for special effects such as depth of field, ambient occlusion, and so forth.

My experience with AAA games up to this point has shown that a couple dozen shaders active is not unreasonable.

share|improve this answer

I know you wanted an answer for PC. But I'll answer for what I do in iOS and I don't expect to get your checkmark.

It looks like you have the right idea. It's going to be different for each app though. In the end it's all about doing what it takes to get your desired frame rate. On the iPhone my goal rate is 60. I don't like for it to get below 30. When I have an explosion with several hundred animating sprite based particles, it gets down to 30 on the iphone5 and much lower on the 4. So I have to make decisions about reducing complexity on certain devices. And sometimes that decision effects shaders.

But it doesn't generally effect how many shaders but rather which shaders I use for a task based on my frame rate goal.

In my opinion shaders when used correctly raises the frame rate. I have an app that has 80 shaders, but it doesn't use all of them on a single frame - probably around 10. And it compiles them as needed and discards them all between scenes. And on my hardware I haven't seen the shader count effect frame rate. Only the techniques used in the shader. But for sure I think that using 2 similar shaders is better for frame rate than having a more complex one that branches.

I also have shaders that reduce quality if the rendering is slow or on a slower device. I use normal mapping on the iPhone 5 but swap out the shader to one that doesn't do normal mapping on the iPhone 4.

I like Roy's answer and I'm always trying to get insight into how "real" games are made. Since I'm self taught I never know if my great discovery of the day is just how it's generally done or still a few steps behind the real professionals.

share|improve this answer
1  
No problem with the fact that your answer "is for iOS". I said about PC mainly because I want to avoid "it depends on platform" short responds ;) Maybe your answer is not 100% for me. But still, it has some platform-independent conclusions so I've found it good. And other members of the community (that use iOS) may find it even more useful, so thanks and vote-up from me :) –  PolGraphic May 11 at 9:46

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.