# Can't get Direct3D11 depth buffer to work

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

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

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;

D3D11_DEPTH_STENCIL_DESC dsDesc;
dsDesc.DepthEnable = true;
dsDesc.DepthFunc = D3D11_COMPARISON_LESS;
... snipped stencil properties ...
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;
}


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.

• 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/… – ErnieDingo May 3 '19 at 23:02
• 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. – AaronHolland May 4 '19 at 1:54
• 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. – Chuck Walbourn May 4 '19 at 4:35
• What blend states are you using? BTW, you might want to take a look at DirectX Tool Kit. – Chuck Walbourn May 4 '19 at 4:39
• @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? – AaronHolland May 5 '19 at 3:45