Skip to content

perf(gpu): clamp indirect arguments in group passes (VICTORY_CLAMP_GROUPS=1) - #616

Draft
hackgods wants to merge 2 commits into
pragyaangaur:mainfrom
hackgods:perf/clamp-groups-on-cuts
Draft

hackgods wants to merge 2 commits into
pragyaangaur:mainfrom
hackgods:perf/clamp-groups-on-cuts

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

What. #453 hoists an indirect execution's argument clamp into its list's prelude when nothing earlier in the list wrote the arguments. #570 starts a new host list for a clamp the list cannot hoist, so the clamps after it, up to the next write, are hoisted into that list's prelude. With VICTORY_CLAMP_GROUPS=1 (off by default), a clamp held out of the prelude goes into a group pass instead, in the same host list:

  • Where a pass goes. One dispatch is recorded where the first held-out execution's inline clamp would have been. It reads a table of jobs in the list's upload memory. The CPU keeps adding jobs while the list records, and the table is complete before the list runs.

  • Which executions join. Executions after it join the pass until one of these happens:

    • a recorded command writes what the next execution reads (the list's tracker of written ranges, now also kept per pass);
    • an inline clamp is recorded;
    • the pass's 512 jobs are taken;
    • a record lies outside the eight buffers the pass binds.

    Only the pass recorded last takes executions, and no clamp is hoisted after a pass, so every clamp of a host list is charged to its budget in recorded order.

  • Exact results. Each execution has its own job and its own 32-byte output slot. The pass's shader is the words clamp walking jobs instead of consecutive records, 64 lanes per tile with the budget charged by lane 0 in order. Its records are word for word what the inline clamps write.

  • Which executions are grouped. One record, no count buffer, 12 to 32 bytes: all of Grand Theft Auto VI's indirect executions.

  • The order of choices. A held-out clamp is put in a group if one can take it, else in a new host list where perf(gpu): start a host list for a clamp the list cannot hoist, instead of clamping every later draw inline #570's cut pays, else inline. A cut then only happens for what no group takes: a count, several records, a record outside the buffers a pass can bind.

  • Barriers. A pass costs two barrier calls. Its output stays readable as indirect arguments until the list closes, with no round trip through COMMON between passes (as in cc11ccc).

  • Prelude. The prelude's hoisted batch, in every list including perf(gpu): start a host list for a clamp the list cannot hoist, instead of clamping every later draw inline #570's cut ones, puts runs of such clamps into group passes too: one dispatch per run, instead of one dispatch per clamp with a UAV barrier after each.

  • In-place hook. indirect::clamped_in_place(arguments, len, count) returns false today. It is there for title memory read in place: such executions are clamped inline, never hoisted, grouped or cut for (avatarmanz 631400f).

The frame line keeps #570's reasons for inline clamps and its count of host lists started for clamps, with "read in place" added to the reasons. It also gives, per frame:

  • the hoisted clamps in prelude group passes and those passes;
  • the executions in group passes and those passes;
  • why clamps stayed out of the prelude: after a clamp of the list not in it, after a command that may write any title buffer, over bytes an earlier command wrote, read in place;
  • the clamp path's barrier calls and barriers;
  • draws recorded right after compute in the same host list, a stand-in for render passes split.

Why. In the prison cell, a frame logs about 4,000 clamps that cannot be hoisted: the title's culling compute writes the arguments of the draws after it in the same list. Inline, each is a dispatch and three barrier calls between two draws, and D3DMetal ends the render pass for it and starts another after it. A Metal System Trace of a frame on an M5 Pro counted 6,917 encoders (3,463 render, 3,453 compute). One command buffer held 1,789 compute/render pairs in turn. An encoder costs ~11 µs of D3DMetal and ~21 µs of kernel driver time. A probe of the turn itself (dispatch, barrier, indirect draw into a render target) measured 12–37 µs of recording and 16–20 µs of submission per turn with the barrier. It measured 0.03 µs and 0.2 µs without it, and recording grew with the square of the turns in one list.

#570's cuts remove the same turns. Each cut costs a new host list: its state is set again, its tables and root views are made again, and it has its own prelude and submission-time steps. A cut list's held-out clamps cut again or go inline. A group pass costs one dispatch in the list, usually in the producer's compute encoder. In game (below) #570 already removes most of the cost; groups add a modest gain on top of it and far fewer barriers.

Tests

  • indirect::tests::group_passes_clamp_as_inline_clamps_do. In one list, a dispatch writes every record over stale words, then an inline clamp spends half the budget. Then come executions of every kind: strides 12–32, 12 source buffers (past the eight one pass binds), one clamped inline among them (with a count), and records the budget no longer holds. Their group passes write word for word what inline clamps after the same producer write, reading what the producer wrote. Passes open at exactly the expected executions.
  • indirect::tests::a_write_ends_the_group_of_what_it_writes. Producer, four indirect dispatches, a write elsewhere (the group goes on), the producer again over the same records, four more dispatches. The executions after the second write get a pass of their own, and each dispatch runs the groups written last before it. The test runs twice through one list, reset between. A record outside its buffer is not grouped.
  • indirect::tests::prelude_group_passes_clamp_as_inline_clamps_do. Hoisted clamps of one record from ten buffers, mixed with multi-record, counted and empty ones, match inline clamps word for word, including records past the budget.
  • indirect::tests::groups_take_one_record_of_12_to_32_bytes_with_no_count, clamp_groups_switch_toggles_per_thread: what can be grouped, the shader's job cap matching the constant, and the switch.
  • perf(gpu): start a host list for a clamp the list cannot hoist, instead of clamping every later draw inline #570's a_clamp_cut_prelude_sees_the_write_of_the_list_before and write_hazard_names_what_blocks_a_clamp, unchanged, still pass.
  • Deliberately breaking the per-pass write tracking, the budget check, or the copy of a record's trailing words makes these tests fail.
  • On main 74d26f9 (with perf(gpu): start a host list for a clamp the list cannot hoist, instead of clamping every later draw inline #570):
  • Before perf(gpu): start a host list for a clamp the list cannot hoist, instead of clamping every later draw inline #570, on main a31fcca and 1fcc386:
    • test-wine.sh -p victory-gpu: 548 passed. Two descriptor tests and one list test, which also fail on plain main under load, failed in earlier parallel runs and passed alone.
    • test-wine.sh -p victory-shader: passed.
    • Native cargo test -p victory-gpu: 188 passed.

In game, on top of #570. M5 Pro, macOS 27, release build of this branch on main 74d26f9. One binary, three arms, runs in the order A B C C B A, last 120 s of each.

arm frame (ms) waiting on the GPU (ms) barrier calls of the clamp path a frame draws recorded after compute a frame
A, inline (2 runs) 582 / 583 323 / 324 13,490–13,514 3,379–3,381
B, #570's cuts (2 runs) 474 / 464 198 / 197 5,913–5,934 109
C, groups (2 runs) 453 / 456 187 / 188 261–263 43

In C, 4,151–4,155 clamps a frame are in 72–73 group passes.

#570 takes most of the gain: against inline, the wait for the GPU drops by ~126 ms. On top of #570, groups take another ~9 ms off the GPU wait and ~14 ms off the frame, a modest gain. They also cut the clamp path's barrier calls 23-fold (from ~5,900 to ~260 a frame), and the draws recorded right after compute drop from 109 to 43. Fewer barriers should count for more on hosts where a barrier stalls the GPU longer than it does under D3DMetal on an M5 Pro (not measured here). Groups also keep each list whole and its one budget charged in recorded order.

In game, before #570 (M5 Pro, macOS 27, release build of this change on main 1fcc386, runs interleaved A B B A, last 120 s of each). Arm A leaves VICTORY_CLAMP_GROUPS unset (inline, main's behaviour then); arm B sets VICTORY_CLAMP_GROUPS=1.

arm frame (ms) waiting on the GPU (ms)
A, inline (2 runs) 649 / 525 334 / 324
B, VICTORY_CLAMP_GROUPS=1 (2 runs) 468 / 417 189 / 184

Counters per frame, A → B:

counter A B
inline clamps 4,039–4,186 0
executions in group passes (passes) 0 4,166–4,203 (73)
hoisted clamps in prelude group passes (passes) 0 ~880 (34)
barrier calls of the clamp path 13,082–13,525 263–264
draws recorded after compute in one list 3,242–3,385 43
clamps held out after a write anywhere 0 0

The wait for the GPU drops by ~140 ms a frame. Preparing does not change (160–170 ms in both arms).

The same switch in an integration build that has this change with other pending fixes (7c216b0). Runs were interleaved A B C D D C B A, and arms B and C differ only by this switch. Last 120 s, medians of the frame line:

arm frame (ms) preparing (ms) waiting on the GPU (ms)
B, VICTORY_CLAMP_GROUPS unset (2 runs) 498 / 557 140 / 163 322 / 338
C, VICTORY_CLAMP_GROUPS=1 (2 runs) 370 / 415 104 / 135 175 / 182

Picture. Diagnostics captures were taken in the integration build, of the prison cell at +600, +700 and +800 s into the world. With VICTORY_CLAMP_GROUPS unset and with it set (and nothing else changed), they show the same content. The per-pixel differences between the two arms (mean 5–6) are the size of the frame-to-frame variation within one run (mean 5.07).

Prior art. The design follows avatarmanz's VIctory-7900XTX 0375962: one pass clamps many executions from a table of jobs that the CPU fills while the list records. It also follows their cc11ccc: clamped records left in INDIRECT_ARGUMENT. halfwave50's main-mac VICTORY_BATCH_CLAMPS, from 64bitMango 18872f48 and 131721b7 after Belobug ccab973, batches a run's clamps on macOS in one dispatch walking a table in draw order, recorded in a host list cut before the drawing one. #570 cuts the list in the same place. Here the pass stays in the drawing list, a group ends only where a write meets what the next execution reads, and the list's budget is charged in recorded order. All three forks are GPL-3.0.

🤖 Generated with Claude Code

@pragyaangaur

Copy link
Copy Markdown
Owner

Main now has #588 (scoped indirect clamp barriers) and #584, which both touch the same code in indirect.rs, draw.rs and host.rs. This PR conflicts in five places there, and its two indirect tests (a_write_ends_the_group_of_what_it_writes and group_passes_clamp_as_inline_clamps_do) failed in CI on the older base. Could you rebase it onto current main, check that the group passes and #588's scoped barriers fit together, and push again? I did not resolve it myself because the right merge of the two clamp designs is a design call that is yours to make.

@hackgods
hackgods force-pushed the perf/clamp-groups-on-cuts branch from ef73e3c to 42d8285 Compare November 28, 2026 02:45
@Zeyphers

Copy link
Copy Markdown
Contributor

Tried this on an M1 Max (32 GB, macOS 15.7.3, D3DMetal 4.0b3) on top of main 4396e31 with VICTORY_RAY_SKIP=1,shadow (#660) and the #454 memory set.

With VICTORY_CLAMP_GROUPS=1 the frame got faster: median world frame 527 ms against 641 ms without it. Every clamp went into group passes ("1337 hoisted (1342 in 44 prelude group passes), 0 inline").

But the picture breaks. In a 7-minute walk around the cell, brightness jumps went from 1.5 to 11.1 per 100 frames. Mean brightness went from about 90 to 144. The frames show white radial rays and tile-shaped blocks in the light, and whole frames washed out white for a few frames at a time. These are the same shapes the local lights' shadow rays made before #660 left them out.

My guess is that some indirect draws or dispatches read arguments clamped before the culling pass that writes them, so they use the previous frame's records. Happy to run a diagnostics build with a log level or switch you name.

@hackgods

Copy link
Copy Markdown
Contributor Author

Thanks @Zeyphers, that's a clear report. The rebased head still fails a_write_ends_the_group_of_what_it_writes, group_passes_clamp_as_inline_clamps_do and argument_clamp_builds in CI, and stale arguments from a group that doesn't end at the culling pass's write would explain both the failures and the white rays. Marking this draft until the group passes end at every write to the argument and count buffers on current main (including #588's scoped barriers and #584's prepared submissions), with a regression test of a culling dispatch writing the arguments right before the draws.

@hackgods
hackgods marked this pull request as draft November 28, 2026 04:06
hackgods and others added 2 commits November 28, 2026 08:09
…OUPS=1)

pragyaangaur#453 hoists an indirect execution's clamp into its list's prelude only
when nothing earlier in the list wrote its arguments. pragyaangaur#570 starts a new
host list for a clamp the list cannot hoist, so the clamps after it, up
to the next write, are hoisted into that list's prelude. A world frame
in the prison cell has ~4,000 such clamps (the title's culling passes
write the arguments in the same list); inline, each is a compute
dispatch and three barrier calls between two draws, which D3DMetal
records as a compute encoder and a new render encoder (Metal System
Trace, M5 Pro: 6,917 encoders a frame).

With VICTORY_CLAMP_GROUPS=1 (off by default) a held-out clamp goes into
a group pass instead, recorded where its inline clamp would be, in the
same host list: one dispatch reading a table of jobs the CPU goes on
filling while the list records. It clamps every execution after it until
a command writes what the next one reads (the list's tracker of written
ranges, also kept per pass), an inline clamp is recorded, its 512 jobs
are taken, or a record is in none of the eight buffers it binds. Each
execution has its own job and slot, charged to the list's budget in
recorded order: the records are word for word what inline clamps write.
One record without a count (all of the title's) is grouped. A pass costs
two barrier calls; its output stays readable as arguments until the list
closes. The prelude's batch puts runs of such clamps in group passes
too, one barrier per pass instead of one per clamp.

The decision is: hoist, else group, else a cut (pragyaangaur#570, where it pays),
else inline. A cut is then made only for what no group takes (a count,
several records, a record outside the bound buffers, no pipeline).

The frame line keeps pragyaangaur#570's reasons for inline clamps and the cuts, and
also counts clamps in prelude group passes, executions in group passes
and the passes, why clamps stayed out of the prelude, the clamps' barrier
calls and barriers, and draws recorded after compute in one host list
(render passes split). A hook, indirect::clamped_in_place, clamps
arguments read in place in title memory inline (never hoisted, grouped or
cut for) for in-place section views.

With VICTORY_CLAMP_SYNC=1 (pragyaangaur#588) every clamp path records its enhanced
global barriers, group passes included, and their outputs stay common.
The barrier before a group pass waits for every shader stage, not only
compute as an inline clamp's does: an execution that joins the pass may
read a record written before it whose title barrier comes after it. In
the prelude the budget's barrier between dispatches is one per group run.

A culling dispatch that writes the records again after a pass ends its
group: tests run culling dispatches and indirect draws in one list, and
the culling write through a table's view (as a title dispatch binds it,
with VICTORY_TABLE_MEMO on or off); with the check of writes since the
pass taken out, the draws after the second culling draw the first one's
records.

On Windows the group pass did not build (fxc), so the group tests found
no seat; Wine's compiler took it. The pass now walks the eight tiles a
table holds whatever its count (fxc refuses barriers in a loop up to a
count read from memory, as indirect-clamp.hlsl notes), its constant
buffer is no longer named Pass (an effects keyword), and
argument_clamp_builds shows the compiler's messages if it fails.

The design follows avatarmanz's VIctory-7900XTX 0375962 (clamps of many
executions in one pass from a job table filled while the list records)
and cc11ccc (clamped records left in INDIRECT_ARGUMENT), GPL-3.0. On
macOS, halfwave50's VICTORY_BATCH_CLAMPS (main-mac, from 64bitMango
18872f48 and 131721b7, after Belobug ccab973) batches a run's clamps in
one dispatch walking a table in draw order, recorded in a host list cut
before the drawing one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every clamp charges a host list's indirect executions to one budget of
2^27 (vertex instances, threads), and a record past what is left runs
empty. One record costs at most half of it (2^26 instances or 2^20 groups
of 64), so a list that spent half or less emptied nothing for the budget.
With pragyaangaur#570's cuts each new host list has a budget of its own; clamp groups
keep a title list in one host list and one budget, as inline clamps do.

At a list's reset the word it spent is read (and cleared), and the clamp
part of the frame line now ends with how many host lists a frame spent
more than half of their budget, and the most one spent. A run with and
without VICTORY_CLAMP_GROUPS=1 shows whether groups empty records there
that cuts did not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hackgods
hackgods force-pushed the perf/clamp-groups-on-cuts branch from 42d8285 to 5518b81 Compare November 28, 2026 04:10
@hackgods

Copy link
Copy Markdown
Contributor Author

Thanks, this is exactly the kind of run the switch needed.

I checked the guess first. A group pass takes an execution only if nothing recorded since the pass writes that
execution's record, and every way a shader write enters a list counts there: views in tables, root views, title
memory, and heap writes as "anywhere". There are two new tests. In the first, a compute culling pass writes four
draw records, four indirect draws run, the culling pass writes the records again, and four more draws run. The
second declares the culling write through a table's view the way a title dispatch does. With the check taken out,
the draws after the second culling pass draw the first pass's records, and the tests fail. With it in, they pass.
So stale records from an earlier culling pass in the same list shouldn't happen, but your run says something
differs.

One difference from main's default is the clamp budget (though see our numbers below: in our runs no list came near it). Every host list's indirect executions share a budget of
2^27 vertex instances or threads, and records past it run empty. #570's cuts start new host lists, each with a fresh
budget. With groups, a title list keeps one budget, as it does with inline clamps. If a list runs out, its last draws
and dispatches are dropped, which could look like what you saw. A record costs at most half the budget, so a list
that spends half or less drops nothing. Head 5518b81 adds the count to the frame line, at the end of the clamp part:
"N host lists past half of their budget, the most X% of it".

If you can, three runs would separate the causes:

  1. The same walk on 5518b81 with VICTORY_CLAMP_GROUPS=1, plus that part of the frame line.
  2. The same walk with VICTORY_CLAMP_CUTS=0 and groups off, which is inline with one budget per list. Flashes there
    too would point at the budget, not the groups.
  3. With the game closed, tools/macos/test-wine.sh -p victory-gpu --lib -- indirect on the M1. The group pass hands
    costs between the lanes of a group through device memory. On an M5 Pro every word matches the inline clamps, but
    I can't check an M1.

On an M5 Pro (main 4396e31 + this PR at 5518b81, one exe, A0 B C C B A0, 5-minute runs, walking and turning in the cell from 150 s):

median frame P95 host lists past half of their budget brightness jumps between shots (>10) near-white pixels, max
A0: main default (cuts) 407 ms 426 ms 0, the most 36 % 0 0.5 %
B: VICTORY_CLAMP_GROUPS=1 391 ms (378, 405) 412 ms 0, the most 35-36 % 2 in one run (a darker shot, no white) 0.4 %
C: B + VICTORY_CLAMP_SYNC=1 394 ms (400, 389) 428 ms 0, the most 36 % 0 0.5 %

So here no list comes near its budget, and I couldn't make the white shapes appear with groups on. Shots are every 20 s, so one-frame flashes can slip between them. I also checked that VICTORY_RAY_SKIP can't be bypassed by the group path: ray_pass_left_out is tested at the top of the indirect call, before the arguments are refreshed or clamped, in both paths. The budget line from your walk on 5518b81 (run 1 above) is the most useful next datum, together with the VICTORY_RAY_SKIP frame line's count of passes left out, so we can see whether the shadow-ray passes are still being skipped in your runs with groups on.

@buddybot89

Copy link
Copy Markdown

Confirmed on an M2 Ultra (64 GB, macOS 26.7.1, D3DMetal 4.0b3) at 5518b81: indirect:: and draw:: gave 81 passed, 0 failed, 3 ignored.

@hackgods

Copy link
Copy Markdown
Contributor Author

Thanks @buddybot89. That covers the check I couldn't do here: the group pass hands lane costs through device memory, and on the M5 Pro every word matched the inline clamps, so a pass on another GPU family (M2 Ultra) at 5518b81 is the confirmation it needed. It stays draft until the in-game walk with the budget part of the frame line comes back.

@nickorlabs

Copy link
Copy Markdown
Contributor

Draft PR; not ready to merge. I’m queuing this after #704/#707/#708 so we can review the non-draft changes first.

@nickorlabs

Copy link
Copy Markdown
Contributor

Keep this draft for one more measurement pass. It needs the M1 budget/ray-skip data to separate causes before merging. The current head's CI is green, but I'd not merge the draft until the in-game data is in.

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.

5 participants