Skip to content

Shaders: a loop whose trip count the program fixes runs every iteration, past the stage's loop cap (PS_IntegrateEnvBRDF) - #3844

Merged
trustno1-coder merged 2 commits into
trustno1-coder:mainfrom
CowboyBingus:fix/loop-cap-constant-trip
Nov 30, 2026
Merged

trustno1-coder merged 2 commits into
trustno1-coder:mainfrom
CowboyBingus:fix/loop-cap-constant-trip

Conversation

@CowboyBingus

@CowboyBingus CowboyBingus commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

What it changes

loop_cap (emit/lower.rs) stops a translated pixel loop after 128 iterations (vertex and mesh 256, compute 4,096). The cap is for loops whose exit count comes from data the host does not have yet. It also cut loops whose count is fixed in the program. PS_IntegrateEnvBRDF (a467d848ace5810d) has three such loops: its counter starts at 0 and is compared with 1,024 (s_cmpk_eq_i32 …, 0x400), stepping by 4, 4 and 2, which is 256, 256 and 512 iterations. All three were stopped at 128. On a conditional back edge the cap joins the loop's own exit, so the program carried on with what it had summed, and nothing was logged.

This change exempts loops whose trip count the program fixes:

  • New emit/lower/trips.rs, constant_trip. A loop qualifies when:
    • it has one back edge, and the latch ends in s_cbranch_scc0/1;
    • SCC comes from an s_cmp_* or s_cmpk_* (eq, lg, gt, ge, lt, le; _i32 or _u32) of an SGPR counter with a constant;
    • the counter is an SGPR phi of the loop header that enters as one constant from every other predecessor and comes back as itself plus constants (s_add_i32/u32, s_sub_i32/u32, s_addk_i32).
    • Constants are inline integers, literals, SOPK immediates, and SGPRs set by s_mov_b32 of a constant or by s_movk_i32.
    • The count is found by stepping the counter with those constants, up to 2^16 iterations.
  • loop_cap(h) is now per loop: the larger of the stage's cap and the loop's own trip count. A limit named by VICTORY_SHADER_LOOP_LIMIT is used unchanged.
  • Every other loop keeps the stage's cap: counters in VGPRs, loops with more than one back edge, starts, steps or bounds read from memory or user data, and loops that do not exit within 2^16 iterations.

Every program of a local known-shaders.bin (19,108 programs: vertex, pixel, compute and mesh) was translated through emit::translate with and without the change. A test-only hook in find_trips, not part of this PR, recorded each loop whose proven trip count is above its stage's cap. The loops this changes are in three pixel programs:

Program Loops (iterations; before: 128 each)
a467d848ace5810d PS_IntegrateEnvBRDF 256, 256, 512
8992ac65c3b6b5eb PS_IntegrateEnvBRDF_Beckmann 256, 256
f9b6e2728e0628b0 PS_IntegrateEnvBRDF_ThinFilm 256

Without the change the hook records none. One of the 19,108 programs does not translate, with or without the change.

The translator's source changes, so cached translations are made again on the first run.

