Wednesday 21 January 2009

Deferred Shading, AA, alpha blending. DirectX 9

Soon after finishing my DX10 demo, Arseny Kapoulkine gave me an idea how to implement the same functionality using DirectX 9.

In DX9 it is impossible to create a multi-sampled G-buffer, and access separate samples independently in a pixel shader. To use multi-sampled render target as a texture, you need to resolve it, and you won't have any control over this process. So, I had to create a super-sampled G-buffer - a buffer with width and height multiplied by the factor of 2, which will give us SSAAx4.

If you remember, this technique used SampleMask to select samples in which the data of transparent objects will be saved. This time, instead of it we will use stencil buffer.
Using alpha test and a simple shader that draws a grid in a following pattern at a per-pixel step (image is magnified):
Grid shader,
we fill up the stencil buffer with values of 1, 2, 4 and 8. This step is performed only once since the grid doesn't change.

Now, using only stencil reference value, we can render surface to a single sample of our improvised super-sampled buffer. And if we ever need to render it to more than one sample, stencil mask will come in hand.

Volumetric lightning was removed from DX9 demo, because it generated too many instructions (but it still could be computed, at a cost of rendering fewer lights per pass).

Another optimization is performed, using hardware early stencil or depth tests (can be switched using 'O' key).
Before rendering the full-screen quad with a shader that computes lightning, we use a different shader that along with alpha test and stencil (or depth) buffer, masks the pixels that don't require per-sample lightning and can live fine with per-pixel computation.
Masking shader checks if the values of 4 samples in a 2x2 block are equal, and if they are (within some epsilon), a more simple shader can be run, with a little quality cost and a large performance boost.

The same trick was added to the DX10 demo, and with multi-sampled buffer there is no quality lost at all.

On GPUs where early stencil test is not available (GF6, GF7), you have to switch to early depth test.
To run the demo you need GeForce6***+ or RadeonX***+.

Download .rar with sources, 341kb.

Some screenshots:



Demo controls:
WASD\Arrow keys - movement. Mouse - camera orientation.
'P' key - turn early stencil\depth optimization on and off. (Default: on)
'G' key - turn transparent surface rendering on and off. (Default: on)
'O' key - switch between early stencil and depth tests. (Default: stencil test)

Some results (minimum is for transparent surface occupying full screen, and the maximum is when none of the transparent surfaces are seen).
7600 GS, GT: 2-4fps
8600 GT: 2-55 fps
9600 GT: 10-200 fps

X1950: 36-91 fps
HD 2600: 25-70 fps
HD 4850: 110-232 fps

On NVidia hardware, the shader with per-sample frequency light computation works much longer while having only 4 times more instructions than the simple one. I expected a maximum of 4 times slowdown. The reason for this is still unknown. If the fps drops dramatically, it is recommended to turn off transparent surface rendering leaving only Deferred Shading and Antialiasing on.

4 comments:

xop said...

Very strange performance. I did implement 4x SSAA with the deferred shading once, and without any special tricks it ran at ~50 FPS on GeForce 8600MGT. At the time there was only one light source (sun) and _lots_ of geometry with simulated alpha-to-coverage (forest along railway).

NULL_PTR said...

Well, you had only one light source, while here you can see 16 (they all are being calculated for every pixel, without optimizations).

Maybe there is something that I've messed up, but I still have no idea what it might be.

Unknown said...

With DX9, would it be possible to do alpha but no AA the way you do it without g_buffer being twice the size of the resolution?

NULL_PTR said...

No, supersampled G-buffer is required.
There is a method using deep buffer: http://humus.name/index.php?page=3D&ID=75 which requires 2 or more slices of G-buffer, and if you use 2 or 3, it'll consume less memory than 2x2 supersampled G-buffer.