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

Open
github-bot wants to merge 1 commit from github-bot/gh-3307 into main AGit
Owner

@⁠KainanP on GitHub · 2026-11-26 16:53 UTC · original
KainanP:perf/indirect-dispatch → main · +274 −61 · 3 files · 1 commits · open

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

**@⁠KainanP** on GitHub · 2026-11-26 16:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/3307) `KainanP:perf/indirect-dispatch` → `main` · +274 −61 · 3 files · 1 commits · open 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](https://claude.com/claude-code)
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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 16:57:40 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3307/head:github-bot/gh-3307'
git switch 'github-bot/gh-3307'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3307'
git switch 'github-bot/gh-3307'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3307'
git switch 'github-bot/gh-3307'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3307'
git switch 'main'
git merge --squash 'github-bot/gh-3307'
git switch 'main'
git merge --ff-only 'github-bot/gh-3307'
git switch 'main'
git merge 'github-bot/gh-3307'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3307
No description provided.