Skip to content

D3D12.X: ExecuteIndirect checks of a run of draws share one pass - #3128

Open
Raul-Paiva wants to merge 1 commit into
xcl-dev:mainfrom
Raul-Paiva:d3d12x/indirect-guards-batched
Open

Raul-Paiva wants to merge 1 commit into
xcl-dev:mainfrom
Raul-Paiva:d3d12x/indirect-guards-batched

Conversation

@Raul-Paiva

@Raul-Paiva Raul-Paiva commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Each checked ExecuteIndirect (indirect_guard) records, in front of itself, a UAV barrier on everything, a Dispatch(1, 1, 1) of the count check and two transitions of its output buffer. A retail title makes thousands of these a frame. Another open-source runtime for the same platform measured the same per-call pattern at 14–16 µs a check; by checking all the arguments between two points where they can change in one compute pass that reads a job table the CPU fills while the list is recorded, it went from 2,355 checks a frame to 980 passes and a replayed frame from 86.1 to 77.7 ms (with fewer batch breaks, 133 passes and 84.6 → 75.3 ms). Those are its numbers, not measured here. This PR does the same in XCL, written anew, behind XCL_INDIRECT_GUARD_BATCH=1 (off by default) so it can be measured first.

With the switch on, the checks of draws (Draw / DrawIndexed signatures) go in batches of up to 32 per list. The first draw of a batch records the barrier, one pass and one transition of the output buffer to INDIRECT_ARGUMENT; each later draw of the batch only writes its job (window, offset, slot, kind, and the program id and title address for the log) into a block of CPU-visible memory before the list is submitted. The pass reads the counts through the memory windows (8 bound as root UAVs). Counts outside them, dispatches (their counts are clamped after the check) and XCL_INDIRECT_GUARD=compare are checked alone, as before.

A batch ends (output buffer back to COMMON) at an ExecuteIndirect that is not of a plain Draw / DrawIndexed signature (dispatches, mesh, root changes, several arguments: checked at the top of the hook, before any of its paths) and at a DispatchMesh; at any other title call on the list but root constant and root view sets, the pipeline set, topology, scissors, viewports, blend factor, stencil ref and index / vertex buffer sets (root signature and descriptor table sets end it); at a check that goes alone; and at the list's Close and the PC Close of a segment (split, KickoffX). Reset, a split and the list's release forget it. Between the first draw of a batch and the last one the title makes no call that writes memory but plain indirect draws, and what those write reaches a later draw's counts only through a barrier, which ends the batch: the pass reads the counts each draw would have read.

A recording keeps its blocks of jobs (16,384 in all, 17 MB) until its PC allocator is reset, when the GPU is done with it: a list submitted again reads its own jobs, and the CPU never writes a block the GPU may still read. With no block free, a check goes alone.

Changed after the review of 12-01 (Foniass): non-draw ExecuteIndirects and DispatchMesh now end a batch (a count written inside a batch could be read before that write); the job blocks went from a ring to blocks held per recording until the allocator reset (a block could be reused with no fence); a GPU test of the batch pass.

Related: #3009 (open) keeps the same buffer in INDIRECT_ARGUMENT between single checks; the two touch the same lines of check and execute_indirect and will need a rebase on each other. amd-dev has a different batching of its own dispatch guard (XCL_INLINE_GUARD_BATCH); indirect_guard is not on that branch.

What is in it

  • hle/d3d12x/indirect_guard.rs: XCL_INDIRECT_GUARD_BATCH, the batch pass (batch_program, BatchPass, 16,384 job blocks, 17 MB, held per recording in Blocks until the allocator reset), batched in check, end_batch, before_execute_indirect (only a plain draw goes on), retire_batches / allocator_reset, Checked::run without transitions for a batched check; unit tests of which checks are batched, which ExecuteIndirects keep a batch, which counts make a job, when a check joins the open batch, how blocks are held and given back, and the pass's window bindings; a GPU test that runs the batch pass over a block of jobs in two windows.
  • hle/d3d12x/bind.rs: guard_batch_end_wanted (the calls a batch goes on across; DispatchMesh ends it) and its test; before_list_call ends a batch; execute_indirect_snapshot calls before_execute_indirect first; flush_two_pass ends it at Close and segment Close; reset, inherit_for_split and the list's release retire its blocks to the allocator.
  • hle/d3d12x/console_list.rs: allocator_reset gives the allocator's job blocks back.
  • hle/d3d12x/real.rs: the hook in front of each list call is also installed with the switch.

Tested

  • Hardware: RTX 3070 Laptop GPU 8 GB, driver 621.40, i7-11800H, 32 GB, Windows 10 19045, rustc 1.100.0, release build.
  • Base for the comparison: main 5b0d5fa (same commands, same machine). The head pushed here is this change, with the review fixes described above, rebased onto main b2b1f10 without conflicts; main's newer commits also change bind.rs, real.rs, which the rebase merged. The full-suite counts below were measured before the review fixes and that rebase.
  • At this head ee2abf2 (with the review fixes, on main b2b1f10), the lib tests of the modules this PR touches (hle::d3d12x::bind, hle::d3d12x::indirect_guard, hle::d3d12x::real, hle::d3d12x::console_list), release, serial, with the 20 skips: 308 passed, 3 failed, 0 ignored; the new GPU test the_batch_pass_checks_each_job_through_its_window ran (not skipped). The 3 failures (bind::tests::gpu_two_pass_indirect_commands_keep_their_own_records, real::dispatch_chain::tests::an_indirect_dispatch_runs_its_chain_without_any_whole_form, real::dispatch_chain::tests::failed_binding_keeps_indirect_commands_until_allocator_reset) fail the same way on main d52ff48 (E_INVALIDARG from the driver on this adapter; main d52ff48 with the first three modules: 287 passed, 3 failed, 0 ignored); not rerun on main b2b1f10.
  • cargo test --release --workspace --no-fail-fast -- --test-threads=4: the lib test binary ends early on main too (exit 0x80004005 after sparse upload preparation still failed after bounded retries), so the lib was also run alone, serially (--test-threads=1), with --skip for the 20 tests that end the binary on main (6 list_x, 12 sparse, 1 texture_upload, 1 uma_driver; each passes alone).
  • This PR tested at 9a2d56ee (on main 5b0d5fa):
    • integration tests: 138 passed, 1 failed, 20 ignored; main: 139 passed, 0 failed, 20 ignored. The failure, gpu_two_pass_cache_e2e::two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs, is intermittent on main too (run 8 times alone on main: 2 failures).
    • lib, serial with the 20 skips: 2609 passed, 13 failed, 29 ignored; main: 2604 passed, 13 failed, 29 ignored. The same 13 failures as main (12 title_textures, 1 vram::d32s8_targets_take_their_own_size_of_video_memory); the 5 more passing are this PR's new tests.
    • The suite does not set XCL_INDIRECT_GUARD_BATCH, so the batch pass is off there.
  • SKIPPED on this adapter (same on main): the wave64 forms (no wave64), three f16/f32 per-lane forms refused by the recompiler, and the heap-window tests (no heap windows).

Regression run
scripts/smoke.ps1 -Seconds 120 -Title 'retail title A', release builds of clean trees, one after the other on the machine in Tested, with XCL_DEVICE_TYPE=lockhart XCL_DIRECT_DXIL=1 XCL_HEAP_WINDOWS=1 XCL_WATCH_HOT=0 XCL_DRED=1 XCL_GPU_WAIT_TIMEOUT_MS=60000. The title is left on its own, with no input; the shader cache was warmed by two runs of main before.

main:

xcl-smoke v1
commit: b2b1f109d27e746a62ec95b04dd1f9924452e682
gpu: NVIDIA GeForce RTX 3070 Laptop GPU
title: retail title A
seconds: 120
exit: 0x0
frames shown: 3358 (PresentX 3358)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15865 MB

This PR:

xcl-smoke v1
commit: ee2abf2e43745434a3996cfe70e1ab188bf8e696
gpu: NVIDIA GeForce RTX 3070 Laptop GPU
title: retail title A
seconds: 120
exit: 0x0
frames shown: 3366 (PresentX 3366)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 15465 MB

Not tested

  • A title run with the switch on, and frame times with it off/on.
  • Indexed draws in a batch measured on a GPU (andrewDoing checked contiguous non-indexed draws across the 32-job boundary on an RTX 5070).
  • A recording submitted again after its job block went to another batch (after 16,384 more batches) runs the counts its first run checked (still in range); not measured how often a title does that.
  • AMD and Intel GPUs.

AI use

  • Claude Code (Claude Opus 5.5) wrote the change, the tests and this description, and ran the tests; I read the diff.

🤖 Generated with Claude Code

@KDenKC

KDenKC commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

@Raul-Paiva the whole suite of your head on an RTX 3090 against its base on the same machine: no failure the base doesn't have; its 5 new tests pass (bind::tests::a_title_call_that_can_write_or_order_memory_ends_a_batch_of_checks, four indirect_guard::tests); the switch (XCL_INDIRECT_GUARD_BATCH) stays off in the suite.

Tested: 5ed4bd7b and its base nvidia-dev 54225f58.

  • RTX 3090 24 GB, NVIDIA 621.24, i7-10700T x8 (a VM with the GPU passed through), Windows 11 26H2 (26300.9974), Rust 1.99.1 (stable-x86_64-pc-windows-msvc), debug, no XCL_* set.
  • cargo test --workspace --no-fail-fast -- --test-threads=4 --skip uma_driver::tests::a_refused_gpu_upload_segment_becomes_custom --skip uma_driver::tests::mixed_window_keeps_its_bytes_and_the_gpu_sees_every_segment --skip uma_driver::tests::failed_restore_stops_before_importing_an_unbacked_window (those three end the lib test binary on 621.24, at the base too)
  • base: 1,900 passed, 7 failed, 48 ignored; head: 1,905 passed, 7 failed, 48 ignored. The 7 failures are the same tests on both, and they come from this VM (4 title_textures: one 0x8007000E in the full run, passes alone, and three PoisonError from it; 2 vram 0x8007000E; e2e test_title_runs_and_game_folder_is_untouched).
  • SKIPPED: 30, the same on both.

Not tested: a title run; a release build; AMD / Intel.

AI use: an agent swarm: Claude Fable 5.2, Claude Opus 5.5, GPT-6.2-Sol and Gemini 4 Neon.

@andrewDoing

Copy link
Copy Markdown
Contributor

On a native RTX 5070 12 GB (driver 616.92), the opted-in draw guard preserved the input-assembler counts across the 32-job batch boundary at 5ed4bd7b317ae13f7ed8dfe07a909993561776df. The 32nd command had zero instances, followed by three active draws in the next batch.

Draw commands XCL_INDIRECT_GUARD_BATCH Batch blocks IA vertices IA primitives Compute shader invocations
1 0 0 3 1 1
1 1 1 3 1 32
35 0 0 294 98 35
35 1 2 294 98 64

Each row ran in a fresh process. The fixture varied vertex count, instance count and start vertex, called production check and Checked::run, executed native ExecuteIndirect(Draw), and read D3D12 pipeline statistics after the queue fence completed. Every guard slot reported the expected first three argument words. All four cases passed, zero skipped. The compute counter counts shader invocations: the batch shader launches 32 lanes per block.

The fixture registered one 4 KiB CPU-visible native argument buffer as test window 0, then used the production window lookup, batch shader and GPU execution. It ended the batch explicitly at close. This covers contiguous non-indexed draws on one direct list. It does not measure retail frame time, guest allocation, automatic batch breaks, or indexed draws.

AI use: OpenAI Codex (GPT-6) prepared the test-only fixture, ran the four native controls and wrote this report.

@Prime1997
Prime1997 changed the base branch from nvidia-dev to main November 28, 2026 20:13
@Raul-Paiva
Raul-Paiva force-pushed the d3d12x/indirect-guards-batched branch from 5ed4bd7 to c7729b4 Compare November 30, 2026 00:12
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Nov 30, 2026
@Raul-Paiva
Raul-Paiva force-pushed the d3d12x/indirect-guards-batched branch from c7729b4 to 2df9e9d Compare November 30, 2026 07:19
@Raul-Paiva
Raul-Paiva marked this pull request as ready for review November 30, 2026 08:11
@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Nov 30, 2026
@Raul-Paiva
Raul-Paiva force-pushed the d3d12x/indirect-guards-batched branch from 2df9e9d to 299104a Compare November 30, 2026 17:28
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 299104a

#3009 goes after the same cost (the per-check barriers of indirect_guard in front of each checked ExecuteIndirect) another way: it keeps the check buffer INDIRECT_ARGUMENT between single checks. This PR batches up to 32 draw checks into one opt-in pass (XCL_INDIRECT_GUARD_BATCH=1). The two edit the same check/run code and would conflict, so only one of them should land.
No problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 493dbd3
head: 299104a
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2905 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+357/-14). Batching is off unless XCL_INDIRECT_GUARD_BATCH=1; with it unset, the only additions on the default path are atomic no-ops. The run below has a second smoke with the switch on. Findings, switch on: (1) A count written inside a batch can be missed. ExecuteIndirect (slots 59 / 61) and DispatchMesh (127 / 129) don't end a batch (bind.rs ~2093, ~2104), and a dispatch ends it only through check -> end_batch (indirect_guard.rs ~593). Some compute work never gets there: a non-plain dispatch signature returns earlier (~556), the PC-pipeline ExecuteIndirect path returns before check (bind.rs ~3858), and DispatchMesh never calls it. bind.rs ~4005 notes that retail titles run an ExecuteIndirect right after the dispatch that writes its count, with no barrier XCL sees, which contradicts the batching premise (indirect_guard.rs ~19-24). A later draw would then use a count read before that write: in range but wrong. (2) Job blocks are reused with no fence (BLOCKS 16,384, ~100, reused at ~503). A list submitted again after its block was reused runs another list's jobs and writes slots that may be in flight, and the CPU can overwrite a block the GPU is still reading. On main the check runs again on every submit, so this failure is new. (3) The five tests check pure logic; none runs batch_program on a GPU or checks that a batch ends at Close, a segment or a barrier. (4) Nothing measured in XCL: the quoted numbers come from another runtime, and the PR's smoke ran with the switch off. Nit: the test says slots 29-32 (root signature and table sets) end a batch, while the description says root signature sets don't. Fine: Close, segment close, Reset and split end a batch (bind.rs ~2405), and dispatches and compare are checked alone. #3009 is a competing design on the same check / run code; #3536 conflicts textually; #3401's c.read(this) on a bind error would need a guard for batched checks.
performance: 60 s smoke of a retail title, main 493dbd3 vs this merge (6fc5d1b2): 1670 -> 1675 frames shown (100 %; last presents per 2 s: 60 60 60), peak commit 17083 -> 17145 MB, 0 device removals, 0 crashes. A second 60 s run of the same merge with XCL_INDIRECT_GUARD_BATCH=1: 1677 frames shown (last presents per 2 s: 60 60 60), peak commit 17406 MB, 0 device removals, 0 crashes; this early in the title it shows only that turning it on breaks nothing at boot.
verdict: not yet: with the switch on, a count written inside a batch by work that never ends it (non-plain signatures, the PC-pipeline ExecuteIndirect path, DispatchMesh) is read before the write (finding 1), and job blocks are reused with no fence (finding 2); both are new failure modes compared with checking at every submit
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

Each checked ExecuteIndirect records, in front of itself, a UAV barrier on
everything, a Dispatch(1, 1, 1) of the count check and two transitions of
its output buffer. A retail title makes thousands of these a frame.
Another open-source runtime for the same platform measured this pattern at
14-16 us a check and got its replayed frame from 86.1 to 77.7 ms by
checking all the arguments between two points where they can change in
one compute pass.

With XCL_INDIRECT_GUARD_BATCH=1 the checks of draws (Draw and
DrawIndexed signatures) go in batches of up to 32 per list. The first
draw of a batch records the barrier, one pass and one transition of the
output buffer to INDIRECT_ARGUMENT; the pass reads a block of jobs in
CPU-visible memory (window, offset, slot, kind; the program id and title
address for the log) that the CPU fills while the list is recorded,
before it is submitted. A later draw of the batch only writes its job:
no barrier, dispatch or transition. The counts are read through the
memory windows (eight bound as root UAVs); counts elsewhere, dispatches
(clamped after the check) and the compare diagnostic are checked alone,
as before.

A batch ends, with the output buffer back to COMMON, at:
- an ExecuteIndirect that is not of a plain Draw / DrawIndexed
  signature (dispatches, mesh, root changes, several arguments), at
  the top of the hook, before any of its paths
  (indirect_guard::before_execute_indirect), and a DispatchMesh;
- any other title call on the list but ExecuteIndirect, root
  constant and root view sets, the pipeline set, topology, scissors,
  viewports, blend factor, stencil ref and index / vertex buffer sets
  (bind::before_list_call, now also installed with this switch): barriers,
  draws, dispatches, copies, clears, render targets, queries, waits,
  writes, flushes, root signature and descriptor table sets and the
  console's other methods all end it;
- a check that goes alone;
- the list's Close and the PC Close of a segment (split, KickoffX);
- a Reset, a split or the list's release forgets it (nothing left to
  record).
Between the first draw of a batch and the last one the title makes no
call that writes memory but plain indirect draws, and what those write
reaches a later draw's counts only through a barrier, which ends the
batch: the pass reads the counts each draw would have read.

A recording keeps its blocks of jobs (16384 in all, 17 MB) until its
PC allocator is reset (console_list::allocator_reset), when the GPU is
done with it: a list submitted again reads its own jobs, and the CPU
never writes a block the GPU may still read. With no block free, a
check goes alone. Off by default, to be measured first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Raul-Paiva
Raul-Paiva force-pushed the d3d12x/indirect-guards-batched branch from 299104a to ee2abf2 Compare December 1, 2026 13:15
@Raul-Paiva

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Head is now ee2abf2 (rebased onto main b2b1f10), with fixes for both findings, squashed into the one commit:

  1. A count written inside a batch. An ExecuteIndirect that is not of a plain Draw / DrawIndexed signature (dispatches, mesh, root changes, several arguments) now ends the list's batch at the top of the hook, before any of its paths (indirect_guard::before_execute_indirect, called first in execute_indirect_snapshot). That covers the non-plain signatures, the PC-pipeline path and the split-dispatch path. DispatchMesh now ends it too (guard_batch_end_wanted). Only plain indirect draws stay in a batch.
  2. Job blocks reused with no fence. The ring is gone. A recording keeps the blocks it wrote (Blocks) until its PC allocator is reset (console_list::allocator_reset), so a list submitted again reads its own jobs, and the CPU never writes a block the GPU may still read. Reset, a split and the list's release retire the blocks to that allocator. With no block free, the check goes alone, as on main.
  3. Tests. the_batch_pass_checks_each_job_through_its_window runs batch_program on the GPU over a block of four jobs in two windows: two in range, one garbage, one past the block's count. There are also unit tests of which ExecuteIndirects keep a batch and of how blocks are held and given back.

The nit is fixed in the commit message and the description: root signature and descriptor table sets end a batch.

Tested on an RTX 3070 Laptop (driver 621.40):

  • The modules this PR touches, release, serially: 308 passed, 3 failed. The 3 failures are the E_INVALIDARG ones that also fail on main here.
  • Smoke against main b2b1f10: 3366 vs 3358 frames, peak commit 15465 vs 15865 MB.

Still open: the smoke ran with the switch off, so the batch path is still not measured in XCL; that is your finding 4.

Claude Code (Claude Opus 5.5) helped with the fixes and this reply; I checked each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Follow-up review (head ee2abf2, since 299104a, diff read, not a merge-check):

New:

  • The new GPU test turns any failure of the batch pass into a skip. the_batch_pass_checks_each_job_through_its_window (indirect_guard.rs ~952) calls skip_test("no DXC") on every make_batch error, including a DXC compile error in batch_program, a bad root signature or a refused pipeline, so a broken batch program shows as SKIPPED. The module's other DXC tests skip only when dxc::location() fails.
  • After merging with main, the batch program compiles under GUARD. Since D3D12.X: compile the indirect guard with DXC, and guard split chains #3536, guard_of compiles the guard's DXIL before taking GUARD so counts does not wait on DXC; make_batch (~408), called from make (~520) with GUARD held, runs dxc::compile there. Once, and only with the switch on.

Fine: with the switch off the default path only gains the atomic batch_open / OPEN_LISTS checks; dispatches, the compare mode and counts outside the 8 bound windows still go alone.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants