1
\$\begingroup\$

I can't get the depth buffer to work correctly. I am rendering 2 cubes in a single Draw function, and from one angle it looks great Good

But swing the camera around to view the opposite sides, and I discover it's just Painter's Algorithm.

Bad

This is my code to setup the depth stencil buffer:

void Graphics::CreateDepthStencilBuffer(ID3D11Texture2D* backBuffer)
{
    D3D11_TEXTURE2D_DESC dsTextureDesc;
    backBuffer->GetDesc(&dsTextureDesc);
    dsTextureDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    dsTextureDesc.Usage = D3D11_USAGE_DEFAULT;
    dsTextureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
    dsTextureDesc.MipLevels = 1;
    dsTextureDesc.ArraySize = 1;
    dsTextureDesc.CPUAccessFlags = 0;
    dsTextureDesc.MiscFlags = 0;
    Microsoft::WRL::ComPtr<ID3D11Texture2D> dsBuffer;
    ASSERT_SUCCEEDED(g_Device->CreateTexture2D(&dsTextureDesc, NULL, dsBuffer.ReleaseAndGetAddressOf()));

    D3D11_DEPTH_STENCIL_DESC dsDesc;
    dsDesc.DepthEnable = true;
    dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
    dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
    ... snipped stencil properties ...
    ASSERT_SUCCEEDED(g_Device->CreateDepthStencilState(&dsDesc, g_DepthStencilState.GetAddressOf()));
    g_pDevCon->OMSetDepthStencilState(g_DepthStencilState.Get(), 1);

    D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
    depthStencilViewDesc.Format = dsTextureDesc.Format;
    depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
    depthStencilViewDesc.Texture2D.MipSlice = 0;
    ASSERT_SUCCEEDED(g_Device->CreateDepthStencilView(dsBuffer.Get(), &depthStencilViewDesc, g_depthStencilView.GetAddressOf()));
}

And I'm calling g_pDevCon->OMSetRenderTargets(1, g_renderTargetView.GetAddressOf(), g_depthStencilView.Get()); after every Present call.

I've been scratching my head for ages wondering what the problem is. Any clues will be much appreciated!

Edit:
I used the graphics debugger, and apparently the output merger is doing its job, as seen in the screenshot below, but that isn't what I am seeing on screen. At the top of the pic is the state of the depth buffer, but I can't make sense of it to determine if it's correct or not. debugger

\$\endgroup\$
5
  • \$\begingroup\$ Can't see anything obvious, I use R32_Typeless for the buffer and D32_float for the view description. I assume you have set the depth stencil state prior to render? As per -> docs.microsoft.com/en-us/windows/desktop/api/d3d11/… \$\endgroup\$
    – ErnieDingo
    May 3, 2019 at 23:02
  • \$\begingroup\$ I'm only setting it once, but setting again after every Present call doesn't help either. Changing the formats also hasn't fixed the problem. Thanks for the suggestions. \$\endgroup\$ May 4, 2019 at 1:54
  • \$\begingroup\$ There are a lot of things that can go wrong since you have to get all the states right. Have you enabled the Debug device and looked for output? You might want to see how I set up basic device & swapchains in these templates. \$\endgroup\$ May 4, 2019 at 4:35
  • \$\begingroup\$ What blend states are you using? BTW, you might want to take a look at DirectX Tool Kit. \$\endgroup\$ May 4, 2019 at 4:39
  • \$\begingroup\$ @ChuckWalbourn Yes, I have device debug on, and only ever see output when exiting the app, with messages about "live resources". I'm not using any blend states. I'll change my code to mimic your templates and see how I go. Just a question on a bit of a tangent though: when I'm inspecting the depth buffer contents, each pixel is either 0 or 1. Shouldn't I be seeing varying values for where the cubes are? \$\endgroup\$ May 5, 2019 at 3:45

1 Answer 1

0
\$\begingroup\$

Well, it turns out I committed the cardinal sin of setting the near Z to zero. (I didn't know this was problem at the time) This resulted in my Z values being the same as the W values, and everything became 1 in the depth buffer.

In general, it pays to push out the near Z as far as is practical. It's highly unlikely you will ever need it to be zero.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .