indirect: clamp a window's records in lane order, not scheduler order #863

Open
xiaotu030202 wants to merge 1 commit from upstream-pr/864 into main
xiaotu030202 commented 2026-11-25 11:03:01 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by xiaotu030202 on 2026-11-25T11:03:01Z.


What this fixes

The argument clamp charges each list's budget as it clamps a window's records, and the draws whose records did not fit run empty. It charged a group's whole chunk of 64 records from the group's first thread and then marked each record's fit from that one running total — which is only right if the groups run in order. D3D12 defines no order between the groups of one dispatch, so they do not.

With 128 records, the first chunk's charges and the second chunk's raced for the one budget word. On WARP the second chunk won, and the test that asserts the lane order caught it:

panicked at crates/victory-gpu/src/indirect.rs:1539:
the first two real draws fit the budget

PROBE kept i=64
PROBE kept i=65

Expected i = 1, 2; got i = 64, 65. The spent counter was still correct (134217728 = 2^27 = BUDGET_CAP), so the budget arithmetic is fine — only which records got it was unordered.

This is not only a test failure. A window that keeps a scheduler-dependent set of records means the same list clamps differently between runs, machines and drivers: a scene that fits on one run can lose draws on the next. Frames that lost the device (#59, #833) are exactly the kind of thing this class of nondeterminism produces.

Measured here (Windows 11, RTX 5070 Ti, Rust 1.98.1 pinned, --test-threads=1):

before after
victory-gpu 158 passed, 1 FAILED 159 passed, 0 failed
--workspace (same failure) all green except a git-on-PATH environment issue in xtask

The same test passes on Zettrox/ztrx, which never had the per-chunk optimisation — that fork charges per record, which is what this returns to.

What changed

crates/victory-gpu/src/indirect.rs, one file:

  1. Each record charges the budget on its own, in lane order within its group, instead of one thread charging the group's chunk. The saturating compare-and-swap is the shape ztrx uses; the costs[]/fits[] shared arrays are gone.
  2. The caller dispatches one group per 64 records, with a UAV barrier between them, so the chunks also charge in lane order. base (a seventh 32-bit root constant) says which 64 a dispatch clamps; the root signature grows by one.
  3. The diagnostics tallies (largest_asked, all_asked) reset per group, which is what they were already doing implicitly.

Why not loop the chunks inside one group

I tried that first, twice, and HLSL 5.1 refused both:

clamp(121,9-41): error X3663: thread sync ...
clamp(64,5-53):  error X3663: this loop dependent on p...

The bound came from a UAV read (eff_count), then from a constant buffer (records), then from a compile-time constant 4096 with a uniform break — all three rejected, because FXC will not put a group barrier in a loop it cannot prove uniform. The dispatch loop is what the compiler accepts, and it is also a stronger guarantee: group order within a dispatch is simply not something HLSL can order.

Cost: a 4,096-record window goes from 64 dispatches to 64 dispatches with a barrier between them, each a 64-thread group. The clamp is a pre-draw reflection pass on argument buffers, not the hot path, and the previous shape was wrong; I have not measured the frame-rate effect because I cannot run the title (no game files). If it shows up, VICTORY_INDIRECT_* switches around bound/bound_ahead are where to turn it down.

Testing

$ cargo test -p victory-gpu --lib -- --test-threads=1
test result: ok. 159 passed; 0 failed; 4 ignored

$ cargo test --workspace -- --test-threads=1
... every suite ok ...
test result: ok. 215 passed; 0 failed; 3 ignored   (victory-shader)

xtask's repository_passes_policy fails in my environment because git is not on the PATH that run used; re-run with it on PATH it is 4 passed. Not related to this change.

No game files were involved: every test here builds its own buffers and shaders.

What I could not check

Whether the clamp now picks the same records a real RDNA2 card does. The lane order is what the budget is defined by, and this restores it, but I have no AMD card and no game files — so the evidence here is the WARP test, the ztrx cross-check, and the reasoning that a single group per dispatch cannot be out of order with itself.

<!-- greyfinch-sync:fxceless/victory-fork:issue:6603004661 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/864). Originally posted by [xiaotu030202](https://github.com/xiaotu030202) on 2026-11-25T11:03:01Z. --- ## What this fixes The argument clamp charges each list's budget as it clamps a window's records, and the draws whose records did not fit run empty. It charged **a group's whole chunk of 64 records from the group's first thread** and then marked each record's fit from that one running total — which is only right if the groups run in order. D3D12 defines **no order** between the groups of one dispatch, so they do not. With 128 records, the first chunk's charges and the second chunk's raced for the one budget word. On WARP the second chunk won, and the test that asserts the lane order caught it: ``` panicked at crates/victory-gpu/src/indirect.rs:1539: the first two real draws fit the budget PROBE kept i=64 PROBE kept i=65 ``` Expected `i = 1, 2`; got `i = 64, 65`. The `spent` counter was still correct (`134217728 = 2^27 = BUDGET_CAP`), so the budget arithmetic is fine — only *which* records got it was unordered. This is not only a test failure. A window that keeps a scheduler-dependent set of records means the same list clamps differently between runs, machines and drivers: a scene that fits on one run can lose draws on the next. Frames that lost the device (#59, #833) are exactly the kind of thing this class of nondeterminism produces. Measured here (Windows 11, RTX 5070 Ti, Rust 1.98.1 pinned, `--test-threads=1`): | | before | after | |---|---|---| | `victory-gpu` | 158 passed, **1 FAILED** | **159 passed, 0 failed** | | `--workspace` | (same failure) | all green except a `git`-on-PATH environment issue in `xtask` | The same test passes on `Zettrox/ztrx`, which never had the per-chunk optimisation — that fork charges per record, which is what this returns to. ## What changed **`crates/victory-gpu/src/indirect.rs`**, one file: 1. **Each record charges the budget on its own**, in lane order within its group, instead of one thread charging the group's chunk. The saturating compare-and-swap is the shape `ztrx` uses; the `costs[]`/`fits[]` shared arrays are gone. 2. **The caller dispatches one group per 64 records**, with a UAV barrier between them, so the chunks also charge in lane order. `base` (a seventh 32-bit root constant) says which 64 a dispatch clamps; the root signature grows by one. 3. The diagnostics tallies (`largest_asked`, `all_asked`) reset per group, which is what they were already doing implicitly. ## Why not loop the chunks inside one group I tried that first, twice, and HLSL 5.1 refused both: ``` clamp(121,9-41): error X3663: thread sync ... clamp(64,5-53): error X3663: this loop dependent on p... ``` The bound came from a UAV read (`eff_count`), then from a constant buffer (`records`), then from a compile-time constant `4096` with a uniform `break` — all three rejected, because FXC will not put a group barrier in a loop it cannot prove uniform. The dispatch loop is what the compiler accepts, and it is also a stronger guarantee: group order within a dispatch is simply not something HLSL can order. Cost: a 4,096-record window goes from 64 dispatches to 64 dispatches with a barrier between them, each a 64-thread group. The clamp is a pre-draw reflection pass on argument buffers, not the hot path, and the previous shape was wrong; I have not measured the frame-rate effect because I cannot run the title (no game files). If it shows up, `VICTORY_INDIRECT_*` switches around `bound`/`bound_ahead` are where to turn it down. ## Testing ``` $ cargo test -p victory-gpu --lib -- --test-threads=1 test result: ok. 159 passed; 0 failed; 4 ignored $ cargo test --workspace -- --test-threads=1 ... every suite ok ... test result: ok. 215 passed; 0 failed; 3 ignored (victory-shader) ``` `xtask`'s `repository_passes_policy` fails in my environment because `git` is not on the PATH that run used; re-run with it on PATH it is 4 passed. Not related to this change. No game files were involved: every test here builds its own buffers and shaders. ## What I could not check Whether the clamp now picks the same records a real RDNA2 card does. The lane order is what the budget is defined by, and this restores it, but I have no AMD card and no game files — so the evidence here is the WARP test, the ztrx cross-check, and the reasoning that a single group per dispatch cannot be out of order with itself.
SkillIssueXO commented 2026-11-25 12:07:01 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by SkillIssueXO on 2026-11-25T11:12:12Z.


The dispatch barriers order the 64-record chunks. Inside each chunk, active && cost > 0 still lets active threads race for the budget word in InterlockedCompareExchange. A later record can take the remaining budget before an earlier one. D3D12 gives no execution-order guarantee within a thread group, including between waves in that group. DirectX spec

Could this keep f85101b’s groupshared costs / fits and the gi == 0 ordered walk, with your per-chunk dispatch and barrier change? That would order records at both levels. A useful synthetic check would put the budget cutoff inside one chunk and place valid records on both sides of lane 32, then require the earliest records to survive on both WARP and a Wave32 GPU.

AI-assisted source review. This comment is based on the shader code and the ordering rules.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7160731939 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/864#issuecomment-7160731939). Originally posted by [SkillIssueXO](https://github.com/SkillIssueXO) on 2026-11-25T11:12:12Z. --- The dispatch barriers order the 64-record chunks. Inside each chunk, `active && cost > 0` still lets active threads race for the budget word in `InterlockedCompareExchange`. A later record can take the remaining budget before an earlier one. D3D12 gives no execution-order guarantee within a thread group, including between waves in that group. [DirectX spec](https://microsoft.github.io/DirectX-Specs/d3d/WorkGraphs.html#thread-group) Could this keep f85101b’s `groupshared costs` / `fits` and the `gi == 0` ordered walk, with your per-chunk dispatch and barrier change? That would order records at both levels. A useful synthetic check would put the budget cutoff inside one chunk and place valid records on both sides of lane 32, then require the earliest records to survive on both WARP and a Wave32 GPU. AI-assisted source review. This comment is based on the shader code and the ordering rules.
MamaLeVrai commented 2026-11-25 13:06:00 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by MamaLeVrai on 2026-11-25T12:23:22Z.


Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head fb5c67a into main f85101b in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails with exactly the errors main itself has on this machine (placed_filler_twins, depth_written, a sort; PR #841 fixes them), also with --all-features; this PR adds none. cargo test --workspace --locked: run 1: 555 passed, 0 failed, 11 ignored; run 2: stopped at inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset (a failure main has now and then (PR #878)) and the victory-gpu test binary died with an access violation (it does that on main about 2 runs in 100, issue #850) (main alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7162306351 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/864#issuecomment-7162306351). Originally posted by [MamaLeVrai](https://github.com/MamaLeVrai) on 2026-11-25T12:23:22Z. --- Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head `fb5c67a` into `main` `f85101b` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings` fails with exactly the errors `main` itself has on this machine (`placed_filler_twins`, `depth_written`, a sort; PR #841 fixes them), also with `--all-features`; this PR adds none. `cargo test --workspace --locked`: run 1: 555 passed, 0 failed, 11 ignored; run 2: stopped at `inplace::tests::warp_a_title_buffer_in_place_shows_its_bytes_at_its_offset` (a failure `main` has now and then (PR #878)) and the `victory-gpu` test binary died with an access violation (it does that on `main` about 2 runs in 100, issue #850) (`main` alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/864:upstream-pr/864'
git switch 'upstream-pr/864'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/864'
git switch 'upstream-pr/864'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/864'
git switch 'upstream-pr/864'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/864'
git switch 'main'
git merge --squash 'upstream-pr/864'
git switch 'main'
git merge --ff-only 'upstream-pr/864'
git switch 'main'
git merge 'upstream-pr/864'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
fxceless/victory-fork!863
No description provided.