2012-06-19 07:03:00 by Homer in Daily Zombie
Welcome to shader wonderland. I will use the following terminology: VS (Vertex Shader), FS (pixel fragment shader), GPU (something that makes pretty lights)
The way that normal rendering works, is known as Forward Rendering (or Shading).
We perform a set of transformations on the vertices of every object in the VS (mostly transforming the normal and position to clip space) followed by a lighting calculation per pixel in the FS (assuming we want decent results).
MULTIPLE SOURCE LIGHTING:
We can do it with a pass per light (this sounds like a bad idea), or with one pass for all lights. Let's assume the latter.
Since each pixel of every object gets only a single FS invocation, we have to provide the FS with information on all light sources, and take all of them into account when calculating the light effect per pixel.
This is a simple approach, but it has its downsides. If the scene is highly complex (as is the case in most modern games) with many objects and a large depth complexity (lots of overdraw) we get a lot of wasted GPU cycles.
For example, if the depth complexity is 4, it means that the lighting calculations are executed on 3 pixels for nothing.. because only the topmost pixel counts. We can try to counter that by sorting the objects front to back, but that doesn't always work well with complex objects. And even when we sort at the Polygon level, we still have problems (classic bsp interweave problem, and z-fighting, are great examples).
Another problem with forward rendering is when there are MANY light sources. In this situation, the light sources tend to be rather small / with a limited area of effect (otherwise they would overwhelm the scene!)... But our FS calculates the effect of every light source, even if it is far away from the pixel !!!
You can try to calculate the distance from the pixel to the light source but that just adds more overhead and branches into the FS. Forward rendering simply doesn't scale well with many light sources. Just image the amount of computation the FS needs to do when there are hundreds of light sources.
Deferred Rendering was developed to address precisely these problems.