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

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

@⁠Exopus-0x65f7ad0 on GitHub · 2026-11-25 19:20 UTC · original
Exopus-0x65f7ad0:pr/loop-cuts-counter → main · +380 −14 · 11 files · 1 commits · open

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

**@⁠Exopus-0x65f7ad0** on GitHub · 2026-11-25 19:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/2733) `Exopus-0x65f7ad0:pr/loop-cuts-counter` → `main` · +380 −14 · 11 files · 1 commits · open 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)
A diagnostics switch for the question of #1819 (is the limit ever
reached by a loop that would have ended?). A lane that leaves a loop
because of the limit adds one to the first dword of a root UAV, u2 in
the memory space: on a conditional back edge where the limit turned the
branch, and in the block an unconditional back edge leaves through.
While the switch is on, every host root signature declares that UAV and
every draw and dispatch binds a counter of ours; the frame line says how
many lanes were cut since the last line and since the start. The switch
is part of the shader cache key only when on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:13:00 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 20:07 UTC · original

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.

**@⁠kotekompotek** on GitHub · 2026-11-25 20:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7173213772) 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.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 20:10 UTC · original

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 20:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7173296719) 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`.
Author
Owner

@⁠Exopus-0x65f7ad0 on GitHub · 2026-11-25 21:42 UTC · original

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

**@⁠Exopus-0x65f7ad0** on GitHub · 2026-11-25 21:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7175425101) 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)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:21 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 23:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7177454984) <!-- 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)_
github-bot force-pushed github-bot/gh-2733 from 6b213e1880 to c7a0a6a08d 2026-11-26 13:36:13 +00:00 Compare
Author
Owner

@⁠Exopus-0x65f7ad0 on GitHub · 2026-11-26 13:33 UTC · original

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

**@⁠Exopus-0x65f7ad0** on GitHub · 2026-11-26 13:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/2733#issuecomment-7193527317) 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 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/2733/head:github-bot/gh-2733'
git switch 'github-bot/gh-2733'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2733'
git switch 'github-bot/gh-2733'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2733'
git switch 'github-bot/gh-2733'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2733'
git switch 'main'
git merge --squash 'github-bot/gh-2733'
git switch 'main'
git merge --ff-only 'github-bot/gh-2733'
git switch 'main'
git merge 'github-bot/gh-2733'
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!2733
No description provided.