Skip to content

Added Conditional Rendering Extension - #272

Closed
HirunaVishwamith wants to merge 2 commits into
NVIDIA-RTX:mainfrom
HirunaVishwamith:main
Closed

HirunaVishwamith wants to merge 2 commits into
NVIDIA-RTX:mainfrom
HirunaVishwamith:main

Conversation

@HirunaVishwamith

Copy link
Copy Markdown

This adds conditional rendering so a draw, dispatch, or CmdClearAttachments can be skipped based on a value in a buffer.

The predicate is a 64-bit value at an 8-byte-aligned offset. A value of 0 skips the commands; any other value runs them. inverted flips that test. Vulkan reads only the low 32 bits, so the high 32 bits must stay 0. Begin and end must be in the same command buffer, both inside or both outside rendering. Nesting is not allowed. Copies, clears, and barriers are not skipped.

Public API

  • features.conditionalRendering
  • BufferUsageBits::CONDITIONAL_RENDERING
  • AccessBits::CONDITIONAL_RENDERING
  • StageBits::CONDITIONAL_RENDERING
  • CmdBeginConditionalRendering / CmdEndConditionalRendering

Backends

  • Vulkan uses VK_EXT_conditional_rendering.
  • D3D12 uses SetPredication and reports the feature as supported. inverted = false maps to D3D12_PREDICATION_OP_EQUAL_ZERO, so a zero predicate skips.
  • D3D11, Metal, WebGPU, and NONE have no buffer predication, so the feature stays false and the commands are empty.

Validation checks the recording state, the feature flag, a graphics or compute queue, buffer usage, the 8-byte range, nesting, and that begin and end are both inside or both outside rendering.

There is also a one-line GCC 11 fix: MemoryHeap::alignment is initialized to 0. Without it, -Werror=maybe-uninitialized fails the Linux Release build.

Testing

Tested on an RTX 3050 Laptop GPU (Vulkan 1.4.312, driver 580.159.03) with NRI validation on. A compute dispatch writes 42 only when the predicate says to run:

  • predicate 0, not inverted → result stays 0
  • predicate 1, not inverted → result is 42
  • predicate 0, inverted → result is 42
  • predicate 1 << 32, not inverted → result stays 0 (Vulkan sees the low 32 bits as 0)

All four cases passed. D3D12, D3D11, Metal, and WebGPU were not compiled on this machine.

GCC 11 -Werror=maybe-uninitialized reads alignment before the first resource sets it.
VK uses VK_EXT_conditional_rendering. D3D12 uses SetPredication. A zero 64-bit predicate skips draws, dispatches, and CmdClearAttachments. D3D11, Metal, WebGPU, and NONE leave the feature disabled.

Copy link
Copy Markdown
Collaborator

D3D12 correctness blocker: conditional rendering also suppresses operations that should remain unconditional

Reviewed at 2201134c5e6427c820ea2d38639df351ec1d8201.

CommandBufferD3D12.hpp:1716 enables native SetPredication until CmdEndConditionalRendering. D3D12 predication affects copies, resolves, and UAV clears in addition to draws, dispatches, and attachment clears. Microsoft’s predication documentation lists these operations.

This differs from the PR’s intended contract: copies and storage clears remain unconditional. Vulkan buffer copies are explicitly unaffected by conditional rendering.

Reproduced on the unchanged PR

Using the public NRI API on an RTX 4080, with NRI and native validation enabled:

  1. Initialize the source buffer to 42, destination to 0, and predicate to 0.
  2. Record CmdBeginConditionalRendering(predicate, 0, false).
  3. Record CmdCopyBuffer.
  4. Record CmdEndConditionalRendering, submit, wait, and read back the destination.
Backend Destination
Vulkan 42 — copy executes
D3D12 0 — copy is skipped

All four zero/nonzero and inverted/non-inverted combinations were tested: Vulkan always executes the copy; D3D12 skips it whenever the predicate rejects the scope. Both backends reported zero validation errors for these probes.

Implicit rendering operations are affected too

The same enabled predication reaches existing internal operations:

  • CmdBeginRendering implements attachment loadOp = CLEAR using ClearRenderTargetView. A conditional scope beginning outside rendering can therefore suppress the load clear, while Vulkan’s attachment load clear remains unconditional.
  • Automatic attachment resolves at rendering end are similarly exposed to native predication.

These implications follow from the implementation and native command semantics; the GPU reproduction above specifically tested buffer copies.

Please preserve the intended conditional command set on D3D12, including unconditional internal load clears and resolves. This may require suspending/restoring predication around the relevant native operations while preserving the intended predicate semantics. Alternatively, narrow the permitted scopes explicitly and enforce those restrictions in Validation.

