Skip to content

GPU: opt-in batched argument clamp for runs of indirect draws - #9

Merged
wbkepler merged 1 commit into
mainfrom
gpu/indirect-clamp-batch
Nov 25, 2026
Merged

wbkepler merged 1 commit into
mainfrom
gpu/indirect-clamp-batch

Conversation

@wbkepler

Copy link
Copy Markdown
Owner

What

Every indirect execution records its own argument clamp: a UAV barrier, a compute dispatch, a second UAV barrier, two transitions of the argument buffer, and a forced pipeline + compute-root rebind afterwards. The world records ~5,100 indirect executions per frame, so each pair of draws has a graphics → compute → graphics round trip between them.

This adds an opt-in batched clamp (VICTORY_INDIRECT_CLAMP=batch, crates/victory-gpu/src/indirect.rs). The default is unchanged (one clamp per execution).

With batch, consecutive indirect draws share one clamp dispatch:

  • The dispatch is recorded before the first draw of a run and walks a job table (offset, kind, stride, records) in the list's upload memory. Later draws of the run only append a job at record time: no dispatch, no barriers, no transitions, no rebind. The budget is still charged in execution order.
  • A record joins the open batch only while nothing recorded since its dispatch can have written it:
    • compute work (bind with a compute pipeline) ends the batch;
    • a draw whose shaders write wherever they compute (heap-direct writes, title-memory writes) closes the batch to later records;
    • a record inside a range the list's draws declared they write (defer_gpu_write) gets a new clamp;
    • a draw joining after a draw that declared writes gets a UAV barrier alone.
  • Indirect dispatches keep a clamp of their own.
  • The pipeline/compute-root rebind is forced only when a clamp was actually recorded.

VICTORY_INDIRECT_CLAMP=always|batch|never: always (default) = previous behaviour, batch = the above, never = no clamp (diagnostics only: garbage arguments can then hang the device).

Diagnostics: the frame-rate line now ends with N argument clamps for M indirect executions/frame, and a second line gives why batches were opened (none open / other buffer / shader writes anywhere / written range / job memory full) plus the lone barriers.

Measured (RTX 5090, diag build, world, 240 s, same binary, VICTORY_GPU_UPLOAD=1 VICTORY_GPU_UPLOAD_WRITES=1 VICTORY_LAZY_COPY_BACK=1, steady state ~+180..+239 s, medians)

always (default) batch
clamps / indirect executions per frame 5,095 / 5,095 ~1,057 / ~5,100
GPU ms/frame 85.8 (81–99) 62.4 (55–79)
frame ms 367 (346–415) 352 (319–398)
preparing ms 217 224

Batching cuts GPU time by ~23 ms/frame (~27%), but the frame is CPU-bound (preparing + copy-back), so frame time moves only within run-to-run noise. No frame-rate win is claimed. Remaining batches per frame: ~33 first draw of a list / after compute, ~36 another argument buffer, ~35 after shader writes anywhere, 0 written range, 0 job memory full; 0 lone barriers.

Why opt-in

In one batch world run, a cutscene frame showed a skinned character with stretched/exploded triangles. A per-execution-clamp run of the same shot showed a clean silhouette. The two later runs (batch and always with identical keys) reached a dark section of the cutscene where both show the same pre-existing triangle fragments, so they neither confirm nor clear the batching. I did not find the missed hazard by reading the code (all in-list writers I could find either end the batch or are declared). The suspect is clamping a record before the draws that come before it in the run. So batch stays opt-in until a like-for-like shot is clean.

Tests

  • consecutive_draws_share_one_clamp: draws join one clamp, a declared write over a record and compute work each start a new one, every named record is clamped, an unnamed one stays as written.
  • the_clamp_switch_reads_its_three_values.
  • Existing clamp tests pass unchanged (they exercise the per-execution path, the default).

Gate: cargo clippy -p victory-gpu --all-targets -- -D warnings, cargo clippy -p victory --features diagnostics --all-targets -- -D warnings, cargo test -p victory-gpu -- --test-threads=1 (181 passed), cargo xtask policy.

Known gaps / follow-ups

  • Find the hazard behind the stretched triangles under batch (capture the same cutscene shot with always and batch), then consider making it the default. Even then it only pays off once the CPU side of the frame is smaller.
  • Arguments the CPU wrote are still clamped on the GPU. Validating them on the CPU at record time is not sound: lists are recorded before the submissions ahead of them have run, so title memory may still hold the bytes from before a shader's write.
  • A batch is per argument buffer: draws that alternate between two argument buffers get a clamp each.
  • More than 16 distinct declared write ranges since a batch's dispatch close it to later records (conservative).
  • While a batch is open, the argument buffer stays in the indirect-argument state across its draws. It goes back to unordered access when the batch ends (compute work, a new batch, list close).

🤖 Generated with Claude Code

Every indirect execution records a clamp of its own: a UAV barrier, a
compute dispatch, a second barrier, two transitions of the argument
buffer and a forced pipeline and compute root rebind, between every two
of the world's thousands of indirect draws.

VICTORY_INDIRECT_CLAMP=batch lets consecutive indirect draws share one
clamp dispatch. It is recorded before the first of them and walks a
table of jobs (offset, kind, stride, records) in the list's upload
memory; the draws after it only add a job while the list is recorded,
and keep the bound pipeline and the buffer's state. A record joins the
open batch only while nothing recorded since its dispatch can have
written it: compute work ends the batch, a draw whose shaders write
wherever they compute (heap-direct or title-memory writes) closes it to
later records, and a record inside a range the list's draws declared
they write gets a new clamp. A draw joining after a draw that declared
writes gets a barrier alone. Indirect dispatches keep a clamp of their
own.

The default stays one clamp per execution (always): batches cut the
world's GPU time by about a third on an RTX 5090, but one run showed a
skinned character with stretched triangles that the per-execution clamp
never showed, and the cause is not found. never records no clamp
(diagnostics).

The frame-rate line now ends with the clamps recorded and the indirect
executions they bound per frame, and a second line gives why batches
were opened and the barriers recorded alone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wbkepler
wbkepler merged commit ec3bd7f into main Nov 25, 2026
1 check passed
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.

1 participant