# Unexpected results with D3D9 depth bias

I've run in to some problems with the D3DRS_DEPTHBIAS render state after converting an older game's D3D8 renderer to D3D9.

D3D9 replaced the old (and poorly supported) D3DRS_ZBIAS with D3DRS_DEPTHBIAS, but it doesn't seem possible to get it to work the same way that ZBIAS did. This game renders shadows and other "decals" onto the ground as flat textures, and to avoid the obvious z-fighting issues that would result would set D3DRS_ZBIAS to 2 when rendering decals in the D3D8 renderer (which I did not write).

The best approximation of the old D3D8 behavior that I've been able to come up with is setting D3DRS_DEPTHBIAS to -0.00003 when rendering shadows. This works fine for shadows on flat terrain that are relatively close to the camera, but when the camera is far above a shadowed object, the shadow will draw on top of the object itself. This can also happen even on near objects if they are on uneven terrain.

It doesn't seem possible to solve this by manipulating the DEPTHBIAS value alone, as no change fixes it entirely without causing other problems.

Is there any way to solve this? Failing that, is there a more modern technique for rendering decals that could be used? I would much rather avoid lots of rewriting as I'm not very familiar with D3D and didn't write any of the original code, but if that is the only option...

Try using D3DRS_SLOPESCALEDEPTHBIAS in addition to D3DRS_DEPTHBIAS. A typical value would be -1.0 for the slope-scaled depth bias, and some very small negative value for the constant depth bias. See the MSDN for a little more information on these flags. The slope-scaled depth bias automatically scales the amount of bias based on the polygon's orientation with respect to the screen; this allows you to use a smaller constant bias, which hopefully will avoid causing the problem you mentioned of objects appearing behind their own shadows when too far away.