I'm trying to use stencil buffer in directx 12. I have problem that probably is really easy to solve. So, it's look like dx12 ignore it, ClearDepthStencilView() affect only depth, writing to stencil doesn't work.


static D3D12_DEPTH_STENCIL_DESC const defaultDepthStencil =
    /*DepthEnable*/                         TRUE
    /*DepthWriteMask*/                      ,D3D12_DEPTH_WRITE_MASK_ALL
    /*DepthFunc*/                           ,D3D12_COMPARISON_FUNC_LESS
    /*StencilEnable*/                       ,TRUE
    /*StencilReadMask*/                     ,D3D12_DEFAULT_STENCIL_READ_MASK
    /*StencilWriteMask*/                    ,D3D12_DEFAULT_STENCIL_WRITE_MASK
    /*FrontFace.StencilFailOp*/             ,{ D3D12_STENCIL_OP_REPLACE
    /*FrontFace.StencilDepthFailOp*/        ,D3D12_STENCIL_OP_REPLACE
    /*FrontFace.StencilPassOp*/             ,D3D12_STENCIL_OP_REPLACE
    /*FrontFace.StencilFunc*/               ,D3D12_COMPARISON_FUNC_ALWAYS }
    /*BackFace.StencilFailOp*/              ,{ D3D12_STENCIL_OP_REPLACE
    /*BackFace.StencilDepthFailOp*/         ,D3D12_STENCIL_OP_REPLACE
    /*BackFace.StencilPassOp*/              ,D3D12_STENCIL_OP_REPLACE
    /*BackFace.StencilFunc*/                ,D3D12_COMPARISON_FUNC_ALWAYS }

DSV heap desc:

D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};
    dsvHeapDesc.NumDescriptors = 1;
    dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
    dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;

DSV desc:

D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {};
    depthStencilDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
    depthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
    depthStencilDesc.Flags = D3D12_DSV_FLAG_NONE;

Resource desc:

m_gBuffer[GB_Depth].Dimension( D3D12_RESOURCE_DIMENSION_TEXTURE2D );
m_gBuffer[GB_Depth].Width( m_width );
m_gBuffer[GB_Depth].Height( m_height );
m_gBuffer[GB_Depth].DepthOrArraySize( 1 );
m_gBuffer[GB_Depth].MipLevels( 1 );
m_gBuffer[GB_Depth].Format( DXGI_FORMAT_R24G8_TYPELESS );
m_gBuffer[GB_Depth].SampleCount( 1 );
m_gBuffer[GB_Depth].Layout( D3D12_TEXTURE_LAYOUT_UNKNOWN );

Then OMSetRenderTargets() and ClearDepthStencilView() with flags D3D12_CLEAR_FLAG_DEPTH, D3D12_CLEAR_FLAG_STENCIL.

Any idea what's missing?

  • \$\begingroup\$ Welcome to stack exchange! When an issue is 'solved' the question owner is expected to mark the accepted answer as the one 'Accepted'; there should be a checkmark below the answer score to allow you to accept the answer. I reverted your edit because of that; we don't change the title to '[SOLVED]'. \$\endgroup\$
    – Vaillancourt
    Apr 12, 2016 at 20:42

1 Answer 1


Ok, I found what was wrong. Nsight have problem with stencil buffer on dx12. Every thing works, only Nsight doesn't show correct data for depth/stencil buffer.


You must log in to answer this question.

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