Deferred shading or Deferred rendering is a technique used in most current AAA games in order to obtain great stunning effects. Let's see how it works and why is usually applied.
The most usual way of rendering geometry is forward rendering. Every mesh that intersects with the frustum is rendered once and using the depth buffer OpenGL can discard those meshes that are located behind other geometry and its pixels have already been drawn. Using this method we can avoid artifacts like in the image below:
In the left image depth test is disabled and, as you can see, the floor is drawn over the cubes. Thanks to the depth test this is resolved with just storing a single texture that goes from 0 as the near plane to 1 as the far plane (some games reverse this order but this goes beyond the scope of this post). This is pretty cool and it seems that resolves all our problems except for implementing lights. When a light is casted all pixels in a certain radius must calculate their final color with some expensive math calculus. In forward rendering, depending on the blit order, this calculus is done several times for each pixel no matter if another geometry that overlaps the current mesh is drawn later. Deferred shading simplifies this with the following method:
We first render all our geometry without any light calculations and we store all relevant data such as Albedo, Positions, Normals, Specular, Irradiance (every game applies it in a way or another) in different textures. After the first rendering pass is done, a quad is blit over the screen and all textures are used to calculate the final pixel colors. Here all light calculus are done decreasing the number of operations by far.
All the images of the posts are courtesy of Joey De Vries and we highly recommend his tutorials for a better understanding of the basics graphics techniques used in most visually stunning games. In future posts we will see how some post processing effects can be easily applied thanks to the deferred shading pipeline.
Thanks for reading!
Guillem Costa Miquel