Shaders, GPU: VICTORY_LOOP_CUTS=1 counts the lanes translated loops leave at the loop limit #2731

Open
Exopus-0x65f7ad0 wants to merge 1 commit from upstream-pr/2733 into main
Exopus-0x65f7ad0 commented 2026-11-25 19:20:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Exopus-0x65f7ad0 on 2026-11-25T19:20:00Z.


Follow-up to #1819, as promised there and asked for by the triage: a way to count how often translated loops are cut at the loop limit, so the default can be decided on evidence instead of argued.

What it adds

VICTORY_LOOP_CUTS=1 (diagnostics builds only, like VICTORY_SHADER_LOOP_LIMIT; off by default, nothing changes without it):

  • Translator (victory-shader): a lane that leaves a loop because of LOOP_LIMIT does one InterlockedAdd(1) on the first dword of a new raw root UAV, u2 in the memory space (LOOP_CUTS): on a conditional back edge, guarded by "the branch wanted to go round again and the limit turned it" (built with select, no xor of compares); on an unconditional back edge, in the block it leaves through (abort_block). Module notes: "Loop cuts".
  • Host (victory-gpu): while the switch is on, every root signature declares that root UAV and every draw and dispatch binds a 256-byte counter of ours (loop_cuts.rs). The frame line gets one more line every 5 s:
    translated loops left at the limit (VICTORY_LOOP_CUTS): N lanes since the last line, M since the start (with the limit in force). The counter is never reset while shaders may add to it; its 32-bit wraps are followed.
  • Shader cache: the switch is part of the key, and only when on, so existing caches keep their key.
  • The offline root signature (emit/host.rs, victory translate --pipelines) declares the counter too.

Why every pipeline and not only those with loops: the switch is a run-wide diagnostic, and a root signature may declare more than its shaders use. It costs one root descriptor (2 DWORDs) per pipeline, only while it is on.

How to use it for the #1819 question

Run a scene twice with a diagnostics build and VICTORY_LOOP_CUTS=1, once at VICTORY_SHADER_LOOP_LIMIT=16384 and once at the default 2^20 (or any lower limit you want to rule out). A loop that ends on its own never reaches the counter; any non-zero count at 2^14 that is not also there at 2^20 is a loop the lower limit would change.

Tests

  • emit::tests::the_loop_cuts_switch_is_on_only_for_1: parsing of the switch.
  • synthetic_tests::a_loop_left_at_the_limit_counts_its_lanes_only_when_asked: a GFX10 loop whose scalar counter only ends on wrap-around; off, the DXIL has no atomic; on, exactly one (guarded by the cut on the back edge), and it passes dxil.dll validation when present.
  • shader_cache::tests::the_key_covers_translator_encoding_and_settings: the switch changes the key.

Ran here on Windows 11, RTX 5060 Ti: the three tests above pass. The rest of victory-shader --lib and victory-gpu --lib behave as on main (13f91ca) on this machine: the same two victory-gpu tests fail there too (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views), and the victory-shader test process ends with the same access violation in a GPU test on both. In-game counts from a run will follow in a comment.

Writing the test showed something worth knowing for the #1819 discussion: on a conditional back edge (the usual loop shape) a lane at the limit does not go through the "killed" exit block at all; the limit is ANDed into the branch, so the lane simply leaves by the loop's normal exit and runs the code after the loop with whatever it had. Only unconditional back edges use the exit block that returns (and discards in pixel programs). The counter covers both.

No title data in this PR (no hashes, addresses, captures or logs from the game).

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:issue:6611927967 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2733). Originally posted by [Exopus-0x65f7ad0](https://github.com/Exopus-0x65f7ad0) on 2026-11-25T19:20:00Z. --- Follow-up to #1819, as promised there and asked for by the triage: a way to **count how often translated loops are cut at the loop limit**, so the default can be decided on evidence instead of argued. ## What it adds `VICTORY_LOOP_CUTS=1` (diagnostics builds only, like `VICTORY_SHADER_LOOP_LIMIT`; off by default, nothing changes without it): - **Translator** (`victory-shader`): a lane that leaves a loop because of `LOOP_LIMIT` does one `InterlockedAdd(1)` on the first dword of a new raw root UAV, `u2` in the memory space (`LOOP_CUTS`): on a conditional back edge, guarded by "the branch wanted to go round again and the limit turned it" (built with `select`, no `xor` of compares); on an unconditional back edge, in the block it leaves through (`abort_block`). Module notes: "Loop cuts". - **Host** (`victory-gpu`): while the switch is on, every root signature declares that root UAV and every draw and dispatch binds a 256-byte counter of ours (`loop_cuts.rs`). The frame line gets one more line every 5 s: `translated loops left at the limit (VICTORY_LOOP_CUTS): N lanes since the last line, M since the start` (with the limit in force). The counter is never reset while shaders may add to it; its 32-bit wraps are followed. - **Shader cache**: the switch is part of the key, and only when on, so existing caches keep their key. - The offline root signature (`emit/host.rs`, `victory translate --pipelines`) declares the counter too. Why every pipeline and not only those with loops: the switch is a run-wide diagnostic, and a root signature may declare more than its shaders use. It costs one root descriptor (2 DWORDs) per pipeline, only while it is on. ## How to use it for the #1819 question Run a scene twice with a diagnostics build and `VICTORY_LOOP_CUTS=1`, once at `VICTORY_SHADER_LOOP_LIMIT=16384` and once at the default 2^20 (or any lower limit you want to rule out). A loop that ends on its own never reaches the counter; any non-zero count at 2^14 that is not also there at 2^20 is a loop the lower limit would change. ## Tests - `emit::tests::the_loop_cuts_switch_is_on_only_for_1`: parsing of the switch. - `synthetic_tests::a_loop_left_at_the_limit_counts_its_lanes_only_when_asked`: a GFX10 loop whose scalar counter only ends on wrap-around; off, the DXIL has no atomic; on, exactly one (guarded by the cut on the back edge), and it passes `dxil.dll` validation when present. - `shader_cache::tests::the_key_covers_translator_encoding_and_settings`: the switch changes the key. Ran here on Windows 11, RTX 5060 Ti: the three tests above pass. The rest of `victory-shader --lib` and `victory-gpu --lib` behave as on `main` (`13f91ca`) on this machine: the same two `victory-gpu` tests fail there too (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`), and the `victory-shader` test process ends with the same access violation in a GPU test on both. In-game counts from a run will follow in a comment. Writing the test showed something worth knowing for the #1819 discussion: on a **conditional** back edge (the usual loop shape) a lane at the limit does not go through the "killed" exit block at all; the limit is ANDed into the branch, so the lane simply leaves by the loop's normal exit and runs the code after the loop with whatever it had. Only unconditional back edges use the exit block that returns (and discards in pixel programs). The counter covers both. No title data in this PR (no hashes, addresses, captures or logs from the game). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
kotekompotek commented 2026-11-25 21:03:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T20:07:04Z.


Useful diagnostic: the UAV is only bound when the pipeline actually carries the loop_cuts param and the counter exists, so pipelines built before the knob existed are untouched. Counter read via volatile loads on the shared buffer is right for a root UAV write. Frame-line integration keeps it zero-cost with the knob off.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7173213772 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7173213772). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T20:07:04Z. --- Useful diagnostic: the UAV is only bound when the pipeline actually carries the loop_cuts param and the counter exists, so pipelines built before the knob existed are untouched. Counter read via volatile loads on the shared buffer is right for a root UAV write. Frame-line integration keeps it zero-cost with the knob off.
fuzeofficlal commented 2026-11-25 21:03:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T20:10:46Z.


Reviewed and verified 6b213e18 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll) against main (13f91ca).

Code review

  • Zero default overhead: when diagnostics is off or VICTORY_LOOP_CUTS != 1, no u2 UAV, atomicBinOp, extra root-signature parameter, or D3D12 buffer is emitted/allocated, and shader_cache::key stays identical to existing caches.
  • Enabled path: u2 (MEMORY_SPACE) adds UAVS_EVERY_STAGE only when needed for non-pixel/compute stages; conditional back-edges use select(orig, stop, false) and update self.tail[b] / counter phis properly; loop_cuts::Counter holds a persistent 256-byte shared buffer with DATA_VOLATILE and handles 32-bit wraparound in total(), while cache keys and root blobs separate enabled/disabled pipelines cleanly. (Minor note: enabling VICTORY_LOOP_CUTS=1 adds 2 DWORDs to root signatures if a pipeline is already right at the 64-DWORD limit.)

Verification (Windows 11, RTX 5090 + WARP + dxil.dll)

  • victory-shader: the_loop_cuts_switch_is_on_only_for_1 passed; a_loop_left_at_the_limit_counts_its_lanes_only_when_asked passed (with dxil.dll).
  • victory-gpu WARP (VICTORY_D3D12_WARP=1, --features diagnostics --lib -- --test-threads=1): 198 passed, 0 failed, 1 ignored.
  • victory-gpu RTX 5090 (--features diagnostics): shader_cache:: 3/3, pipeline:: 16/16 — both with VICTORY_LOOP_CUTS unset and with VICTORY_LOOP_CUTS=1.
  • cargo clippy --locked (victory-shader + victory-gpu, default & extra features): 0 warnings; cargo xtask policy: 185 files ok.
  • Clean git merge-tree against 13f91ca, #2668, and #2755.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7173296719 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7173296719). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T20:10:46Z. --- Reviewed and verified `6b213e18` on Windows 11 (RTX 5090 + WARP + Windows SDK `10.0.26100.0` `dxil.dll`) against `main` (`13f91ca`). ### Code review - **Zero default overhead**: when `diagnostics` is off or `VICTORY_LOOP_CUTS` != `1`, no `u2` UAV, `atomicBinOp`, extra root-signature parameter, or D3D12 buffer is emitted/allocated, and `shader_cache::key` stays identical to existing caches. - **Enabled path**: `u2` (`MEMORY_SPACE`) adds `UAVS_EVERY_STAGE` only when needed for non-pixel/compute stages; conditional back-edges use `select(orig, stop, false)` and update `self.tail[b]` / counter phis properly; `loop_cuts::Counter` holds a persistent 256-byte shared buffer with `DATA_VOLATILE` and handles 32-bit wraparound in `total()`, while cache keys and root blobs separate enabled/disabled pipelines cleanly. (Minor note: enabling `VICTORY_LOOP_CUTS=1` adds 2 DWORDs to root signatures if a pipeline is already right at the 64-DWORD limit.) ### Verification (Windows 11, RTX 5090 + WARP + `dxil.dll`) - `victory-shader`: `the_loop_cuts_switch_is_on_only_for_1` **passed**; `a_loop_left_at_the_limit_counts_its_lanes_only_when_asked` **passed** (with `dxil.dll`). - `victory-gpu` **WARP** (`VICTORY_D3D12_WARP=1`, `--features diagnostics --lib -- --test-threads=1`): **198 passed, 0 failed, 1 ignored**. - `victory-gpu` **RTX 5090** (`--features diagnostics`): `shader_cache::` **3/3**, `pipeline::` **16/16** — both with `VICTORY_LOOP_CUTS` unset and with `VICTORY_LOOP_CUTS=1`. - `cargo clippy --locked` (`victory-shader` + `victory-gpu`, default & extra features): **0 warnings**; `cargo xtask policy`: **185 files ok**. - Clean `git merge-tree` against `13f91ca`, `#2668`, and `#2755`.
Exopus-0x65f7ad0 commented 2026-11-25 22:04:31 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Exopus-0x65f7ad0 on 2026-11-25T21:42:01Z.


Thanks @fuzeofficlal and @kotekompotek for the review and the RTX 5090 / WARP runs.

In-game counts, as asked on #1819. RTX 5060 Ti 8 GB, Windows 11, Series S. I ported the counter to the tree I run the game on (fxceless/victory-fork main plus PRs); same lowering, with the fork's root-slot binding instead of u2. VICTORY_LOOP_CUTS=1, the same limit for every stage (VICTORY_SHADER_LOOP_LIMIT=16383, so the fork's per-stage limits are out of the way):

limit what ran lanes cut
16383 boot, menus, loading, the whole opening cutscene (~155 s), then the first gameplay scene 0
64 (control) boot to the loading screen 202,838 within ~3 min

