Category Archives: Rendering Methods

Description

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)
http://www.beyond3d.com/content/articles/19/

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.
http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter09.html 

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.
http://www.ziggyware.com/readarticle.php?article_id=155

A long discussion on the gamedev.net forum of pros and cons of using deferred rendering compared to traditional forward rendering.
http://www.gamedev.net/community/forums/topic.asp?topic_id=424979

DirectX9 implementation if deferred shading, and some optimization talk
http://www.gamedev.net/reference/programming/features/shaderx2/Tips_and_Tricks_with_ DirectX_9.pdf

Deferred Lightning in Leadwerk Engine

Deferred Lightning in Leadwerk Engine

Info about the implementation of deferred shading in the Leadwerks Engine.
http://www.leadwerks.com/files/Deferred_Rendering_in_Leadwerks_Engine.pdf

Deferred Lighting in Killzone 2

Deferred Lighting in Killzone 2

A presentation about deferred lighting in the game Killzone 2:
http://www.guerrilla-games.com/publications/dr_kz2_rsx_dev07.pdf

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.
http://www.opengl.org/sdk/libs/OpenSceneGraph/glsl_quickref.pdf

The full specification of GLSL 1.20.8.
http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf

The full specification of GLSL 1.30.08
http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.30.08.pdf

GPU Programming guide from Nvidia for both OpenGL and DirectX.
http://developer.download.nvidia.com/GPU_Programming_Guide/GPU_Programming_Guide.pdf

Guide to write shaders in DirectX9 in HLSL
http://msdn.microsoft.com/en-us/library/bb944006(VS.85).aspx

Guide to write shaders in DirectX10 in HLSL
http://msdn.microsoft.com/en-us/library/bb509703(VS.85).aspx

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
http://opengl.org/documentation/specs/version1.1/glspec1.1/node96.html

Alpha testing in DirectX9 (in DirectX10 you must do your own implementation in a shader)
http://msdn.microsoft.com/en-us/library/bb172254.aspx