Single-pass midpoint shadow maps May 10th, 2011
Here's a little idea I had today. I haven't seen this posted anywhere else so I thought I'd note it down.
How to generate a midpoint shadow map in one pass:
- Create a shadowmap render target with two channels, which supports blending (e.g. D3DFMT_G16R16F). No z-buffer is needed here.
- Clear your shadowmap texture to the far clip (in both channels).
- Turn off z-writes and z-tests.
- Set the framebuffer blend mode to 'MIN'.
- Turn off backface culling.
- Render your model.
The trick is, in the shader, simply check the VFACE semantic and based on the result,
r=depth, g=+inf or
r=+inf, g=depth out (where +inf is the far-clip value).
There's probably other ways too to work out if it's a backfacing triangle instead of VFACE.
To test against this, when you read the shadowmap simply average the R and G channels to obtain the midpoint depth. You can run a pass to do this first if you like, or you can do it as you sample the texture.
I do not claim that this will stop shadowmaps from making you cry yourself to sleep at night.
Written by Richard Mitton,
software engineer and travelling wizard.
Follow me on twitter: http://twitter.com/grumpygiant