> ## 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](/zh-TW/reference/graphics/d3d12/interfaces/id3d12descriptorheap/id3d12descriptorheap_public)**\*

指向 [ID3D12DescriptorHeap](/zh-TW/reference/graphics/d3d12/interfaces/id3d12descriptorheap/id3d12descriptorheap_public) 物件陣列的指標，這些物件代表要在命令清單上設定的堆積。

您只能繫結類型為 [**D3D12\_DESCRIPTOR\_HEAP\_TYPE\_CBV\_SRV\_UAV**](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_setdescriptorheaps_public) 和 [**D3D12\_DESCRIPTOR\_HEAP\_TYPE\_SAMPLER**](/zh-TW/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**](/zh-TW/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_copydescriptors_public) 和 [**ID3D12Device::CopyDescriptorsSimple**](/zh-TW/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());
}
```

請參閱 [D3D12 參考中的範例程式碼](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](/zh-TW/reference/graphics/d3d12_xs/interfaces/ID3D12GraphicsCommandList/id3d12graphicscommandlist_xs)


## Related topics

- [ClearUnorderedAccessViewFloat](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_clearunorderedaccessviewfloat_public.md)
- [ClearUnorderedAccessViewUint](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_clearunorderedaccessviewuint_public.md)
- [CreateDescriptorHeap](/zh-TW/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_createdescriptorheap_public.md)
- [ID3D12DescriptorHeap](/zh-TW/reference/graphics/d3d12/interfaces/id3d12descriptorheap/id3d12descriptorheap_public.md)
- [D3D12_DESCRIPTOR_HEAP_TYPE](/zh-TW/reference/graphics/d3d12/enums/d3d12_descriptor_heap_type_public.md)
