c++ - directx-12 - 如何使用具有多个描述符堆的命令列表?

标签 c++ direct3d descriptor directx-12

目前通过微软的例子,值得注意的是,每个命令列表只使用一个 cbv_srv_uav 堆(+ 可能在额外的采样器堆上)。

每个 CommandList 是否可以使用多个堆?

所以我设置堆和范围

this->mRSVHeap = new urd::DescriptorHeap(
    *this->mDevice,
    urd::DescriptorHeapType::CBV_SRV_UAV,
    1, // shader visible
    2); // space for 2 descriptors (2 textures)

this->mConstHeap = new urd::DescriptorHeap(
    *this->mDevice,
    urd::DescriptorHeapType::CBV_SRV_UAV,
    1, // shader visible
    1); // space for 1 descriptor

urd::DescriptorRange ranges[3];
ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 2, 0); // first and second descriptor in rsv heap (t0, t1)
ranges[1].Init(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0); // first descriptor in cbv heap (b0)
ranges[2].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 2); // same texture used as first range (again first descriptor in rsv, accessable by t2)

之后我定义了描述符表

rootParam[0].InitDescTable(1, &ranges[0], D3D12_SHADER_VISIBILITY_PIXEL);
rootParam[1].InitDescTable(1, &ranges[1], D3D12_SHADER_VISIBILITY_ALL);
rootParam[2].InitDescTable(1, &ranges[2], D3D12_SHADER_VISIBILITY_PIXEL);

所以我在 rsv 堆中的 cpu 偏移量 0 和 1 处为纹理 1 和 2 创建 shaderResourceViews,并在 cbv 堆中的 cpu 偏移量 0 处为常量缓冲区创建一个 constantbufferview

像这样:

D3D12_CPU_DESCRIPTOR_HANDLE handle = this->ConstHeap->GetCPUDescriptorHandleForHeapStart();
handle.ptr += index * SIZE_OF_ONE_DESCRIPTOR_CBV_SRV_UAV_TYPE;
CreateConstantBufferView(&desc, handle)

现在是告诉命令列表引用这些堆的时候了

ID3D12DescriptorHeap* ppHeaps[] = { this->mRSVHeap.Get(), this->mConstHeap.Get() };
this->mCommandList->GetRef()->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);

在此之后,关闭命令列表总是会抛出异常。

ThrowIfFailed(this->mCommandList->Close());

这是我如何告诉命令列表哪个堆对应哪个表:

this->mCommandList->GetRef()->SetGraphicsRootDescriptorTable(0, this->mRSVHeap->GetGPUHeapAddressAtOffset(0));
this->mCommandList->GetRef()->SetGraphicsRootDescriptorTable(1, this->mConstHeap->GetGPUHeapAddressAtOffset(0));
this->mCommandList->GetRef()->SetGraphicsRootDescriptorTable(2, this->mRSVHeap->GetGPUHeapAddressAtOffset(0));

如果我将所有对象描述到一个描述符堆中(就像示例中那样)并且只使用该堆的不同偏移量,它就可以正常工作。

调试输出:

D3D12 ERROR: ID3D12CommandList::SetDescriptorHeaps: pDescriptorHeaps[1] sets a descriptor heap type that appears earlier in the pDescriptorHeaps array. Only one of any given descriptor heap type can be set at a time. [ EXECUTION ERROR #554: SET_DESCRIPTOR_HEAP_INVALID] D3D12 ERROR: CCommandList::SetGraphicsRootDescriptorTable: No CBV_SRV_UAV descriptor heap is currently set on the command list so setting a root descriptor table of CBV_SRV_UAV handles is invalid. [ EXECUTION ERROR #708: SET_DESCRIPTOR_TABLE_INVALID] D3D12 ERROR: CCommandList::SetGraphicsRootDescriptorTable: No CBV_SRV_UAV descriptor heap is currently set on the command list so setting a root descriptor table of CBV_SRV_UAV handles is invalid. [ EXECUTION ERROR #708: SET_DESCRIPTOR_TABLE_INVALID] D3D12 ERROR: CCommandList::SetGraphicsRootDescriptorTable: No CBV_SRV_UAV descriptor heap is currently set on the command list so setting a root descriptor table of CBV_SRV_UAV handles is invalid. [ EXECUTION ERROR #708: SET_DESCRIPTOR_TABLE_INVALID]

最佳答案

限制是每次只能设置一个堆(CBV/SRV/UAV 和采样器)。因此一次只能设置两个描述符堆。

但是,重要的是要注意描述符堆集在命令列表期间可能会有所不同。

关于c++ - directx-12 - 如何使用具有多个描述符堆的命令列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32114174/

相关文章:

c++ - 在带有 mingw 的 Windows 上使用 openmp。找不到 -lpthread

c++ - 学习DirectX需要具备哪些知识

opengl - 延迟着色抗锯齿

c++ - symbian C++ 描述符类

c - 递归函数后在 C 中打开文件的错误文件描述符

c++ - 如何在不复制此代码的情况下将多个函数放入多个 namespace ?

c++ - 可变参数模板 : invalid use of void expression

c++ - 托管和非托管 C++ 和 Boost

resize - ID3D11On12Device::AcquireWrappedResources() 中的额外交换链缓冲区引用

c++ - 使用 HOG 描述符进行人体检测