[SlimDX] Direct3d10 - Depth buffer only works backwards...

Started by
1 comment, last by AaronAsh 12 years, 10 months ago
So the problem is pretty much as advertised: When I create a DepthStencilState with DepthComparison = Comparison.Greater, it works as it should; primitives in the back are consistently drawn over primitives in the foreground. Simply changing the comparison to Comparison.Less draws nothing.

I've been through PIX, and when set to Greater I can see that the depth buffer is being filled as expected, farther objects become darker. When it's set to Less the buffer stays all white.
I can't see that anything is being drawn into the stencil buffer in either case.

My projection matrix has a near plane of 1.0f and a far plane of 50.0f. The devices viewport has a minZ of 0.0f and a maxZ of 1.0f. I've ensured that both are properly set onto the device at render.

I'm also setting the RasterizerState and DepthStencilState at the beginning of every frame. I've disabled culling deliberately, and it has no effect either way.

Here are the states I'm setting:


_defaultRasterizerState = SlimDX.Direct3D10.RasterizerState.FromDescription(context10.Device, new RasterizerStateDescription()
{
CullMode = CullMode.None,
FillMode = FillMode.Solid,
DepthBias = 0,
DepthBiasClamp = 0,
SlopeScaledDepthBias = 0.0f,
IsFrontCounterclockwise = false,
IsDepthClipEnabled = true,
IsScissorEnabled = false,
IsMultisampleEnabled = false,
IsAntialiasedLineEnabled = false
});
_defaultDepthStencilState = DepthStencilState.FromDescription(context10.Device,
new DepthStencilStateDescription()
{
// Depth test parameters
IsDepthEnabled = true,
DepthWriteMask = SlimDX.Direct3D10.DepthWriteMask.All,
DepthComparison = Comparison.Less,

// Stencil test parameters
IsStencilEnabled = true,
StencilReadMask = 0xFF,
StencilWriteMask = 0xFF,

// Stencil operations if pixel is front-facing
FrontFace = new DepthStencilOperationDescription()
{
FailOperation = StencilOperation.Keep,
DepthFailOperation = StencilOperation.Increment,
PassOperation = StencilOperation.Keep,
Comparison = SlimDX.Direct3D10.Comparison.Always
},

// Stencil operations if pixel if back-facing
BackFace = new DepthStencilOperationDescription()
{
FailOperation = StencilOperation.Keep,
DepthFailOperation = StencilOperation.Increment,
PassOperation = StencilOperation.Keep,
Comparison = SlimDX.Direct3D10.Comparison.Always
}
});



And here's where I create my RenderTargetView and DepthStencilView



using (var backBuffer = Texture2D.FromSwapChain<Texture2D>(Context10.SwapChain, 0))
{
var depthStencilDesc = new Texture2DDescription()
{
Width = backBuffer.Description.Width,
Height = backBuffer.Description.Height,
MipLevels = 1,
ArraySize = 1,
Format = SlimDX.DXGI.Format.D24_UNorm_S8_UInt,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = SlimDX.Direct3D10.BindFlags.DepthStencil,
CpuAccessFlags = SlimDX.Direct3D10.CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None
};

using (var depthStencilBuffer = new Texture2D(Context10.Device, depthStencilDesc))
DepthView = new DepthStencilView(Context10.Device, depthStencilBuffer,
new DepthStencilViewDescription()
{
Format = SlimDX.DXGI.Format.D24_UNorm_S8_UInt,
FirstArraySlice = 0,
ArraySize = 1,
MipSlice = 0,
Dimension = DepthStencilViewDimension.Texture2D
});

RenderView = new RenderTargetView(Context10.Device, backBuffer);
}

Context10.Device.OutputMerger.SetTargets(DepthView, RenderView);



Would greatly appreciate any suggestions at all, admittedly still learning Direct3d10, but I'm about out of ideas, and I've been through pages of google search results without much help.
Advertisement
How are you creating your projection matrix? It sounds like the resulting z/w in your depth buffer is backwards...typically it will be 0 at the near plane and 1 at the far plane, which means in PIX further objects should be whiter and not darker as you described.
Here's where the projection matrix is set:

Projection = Matrix.PerspectiveFovLH(FieldOfView, (float)Viewport.Width / (float)Viewport.Height, 1.0f, 50.0f);


and here's the viewport:

MainViewport = new Viewport(0, 0, WindowWidth, WindowHeight, 0.0f, 1.0f);


Bear in mind that when it's drawing farther objects darker, it's when I've set DepthComparison = Comparison.Greater. If I set it to Comparison.Less, the depth buffer remains all white, and nothing gets drawn to the screen.

Edit: Okay, I made it work by reversing my near and far planes in the projection matrix, making it now look like:

Projection = Matrix.PerspectiveFovLH(FieldOfView, (float)Viewport.Width / (float)Viewport.Height, 50.0f, 1.0f);


With that as the projection, and the depth comparison set to Greater, it gives appropriate results. So logically, if I set the projection back the way it was, and set the comparison to Less, it should also work... but it doesn't. I'd really love to know what's going on here, besides black-magic and witchcraft.

This topic is closed to new replies.

Advertisement