Both reviewers retain NoGo for this behavioral issue. API/layout changes requiring recompilation are acceptable and are not part of this finding.

Copy link
Copy Markdown
Collaborator

Following up on the D3D12/Vulkan behavior discrepancy: the native calls themselves are correct. The clean solution is to tighten the NRI usage contract and enforce it in Validation, keeping the proposed API and thin backend mappings.

Suggested contract:

  • Conditional execution applies to the explicitly supported draw commands, compute dispatches, and CmdClearAttachments.
  • State-setting commands and synchronization remain legal, subject to their usual restrictions, and are not conditional.
  • Copies, storage clears, explicit resolves, and other operations outside that supported set must not be recorded while conditional rendering is active.
  • CmdBeginRendering and CmdEndRendering must not be called while conditional rendering is active. A graphics conditional scope must be entirely inside one rendering instance; a compute conditional scope stays outside rendering.
  • Keep the existing requirements for recording state, capability, predicate usage/range/alignment, no nesting, and closing the scope before ending the command buffer.

For graphics:

CmdBeginRendering(...);          // Unconditional attachment load operations

CmdBeginConditionalRendering(...);
CmdDraw(...);
CmdClearAttachments(...);        // Conditional explicit clear
CmdEndConditionalRendering(...);

CmdEndRendering(...);            // Unconditional attachment resolves

For compute:

CmdBeginConditionalRendering(...);
CmdDispatch(...);
CmdEndConditionalRendering(...);

CmdCopyBuffer(...);              // Outside the conditional scope

Validation can use the active-scope flag already introduced to reject the disallowed operations and rendering boundaries. The public documentation should enumerate the supported conditional commands rather than relying on broad wildcards. Add focused coverage for both legal sequences and rejection of copies, storage clears, resolves, rendering boundaries, nesting, and unclosed scopes.

This avoids hidden backend state management. Automatically suspending/restoring D3D12 predication is less attractive: D3D12 snapshots the predicate at SetPredication, so restoring it can read a different value if the buffer changed.

With these restrictions, attachment load clears and automatic resolves execute outside the conditional scope, and the D3D12/Vulkan discrepancy is excluded by the NRI contract. No extra API parameters or backend emulation are needed.

Copy link
Copy Markdown
Collaborator

Naming suggestion: use CmdBeginPredication / CmdEndPredication.

In NRI, “rendering” denotes graphics rendering, as in CmdBeginRendering / CmdEndRendering. This feature also controls compute dispatches, so “ConditionalRendering” suggests a narrower scope than the API actually has.

“Predication” describes conditional GPU execution without implying graphics or a rendering instance. It is concise and fits the proposed explicit begin/end usage contract.

For consistency, rename the associated capability and flags too:

Current Suggested
CmdBeginConditionalRendering CmdBeginPredication
CmdEndConditionalRendering CmdEndPredication
features.conditionalRendering features.predication
CONDITIONAL_RENDERING buffer-usage/access/stage bits PREDICATION

CmdBeginConditionalExecution / CmdEndConditionalExecution would also be clear, but longer. My preference for NRI’s low-level API is Predication.

@dzhdanNV

Copy link
Copy Markdown
Collaborator

, alignment(0) // FIXED BY AI: GCC 11 -Werror=maybe-uninitialized reads "alignment" before the first resource sets it - thanks for this fix, I have integrated it in a bit different form.

@HirunaVishwamith

HirunaVishwamith commented Oct 19, 2026 •

Copy link
Copy Markdown
Author

, alignment(0) // FIXED BY AI: GCC 11 -Werror=maybe-uninitialized reads "alignment" before the first resource sets it - thanks for this fix, I have integrated it in a bit different form.

Thanks for the update @dzhdanNV . I'll fix those.

@dzhdanNV

Copy link
Copy Markdown
Collaborator

After looking into the implementation more closely and thinking about the gains and maintenance cost: I'm cancelling this PR, sorry.

Inefficient GPU Execution:
Predication only skips primitive generation; it does not skip the associated GPU front-end overhead. Command processors still process the packets, state transitions (PSOs, descriptor sets, root signatures) are still evaluated, and pipeline bubbles can occur if waiting on predicate buffer retirement.

Obsolete Architecture:
Modern rendering architectures have superseded predication. Compute-based visibility culling combined with compacted indirect draws (DrawIndirectCount / ExecuteIndirect) or mesh shaders provides strictly superior performance with zero redundant state-binding overhead.

Poor Portability & Maintenance Overhead:
Predication is an optional extension in Vulkan, non-existent in Metal, and discouraged on modern console hardware. Adding and maintaining an abstraction for a legacy path does not align with keeping NRI lean and aligned with modern GPU workflows.

@dzhdanNV dzhdanNV closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants