创建描述符堆
若要创建和配置描述符堆,必须选择描述符堆类型,确定所包含的描述符数,并设置指示 CPU 是否可见和/或着色器是否可见的标志。
描述符堆类型
堆的类型由 D3D12_DESCRIPTOR_HEAP_TYPE 枚举的一个成员决定:
typedef enum D3D12_DESCRIPTOR_HEAP_TYPE
{
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, // Constant buffer/Shader resource/Unordered access views
D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, // Samplers
D3D12_DESCRIPTOR_HEAP_TYPE_RTV, // Render target view
D3D12_DESCRIPTOR_HEAP_TYPE_DSV, // Depth stencil view
D3D12_DESCRIPTOR_HEAP_TYPE_NUM_TYPES // Simply the number of descriptor heap types
} D3D12_DESCRIPTOR_HEAP_TYPE;
描述符堆属性
堆属性在 D3D12_DESCRIPTOR_HEAP_DESC 结构上设置,该结构同时引用 D3D12_DESCRIPTOR_HEAP_TYPE 和 D3D12_DESCRIPTOR_HEAP_FLAGS 枚举。
可以选择在描述符堆上设置D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE标志,以指示它绑定到命令列表以供着色器引用。 为了方便起见,创建的描述符堆(不带此标志)允许应用程序在将描述符复制到着色器可见描述符堆之前将其暂存到 CPU 内存中。 但是,应用程序也可以直接将描述符创建到着色器可见描述符堆中,而无需将任何描述符暂存到 CPU 上。
此标志仅适用于 CBV、SRV、UAV 和取样器。 它不适用于其他描述符堆类型,因为着色器不会直接引用其他类型。
例如,描述和创建取样器描述符堆。
// Describe and create a sampler descriptor heap.
D3D12_DESCRIPTOR_HEAP_DESC samplerHeapDesc = {};
samplerHeapDesc.NumDescriptors = 1;
samplerHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
samplerHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
ThrowIfFailed(m_device->CreateDescriptorHeap(&samplerHeapDesc, IID_PPV_ARGS(&m_samplerHeap)));
描述和创建常量缓冲区视图 (CBV)、着色器资源视图 (SRV) 和无序访问视图 (UAV) 描述符堆。
// Describe and create a shader resource view (SRV) and unordered
// access view (UAV) descriptor heap.
D3D12_DESCRIPTOR_HEAP_DESC srvUavHeapDesc = {};
srvUavHeapDesc.NumDescriptors = DescriptorCount;
srvUavHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
srvUavHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
ThrowIfFailed(m_device->CreateDescriptorHeap(&srvUavHeapDesc, IID_PPV_ARGS(&m_srvUavHeap)));
m_rtvDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
m_srvUavDescriptorSize = m_device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
描述符句柄
D3D12_GPU_DESCRIPTOR_HANDLE和D3D12_CPU_DESCRIPTOR_HANDLE结构标识描述符堆中的特定描述符。 句柄有点类似于指针,但应用程序不得手动对其取消引用;否则,行为是未定义的。 必须通过执行 API 来使用句柄。 句柄本身可以自由复制或传递到在其上操作/使用描述符的 API 中。 未对引用进行计数,因此应用程序必须确保在已删除基础描述符堆后不使用句柄。
应用程序可以找出给定描述符堆类型的描述符的增量大小,以便它们可以手动将句柄生成到描述符堆中的任何位置(从句柄开始到库)。 应用程序决不能对描述符句柄增量大小进行硬编码,并且应该始终针对给定设备实例对其进行查询;否则,行为是未定义的。 应用程序还不得使用增量大小和句柄来执行自己的检查或描述符堆数据操作,因为执行此操作的结果是未定义的。 句柄实际上可能不会用作指针,而是作为指针的代理以便避免意外取消引用。
注意
标头 d3dx12.h 中定义了一个帮助程序结构(CD3DX12_GPU_DESCRIPTOR_HANDLE),该结构继承 D3D12_GPU_DESCRIPTOR_HANDLE 结构并提供初始化和其他有用操作。 同样,CD3DX12_CPU_DESCRIPTOR_HANDLE帮助程序结构是为 D3D12_CPU_DESCRIPTOR_HANDLE 结构定义的。
这两个帮助程序结构在填充命令列表时使用。
// Fill the command list with all the render commands and dependent state.
void D3D12nBodyGravity::PopulateCommandList()
{
// Command list allocators can only be reset when the associated
// command lists have finished execution on the GPU; apps should use
// fences to determine GPU execution progress.
ThrowIfFailed(m_commandAllocators[m_frameIndex]->Reset());
// However, when ExecuteCommandList() is called on a particular command
// list, that command list can then be reset at any time and must be before
// re-recording.
ThrowIfFailed(m_commandList->Reset(m_commandAllocators[m_frameIndex].Get(), m_pipelineState.Get()));
// Set necessary state.
m_commandList->SetPipelineState(m_pipelineState.Get());
m_commandList->SetGraphicsRootSignature(m_rootSignature.Get());
m_commandList->SetGraphicsRootConstantBufferView(RootParameterCB, m_constantBufferGS->GetGPUVirtualAddress() + m_frameIndex * sizeof(ConstantBufferGS));
ID3D12DescriptorHeap* ppHeaps[] = { m_srvUavHeap.Get() };
m_commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);
m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_POINTLIST);
m_commandList->RSSetScissorRects(1, &m_scissorRect);
// Indicate that the back buffer will be used as a render target.
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);
m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr);
// Record commands.
const float clearColor[] = { 0.0f, 0.0f, 0.1f, 0.0f };
m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
// Render the particles.
float viewportHeight = static_cast<float>(static_cast<UINT>(m_viewport.Height) / m_heightInstances);
float viewportWidth = static_cast<float>(static_cast<UINT>(m_viewport.Width) / m_widthInstances);
for (UINT n = 0; n < ThreadCount; n++)
{
const UINT srvIndex = n + (m_srvIndex[n] == 0 ? SrvParticlePosVelo0 : SrvParticlePosVelo1);
D3D12_VIEWPORT viewport;
viewport.TopLeftX = (n % m_widthInstances) * viewportWidth;
viewport.TopLeftY = (n / m_widthInstances) * viewportHeight;
viewport.Width = viewportWidth;
viewport.Height = viewportHeight;
viewport.MinDepth = D3D12_MIN_DEPTH;
viewport.MaxDepth = D3D12_MAX_DEPTH;
m_commandList->RSSetViewports(1, &viewport);
CD3DX12_GPU_DESCRIPTOR_HANDLE srvHandle(m_srvUavHeap->GetGPUDescriptorHandleForHeapStart(), srvIndex, m_srvUavDescriptorSize);
m_commandList->SetGraphicsRootDescriptorTable(RootParameterSRV, srvHandle);
m_commandList->DrawInstanced(ParticleCount, 1, 0, 0);
}
m_commandList->RSSetViewports(1, &m_viewport);
// Indicate that the back buffer will now be used to present.
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));
ThrowIfFailed(m_commandList->Close());
}
描述符堆方法
描述符堆 (ID3D12DescriptorHeap) 继承自 ID3D12Pageable。 这会施加在应用程序上对描述符进行驻留管理的责任,就像资源堆一样。 驻留管理方法仅适用于着色器可见堆,因为非着色器可见堆不会直接向 GPU 显示。
ID3D12Device::GetDescriptorHandleIncrementSize 方法允许应用程序手动将句柄偏移到堆中(将句柄生成到描述符堆中的任意位置)。 堆开始位置的句柄来自 ID3D12DescriptorHeap::GetCPUDescriptorHandleForHeapStart/ID3D12DescriptorHeap::GetGPUDescriptorHandleForHeapStart。 通过添加增量大小 * 要偏移到描述符堆开始 时偏移的描述符数来完成偏移。 请注意,增量大小不能被视为字节大小,因为应用程序不得取消引用句柄(如同它们是内存一样),所指向的内存具有非标准化的布局,甚至对于给定设备也会有所不同。
GetCPUDescriptorHandleForHeapStart 返回 CPU 可见描述符堆的 CPU 句柄。 如果描述符堆对 CPU 不可见,则将返回 NULL 句柄(并且调试层将报告错误)。
GetGPUDescriptorHandleForHeapStart 返回着色器可见描述符堆的 GPU 句柄。 如果描述符堆对着色器不可见,则将返回 NULL 句柄(并且调试层将报告错误)。
例如,使用 11on12 设备创建呈现器目标视图以显示 D2D 文本。
// Create descriptor heaps.
{
// Describe and create a render target view (RTV) descriptor heap.
D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {};
rtvHeapDesc.NumDescriptors = FrameCount;
rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
ThrowIfFailed(m_d3d12Device->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap)));
m_rtvDescriptorSize = m_d3d12Device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
}
// Create frame resources.
{
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
// Create a RTV, D2D render target, and a command allocator for each frame.
for (UINT n = 0; n < FrameCount; n++)
{
ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n])));
m_d3d12Device->CreateRenderTargetView(m_renderTargets[n].Get(), nullptr, rtvHandle);
// Create a wrapped 11On12 resource of this back buffer. Since we are
// rendering all D3D12 content first and then all D2D content, we specify
// the In resource state as RENDER_TARGET - because D3D12 will have last
// used it in this state - and the Out resource state as PRESENT. When
// ReleaseWrappedResources() is called on the 11On12 device, the resource
// will be transitioned to the PRESENT state.
D3D11_RESOURCE_FLAGS d3d11Flags = { D3D11_BIND_RENDER_TARGET };
ThrowIfFailed(m_d3d11On12Device->CreateWrappedResource(
m_renderTargets[n].Get(),
&d3d11Flags,
D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_PRESENT,
IID_PPV_ARGS(&m_wrappedBackBuffers[n])
));
// Create a render target for D2D to draw directly to this back buffer.
ComPtr<IDXGISurface> surface;
ThrowIfFailed(m_wrappedBackBuffers[n].As(&surface));
ThrowIfFailed(m_d2dDeviceContext->CreateBitmapFromDxgiSurface(
surface.Get(),
&bitmapProperties,
&m_d2dRenderTargets[n]
));
rtvHandle.Offset(1, m_rtvDescriptorSize);
ThrowIfFailed(m_d3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocators[n])));
}
}
最小描述符堆包装器
应用程序开发人员可能想要生成自己的帮助程序代码以便管理描述符句柄和堆。 基本示例如下所示。 例如,更复杂的包装器可以跟踪堆中的描述符的类型并存储描述符创建参数。
class CDescriptorHeapWrapper
{
public:
CDescriptorHeapWrapper() { memset(this, 0, sizeof(*this)); }
HRESULT Create(
ID3D12Device* pDevice,
D3D12_DESCRIPTOR_HEAP_TYPE Type,
UINT NumDescriptors,
bool bShaderVisible = false)
{
D3D12_DESCRIPTOR_HEAP_DESC Desc;
Desc.Type = Type;
Desc.NumDescriptors = NumDescriptors;
Desc.Flags = (bShaderVisible ? D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE : D3D12_DESCRIPTOR_HEAP_FLAG_NONE);
HRESULT hr = pDevice->CreateDescriptorHeap(&Desc,
__uuidof(ID3D12DescriptorHeap),
(void**)&pDH);
if (FAILED(hr)) return hr;
hCPUHeapStart = pDH->GetCPUDescriptorHandleForHeapStart();
hGPUHeapStart = pDH->GetGPUDescriptorHandleForHeapStart();
HandleIncrementSize = pDevice->GetDescriptorHandleIncrementSize(Desc.Type);
return hr;
}
operator ID3D12DescriptorHeap*() { return pDH; }
D3D12_CPU_DESCRIPTOR_HANDLE hCPU(UINT index)
{
return hCPUHeapStart.MakeOffsetted(index,HandleIncrementSize);
}
D3D12_GPU_DESCRIPTOR_HANDLE hGPU(UINT index)
{
assert(Desc.Flags&D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE);
return hGPUHeapStart.MakeOffsetted(index,HandleIncrementSize);
}
D3D12_DESCRIPTOR_HEAP_DESC Desc;
CComPtr<ID3D12DescriptorHeap> pDH;
D3D12_CPU_DESCRIPTOR_HANDLE hCPUHeapStart;
D3D12_GPU_DESCRIPTOR_HANDLE hGPUHeapStart;
UINT HandleIncrementSize;
};
相关主题