建立描述項堆積
若要建立和設定描述元堆積,您必須選取描述元堆積類型、判斷它所包含的描述元數目,以及設定旗標,指出它是 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。 沒有 ref 計數,因此應用程式必須確定它不會在刪除基礎描述元堆積之後使用控制碼。
應用程式可以找出指定描述項堆積類型的描述元遞增大小,以便從控制碼手動開始,產生描述元堆積中任何位置的控制碼。 應用程式絕對不能硬式編碼描述元處理遞增大小,而且應該一律查詢指定的裝置實例;否則,行為為未定義。 應用程式也不得使用遞增大小和控制碼自行檢查或操作描述元堆積資料,因為這樣做的結果未定義。 控制碼可能實際上不做為指標使用,而是做為指標的 Proxy,以避免意外取消參考。
注意
標頭 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 控制碼。 它會傳回 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;
};
相關主題