Lecture #17. Depth buffer in DX12

Computer graphics in Game development

Ivan Belyavtsev

02.12.2022

Plan

  1. Create a depth buffer resource
  2. Create a depth-stencil descriptor heap
  3. Create a depth-stencil view
  4. Setup pso to work with depth-stencil buffer
  5. Set the depth-stencil descriptor as OM target
  6. Clear the depth-stencil view [1]

Create a depth buffer resource

  • Size: same as for render target
  • Format: DXGI_FORMAT_D32_FLOAT
  • Flags: ALLOW_DEPTH_STENCIL and DENY_SHADER_RESOURCE
  • Default values: 1.f for depth, 0 for stencil
  • Heap: default
  • Initial state: DEPTH_WRITE

Create a depth-stencil descriptor heap

D3D12_DESCRIPTOR_HEAP_DESC dsv_heap_desc = {};
    dsv_heap_desc.NumDescriptors = 1;
    dsv_heap_desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
    dsv_heap_desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
    THROW_IF_FAILED(device->CreateDescriptorHeap(
        &dsv_heap_desc, IID_PPV_ARGS(&dsv_heap)));

Create a depth-stencil view

device->CreateDepthStencilView(
    depth_buffer.Get(), nullptr,
    dsv_heap->GetCPUDescriptorHandleForHeapStart());

Setup pso to work with depth-stencil buffer

pso_descriptor.DepthStencilState.DepthEnable = TRUE;
pso_descriptor.DepthStencilState.StencilEnable = FALSE;
pso_descriptor.DepthStencilState.DepthFunc = 
                    D3D12_COMPARISON_FUNC_LESS_EQUAL;
pso_descriptor.DepthStencilState.DepthWriteMask = 
                    D3D12_DEPTH_WRITE_MASK_ALL;
pso_descriptor.DSVFormat = DXGI_FORMAT_D32_FLOAT;

Set render target and clear

CD3DX12_CPU_DESCRIPTOR_HANDLE dsv_handle(
    dsv_heap->GetCPUDescriptorHandleForHeapStart());
command_list->OMSetRenderTargets(
    1, &rtv_handle, TRUE, &dsv_handle);
command_list->ClearDepthStencilView(
    dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

How to render a wireframe image

//Wireframe image
pso_descriptor.RasterizerState.FillMode = 
                        D3D12_FILL_MODE_WIREFRAME;

// Solid image (default)
pso_descriptor.RasterizerState.FillMode = 
                        D3D12_FILL_MODE_SOLID;

[1]

References

1.
Satran M. et al. Direct3D 12 programming guide [Electronic resource]. 2019. URL: https://docs.microsoft.com/en-us/windows/win32/direct3d12/directx-12-programming-guide.
// reveal.js plugins