Category Archives: Rendering Methods


Deferred Lighting

This is a lighting technique that lately has increased a lot in popularity. The normal way of shading is to perform the lighting calculations on a fragment when it is rasterized to the screen. This is often good but requires a lot of calculations if there are many lights. And the bad thing is that this fragment might later on be overwritten by some other fragment so the calculations might be a waste.

In deferred lighting (or deferred shading, or deferred rendering), you save the information about the fragment that is necessary to perform the shading (lighting) by rendering them to textures instead of doing the actual lighting calculation. When all geometry is rendered, the lighting will now be calculated only once per pixel on the screen. So no calculations will be wasted. You can perhaps say that it is some sort of a lazy evaluator.

The information saved per fragment is often:

  • position ( or just depth )
  • albedo ( the diffuse texture )
  • normal
  • specular

And these are sometimes also used:

  • shininess
  • material ID (for selecting material behaviour)

When all geometry has been rendered and it’s time to perform the lighting, the lights needs to be represented as something when sent to rasterization. Point lights can be drawn either as spheres or just square billboards. Directional light should be drawn as a full screen rectangle. And spotlights will be cones. Note that this shading technique allows for lights shaped in any form, not just these traditional ones.

The big reason for using deferred rendering is how well it scales with more lights. Another reason that it has increased in popularity lately is how nice it works with new rendering methods like SSAO and depth of field. The problem areas with deferred lighting is transparent objects and multisampling (antialiasing). If the original scene didn’t have per pixel lighting (but instead maybe vertex lightning) on the whole scene then the deferred rendering might be slower than traditional rendering.

Deferred Lightning example

Deferred Lighting example

Explanation of deferred lighting (and source code)

Deferred Rendering in S.T.A.L.K.E.R.

Deferred Rendering in S.T.A.L.K.E.R.

Explanation of how deferred lighting was used in the game S.T.A.L.K.E.R. 

The result of the deferred rendering XNA tutorial

The result of the deferred rendering XNA tutorial

A very good tutorial of how to implement deferred lighting in XNA 2.0. This is good reading even when you are rendering in an other API.

A long discussion on the forum of pros and cons of using deferred rendering compared to traditional forward rendering.

DirectX9 implementation if deferred shading, and some optimization talk DirectX_9.pdf

Deferred Lightning in Leadwerk Engine

Deferred Lightning in Leadwerk Engine

Info about the implementation of deferred shading in the Leadwerks Engine.

Deferred Lighting in Killzone 2

Deferred Lighting in Killzone 2

A presentation about deferred lighting in the game Killzone 2:

Shader Programming Guides

Here are some links to a couple of quick guides and references that’s useful when writing shaders.

The OpenGL GLSL shader language Quick Reference Guide.

The full specification of GLSL 1.20.8.

The full specification of GLSL 1.30.08

GPU Programming guide from Nvidia for both OpenGL and DirectX.

Guide to write shaders in DirectX9 in HLSL

Guide to write shaders in DirectX10 in HLSL

Alpha Testing

When drawing for example lots of vegetation (with textures containing an alpha channel) you need to minimize overdraw and cost for each draw call. If you use blending for archiving the desired transparent effect on the leaves then you will need to sort all the triangles (back-to-front-order) to draw, (and sometimes this needs splitting of triangles) to get the correct look. Also when blending you might render the same pixel multiple times because you want the result to blend together. All this drawing and sorting takes a lot of time and a better way is to do alpha testing. In alpha testing (or alpha killing) you select an alpha threshold and all pixels with an alpha above (if you use GREATER as test function) this threshold will be drawn. Because no blending occurs, you don’t need to sort the triangles anymore (but preferably you will sort them front-to-back-order for optimization) . The drawback with alpha killing is that you will get sharp edges around the pixels drawn.
Alpha killing does not work with shadow volumes.

Alpha testing in OpenGL

Alpha testing in DirectX9 (in DirectX10 you must do your own implementation in a shader)