Loop limits of their own for named shaders (grid march 39f0e9: 12.3 -> 0.4 ms) #108

Open
SousaTrashBin wants to merge 1 commit from upstream-pr/108 into main
SousaTrashBin commented 2026-11-26 03:32:59 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by SousaTrashBin on 2026-11-26T03:32:59Z.


Loop limits of their own for named shaders (VICTORY_SHADER_LOOP_LIMITS)

The case

In a timing run on an RX 9070 XT, the title's compute shader 39f0e9aeb156a61c (one Dispatch(3600, 1, 1) a frame, a march through a 3D grid: per step a typed load of a cell and a test of bit 29) took 12.3 ms of the card a frame in a street, with one dispatch at 70 ms. With every loop stopped at 64 (VICTORY_SHADER_LOOP_LIMIT=64) it took 0.4 ms. So its time is iterations past 64.

The compute limit is 4,096 per loop entry (lower.rs count_iteration), and the comment there names the case: a loop whose exit is in data the host does not have yet runs to the limit in every lane. A rough count of ~3,400 dependent steps per ray from the timings puts its rays at that limit: they march past where they leave any grid, and what they compute there is wrong anyway.

The change

  • victory_shader::emit::with_loop_limit(Some(n), || translate(..)) runs one translation with every loop stopped after n iterations, on its thread only (count_iteration reads loop_limit_here()).
  • shader_cache::host_shader takes the limit for the container's hash (16 hex digits of its BLAKE3, as the runtime names shaders) from VICTORY_SHADER_LOOP_LIMITS (hash:iterations,...). Unset, it defaults to 39f0e9aeb156a61c:512; empty, there are none. Only that shader's cache key changes, so no other shader is translated again.
  • At 512 a march that meets its exit within 512 steps is as before; one that runs to the limit costs an eighth.

Checked on the title's container: its 11 loop limits go from 4,096 to 512, and nothing else in the translation changes size. Measured in the title: 0.3–0.5 ms a frame instead of 12.3 ms.

This is a shortcut, as the other limits are: what the march reads that keeps it from its exit is not found yet. With it found, the default can go. It may change what that pass draws where rays used to reach the limit; I have not compared the picture.

Also

Two examples in victory-shader that helped here:

  • disasm: prints the GFX10 code of a saved container (VICTORY_SHADER_SAVE writes them).
  • translate_limit: writes a container's DXIL with the stage limits and with a named limit.

Tests

  • The named limit holds for its translation only, nests, and does not reach another thread.
  • VICTORY_SHADER_LOOP_LIMITS parsing, and naming a shader by its container's BLAKE3.
  • cargo test -p victory-shader --lib passes except size_tests::the_record_count_of_a_structured_buffer_read_by_index, which crashes (access violation) the same way on main. cargo test -p victory-gpu --lib passes apart from timeline::report::tests::real_waits_are_recorded_with_who_ended_them, which also fails on main.

