Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

DX11 Depth-stencil buffer not working

This topic is 2751 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

I'm trying to setup drawing of models in my engine. But my depth buffer won't let them draw correctly. I have this code to setup my depth buffer:

void CDX11Driver::initDepthStencil(float width, float height)
//Depth stencil buffer.
D3D11_TEXTURE2D_DESC depthStencilDesc;
depthStencilDesc.Width = width;
depthStencilDesc.Height = height;
depthStencilDesc.MipLevels = 1;
depthStencilDesc.ArraySize = 1;
depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilDesc.SampleDesc.Count = 4;
depthStencilDesc.Usage = D3D11_USAGE_DEFAULT;
depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthStencilDesc.CPUAccessFlags = 0;
depthStencilDesc.MiscFlags = 0;
m_d3dDevice->CreateTexture2D(&depthStencilDesc, NULL, &m_depthStencilBuffer);
m_d3dDevice->CreateDepthStencilView(m_depthStencilBuffer, NULL, &m_depthStencilView);
//Depth stencil state.
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing.
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;
// Stencil operations if pixel is back-facing.
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;
m_d3dDevice->CreateDepthStencilState(&depthStencilDesc, &m_depthStencilState);
m_deviceContext->OMSetDepthStencilState(m_depthStencilState, 1);

Before I draw, I make sure to clear the depth buffer and back buffer.
I draw meshes with this code:

void CStaticMesh::draw()
for ( unsigned int i=0; i<m_buffers.size(); ++i )
if ( m_buffers )
void CDX11Driver::drawMeshBuffer(IMeshBuffer* mb, bool ignoreMaterial)
CDX11MeshBuffer* dxMb = (CDX11MeshBuffer*)mb;
m_deviceContext->IASetIndexBuffer(dxMb->m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
m_deviceContext->IASetVertexBuffers( 0, 1, &dxMb->m_vertexBuffer, &dxMb->m_stride, &dxMb->m_offset );
if ( !ignoreMaterial )
m_deviceContext->DrawIndexed(dxMb->getNumIndices(), 0, 0);

I end up with the render in the two pictures I attached. As you can see, his shoulder pads don't render over him, and his beard doesn't render in front either. His arms and legs also shows through. There's only two mesh buffers, the dwarf itself, and the axe. That's why I'm pretty sure it's a problem with the depth buffer.

Share this post

Link to post
Share on other sites
I added error checking, and found that the call to CreateTexture2D for creating my depth-stencil buffer is failing. What could be causing it to fail? It's returning a value of E_INVALIDARG.

Share this post

Link to post
Share on other sites
Use the DEBUG flag when creating the device, and you'll get detailed error messages in the debug output window.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!