So the counter does count, and in these scenes, which render correctly, no translated loop reaches 2^14. That is one machine and one route through the game; other scenes and the ray-tracing builder (off here) are not covered.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7175425101 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7175425101). Originally posted by [Exopus-0x65f7ad0](https://github.com/Exopus-0x65f7ad0) on 2026-11-25T21:42:01Z. --- Thanks @fuzeofficlal and @kotekompotek for the review and the RTX 5090 / WARP runs. **In-game counts, as asked on #1819.** RTX 5060 Ti 8 GB, Windows 11, Series S. I ported the counter to the tree I run the game on (fxceless/victory-fork main plus PRs); same lowering, with the fork's root-slot binding instead of `u2`. `VICTORY_LOOP_CUTS=1`, the same limit for every stage (`VICTORY_SHADER_LOOP_LIMIT=16383`, so the fork's per-stage limits are out of the way): | limit | what ran | lanes cut | |---|---|---| | 16383 | boot, menus, loading, the whole opening cutscene (~155 s), then the first gameplay scene | **0** | | 64 (control) | boot to the loading screen | **202,838** within ~3 min | So the counter does count, and in these scenes, which render correctly, no translated loop reaches 2^14. That is one machine and one route through the game; other scenes and the ray-tracing builder (off here) are not covered. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Marvisak commented 2026-11-26 00:03:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:21:42Z.


Automated triage for the maintainers. This branch conflicts with main in crates/victory-gpu/src/draw.rs and crates/victory-shader/src/emit/synthetic_tests.rs, so it cannot be merged as is. It will be reviewed once it is rebased.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177454984 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7177454984). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:21:42Z. --- <!-- victory-triage v1 head=6b213e1880e8dc4454d050caee0b3f1c11349af7 verdict=needs-rebase at=2026-11-25T23:21:33Z --> Automated triage for the maintainers. This branch conflicts with `main` in crates/victory-gpu/src/draw.rs and crates/victory-shader/src/emit/synthetic_tests.rs, so it cannot be merged as is. It will be reviewed once it is rebased. --- _Generated by [Claude Code](https://claude.ai/code)_
Exopus-0x65f7ad0 commented 2026-11-26 14:01:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Exopus-0x65f7ad0 on 2026-11-26T13:33:59Z.


Rebased onto main (63bcca4); the head is now c7a0a6a.

  • draw.rs: the marks block is now a let … = if … else { None }; expression on main. The loop-cut counter's root UAV binding now comes right after it, unchanged.
  • synthetic_tests.rs: I kept main's build / build_wave / run_permute_cs as they are. The test only adds two small helpers next to lowered, native and host_shader, plus a_loop_left_at_the_limit_counts_its_lanes_only_when_asked.

Results on an RTX 5060 Ti:

  • cargo test -p victory-shader --lib synthetic: the loop-cuts test passes. gathers_at_a_level_match_the_hardware fails, but it fails the same way on main itself on this card, so it is unrelated.
  • cargo check -p victory-gpu passes.

🤖 Generated with Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7193527317 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7193527317). Originally posted by [Exopus-0x65f7ad0](https://github.com/Exopus-0x65f7ad0) on 2026-11-26T13:33:59Z. --- Rebased onto `main` (`63bcca4`); the head is now `c7a0a6a`. - `draw.rs`: the marks block is now a `let … = if … else { None };` expression on `main`. The loop-cut counter's root UAV binding now comes right after it, unchanged. - `synthetic_tests.rs`: I kept `main`'s `build` / `build_wave` / `run_permute_cs` as they are. The test only adds two small helpers next to `lowered`, `native` and `host_shader`, plus `a_loop_left_at_the_limit_counts_its_lanes_only_when_asked`. Results on an RTX 5060 Ti: - `cargo test -p victory-shader --lib synthetic`: the loop-cuts test passes. `gathers_at_a_level_match_the_hardware` fails, but it fails the same way on `main` itself on this card, so it is unrelated. - `cargo check -p victory-gpu` passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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/2733:upstream-pr/2733'
git switch 'upstream-pr/2733'

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/2733'
git switch 'upstream-pr/2733'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2733'
git switch 'upstream-pr/2733'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2733'
git switch 'main'
git merge --squash 'upstream-pr/2733'
git switch 'main'
git merge --ff-only 'upstream-pr/2733'
git switch 'main'
git merge 'upstream-pr/2733'
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
Marvisak/VIctory!2731
No description provided.