To the best of my knowledge I have the depth testing set up correctly however objects are just drawn first to last. I’ve looked in the graphics debugger and it appears that when a mesh is drawn it sets the depth buffer to 0 no matter its distance.
D3D11_TEXTURE2D_DESC depthBufferDesc;
d3d11FrameBuffer->GetDesc(&depthBufferDesc);
d3d11FrameBuffer->Release();
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthBufferDesc.Width = 1920;
depthBufferDesc.Height = 1080;
depthBufferDesc.MipLevels = 1;
depthBufferDesc.ArraySize = 1;
depthBufferDesc.SampleDesc.Count = 1;
depthBufferDesc.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;
ID3D11Texture2D* depthBuffer;
HRESULT res = d3d11Device->CreateTexture2D(&depthBufferDesc, nullptr, &depthBuffer);
D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
res = d3d11Device->CreateDepthStencilView(depthBuffer, nullptr, depthBufferView);
ID3D11DepthStencilState* depthStencilState;
{
D3D11_DEPTH_STENCIL_DESC depthStencilDesc = {};
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = false;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
d3d11Device->CreateDepthStencilState(&depthStencilDesc, &depthStencilState);
}
d3d11DeviceContext->RSSetState(rasterizerState);
d3d11DeviceContext->OMSetDepthStencilState(depthStencilState, 1);
d3d11DeviceContext->OMSetRenderTargets(1, &d3d11FrameBufferView, depthBufferView);
I’ve tried changing the various parameters and moving the location of some of the code however that has not worked. I couldn’t find any other question that seemed to work for this.
I have the depth clip disabled on the rasterizer as when I enable it nothing is rendered though as far as I know it shouldn’t have any effect on this.
1
Why do you have:
depthStencilDesc.StencilEnable = false;
This must be set to true
.
1