I’m new to DX12 and I’m at present making an attempt to get the depth buffer to work. I’ve finished just about all the things an identical to weblog posts I’ve seen on-line. I may also see that the depthbuffer-texture
is there in RenderDoc, it is simply utterly white:
Additionally, the depth goal is in D3D12_RESOURCE_STATE_DEPTH_WRITE
state. I’ve set all of the settings in my pipeline so it helps depth testing, matched the codecs and all the things.
Pipeline setup:
psoDesc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
psoDesc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
psoDesc.DSVFormat = DXGI_FORMAT_D32_FLOAT;
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
psoDesc.SampleDesc.Depend = 1;
ThrowIfFailed(_device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&_pipelineState)));
DepthBuffer-Useful resource:
// Create the DSV Heap
D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {};
dsvHeapDesc.NumDescriptors = 1;
dsvHeapDesc.Kind = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
ThrowIfFailed(_device->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&_dsvHeap)));
// Heap properties for creating the feel (GPU learn/write)
D3D12_HEAP_PROPERTIES heapProps = {};
heapProps.Kind = D3D12_HEAP_TYPE_DEFAULT;
heapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
heapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
heapProps.CreationNodeMask = 1;
heapProps.VisibleNodeMask = 1;
// Create Depth-Stencil Useful resource (Texture2D)
D3D12_RESOURCE_DESC depthResourceDesc = {};
depthResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
depthResourceDesc.Alignment = 0;
depthResourceDesc.Width = _width; // Width of the feel
depthResourceDesc.Peak = _height; // Peak of the feel
depthResourceDesc.DepthOrArraySize = 1;
depthResourceDesc.MipLevels = 1;
depthResourceDesc.Format = DXGI_FORMAT_D32_FLOAT; // Depth format (may be D24_UNORM_S8_UINT)
depthResourceDesc.SampleDesc.Depend = 1;
depthResourceDesc.SampleDesc.High quality = 0;
depthResourceDesc.Format = D3D12_TEXTURE_LAYOUT_UNKNOWN;
depthResourceDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
D3D12_CLEAR_VALUE depthOptimizedClearValue = {};
depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
depthOptimizedClearValue.DepthStencil.Depth = 1.0f; // Default clear depth
depthOptimizedClearValue.DepthStencil.Stencil = 0; // Default clear stencil
ThrowIfFailed(_device->CreateCommittedResource(
&heapProps,
D3D12_HEAP_FLAG_NONE,
&depthResourceDesc,
D3D12_RESOURCE_STATE_DEPTH_WRITE,
&depthOptimizedClearValue,
IID_PPV_ARGS(&_depthStencilBuffer)
));
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
dsvDesc.Format = DXGI_FORMAT_D32_FLOAT;
dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Flags = D3D12_DSV_FLAG_NONE;
// Create the DSV for the depth-stencil buffer
_device->CreateDepthStencilView(_depthStencilBuffer.Get(), &dsvDesc, _dsvHeap->GetCPUDescriptorHandleForHeapStart());
And lastly binding:
D3D12_CPU_DESCRIPTOR_HANDLE rtvHandle = _rtvHeap->GetCPUDescriptorHandleForHeapStart();
rtvHandle.ptr += (_frameIndex * _rtvDescriptorSize);
D3D12_CPU_DESCRIPTOR_HANDLE dsvHandle = _dsvHeap->GetCPUDescriptorHandleForHeapStart();
_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, &dsvHandle);
_commandList->ClearDepthStencilView(dsvHandle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0.0f, 0, nullptr);
I do not perceive why it is not working — assist can be very a lot appreciated. I do not need to spam this thread with code (because it’s various boilerplate) so if you wish to take a look at the entire code you’ll be able to test it out right here: https://github.com/eliasfuericht/artisDX/blob/important/src/Software.cpp