> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SetDescriptorHeaps

> 更改与命令列表关联的当前绑定的描述符堆。

更改与命令列表关联的当前绑定的描述符堆。

## 语法

```cpp theme={null}
void SetDescriptorHeaps(
    UINT NumDescriptorHeaps,
     ID3D12DescriptorHeap* ppDescriptorHeaps
)
```

### 参数

*NumDescriptorHeaps*\
类型：UINT

要绑定的描述符堆数量。

*ppDescriptorHeaps*\
类型：\[in] **[ID3D12DescriptorHeap](/reference/graphics/d3d12/interfaces/id3d12descriptorheap/id3d12descriptorheap_public)**\*

指向 [ID3D12DescriptorHeap](/reference/graphics/d3d12/interfaces/id3d12descriptorheap/id3d12descriptorheap_public) 对象数组的指针，这些对象用于要在命令列表上设置的堆。

只能绑定类型为 [**D3D12\_DESCRIPTOR\_HEAP\_TYPE\_CBV\_SRV\_UAV**](/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_setdescriptorheaps_public) 和 [**D3D12\_DESCRIPTOR\_HEAP\_TYPE\_SAMPLER**](/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_setdescriptorheaps_public) 的描述符堆。

一次只能设置每种类型的一个描述符堆，这意味着一次最多可以设置 2 个堆（一个采样器，一个 CBV/SRV/UAV）。

### 返回值

类型：void

无。

## 备注

可以对捆绑包调用 **SetDescriptorHeaps**，但捆绑包描述符堆必须与调用命令列表描述符堆匹配。有关捆绑包限制的详细信息，请参阅[创建和记录命令列表和捆绑包](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/recording-command-lists-and-bundles)。

调用会取消设置所有先前设置的堆。在调用中，每种着色器可见类型最多可以设置一个堆。

在某些硬件上，更改描述符堆可能会导致管道刷新。因此，建议每种类型使用单个着色器可见的堆，并每帧设置一次，而不是定期更改绑定的描述符堆。相反，请使用 [**ID3D12Device::CopyDescriptors**](/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_copydescriptors_public) 和 [**ID3D12Device::CopyDescriptorsSimple**](/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_copydescriptorssimple_public) 在渲染期间根据需要将所需的描述符从着色器不可见的堆复制到单个着色器可见的堆。

## 示例

[D3D12Bundles](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/working-samples) 示例按以下方式使用 **ID3D12GraphicsCommandList::SetDescriptorHeaps**：

```cpp theme={null}
void D3D12Bundles::PopulateCommandList(FrameResource* pFrameResource)
{
// 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_pCurrentFrameResource->m_commandAllocator->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_pCurrentFrameResource->m_commandAllocator.Get(), m_pipelineState1.Get()));

// Set necessary state.
m_commandList->SetGraphicsRootSignature(m_rootSignature.Get());

ID3D12DescriptorHeap* ppHeaps[] = { m_cbvSrvHeap.Get(), m_samplerHeap.Get() };
m_commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);

m_commandList->RSSetViewports(1, &m_viewport);
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);
CD3DX12_CPU_DESCRIPTOR_HANDLE dsvHandle(m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, &dsvHandle);

// Record commands.
const float clearColor[] = { 0.0f, 0.2f, 0.4f, 1.0f };
m_commandList->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
m_commandList->ClearDepthStencilView(m_dsvHeap->GetCPUDescriptorHandleForHeapStart(), D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

if (UseBundles)
{
// Execute the prebuilt bundle.
m_commandList->ExecuteBundle(pFrameResource->m_bundle.Get());
}
else
{
// Populate a new command list.
pFrameResource->PopulateCommandList(m_commandList.Get(), m_pipelineState1.Get(), m_pipelineState2.Get(), m_currentFrameResourceIndex, m_numIndices, &m_indexBufferView,
&m_vertexBufferView, m_cbvSrvHeap.Get(), m_cbvSrvDescriptorSize, m_samplerHeap.Get(), m_rootSignature.Get());
}

// 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());
}
```

请参阅 [Direct3D 12 参考中的示例代码](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/notes-on-example-code)。

## 要求

**头文件：** d3d12\_xs.h 或 d3d12\_x.h\
**库：** d3d12\_xs.lib 或 d3d12\_x.lib\
**支持的平台**：XBOX Series 主机和 XBOX One 系列

## 另请参阅

[描述符堆](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/descriptor-heaps)

[ID3D12GraphicsCommandList](/reference/graphics/d3d12_xs/interfaces/ID3D12GraphicsCommandList/id3d12graphicscommandlist_xs)


## Related topics

- [ClearUnorderedAccessViewFloat](/zh-CN/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_clearunorderedaccessviewfloat_public.md)
- [ClearUnorderedAccessViewUint](/zh-CN/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_clearunorderedaccessviewuint_public.md)
- [CreateDescriptorHeap](/zh-CN/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_createdescriptorheap_public.md)
- [D3D12_GPU_DESCRIPTOR_HANDLE](/zh-CN/reference/graphics/d3d12/structs/d3d12_gpu_descriptor_handle_public.md)
- [ID3D12DescriptorHeap](/zh-CN/reference/graphics/d3d12/interfaces/id3d12descriptorheap/id3d12descriptorheap_public.md)
