Reputation: 359
I want to copy the contents of the depth buffer back to the CPU to be able to read them.
I create the depth buffer (view) the following way:
// Create the depth stencil view.
{
D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {};
depthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
depthStencilDesc.Flags = D3D12_DSV_FLAG_NONE;
D3D12_CLEAR_VALUE depthOptimizedClearValue = {};
depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
depthOptimizedClearValue.DepthStencil.Depth = 0.0f;
depthOptimizedClearValue.DepthStencil.Stencil = 0;
ThrowIfFailed(m_device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT, m_width, m_height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL),
D3D12_RESOURCE_STATE_DEPTH_WRITE,
&depthOptimizedClearValue,
IID_PPV_ARGS(&m_depthStencil)
));
NAME_D3D12_OBJECT(m_depthStencil);
m_device->CreateDepthStencilView(m_depthStencil.Get(), &depthStencilDesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
}
For the pipeline I use the following description for the depth buffer:
D3D12_DEPTH_STENCIL_DESC depthDesc;
ZeroMemory(&depthDesc, sizeof(D3D12_DEPTH_STENCIL_DESC));
depthDesc.DepthEnable = TRUE;
depthDesc.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
depthDesc.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
depthDesc.StencilEnable = FALSE;
So far everything works, with NSight I can see that after the renderpass is finished the correct values are stored in the depth buffer.
Now I want to read back the values to the CPU, where I encounter the problem, if I use: D3D12_HEAP_PROPERTIES readbackHeapProperties{ CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK) };
the heap type readback I cannot allocate a buffer with the same description as the depth buffer. But if I use CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT)
I am not able to "map" the memory later (according to the microsoft documentation https://learn.microsoft.com/en-us/windows/win32/direct3d12/readback-data-using-heaps)
The rest of the copying I'd do analogue to the documented example:
D3D12_RESOURCE_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc = m_depthStencil->GetDesc();
//D3D12_HEAP_PROPERTIES readbackHeapProperties{ CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_READBACK) };
//D3D12_RESOURCE_DESC readbackBufferDesc{ CD3DX12_RESOURCE_DESC::Buffer(500 * 500) };
D3D12_RESOURCE_DESC readbackBufferDesc = m_depthStencil->GetDesc();
ComPtr<ID3D12Resource> readbackBuffer;
ThrowIfFailed(m_device->CreateCommittedResource(
//&readbackHeapProperties,
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
D3D12_HEAP_FLAG_NONE,
&readbackBufferDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
__uuidof(readbackBuffer),
&readbackBuffer));
Bringing the depth buffer in into the correct state.
{
D3D12_RESOURCE_BARRIER outputBufferResourceBarrier
{
CD3DX12_RESOURCE_BARRIER::Transition(
m_depthStencil.Get(),
D3D12_RESOURCE_STATE_DEPTH_WRITE,
D3D12_RESOURCE_STATE_COPY_SOURCE)
};
m_commandList->ResourceBarrier(1, &outputBufferResourceBarrier);
}
m_commandList->CopyResource(readbackBuffer.Get(), m_depthStencil.Get());
ThrowIfFailed(m_commandList->Close());
ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
D3D12_RANGE readbackBufferRange{ 0, 500 * 500 }; // outputbuffer size = 500x500 = window size?
FLOAT* pReadbackBufferData{};
ThrowIfFailed(
readbackBuffer->Map
(
0,
&readbackBufferRange,
reinterpret_cast<void**>(&pReadbackBufferData)
)
);
D3D12_RANGE emptyRange{ 0, 0 };
readbackBuffer->Unmap
(
0,
&emptyRange
);
Transition the depth buffer to the original state.
{
D3D12_RESOURCE_BARRIER outputBufferResourceBarrier
{
CD3DX12_RESOURCE_BARRIER::Transition(
m_depthStencil.Get(),
D3D12_RESOURCE_STATE_COPY_SOURCE,
D3D12_RESOURCE_STATE_DEPTH_WRITE)
};
m_commandList->ResourceBarrier(1, &outputBufferResourceBarrier);
}
I'd be thankful for any help!
Upvotes: 0
Views: 1472
Reputation: 41077
In DirectX 12, you don't create a 'readback' buffer of the same format as your GPU resource. Instead, you just create a 1D buffer of the same number of bytes as the source.
The 'trick' is that you need to have the same pitch as the original resource.
CD3DX12_HEAP_PROPERTIES readBackHeapProperties(D3D12_HEAP_TYPE_READBACK);
// Readback resources must be buffers
D3D12_RESOURCE_DESC bufferDesc = {};
bufferDesc.DepthOrArraySize = 1;
bufferDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
bufferDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
bufferDesc.Format = DXGI_FORMAT_UNKNOWN;
bufferDesc.Height = 1;
bufferDesc.Width = srcPitch * m_height; // <<----
bufferDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
bufferDesc.MipLevels = 1;
bufferDesc.SampleDesc.Count = 1;
The srcPitch
is obtained from the original resource:
auto depthBufferDesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT, m_width, m_height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
UINT64 totalResourceSize = 0;
UINT64 fpRowPitch = 0;
UINT fpRowCount = 0;
m_device->GetCopyableFootprints(
&depthBufferDesc,
0,
1,
0,
nullptr,
&fpRowCount,
&fpRowPitch,
&totalResourceSize);
// Round up the srcPitch to multiples of 256
UINT64 srcPitch = (fpRowPitch + 255) & ~0xFFu;
See ScreenGrab in the DirectX Tool Kit for DX12.
I also filed an edit for that Microsoft Docs page. It uses an example of a buffer reading back to a buffer, but didn't mention the important detail that the read-back resource is always a buffer.
Upvotes: 2