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;
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.