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

# SetPredication

> 設定轉譯述詞。

設定轉譯述詞。

## 語法

```cpp theme={null}
void SetPredication(
    ID3D12Resource  pBuffer,
    UINT64 AlignedBufferOffset,
    D3D12_PREDICATION_OP Operation
)
```

### 參數

*pBuffer \[in, optional]*\
類型：ID3D12Resource \*

緩衝區 (以 [ID3D12Resource](/zh-TW/reference/graphics/d3d12_xs/interfaces/ID3D12Resource/id3d12resource_xs) 表示)，必須處於 [**D3D12\_RESOURCE\_STATE\_PREDICATION**](/zh-TW/reference/graphics/d3d12_x/enums/d3d12_resource_states) 或 [**D3D21\_RESOURCE\_STATE\_INDIRECT\_ARGUMENT**](/zh-TW/reference/graphics/d3d12_x/enums/d3d12_resource_states) 狀態 (兩個值完全相同，為了清楚起見而提供為別名)；或為 **NULL** 以停用述詞。

*AlignedBufferOffset \[in]*\
類型：UINT64

對齊的緩衝區位移，以 UINT64 表示。

*Operation \[in]*\
類型：D3D12\_PREDICATION\_OP

指定 [D3D12\_PREDICATION\_OP](/zh-TW/reference/graphics/d3d12/enums/d3d12_predication_op_public)，例如 D3D12\_PREDICATION\_OP\_EQUAL\_ZERO 或 D3D12\_PREDICATION\_OP\_NOT\_EQUAL\_ZERO。

### 傳回值

類型：void

無。

## 備註

使用此方法表示，如果述詞產生的述詞資料等於指定的作業，則實際上不會執行後續的轉譯和資源操作命令。

與 Direct3D 11 不同的是，在 Direct3D 12 中，直接命令清單不會繼承述詞狀態，而且一律會遵守述詞 (沒有述詞提示)。
所有直接命令清單一開始都會停用述詞。
套件組合會繼承述詞狀態。
多次繫結相同的述詞是合法的。

不合法的 API 呼叫會導致 [Close](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_close_public) 傳回錯誤，
或導致 [ID3D12CommandQueue::ExecuteCommandLists](/zh-TW/reference/graphics/d3d12_x/interfaces/id3d12commandqueue/methods/id3d12commandqueue_executecommandlists) 捨棄命令清單並移除裝置。

每當執行階段驗證失敗時，偵錯層都會發出錯誤。

如需詳細資訊，請參閱[述詞](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/predication)。

#### 範例

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

```cpp theme={null}
// Fill the command list with all the render commands and dependent state.
void D3D12PredicationQueries::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->SetGraphicsRootSignature(m_rootSignature.Get());

ID3D12DescriptorHeap* ppHeaps[] = { m_cbvHeap.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(dsvHandle, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

// Draw the quads and perform the occlusion query.
{
CD3DX12_GPU_DESCRIPTOR_HANDLE cbvFarQuad(m_cbvHeap->GetGPUDescriptorHandleForHeapStart(), m_frameIndex * CbvCountPerFrame, m_cbvSrvDescriptorSize);
CD3DX12_GPU_DESCRIPTOR_HANDLE cbvNearQuad(cbvFarQuad, m_cbvSrvDescriptorSize);

m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
m_commandList->IASetVertexBuffers(0, 1, &m_vertexBufferView);

// Draw the far quad conditionally based on the result of the occlusion query
// from the previous frame.
m_commandList->SetGraphicsRootDescriptorTable(0, cbvFarQuad);
m_commandList->SetPredication(m_queryResult.Get(), 0, D3D12_PREDICATION_OP_EQUAL_ZERO);
m_commandList->DrawInstanced(4, 1, 0, 0);

// Disable predication and always draw the near quad.
m_commandList->SetPredication(nullptr, 0, D3D12_PREDICATION_OP_EQUAL_ZERO);
m_commandList->SetGraphicsRootDescriptorTable(0, cbvNearQuad);
m_commandList->DrawInstanced(4, 1, 4, 0);

// Run the occlusion query with the bounding box quad.
m_commandList->SetGraphicsRootDescriptorTable(0, cbvFarQuad);
m_commandList->SetPipelineState(m_queryState.Get());
m_commandList->BeginQuery(m_queryHeap.Get(), D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0);
m_commandList->DrawInstanced(4, 1, 8, 0);
m_commandList->EndQuery(m_queryHeap.Get(), D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0);

// Resolve the occlusion query and store the results in the query result buffer
// to be used on the subsequent frame.
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_queryResult.Get(), D3D12_RESOURCE_STATE_PREDICATION, D3D12_RESOURCE_STATE_COPY_DEST));
m_commandList->ResolveQueryData(m_queryHeap.Get(), D3D12_QUERY_TYPE_BINARY_OCCLUSION, 0, 1, m_queryResult.Get(), 0);
m_commandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_queryResult.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PREDICATION));
}

// 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)。

<div class="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)

[述詞查詢逐步解說](https://learn.microsoft.com/en-us/windows/desktop/direct3d12/predication-queries)


## Related topics

- [D3D12_PREDICATION_OP](/zh-TW/reference/graphics/d3d12/enums/d3d12_predication_op_public.md)
- [BeginQuery](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_beginquery_public.md)
- [EndQuery](/zh-TW/reference/graphics/d3d12/interfaces/id3d12graphicscommandlist/methods/id3d12graphicscommandlist_endquery_public.md)
- [ID3D12QueryHeap](/zh-TW/reference/graphics/d3d12/interfaces/id3d12queryheap/id3d12queryheap_public.md)
- [CreateQueryHeap](/zh-TW/reference/graphics/d3d12/interfaces/id3d12device/methods/id3d12device_createqueryheap_public.md)
