> ## 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.

# ClearDepthStencilView

> 清除深度樣板資源。 (ID3D12GraphicsCommandList.ClearDepthStencilView)

清除深度樣板資源。

## 語法

```cpp theme={null}
void ClearDepthStencilView(
    D3D12_CPU_DESCRIPTOR_HANDLE DepthStencilView,
    D3D12_CLEAR_FLAGS ClearFlags,
    FLOAT Depth,
    UINT8 Stencil,
    UINT NumRects,
    const D3D12_RECT* pRects
)
```

### 參數

*DepthStencilView \[in]*\
類型：D3D12\_CPU\_DESCRIPTOR\_HANDLE

描述 CPU 描述元控制代碼，代表要清除之深度樣板的堆積開頭。

*ClearFlags \[in]*\
類型：D3D12\_CLEAR\_FLAGS

[D3D12\_CLEAR\_FLAGS](/zh-TW/reference/graphics/d3d12/enums/d3d12_clear_flags_public) 值的組合，以位元 OR 運算結合。產生的值會識別要清除的資料類型 (深度緩衝區、樣板緩衝區或兩者)。

*Depth \[in]*\
類型：FLOAT

用來清除深度緩衝區的值。此值會限制在 0 與 1 之間。

*Stencil \[in]*\
類型：UINT8

用來清除樣板緩衝區的值。

*NumRects \[in]*\
類型：UINT

<i>pRects</i> 參數所指定之陣列中的矩形數目。

*pRects \[in]*\
類型：**const **D3D12\_RECT****\*

**D3D12\_RECT** 結構的陣列，代表資源檢視中要清除的矩形。如果為 **NULL**，**ClearDepthStencilView** 會清除整個資源檢視。

### 傳回值

類型：void

無。

## 備註

只有直接命令清單和套件組合命令清單支援此作業。

**ClearDepthStencilView** 可用來初始化別名相同堆積記憶體的資源。如需詳細資訊，請參閱 [CreatePlacedResource](/zh-TW/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_createplacedresource_public)。

<h3><a id="Runtime_validation" /><a id="runtime_validation" /><a id="RUNTIME_VALIDATION" />執行階段驗證</h3>
對於浮點輸入，執行階段會將非正規化值設定為 0 (同時保留 NAN)。

驗證失敗會導致對 [Close](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_close_public) 的呼叫傳回 **E\_INVALIDARG**。

<h3><a id="Debug_layer" /><a id="debug_layer" /><a id="DEBUG_LAYER" />偵錯層</h3>
如果輸入色彩為非正規化，偵錯層會發出錯誤。

如果檢視所參考的子資源不處於適當的狀態，偵錯層會發出錯誤。
對於 **ClearDepthStencilView**，狀態必須為 [D3D12\_RESOURCE\_STATE\_DEPTH\_WRITE](/zh-TW/reference/graphics/d3d12_x/enums/d3d12_resource_states)。

## 範例

[D3D12Bundles](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/working-samples) 範例使用 **ID3D12GraphicsCommandList::ClearDepthStencilView** 的方式如下：

```cpp theme={null}
// Pipeline objects.
D3D12_VIEWPORT m_viewport;
ComPtr<IDXGISwapChain3> m_swapChain;
ComPtr<ID3D12Device> m_device;
ComPtr<ID3D12Resource> m_renderTargets[FrameCount];
ComPtr<ID3D12Resource> m_depthStencil;
ComPtr<ID3D12CommandAllocator> m_commandAllocator;
ComPtr<ID3D12GraphicsCommandList> m_commandList;
ComPtr<ID3D12CommandQueue> m_commandQueue;
ComPtr<ID3D12RootSignature >m_rootSignature;
ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
ComPtr<ID3D12DescriptorHeap> m_cbvSrvHeap;
ComPtr<ID3D12DescriptorHeap> m_dsvHeap;
ComPtr<ID3D12DescriptorHeap> m_samplerHeap;
ComPtr<ID3D12PipelineState> m_pipelineState1;
ComPtr<ID3D12PipelineState> m_pipelineState2;
D3D12_RECT m_scissorRect;
```

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

[D3D12Multithreading](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/working-samples) 範例使用 **ID3D12GraphicsCommandList::ClearDepthStencilView** 的方式如下：

```cpp theme={null}
void FrameResource::Init()
{
// Reset the command allocators and lists for the main thread.
for (int i = 0; i < CommandListCount; i++)
{
ThrowIfFailed(m_commandAllocators[i]->Reset());
ThrowIfFailed(m_commandLists[i]->Reset(m_commandAllocators[i].Get(), m_pipelineState.Get()));
}

// Clear the depth stencil buffer in preparation for rendering the shadow map.
m_commandLists[CommandListPre]->ClearDepthStencilView(m_shadowDepthView, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

// Reset the worker command allocators and lists.
for (int i = 0; i < NumContexts; i++)
{
ThrowIfFailed(m_shadowCommandAllocators[i]->Reset());
ThrowIfFailed(m_shadowCommandLists[i]->Reset(m_shadowCommandAllocators[i].Get(), m_pipelineStateShadowMap.Get()));

ThrowIfFailed(m_sceneCommandAllocators[i]->Reset());
ThrowIfFailed(m_sceneCommandLists[i]->Reset(m_sceneCommandAllocators[i].Get(), m_pipelineState.Get()));
}
}
```

```cpp theme={null}
// Assemble the CommandListPre command list.
void D3D12Multithreading::BeginFrame()
{
m_pCurrentFrameResource->Init();

// Indicate that the back buffer will be used as a render target.
m_pCurrentFrameResource->m_commandLists[CommandListPre]->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));

// Clear the render target and depth stencil.
const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize);
m_pCurrentFrameResource->m_commandLists[CommandListPre]->ClearRenderTargetView(rtvHandle, clearColor, 0, nullptr);
m_pCurrentFrameResource->m_commandLists[CommandListPre]->ClearDepthStencilView(m_dsvHeap->GetCPUDescriptorHandleForHeapStart(), D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

ThrowIfFailed(m_pCurrentFrameResource->m_commandLists[CommandListPre]->Close());
}

// Assemble the CommandListMid command list.
void D3D12Multithreading::MidFrame()
{
// Transition our shadow map from the shadow pass to readable in the scene pass.
m_pCurrentFrameResource->SwapBarriers();

ThrowIfFailed(m_pCurrentFrameResource->m_commandLists[CommandListMid]->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 系列

## 另請參閱

[ID3D12GraphicsCommandList](/zh-TW/reference/graphics/d3d12_xs/interfaces/ID3D12GraphicsCommandList/id3d12graphicscommandlist_xs)


## Related topics

- [D3D12_CLEAR_FLAGS](/zh-TW/reference/graphics/d3d12/enums/d3d12_clear_flags_public.md)
- [D3D12_CLEAR_VALUE](/zh-TW/reference/graphics/d3d12/structs/d3d12_clear_value_public.md)
- [D3D12_CPU_DESCRIPTOR_HANDLE](/zh-TW/reference/graphics/d3d12/structs/d3d12_cpu_descriptor_handle_public.md)
- [CreatePlacedResource](/zh-TW/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_createplacedresource_public.md)
- [CreateDepthStencilView](/zh-TW/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_createdepthstencilview_public.md)
