Archive for category directx
Light Pre-Pass Round 2
Posted by Michael in Games, Programming, directx, xna on March 11th, 2010
Those who have been following this blog know that I wrote an article about implementing a Light Pre-Pass renderer last year. Since then I have made numerous improvements and fixes as I have tried the system over different PC configurations.
This time around I will be including those changes into the implementation, and also releasing sample code for educational use. If you feel you have learned enough from the previous article, then feel free to skip this. For those new to the topic, please use this article instead of the older one.
An update on LPP + Sample
Posted by Michael in Programming, directx, xna on February 21st, 2010
Hi everyone, first of all apologies for the delay. there has been quite a lot going on in my life, but I am working hard on getting the sample done. This time around I wanted to make sure I had a sample ready to go with the article, especially since the article will focus on the technique and theory and have little to no code – although I will certainly focus on XNA when it comes to mentioning issues and benefits to certain parts.
One of the main delays was getting the sample code out of my engine, and cleaning it up so it can be used as a learning tool. Unfortunately most of the code was hacked on as I fixed issues in the LPP renderer, and added features.
I have a fair bit of the article written, however recently I was implementing shadows and realised that I never thought about how they would integrate into the system, so I decided to rewrite my own renderer (which should not take long) and at the same time keep the code clean so it can serve as a sample as well.
This means I will probably also have Directional Light shadows in the sample. This then allows me to write about point lights, and spotlights later on, and include shadows for both.
So again, sorry for the delay, I am working hard to get it out soon.
As an aside, I noticed that Game Programming Gems is getting an 8th Edition, something I was not expecting, so with that, GPU Pro, and Game Engine Gems, I might be able to find some more cool things to write about.
Thank You for your patience.
Autodesk Student Subscriptions [Freebies]
I know that many of you will want to make use of either Blender or the nice free version of Softimage (Previously XSI) on the Creators Club website, however for those interested in expanding their skill set, perhaps with the intention of entering the industry, getting access to 3ds Max or Maya can be quite hard to do.* (legitimately)
I recently found that if you are a student and have an email address with your institution (.edu) then you can get access to licenses for Max, Maya, even AutoCAD and Revit for free using the Autodesk Student subscription.
Just go to http://students.autodesk.com to see if you qualify and register.
* I am not implying that 3ds Max + Maya are the only tools the industry uses, however they are the focus of this post.
Side Note: There are way too many versions of AutoCAD.
Rewrite of LPP Article Planned
Posted by Michael in Programming, Xbox 360, directx, xna on December 12th, 2009
I recently went about making the current Light Pre Pass system run on the XBOX 360, and had to fix some issues that appeared there. As a result, the next article to go up will be a rewrite with all of these fixes included. I should also be able to provide the sample code at the same time.
This time around I will cover Point, Ambient and Directional lights in the same post, as well as including information about an alternate light buffer format that allows for improved specular + per-material specular.
Expect this either during the weekend of 20-21st Dec, or shortly after that.
Unfortunately I could not test the code on the XBOX the first time around due to subscription issues, however everything is fine now, and I can and will test on both XBOX and my desktop PC beforehand. (Perhaps also other PC configs depending on who I can find online at the time, and what other machines I have on hand
)
Light Pre Pass in XNA: Basic Implementation
Posted by Michael in Programming, Xbox 360, directx, xna on December 1st, 2009
In this part I will cover how to implement the basic form of the Light Pre Pass renderer, with support for point lights, and the basic Blinn-Phong shader, including Albedo texture support.
As this article is fairly advanced in nature, I have to make certain assumptions about my audience, so that I do not spend half my time explaining basics. Firstly, you should have an understanding of basic concepts such as Cameras, Fullscreen Quads (including how to render one) and rendering a mesh with custom effects.
This pretty much means that as long as you have done some 3D work before, you should be fine. It would be best if you also knew XNA, as I will be using that to write this implementation, however as long as you can translate from C# and get the basic idea, that should be enough.
As you can see from these requirements, this article is not aimed at beginners, and if you are looking for tutorials on how to get started with XNA for 3D development, I would recommend you visit some great sites such as:
Those sites will help you get started with XNA, and once you are familiar and comfortable with the concepts behind 3D graphics, you can return here to learn an advanced renderer implementation.
My focus in this article will be on the implementation of the renderer, as a result, I will not be referring to the implementation of cameras or scene graphs.
Now that the housekeeping is out of the way, we can begin.
Light Pre Pass Implementation Ready
As you can see, it is rendering correctly, and now I can begin writing about the implementation.
Just have to clean some code, maybe improve one or two things. I hope to begin the article series (the previous entry was an intro) after my final exam on friday.
Light Pre-Pass in XNA [Part 1]
Posted by Michael in Programming, directx, xna on November 13th, 2009
This is the first part of the article I began writing not too long ago. Unfortunately university assignments and exams halted my progress, however as I am almost done, I thought I might as well put up the first part, and then add more as I get them done once my exams are finished.
I hope you like this introduction, and you return to see the rest of the series. I also hope that this introduction will encourage you to seek out resources, linked or otherwise, on the topic so you can have a try before I post how I did it. Discussion on my implementation/views, and what you have done is welcome and encouraged, feel free to post in the comments and provide feedback.
Introduction
Recently I have found the time to get started on the engine design I’ve had in mind for a while now, and finally get started on some games I want to develop. Since my interest and focus is on 3D rather than 2D at the moment, I decided to go with a deferred renderer, however I remembered back to last year when I read a post by Wolfgang Engel about a new renderer design, called the Light Pre-Pass renderer, that took the ideas of deferred shading, and added back some of the flexibility of forward rendering, at the cost of an extra pass.
Since then, Crytek have announced they are using a similar technique in CryEngine 3, and other companies have used the technique, or a similar version in their own games, sometimes going under the name of Deferred Lighting.
For those who have implemented deferred shading before, this will be quite easy to understand, however for those who do not understand the concept of a deferred renderer and deferred shading yet, I will explain it here before I continue.
Deferred Rendering
In traditional forward rendering, each light is applied to the affected meshes when those meshes are rendered. This means that inside the material shader for the mesh, each individual light that affects the model must be processed. This has traditionally limited meshes to 3-4 lights, as the instruction counts for earlier shader models have been limiting, and even as those limits are lifted, the time needed to render these lights on every pixel of the mesh (even parts that are not lit) for each mesh, can add up to a substantial time.
Deferred rendering aims to solve this problem by rendering lights and models separately, and therefore reducing the complexity of the shaders for the meshes. The idea is that the meshes are drawn, and all important data such as position, normals, texture colour (diffuse), etc are stored in a series of render targets, taking advantage of the Multiple Render Targets (MRT) feature of modern graphics cards.
Then the lights are then drawn, either using a full screen quad for each light, or simple meshes that represent the shape of the light volume in the world. As these are drawn, the lighting calculations make use of the pre-processed data from the meshes in the other render targets, and so lights are not processed for any unnecessary pixels. This allows for tens-hundreds of lights, as lights are essentially just simple meshes.
There are downsides here however, for one thing, Direct3D9 and the XBOX 360 do not support MultiSample Anti-Aliasing (MSAA) for MRTs, and due to the fact that only one position (or depth) can be stored for each pixel, transparent objects cannot be properly rendered.
The former issue can be resolved with some post processing. A selective blur with edge detect can soften the edges enough to remove aliasing, however the transparency issue will require a second pass using forward rendering. (or more complexity if you choose to use the stippling route – something that I will not be covering)
Material diversity is also an issue, as all lighting calculations are done with a single shader. This means that a single uber-shader must be written to cover all the techniques needed, which is limiting, but if you do not have a diverse selection of materials, this should not be that much of an issue.
Deferred Lighting aka Light Pre-Pass
Deferred Shading also has the negative of having a rather fat frame-buffer during the mesh stage, as quite often 4 MRTs are used to store all the required information. Rendering each of these one by one would impart a huge cost, and so MRT support is required on the graphics card.
Light Pre-Pass attempts to solve not only this issue, but also the material issue mentioned before. It works by just rendering position/depth and normals during the mesh stage. This can be split into two passes for cards that do not have MRT support, and so therefore this will support DX8.1 cards. The cost of rendering meshes a second time at this point is much better than rendering 4x.
Then the lights are drawn into their own single render target. The standard components of the Blinn-Phong lighting calculation are stored for the light being rendered, and all lights are alpha-blended into the render target.
The lighting components stored are as follows:
- LightColour.r * N.L * Att
- LightColour.g * N.L * Att
- LightColour.b * N.L * Att
- (H.N)^SpecPower * N.L * Att
(Att = Attenuation)
As you can see, the three channels of colour for the light are stored in the rgb channels of the render target, and the specular component is stored in the alpha channel.
Modifications can be made to this to store the above in a different colour space, however that is outside the scope of this article. (For more information, check out the article on the CIE Colour Space by Pat Wilson from GarageGames in ShaderX7 – also the comments here for an interesting discussion on using Luminance and/or HSL for storing the light data)
The lights are alpha blended together, and end up as a single lighting value in a pixel in the buffer.
Once this buffer is rendered, the meshes are re-rendered, but this time we simply take the lighting data stored in the existing buffer, and apply that to the material for the mesh. Using LPP we gain the ability to have diverse material types, and each material can have its own shader again.
You no doubt see here that over forward rendering, we can have 10, 20, even 50 lights all lighting a single pixel on the mesh, and we have to draw much less.
So far you can see the following benefits to using Light Pre Pass over Deferred Shading:
- Lower cost per light due to smaller calculations in the light pass
- A greater material variety
- Less memory bandwidth usage and texture fetches – at most 2 during the light phase
- Does NOT require SM3.0 hardware for the 4x MRT support, can actually run on DX8.1 hardware with no MRT support (just split depth + normal rendering)
MSAA is something I have not covered for LPP because it technically can be done if you are fine with splitting the depth and normals rendering. If you go the same route as supporting a DX 8.1card, you then only have a single render target at a time, and MSAA can be enabled for all passes. (You can turn it off for the light stage though, not absolutely required there)
I would suggest that if MSAA is enabled, you use the split path of separate depth and normals, and when MSAA is not enabled (and the hardware supports MRTs) you do the combined rendering. A speed up during a depth only write still is not the same as only rendering the mesh once during that stage. I will not be covering this method in this article, as I want to keep it simple for newcomers. This is easy enough to add once you understand how to implement the normal form.
Implementing
Requirements
For this particular article, I am only going to implement the combined depth and normals path. This means that to properly follow along with this article, you will need a card capable of at least two simultaneous render targets. Most modern cards will do this, and the XBOX 360 will do up to 4 at a time, so that will work fine. If you have a Shader Model 3.0 card, then you are set, although anything before and you would need to check.
Other requirements include:
- Understanding of the Blinn-Phong lighting equation – This is outside the scope of this article. Basic Phong will suffice if you know the different specular calculation.
- Visual Studio 2008 (Express will do)
- XNA Game Studio 3.1 (3.0 will suffice if you only have that, but why not get 3.1? Its free)
- FX Composer or ATI RenderMonkey – or anything else that will let you write shaders, these just provide syntax highlighting
- An understanding of Vector/Matrix maths
Continue to Implementation.
Adding Direct3D 10.1 support to a D3D10 application
Since my graphics card (ATI Radeon 4870×2) supports Direct3D 10.1, I thought I might quickly add support for Direct3D 10.1 to my in progress D3D10 engine. Using MSDN docs and a slide from the 2008 GDC, I found it was really easy to add D3D10.1 support, since the device simply inherits from the D3D10 device, and since 10.1 simply adds features, you can use it selectively for some nice techniques and/or performance gains.
MSDN suggests that if you are going to be using the device a lot, and already have some place holding the D3D10 device, you keep a pointer to it, so I will assume you have such a class.
First you must create the Direct3D 10 device as you normally would, however you must use the D3DX10CreateDevice() method instead (see below), this is outlined in the SDK and multiple tutorials online. I may write up how to do this at a later point, however it is outside the scope of this little article.
Once the device has been successfully created, we can try and get a Direct3D 10.1 device from it. To do this, we will be using the following methods:
- HRESULT D3DX10CreateDevice( IDXGIAdapter*, D3D10_DRIVER_TYPE, HMODULE, UINT, ID3D10Device** ); [This will create the D3D10 device, and makes it easier to do so and then create the 10.1 device.]
- HRESULT D3DX10GetFeatureLevel1( ID3D10Device*, ID3D10Device1** ); [This will create the 10.1 device.]
The second method will take the current D3D10 device and create a 10.1 device from that if possible.
After we have our D3D10 device using D3DX10CreateDevice(), we can then call D3DX10GetFeatureLevel1, passing in the pointer to the device we just created, and a pointer-to-the-pointer to our D3D 10.1 device. This method returns a HRESULT indicating if the operation was a success.
If so, we now have a D3D10.1 device we can use for the extra functionality. I personally also keep a BOOL inside my Graphics class to allow me to easily check if I have 10.1 support.
Now you can take advantage of the new features in Direct3D 10.1, and SM4.1!