Disqus Comments Added

Just letting everyone know I added Disqus commenting to this site, so if you have a profile with them, use that, otherwise they offer many other ways to login via Facebook, Twitter, Yahoo etc.

This adds some more commenting features, and should make the experience better overall. All previous comments should have imported correctly into Disqus.

Happy New Year

Happy New Year everyone, hope you all have a great 2010.

I for one know that my new years resolution will be to get more content up. Once I am better of course, I caught a cold + ear infection at #ozgamecamp and haven’t been in the mood to do much development or writing over the past two weeks. Expect content as that clears up (It is slowly going away) and things get started again for 2010.

Enjoy!

Rewrite of LPP Article Planned

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 ;) )

Get Great Benefits from the MSP Program

headerLogo Just a quick break from the regular programming (haha) to let every reader of this blog know that the Microsoft Student Partner program is open for signups.

If you are a student in tertiary education (University etc) then you can apply for the role by going to http://student-partners.com and following the instructions there.

Benefits vary per country, however the common one seems to be that you get a free MSDN Universal subscription, which gives you access to every piece of MS software you could want. (As long as it relates to development)

I have been a student partner for 2 years now, and I am really enjoying the experience. you meet like minded students from your country and get into some really awesome and at times exclusive events. Here in Australia we get free access to TechEd AU, and even get into exclusive XBOX parties. One big event of note this year was the local Windows 7 launch, which many MSPs from Sydney attended.

Tons of benefits, and plenty of reasons to join, so why don’t you?

Important Additions & Fixes to LPP Implementation

I have finally tracked down an issue that I was having with the Light Pre Pass implementation, and as a result have had to make some changes to the implementation.

Firstly, I would recommend ensuring RenderState.AlphaTestEnable is false before rendering materials, it appears this is set (at least in my sample) somewhere and it corrupts the light buffer.

Secondly, move the postProjToScreen and halfPixel calls from the vertex shader in blinnphong.fx, to the pixel shader. Some GPUs (like my laptop’s) do not correctly interpolate the adjusted screen coordinates, and this ensures it is correctly interpolated per pixel.

gfx.SetRenderTarget(0, light);
gfx.SetRenderTarget(1, null);
depthImage = depth.GetTexture();
normImage = normals.GetTexture();
gfx.RenderState.AlphaTestEnable = false;

Finally, when writing the Normal in the vertex shader for def_depthnorm.fx, swap the parameters of mul(input.Normal, World) to mul(World, input.Normal).

That should be all, if you notice any other issues, feel free to post them in the comments, it would be greatly appreciated.

A Note on Sample Code

Just a quick note about the sample code download for the current Light Pre Pass tutorial, and probably future tutorials. I am in the process of cleaning up and commenting the code, and hope to have it out really soon. I am to have a complete tutorial with all the information on the page, however in some cases I don’t explicitly provide every bit of code, to prevent the tutorial from becoming too long.

As this implementation and sample is a part of my own engine, I need to split the code from the main project and ensure it is readable and in a suitable educational state. This should not take too long, so if you are watching this blog, I will post when this code is available, and link to it in the original article as well.

Sorry for any inconvenience, I hope the article will satisfy your LPP needs in the meanwhile. As usual if you have any questions, post them in the comments of the respective article and I will answer them as soon as possible.

Light Pre Pass in XNA: Basic Implementation

NOTE: This article is now obsolete. An up-to-date sample and article can be found at http://mquandt.com/blog/2010/03/light-pre-pass-round-2/

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.

Continue reading

Light Pre-Pass in XNA [Part 1]

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.

SQL Azure – First Impressions

Recently I decided to embark on a personal project I had on my mind for a while now. My intention has been to write a personal finance app that would manage things like invoices, timesheets, and expenses, and allow me to easily generate the reports and keep track of the information I need.

Previously I have been keeping track of this in a spreadsheet stored on Live Mesh, but recently I have had a need to store and present the data in a much more intuitive form, and allow for greater connections, ie Invoices can be easily marked as paid without having to manually add the value to my current balance when it happens.

Since I have a need to be able to access this information from any computer, in at least two physically separate locations, I need to keep the system in the cloud somewhere, with easy access with an internet connection, and as a bonus if possible, be able to edit whilst offline and sync when connection is restored.

Since my webserver is an apache host, I found that SQL Azure would be perfect for me, at least for now. (ie. Until the pricing comes into effect and I can decide if $9.95 per month is worth it for my needs.

It is quite easy to get going with SQL Azure, you just need an invite code (register and you should get one really quickly, I believe there is no more waiting list anymore) and a Windows Live ID. Once you enter your code, you are provisioned with a server which you can add databases to (up to 5 for the CTP) and use just like you would a MSSQL 2008 instance.

Now here is where I encountered some issues. Normally I would set the server up in the VS2008 server explorer and begin creating the database using the tools there, however unfortunately since SQL Data Services (SDS) does not support the entire set of features that MSSQL supports, this simply gives me an error, and further reading reveals I need to use the sqlcmd tool, write my own app, or with some configuration I can use the management tool that comes with MSSQL 2008. For reference, sqlcmd also only comes with MSSQL 2008, and so it just depends how comfortable you are with T-SQL, as sqlcmd will work out of the box with SDS, whilst the management tool will not.

Once you have your details (Get them from the connection string) you can login to your SDS database using sqlcmd and begin issuing SQL statements to your database. This is where I begin to setup my tables, (of course I pre-plan them) and then its just a matter of using the database as you normally would in your application, either the long manual way, using LINQ to SQL, using the ADO.Net Entity Framework, or any of the other ORM libraries/tools out there.

This is probably the easiest of the Azure services to get started with, and quite powerful for connected desktop applications.

I plan to write more about the app I am writing, since it is also a learning experience for me when it comes to the in depth aspects of WPF, and also database access using LINQ2SQL or the Entity Framework. (I am undecided over which to use)

If anyone knows of any better tools to manage the SDS databases, especially tools that don’t require me to install MSSQL2008, then please let me know in the comments.