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

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

@⁠SousaTrashBin on GitHub · 2026-11-26 03:32 UTC · original
SousaTrashBin:rdna4/shader-loop-limits → main · +144 −3 · 5 files · 1 commits · open

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

**@⁠SousaTrashBin** on GitHub · 2026-11-26 03:32 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/108) `SousaTrashBin:rdna4/shader-loop-limits` → `main` · +144 −3 · 5 files · 1 commits · open ## 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)
VICTORY_SHADER_LOOP_LIMITS (hash:iterations,...) stops the loops of the shaders
it names sooner; only their cache keys change. The default stops the grid march
39f0e9aeb156a61c at 512 steps: its rays ran to the compute limit of 4,096 (12.3
ms a frame, 0.4 ms with every loop at 64). Adds the disasm and translate_limit
examples to victory-shader.

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

@⁠Yoshi-233 on GitHub · 2026-11-26 03:58 UTC · original

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

**@⁠Yoshi-233** on GitHub · 2026-11-26 03:58 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/108#issuecomment-7182183290) 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)
Author
Owner

@⁠avatarmanz on GitHub · 2026-11-26 07:33 UTC · original

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.

**@⁠avatarmanz** on GitHub · 2026-11-26 07:33 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/108#issuecomment-7185589333) 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.
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-26 16:32 UTC · original

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.

**@⁠HAL9000RELOADED** on GitHub · 2026-11-26 16:32 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/108#issuecomment-7197777869) 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 has changes conflicting with the target branch.
  • crates/victory-gpu/src/shader_cache.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/108/head:github-bot/gh-108'
git switch 'github-bot/gh-108'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-108'
git switch 'github-bot/gh-108'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-108'
git switch 'github-bot/gh-108'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-108'
git switch 'main'
git merge --squash 'github-bot/gh-108'
git switch 'main'
git merge --ff-only 'github-bot/gh-108'
git switch 'main'
git merge 'github-bot/gh-108'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.