🤖 Generated with Claude Code

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:6618909034 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/108). Originally posted by [SousaTrashBin](https://github.com/SousaTrashBin) on 2026-11-26T03:32:59Z. --- ## Loop limits of their own for named shaders (`VICTORY_SHADER_LOOP_LIMITS`) ### The case In a timing run on an RX 9070 XT, the title's compute shader `39f0e9aeb156a61c` (one `Dispatch(3600, 1, 1)` a frame, a march through a 3D grid: per step a typed load of a cell and a test of bit 29) took 12.3 ms of the card a frame in a street, with one dispatch at 70 ms. With every loop stopped at 64 (`VICTORY_SHADER_LOOP_LIMIT=64`) it took 0.4 ms. So its time is iterations past 64. The compute limit is 4,096 per loop entry (`lower.rs` `count_iteration`), and the comment there names the case: a loop whose exit is in data the host does not have yet runs to the limit in every lane. A rough count of ~3,400 dependent steps per ray from the timings puts its rays at that limit: they march past where they leave any grid, and what they compute there is wrong anyway. ### The change - `victory_shader::emit::with_loop_limit(Some(n), || translate(..))` runs one translation with every loop stopped after `n` iterations, on its thread only (`count_iteration` reads `loop_limit_here()`). - `shader_cache::host_shader` takes the limit for the container's hash (16 hex digits of its BLAKE3, as the runtime names shaders) from `VICTORY_SHADER_LOOP_LIMITS` (`hash:iterations,...`). Unset, it defaults to `39f0e9aeb156a61c:512`; empty, there are none. Only that shader's cache key changes, so no other shader is translated again. - At 512 a march that meets its exit within 512 steps is as before; one that runs to the limit costs an eighth. Checked on the title's container: its 11 loop limits go from 4,096 to 512, and nothing else in the translation changes size. Measured in the title: 0.3–0.5 ms a frame instead of 12.3 ms. This is a shortcut, as the other limits are: what the march reads that keeps it from its exit is not found yet. With it found, the default can go. It may change what that pass draws where rays used to reach the limit; I have not compared the picture. ### Also Two examples in `victory-shader` that helped here: - `disasm`: prints the GFX10 code of a saved container (`VICTORY_SHADER_SAVE` writes them). - `translate_limit`: writes a container's DXIL with the stage limits and with a named limit. ### Tests - The named limit holds for its translation only, nests, and does not reach another thread. - `VICTORY_SHADER_LOOP_LIMITS` parsing, and naming a shader by its container's BLAKE3. - `cargo test -p victory-shader --lib` passes except `size_tests::the_record_count_of_a_structured_buffer_read_by_index`, which crashes (access violation) the same way on `main`. `cargo test -p victory-gpu --lib` passes apart from `timeline::report::tests::real_waits_are_recorded_with_who_ended_them`, which also fails on `main`. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Yoshi-233 commented 2026-11-26 03:58:26 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by Yoshi-233 on 2026-11-26T03:58:26Z.


I reproduced a scope-restoration bug in with_loop_limit at 66e0034. With an outer limit of 7, I entered a limit of 64, raised a synthetic panic and caught it inside the outer callback. On that same thread, loop_limit_here() then returned 64, where the still-active outer callback expected 7. The existing normal-return and separate-thread tests pass; it's the unwind path that skips the restore.

I tried saving the previous value in a small Drop guard for the duration of the callback. The new regression and the full Linux victory-shader --lib suite then passed (204 passed, 2 ignored). A minimal regression inside the module is:

with_loop_limit(Some(7), || {
    let caught = std::panic::catch_unwind(|| {
        with_loop_limit(Some(64), || panic!("synthetic callback panic"))
    });
    assert!(caught.is_err());
    assert_eq!(loop_limit_here(), 7);
});

One separate hardware data point for the shader you identified: on my earlier RTX 4070 Ti SUPER run, this exact hash (39f0e9aeb156a61c) passed DXIL validation, but standalone CreateComputePipelineState returned 0x8007000E before any dispatch. That was a different fork/build, driver 32.0.16.1742. I haven't measured the 512-limit translation on that GPU. Original test and controls

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7182183290 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/108#issuecomment-7182183290). Originally posted by [Yoshi-233](https://github.com/Yoshi-233) on 2026-11-26T03:58:26Z. --- I reproduced a scope-restoration bug in `with_loop_limit` at `66e0034`. With an outer limit of 7, I entered a limit of 64, raised a synthetic panic and caught it inside the outer callback. On that same thread, `loop_limit_here()` then returned **64**, where the still-active outer callback expected **7**. The existing normal-return and separate-thread tests pass; it's the unwind path that skips the restore. I tried saving the previous value in a small `Drop` guard for the duration of the callback. The new regression and the full Linux `victory-shader --lib` suite then passed (204 passed, 2 ignored). A minimal regression inside the module is: ```rust with_loop_limit(Some(7), || { let caught = std::panic::catch_unwind(|| { with_loop_limit(Some(64), || panic!("synthetic callback panic")) }); assert!(caught.is_err()); assert_eq!(loop_limit_here(), 7); }); ``` One separate hardware data point for the shader you identified: on my earlier RTX 4070 Ti SUPER run, this exact hash (`39f0e9aeb156a61c`) passed DXIL validation, but standalone `CreateComputePipelineState` returned `0x8007000E` before any dispatch. That was a different fork/build, driver 32.0.16.1742. I haven't measured the 512-limit translation on that GPU. [Original test and controls](https://github.com/Belobug/VIctory-Belobug-AMD/issues/15#issuecomment-7165184524)
avatarmanz commented 2026-11-26 07:33:42 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by avatarmanz on 2026-11-26T07:33:42Z.


Left open: conflicts with main's shader_cache.rs (the cache is keyed by translator version now), and the loop limit changes the grid march on every card by default; needs a rebase and an in-game check.

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7185589333 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/108#issuecomment-7185589333). Originally posted by [avatarmanz](https://github.com/avatarmanz) on 2026-11-26T07:33:42Z. --- Left open: conflicts with main's shader_cache.rs (the cache is keyed by translator version now), and the loop limit changes the grid march on every card by default; needs a rebase and an in-game check.
HAL9000RELOADED commented 2026-11-26 16:32:25 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by HAL9000RELOADED on 2026-11-26T16:32:25Z.


RDNA3 data point for the shader this PR caps: in a VICTORY_GPU_TIMING=1 run on main a9400a6 + #165 (RX 7900 XTX (RDNA3), driver 32.0.32015.7029, i7-13700KF, Windows 10 22H2 (19045), 4K output, Series X, VICTORY_RENDER_SCALE=0.5), the timing group that contains 39f0e9aeb156a61c takes about 1.2 ms of card time a frame, against 12.3 ms on the RX 9070 XT in the description.

So the grid march looks expensive mainly on RDNA4; on this card the cap would save at most about a millisecond. This is main, not the current head 66e0034 of this PR: the PR itself was not run here (it conflicts with main).

AI-assisted (Claude Code): tests run on my machine, text drafted with Claude.

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7197777869 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/108#issuecomment-7197777869). Originally posted by [HAL9000RELOADED](https://github.com/HAL9000RELOADED) on 2026-11-26T16:32:25Z. --- RDNA3 data point for the shader this PR caps: in a `VICTORY_GPU_TIMING=1` run on main `a9400a6` + #165 (RX 7900 XTX (RDNA3), driver 32.0.32015.7029, i7-13700KF, Windows 10 22H2 (19045), 4K output, Series X, `VICTORY_RENDER_SCALE=0.5`), the timing group that contains `39f0e9aeb156a61c` takes about 1.2 ms of card time a frame, against 12.3 ms on the RX 9070 XT in the description. So the grid march looks expensive mainly on RDNA4; on this card the cap would save at most about a millisecond. This is main, not the current head `66e0034` of this PR: the PR itself was not run here (it conflicts with main). _AI-assisted (Claude Code): tests run on my machine, text drafted with Claude._
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/108:upstream-pr/108'
git switch 'upstream-pr/108'

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/108'
git switch 'upstream-pr/108'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/108'
git switch 'upstream-pr/108'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/108'
git switch 'main'
git merge --squash 'upstream-pr/108'
git switch 'main'
git merge --ff-only 'upstream-pr/108'
git switch 'main'
git merge 'upstream-pr/108'
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
avatarmanz/VIctory-7900XTX!108
No description provided.