Sign in to follow this  

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

This topic is 2395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 [b][u]over[/u][/b] 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:
[code]

_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
}
});
[/code]


And here's where I create my RenderTargetView and DepthStencilView
[code]


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);
[/code]


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Here's where the projection matrix is set:
[code]
Projection = Matrix.PerspectiveFovLH(FieldOfView, (float)Viewport.Width / (float)Viewport.Height, 1.0f, 50.0f);
[/code]

and here's the viewport:
[code]
MainViewport = new Viewport(0, 0, WindowWidth, WindowHeight, 0.0f, 1.0f);
[/code]

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:
[code]
Projection = Matrix.PerspectiveFovLH(FieldOfView, (float)Viewport.Width / (float)Viewport.Height, 50.0f, 1.0f);
[/code]

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.

Share this post


Link to post
Share on other sites

This topic is 2395 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this