I began writing my own deferred renderer in XNA about two years ago. There were a few excellent resources on ZiggyWare, Catalin Zima's site and Riemer Grootjan's site regarding deferred rendering. They helped me quickly get a working renderer written but I still didn't understand much of the maths and concepts behind it.. I just thought it looked cool.
Over the next few weeks I will be writing a series of articles detailing the way I use deferred rendering in my current projects and break it down step by step to not only help build understanding of how the renderer works but also show how to optimize various steps.
There are a number of ways you can create a deferred renderer but it usually entails creating a G buffer. This buffer is effectively a set of Render Targets which hold information on your scene (G for Geometry). You can then use the contents of these buffers to quickly and easily light your scene. It allows you to use potentially hundreds of lights with very little slow-down (note that performance on the Xbox 360 has to be monitored very carefully).
I'll run down the list of buffers I use in my renderer with a brief description of how they're used. In my next article I'll detail the process of creating the G buffer and writing to it.
The
Diffuse Buffer stores the colour data for our scene. This is read from the various textures that make up each object in the scene.
The
Normal Buffer stores the normal data for our scene. This is determined by the orientation of the vertices we are rendering but we also use additional detail from textures which contain normal data.
The
Depth Buffer stores the depth data for our scene. Without hardware access to the Render Target's own depth buffer we have to store depth (as seen from the camera) in a Render Target which we can later access. Note the image above is a visualization of depth as seen from the camera.
The
Shadow Buffer stores the depth data as seen by a light. To properly determine which areas of the scene are in shadow we render the entire scene from the perspective of the light. Using this depth map we can test against our G Buffer to determine if an area is shadowed.
The
Shading Buffer stores the shading data for our scene. For each light in our scene we can use the G Buffer to quickly determine how much each area of the scene is illuminated, we store this here and exclude areas that are shadowed. I love the look of the shading buffer and sometimes like to use it in the final output for various effects.
The
Combined Buffer stores the combination of the other buffers. We use all of the information we've stored in the previous buffers to generate the final rendered scene here. This buffer can be displayed straight to the screen but in my renderer I have a number of Post Processing steps.
The final output of the scene. Here I have applied a simple bloom post process shader to the contents of the Combined Buffer. This enhances some of the brighter areas which exaggerates the lit areas. Other available post processing shaders I use are Depth of Field, Motion Blur, Hue Shift, Distortion and more.
The process of getting from our Diffuse Buffer to the final output is fairly complex especially when using multiple types of light and casting shadows. In the end though I think you'll agree it's worth it and our game renders pretty nicely. I look forward to writing the rest of this series and will include all the code to recreate each step as we go.
If you found this post helpful please leave a comment below:
Blam / 2011-10-27 01:06:47
Hey I was wondering if you were ever going to create/post your tutorials?
Martin Caine / 2011-10-27 20:13:49
Funny you should ask actually. I ran out of time and never managed to write up the tutorials but I have recently rewritten a chunk of my renderer so I'm in a good position to come back to this and get the series written in the next few weeks.
Mike / 2011-12-26 21:33:25
I know this is almost a year old, but would it be possible for a full length tutorial on this subject?
That screenshot looks amazing!
JBizzle / 2012-11-29 14:54:14
I would still be interested in the write up if you have the time. currently in school and trying to learn. cheers nice article.
Durli / 2012-12-11 15:56:52
Hey your result is very nice i want the tutorial please :-)