How it was tested

  • GPU and profile: RTX 5070 Ti (driver 621.24), Series X, native 2560x1440, DLSS on (quality), frame generation off, ray tracing on (VICTORY_BVH_BUILDER=1, VICTORY_BVH_SIZE_COPY=1, VICTORY_LDS_LAST_RESORT=1).
  • Wave sizes: the host's waves are 32 lanes (host wave lanes min=32 max=32); a467d848ace5810d is a wave64 console program.
  • cargo test -p victory-shader --locked: 465 passed, 0 failed, 9 ignored. Three new tests in emit/lower/tests.rs:
    • a_loop_of_constant_trip_count_runs_every_iteration: a pixel loop of four adds to 0x400 gets a limit of 256, two adds to 0x400 gets 512, one add to 0x100 gets 256; one add to 10 keeps 128; a count of 2^16 is kept and 2^16 + 1 keeps 128; a counter that never meets its bound keeps 128.
    • a_loop_bounded_by_data_keeps_the_cap: a bound loaded with s_load_dword is not a constant trip; a bound in a root constant keeps the compute cap of 4,096; the same loop bounded by s_movk_i32 gets its own count.
    • constant_trips_count_down_swap_and_compare_signed: s_sub_i32 from 0x400 by 4 until s_cmpk_lg_i32 …, 0 gets 256; s_cmp_gt_u32 0x100, s2 (constant first) gets 256; from -512 by 2 while s_cmp_lt_i32 …, 0 gets 256, and with s_cmp_lt_u32 (one iteration) keeps 128; a counter set again by s_mov_b32 in the body keeps 128.
  • shader_cache::tests::every_switch_of_the_translator_is_in_the_key (victory-gpu) fails with and without this change, with the same message: VICTORY_SCALAR_SPLITS is not read by the translator any more.
  • CI's Clippy step (cargo clippy --workspace --all-targets --locked -- -D warnings) fails on main 41adb55 too.
    • Main's own CI run stops at victory-loader/src/crash.rs:804, function unwind_copy is never used: https://github.com/trustno1-coder/VIctory-NVIDIA/actions/runs/46320380085
    • Run locally with --keep-going, main and this branch both give the same five errors: that one, plus four in victory-shader:
      • unused import: PAIR_MEETINGS (emit/lower/analysis.rs:6);
      • unused import: analysis::lane_regions (emit/lower.rs:594 on main, 603 here: the same line, moved down by this change's lines above it);
      • variables in the condition are not mutated in the loop body (emit/lower/analysis.rs:467);
      • this function has too many arguments (8/7) (native.rs:1655).
    • git blame puts all five lines in commits that predate this change.
    • This PR's CI run stops at the four victory-shader errors.
  • a467d848ace5810d translated with and without the change, then dxc -dumpbin: exactly three lines differ, the loop-counter compares (icmp uge i32 …, 128 became 256, 256 and 512). Each is or'ed with the loop's own icmp eq i32 …, 1024 exit.
  • In the game: runs of main 41adb55 + Ray tracing: the builder's geometry buffers are made ready before it reads them (zero triangles) #3265 (A) and A + this change's first commit (B), release builds with --features diagnostics, from boot to the end of an indoor scene early in the game. (The second commit adds the third test and an early return in find_trips when a program has no loops.)
    • Both test builds also carry a local capture commit, the same in A and B and not part of this PR, for saving every presented frame 41 to 48 s after the ray-tracing builder starts.
    • Six runs: three per build, the first of each a warm-up (the B warm-up logged hits 0, misses 3,426). All six reached the end of that scene, all logged "the ray-tracing structure builder runs", and none logged a lost device (0x887A0005, 0x887A0004, device removed or lost). That includes the three B runs, each through the opening cutscene.
    • The runs after the warm-ups logged shaders from the cache and translated so far hits 3,426 to 3,494, misses 0 to 2, and host pipeline library loaded 282,621,440 to 282,772,268 bytes.
  • Shot 2 of that scene, brightness of the back walls. In each run, the captured frame closest to one reference frame of that shot (mean absolute RGB difference). Mean luma (0.2126 R + 0.7152 G + 0.0722 B) of the 8-bit 1280x720 capture, in three regions chosen before the runs were compared. Each cell lists the warm-up first.
Region (1280x720) A runs B runs
Left wall (20,100)-(200,380) 8.42, 8.43, 8.23 10.67, 10.72, 10.66
Back wall behind the bar (200,180)-(380,260) 9.79, 9.75, 9.40 11.99, 11.82, 11.82
Upper back wall right of the light (540,60)-(670,200) 11.10, 11.10, 12.49 13.61, 13.61, 13.32

Across the two compared runs of each build, the A/B pair of frames with the smallest mean absolute RGB difference outside the three regions is A frame 32692 and B frame 33525 (3.26; both 41.09 s after the builder started). Their regions: left wall 8.53 to 10.84, behind the bar 10.16 to 12.27, upper back wall 14.64 to 12.96 (darker after). With the same measure, the closest pair between A's two runs is 2.01 and between B's two runs 1.55.

  • How often the three programs run, and frame times:
    • In each of the six runs, the three PS_IntegrateEnvBRDF programs are first bound 0.65 to 0.93 s before the ray-tracing builder starts.
    • The per-frame ops listings captured 41 to 48 s after the builder starts contain no draw with them.
    • In an earlier ops listing, each of them was one 3-vertex draw into a 256x256 target.
    • Frame times, from the builder starting to 40 s after it, as the median of the frame times log lines' medians (one line about every second): A runs 59.4, 84.5 and 93.4 ms; B runs 90.2, 55.9 and 97.8 ms.
    • The spread between runs of the same build (34.0 ms for A, 41.9 ms for B, highest to lowest median) is larger than the difference between A and B (2.2 ms between the means of the medians).

Checked for overlap

🤖 Generated with Claude Code

…ast the stage's loop cap

loop_cap cuts pixel loops at 128 iterations (vertex and mesh 256, compute
4096), for loops whose count comes from data the host does not have yet. A
loop whose counter starts from a constant, steps by constants and leaves on
a compare with a constant cannot run away; it now gets its own trip count
(up to 2^16) as its limit. PS_IntegrateEnvBRDF a467d848ace5810d's loops of
256, 256 and 512 samples were cut to 128.

Covered: one back edge, an s_cbranch_scc0/1 on an s_cmp(k) of an SGPR
counter with a constant; the counter a header phi entering as one constant
and coming back as itself plus constants (s_add/s_sub _i32/_u32,
s_addk_i32). Everything else keeps the cap, as does a limit a switch names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mpares, a counter set again; find_trips returns early without loops

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the pr-fix Pull request: fixes a bug label Nov 30, 2026
@github-actions github-actions Bot changed the title [pr-fix] Shaders: a loop whose trip count the program fixes runs every iteration, past the stage's loop cap (PS_IntegrateEnvBRDF) Shaders: a loop whose trip count the program fixes runs every iteration, past the stage's loop cap (PS_IntegrateEnvBRDF) Nov 30, 2026
@Yoruiopz

Copy link
Copy Markdown
Contributor

Independent verification on Windows using the official .05 binary and a locked build of exact PR head 04ad9975. Both runs used the same four inputs and the same Wave32 and descriptor-range settings. The three BRDF shaders all first bound together during a separate .05 boot on this machine.

Shader Stock safety caps PR safety caps
Main BRDF 128 / 128 / 128 256 / 256 / 512
Beckmann BRDF 128 / 128 256 / 256
Thin-film BRDF 128 256

I checked the emitted DXIL for each cap and the original 1024 loop exits. Both builds validated all four shaders and created all four D3D12 test pipelines on the RTX 5070 Laptop GPU (driver 610.88). The unrelated tiled-water compute shader produced byte-identical DXIL.

This independently confirms the loop-cap correction and pipeline creation on a second NVIDIA configuration. Matched in-game lighting and frame pacing were outside this check.

@trustno1-coder
trustno1-coder merged commit ff6f5e6 into trustno1-coder:main Nov 30, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-fix Pull request: fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants