27
\$\begingroup\$

How many shaders are usually active, at the same time in one scene, in modern games? I know that multiple shaders are being used, with the games switching between them in each frame, and it's common to draw objects via the shader:

  1. Draw all objects with shader one
  2. Change from shader one to shader two
  3. Draw all objects with shader two

Still, I know it's not as simple, especially with effects like a glow effect for whole scene, render to texture, etc., but I guess we can assume it works that way most of the time, right? The "group by shader" approach is good, because switching shaders is an expensive operation.

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

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, counting only active, at some "frame X"? I know it's not one number, but I wonder what scale and factors are important, in consideration for a PC game.


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?

\$\endgroup\$

3 Answers 3

36
\$\begingroup\$

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.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ 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 :) \$\endgroup\$
    – PolGraphic
    Commented May 9, 2014 at 11:09
  • \$\begingroup\$ s/less/fewer/ ... \$\endgroup\$
    – error
    Commented Jun 9, 2017 at 4:21
5
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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 :) \$\endgroup\$
    – PolGraphic
    Commented May 11, 2014 at 9:46
1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .