Skip to content

GPU: an indirect execution's argument buffer stays readable until the next clamp (one barrier fewer, ~0.4 µs per indirect call) - #3307

Open
KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/indirect-dispatch
Open

KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:perf/indirect-dispatch

Conversation

@KainanP

@KainanP KainanP commented Nov 26, 2026

Copy link
Copy Markdown

Follow-up to #2586. ExecuteIndirect with dispatch arguments costs about 3.0 µs a call on main, against 0.86 µs for a plain Dispatch (list bench, comment 7197644545). #2472 and #3197 batch the clamp for indirect draws only, and I found no open PR for dispatches. This PR makes the per-call clamp cheaper on the CPU, for indirect dispatches and for every indirect draw that is still clamped alone. The arguments the GPU reads and the clamping rules don't change.

Where the time goes

I timed each step of one indirect dispatch with rdtsc (temporary counters, not in this PR), on 1 thread, on main. Shares of the call:

step share
the clamp in indirect::bound: two ResourceBarrier calls 20%
the clamp's own Dispatch 9%
the clamp's root signature, pipeline and four root arguments 6%
list.budget() + list.arguments() (lock, GetDesc, GetGPUVirtualAddress) 3%
indirect::done: a third ResourceBarrier call, right after the execution 9%
binding the title's pipeline again after the clamp: SetPipelineState + root signature 3%
...and keeping it again: used_pipeline (string set) + two keep (QueryInterface + AddRef + push) 4%
ExecuteIndirect itself 13%
lookups: host_signature (global mutex + COM clone), shadow::find, refresh, signature layout 5%
binding the title's tables and roots (a plain Dispatch pays this too) the rest

Most of the gap to a plain Dispatch is driver calls the clamp needs: two barrier calls, a compute dispatch, and a pipeline switch away from the title's pipeline and back.

Change

Only crates/victory-gpu:

  • One barrier call fewer per indirect execution (indirect.rs, host.rs). The list's argument buffer used to go common → unordered access → indirect argument → common on every call. Now it stays in the indirect-argument state after the execution. The next clamp's existing barrier moves it from there to unordered access. HostList::close puts it back to common, and so does a bigger buffer replacing it mid-list (the old one is transitioned before it is kept). Between lists the buffer is common as before, so the "its states are known whatever order lists run in" rule still holds. indirect::done is gone. The GPU sees the same ordering: the transition from indirect argument to unordered access waits for the previous execution's reads, as the transition to common did.
  • The title's pipeline is not kept again after the clamp (draw.rs bind, HostList::holds_pipeline). The clamp forces SetPipelineState and the root signature again (kept), but the list already holds that pipeline. used_pipeline and the two keep calls now run only when the pipeline differs from the last one the list kept since its reset.
  • Command signatures without a lock or refcount per call (draw.rs host_signature). They are made once and kept for good (as before, but leaked as &'static), and each thread remembers the last one it asked for. Five title threads no longer share a mutex and one object's reference count per call.
  • Cached widths and addresses (host.rs). The argument buffer's width and the budget word's GPU address are remembered, not asked of the driver on every call.

I didn't do a window or a hoist (#3197's or #2472's approach) for dispatches. An indirect dispatch's arguments are usually written by the dispatch just before it in the same list, and compute pipelines store through views. Both PRs end their windows at every dispatch for this reason, so batching would cover no dispatch without proving which buffers each dispatch writes.

Measured

List bench (pipeline::list_bench, fork branch perf/list-call-bench, VICTORY_LIST_BENCH_INDIRECT=1): each thread records one indirect call at a time into its own list, and the list's close is timed with the calls. Release build, ns per call, median of 15 interleaved runs [min–max]:

main + this PR stack¹ stack¹ + this PR
dispatch, 1 thread 3,201 [2,992–3,450] 2,817 [2,699–3,104] 2,748 [2,594–2,883] 2,360 [2,191–2,525]
dispatch, 5 threads 4,106 [3,856–5,427] 3,681 [3,487–4,058] 3,495 [3,287–3,687] 3,073 [2,925–3,223]
indexed draw, 1 thread 5,096 [4,736–6,440] 4,522 [4,285–4,873] 3,147 [2,943–3,452] 2,772 [2,634–2,930]
indexed draw, 5 threads 9,756 [8,966–10,045] 9,905 [8,750–11,092] 4,476 [4,302–4,949] 4,066 [3,888–4,396]

¹ main + #1978 + #2821 + #3258 + #3274 + #3275.

That is about −0.4 µs per indirect call (−10 to −14%). The exception is indexed draws on 5 threads on main, which are dominated by lock convoys elsewhere (#2586) and stay within their spread. The rest of the gap to a plain Dispatch is the clamp's barriers, its dispatch and the pipeline switch.

Hardware: RTX 4060 Laptop, i7-14650HX, Windows 11. Foreign CPU load: several unrelated Python jobs were running throughout, the same for all builds. Builds were interleaved run by run so they share it.

Not measured: the title. I have no in-game numbers. The bench records only indirect calls, which is the best case for the per-call saving. GPU time should not change (same commands, one barrier fewer), but I didn't time it.

Tests

New:

  • indirect::tests::each_execution_reads_its_own_clamped_records: five indirect dispatches in one list through the clamp and the list's one argument buffer, including one past 65,535 groups in x, one past 2^20 groups (runs empty), and one with 6,000 records that makes the list replace its 64 KiB buffer while it is readable. A counting compute shader records how many groups each execution ran: 6, 65,535, 0, 5, 7. Each barrier is also replayed from the buffers' states (the debug layer's rule), and all end common. With the clamp's barrier changed back to "from common", the test fails.
  • host::tests::a_list_holds_the_pipeline_it_last_kept_until_its_reset
  • draw::tests::host_signatures_are_made_once (same object across threads and kinds)

Existing indirect:: tests are updated for done's removal and still check the same values and states.

cargo test -p victory-gpu, same failures as main on each adapter:

adapter main 63bcca4 this PR
RTX 4060 Laptop (skipping memory_tests::heap_direct_texture_reads_mark_their_heap_index, which access-violates on main too) 229 passed, 2 failed 232 passed, same 2 failed
Intel UHD a78b (local-only adapter-selection hook, not part of this PR) 228 passed, 3 failed 231 passed, same 3 failed
WARP (VICTORY_D3D12_WARP=1, --features diagnostics, one thread) 246 passed 249 passed

The failures on main are descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views, plus draw::tests::a_texture_written_in_one_format_reads_in_another_through_a_placed_view on Intel. cargo clippy --workspace --all-targets -- -D warnings passes with and without --all-features, and cargo xtask policy passes. rustfmt was applied to the changed lines only.

Overlap

This conflicts textually with #2472, #3197, #2168 and #2201. All four call indirect::done or take list.arguments() expecting a common buffer. If one of them lands first, I'll rebase this onto it. The combination is straightforward: their batched passes remove the clamp for the draws they cover, and this PR makes the clamps that are left cheaper. It also touches host_signature, which #1356 turns into an RwLock (I'm offering #1356 a rebased branch in its thread).

AI use: Claude Code (Claude Opus 5.5) profiled, wrote the change and its tests, ran the measurements and drafted this description.

🤖 Generated with Claude Code

… next clamp

Each indirect execution recorded three barrier calls for the list's
argument buffer (common -> unordered access, -> indirect argument, then
back to common right after the execution). The buffer now stays readable
as arguments after the execution: the next clamp's barrier moves it
from there (one call fewer per execution), and the list's close, or a
bigger buffer replacing it, puts it back to common, so between lists it
is common as before. The arguments and the barriers' ordering the GPU
sees are the same.

Also on the per-call path of every indirect execution:
- a pipeline bound again only because the clamp replaced it is not kept
  and named again (two QueryInterface + AddRef and a set lookup a call);
- command signatures are made once for good and each thread remembers
  the last one, so no shared lock or reference count per call;
- the argument buffer's width and the budget's address are remembered
  instead of asked of the driver each call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff and verified locally on Windows 11 x64 (RTX 5090):

  • Tracking the indirect argument buffer's state so consecutive clamps transition directly from INDIRECT_ARGUMENT to UNORDERED_ACCESS (and back to COMMON once on HostList::close) plus caching host command signatures is clean and sound.
  • cargo test -p victory-gpu --lib indirect:: -- --test-threads=1 (RTX 5090): 7 passed, 0 failed (including argument_buffers_move_from_their_current_states and each_execution_reads_its_own_clamped_records).
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1 (RTX 5090): 21 passed, 0 failed (including host_signatures_are_made_once).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

Independent local verification of PR #3307 on exact head f23c88f90d322f2dcb7b13b143711e0d4dc34f39. Control: origin/main 63bcca43c6a00a758510398fa43096ce7e4f7df0.

Environment: Windows 11 x64, Rust 1.98.1 MSVC. WARP only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11 on PATH, no physical GPU, no game files. All runs used --locked -j 4 --no-fail-fast -- --test-threads=1.

Head, cargo test -p victory-gpu --lib --features diagnostics: 248 passed, 1 failed, 1 ignored. The PR's new and changed tests all passed: indirect::tests::each_execution_reads_its_own_clamped_records, host::tests::a_list_holds_the_pipeline_it_last_kept_until_its_reset, draw::tests::host_signatures_are_made_once, and the updated indirect::tests:: tests (argument_buffers_move_from_their_current_states, argument_clamp_builds, a_record_that_does_not_fit_ends_the_budget, a_spent_budget_stays_spent, rejected_records_do_not_wrap_the_budget, the_budget_goes_to_the_first_records).
Head, without diagnostics: 233 passed, 1 failed, 1 ignored.

The one failure is frame::tests::waits_end_on_ticks_and_count_frames (frame.rs:231). It failed in every run on this machine under WARP, on head and on main (5/5 each, run alone). The PR doesn't touch frame.rs.

Control on origin/main (--features diagnostics): 244 passed, 2 failed, 1 ignored. The failures were the same frame test plus pipeline::placed_uav_tests::placed_uav_dispatch_writes_its_texture, which passed on head. I saw that one only once, so I'm treating it as a flake and not as a result of this PR. Head has 250 tests against main's 247, the three new tests.

I did not mutation-test the PR's claim that the test fails with the clamp barrier reverted to "from common". Timing claims were not checked (WARP is not a performance measure).

AI use: Claude Code (Claude Sonnet 5.5).

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

I resolved the conflicts with #3197 at 0c997d4 and #2996 at 5385cc6 in the exact integration af504f3. The window and single-clamp paths share one argument buffer. Both now take its incoming state from HostList::arguments(). Closing a logical window leaves the buffer readable. Growth or command-list close returns the retired buffer to COMMON. Allocating window scratch first means a refused scratch allocation cannot advance the tracked argument state.

On verified app-local Agility 1.620.2/WARP 1.0.22, all 15 indirect tests passed. The mixed test alternates mesh windows and single dispatch clamps in one list, includes a 6,000-record clamp that grows the argument buffer, checks every readback, and replays the recorded barriers to COMMON. The translated 24-group mesh test passed with the window switch both off and on. Full GPU diagnostics passed 259 tests with 1 ignored in both settings on rerun. One parallel window-on run hit a_slot_rewritten_while_its_view_is_prepared_views_the_new_record; the exact descriptor test passed four isolated runs on each of main and the integration, and the subsequent serial and parallel integration suites passed. Strict GPU Clippy passed with default and all features, and policy passed.

@KainanP and @Slay326 this branch preserves both incoming heads if the merge order makes it useful. The tests cover correctness; I did not rerun the laptop timing comparison. If reusing the resolution, please retain its Git author or add Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com> if folding it into another commit. AI-assisted with Codex.

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.

4 participants