Skip to content

Shaders: scalar loads from a code table at the first lane's address - #1276

Closed
mercuryVM wants to merge 1 commit into
Marvisak:mainfrom
mercuryVM:impl2/first-lane-code-address
Closed

mercuryVM wants to merge 1 commit into
Marvisak:mainfrom
mercuryVM:impl2/first-lane-code-address

Conversation

@mercuryVM

Copy link
Copy Markdown

What

The console compiler reads a constant array at an index it treats as uniform in a specific way. It builds a VGPR address (the s_getpc_b64 table base plus the scaled index), reads both dwords with v_readfirstlane_b32, and then issues an s_load_dword* at that address. The resource tracker refused these programs with an address not from a root parameter.

  • resource.rs: Tracer::address now follows the two first-lane reads back to the VGPR address. If that address is a code-table base plus a per-lane term, it records the address with the lane's term and the read (Address::first_lane, new field). This only applies when the load is later in the same block and nothing in between writes EXEC. Otherwise the program is refused with a first-lane address read with other lanes. A root-parameter base in this form is refused with its own reason, a first-lane address from a root parameter, because the descriptor paths re-derive addresses from registers.
  • emit/lower.rs: the three places that add Address::dynamic now share one helper, address_term. For a first-lane address it emits WaveReadLaneFirst of the term inside the v_readfirstlane_b32's lane guard. It then broadcasts the result to every lane with an unsigned WaveActiveMax over the guarded value and zeros.
    • The guard matters. Guarded code runs in every host lane, and only the writes are masked. Without the guard, the read takes host lane 0's value: the new GPU test got entry 0 instead of entry 1 on the RTX 5070.
    • Programs with such an address set console_lanes, so they declare the console's wave size, like other code that reads other lanes.
  • A descriptor built around a first-lane address is refused in the lowering (a descriptor built around a first-lane address). None was seen in the package.

Measured

Package: the user's own Series 1.0.2.1 shaders.rpf. victory translate <shaders.rpf> --validate --list --jobs 4, origin/main ffe310e vs this branch:

main this
compute emitted / validated 1441 / 1441 of 2582 1445 / 1445 of 2587
vertex+pixel emitted 12231 of 14027 12235 of 14031
an address not from a root parameter 73 64
  • 9 programs leave that reason:
    • 4 compute programs now emit and validate.
    • 4 pixel programs now emit. 2 of them validate, and 2 fail main's existing PSV0 mismatch, which also hits about 15k other programs on main.
    • 1 compute program moves to a descriptor read only as data.
  • Of the 4 that validate, all 4 were refused at run time in two world runs on an RTX 5070 (hashes from the shader does not translate warnings): an exposure downsample pass (2 programs) and an irradiance SH integration (2 programs).
  • The other 64 programs with that reason build their address with s_or_b32 from a descriptor's base address. They are inline ray tracing that reads the BVH (see Ray tracing: what Leonida measured about XboxBvhBuilder and the console BVH format #274), which is out of scope here.

--pipelines over the whole package did not finish within the 20 minutes I can hold the shared GPU, on either build, so I have no pipeline counts. The new GPU test creates the pipeline for its synthetic program.

Tests

Windows 11 Pro, NVIDIA GeForce RTX 5070, AMD CPU. All fixtures are hand-written GFX10 words, with no game data.

  • resource::tests::a_first_lane_address_of_a_code_table_is_traced: the load is traced to CODE at the table's offset, with first_lane set.
  • resource::tests::a_first_lane_address_read_with_other_lanes_is_refused: with EXEC restored between the reads and the load, it is refused with the new reason.
  • emit::first_lane_tests::every_lane_loads_the_entry_the_first_lane_addresses: 32 lanes, with v_cmpx leaving lanes 6..31. Each lane addresses entry (lane + 3) & 7 and reads it from the first lane. Expected result: lanes 6..31 all store entry 1, lanes 0..5 store nothing. Passes on the RTX 5070. WARP is skipped because it creates no WaveSize(32) pipeline (the diff tests skip it the same way).

Commands:

  • cargo test -p victory-shader --lib first_lane -- --test-threads=1: 11 passed.
  • cargo test --workspace -- --test-threads=1: 345 passed, 0 failed, 6 ignored.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • rustfmt --edition 2024 --check on the changed files: no diffs in changed lines. The remaining diffs in lower.rs and resource.rs are already on main.
  • cargo xtask policy: 155 files ok.

Not tested

  • Not run in the title.
  • No package-wide --pipelines counts (see above).
  • Not tested on AMD hosts or WARP.
  • Pixel and vertex programs with such an address use host lanes for "first", as the existing v_readfirstlane_b32 lowering does.

Overlap

AI-assisted (Claude Code).

🤖 Generated with Claude Code

The console compiler reads a constant array at an index it takes as
uniform through `v_readfirstlane_b32` of both dwords of a VGPR address
(table base from `s_getpc_b64`, plus the index scaled) and an
`s_load_dword*` there. The tracker refused such addresses as "an address
not from a root parameter".

It now follows the two first-lane reads to the VGPR address and records
the address with each lane's term and the read (`Address::first_lane`),
when the load runs in the same block with the same EXEC. The lowering
takes the term of the first lane the read's guard holds in, in every
lane, and the program declares the console's wave size, as other code
reading other lanes does. Root-parameter bases stay refused, with their
own reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scalar loads from the code table at a first-lane address: resource.rs recognizes v_readfirstlane_b32 pairs as uniform addresses (CODE param only, root-param case rejected), lower.rs emits waveReadLaneFirst + waveActiveOp under the instruction guard and sets WAVE_OPS. The guard/undef handling is careful, but the execution tests (first_lane_tests) are Windows-gated, so macOS only compile-checks the new paths. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 98 passed, 0 failed. Given the wave-op subtlety, a Windows hardware/WARP run of first_lane_tests before merge would be worthwhile.

@MichaelSmithAI

Copy link
Copy Markdown

Independent code-table readback on b95831e with an RTX 4090 under Wine/GE 3.2. The synthetic table contained 0x100 + index. With a 32-lane shader and EXEC restricted to lanes 10–31, the first active lane selected entry 2, and all active lanes stored 0x102. A second shader initialized the index to 0, wrote 3 only on lanes ≥10, then restored EXEC before the first-lane read. Every lane stored 0x100, as expected for the reconvergence case discussed on #1231. Wine returned the same hardware adapter for the WARP request.

Both 64-lane versions were refused at pipeline creation on this 32-lane adapter. In a separate local merge with #439 at bf0f951, those shaders ran. The divergent case returned entry 0 for all 54 active lanes, where lane 10 selected entry 2. The reconverged case returned entry 3 in the upper 32 lanes, where the console’s lane 0 selected entry 0. This is a concrete integration test for routing the address broadcast through #439’s console-wave exchange.

For the four exposure/irradiance programs in the report, do you have the declared wave size for each hash? The cited world logs establish that these hashes hit translation refusals. Their wave sizes would help distinguish which ones need the cross-half path when checking pipeline creation after this change.

@pablogonz12

Copy link
Copy Markdown

Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main ffe310e plus #1017 (main's host build fails without it: E0425 host_resource), then the four-stage offline check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped).

translate valid SPIR-V RADV pipeline
base 666 / 768 656 656
your head 668 / 768 658 658

+2 programs translate and none is lost: "an address not from a root parameter" goes from 3 compute programs to 1, and no other reason moves. Same 10 SPIR-V validation failures as the base. (It clears the same two programs as #1225 and #1231 — the three reach the same class from different sides.)

Not tested: in-game.

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…o the wave64 mesh test build

Conflicts: emit/mod.rs (both add a test module) and emit/lower.rs (the
first-lane console_lanes line after the fork's formatting of the mask test);
first_lane_tests sets the stride_table field Marvisak#704 added to HostShader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wwwwwwww3333333

Copy link
Copy Markdown

Native Windows run on AMD RDNA2: RX 6750 XT (RDNA2, gfx1031), AMD driver 32.0.21045.29003, i5-13400F, Windows 11, Rust 1.98.1 MSVC. Head b95831e on main ffe310e (clean merge).

cargo test -p victory-shader --lib -- --test-threads=1: 123 passed, 0 failed, 1 ignored (main alone: 120). The new tests pass: first_lane_tests::every_lane_loads_the_entry_the_first_lane_addresses, resource::tests::a_first_lane_address_of_a_code_table_is_traced and resource::tests::a_first_lane_address_read_with_other_lanes_is_refused. The description lists AMD hosts as untested; this is AMD's native D3D12 driver. (Output was captured, so I can't say from this run whether WARP took its skip path.)

Synthetic tests only, no game files. AI-assisted (Claude picked the PRs and wrote the batch script; I ran it on my machine).

@mercuryVM

Copy link
Copy Markdown
Author

Thanks for the runs.

@MichaelSmithAI, wave sizes. I read the wave64 flag of each program on the package.

  • Exposure downsample: both programs are wave64.
  • Irradiance SH: one program is wave32 and one is wave64.

So 3 of the 4 are wave64. This PR makes a program with a per-lane first-lane address declare the console's wave size (console_lanes). On a 32-lane host, those 3 then translate and validate but fail pipeline creation until wave64 emulation (#233 / #439) lands. On a 32-lane host this PR only changes the outcome for the wave32 one. Your #439 result (entry 0 for all lanes where lane 10 picked entry 2) suggests the broadcast has to go through #439's console-wave exchange there. I'll note that in the body.

Overlap with #1225 / #1231. Both are older than this PR, and I missed them when I searched. #1225 handles the uniform case without any wave op: when every lane holds the same index, it reads the source directly. That is better for wave64 programs on 32-lane hosts, since no wave size is declared. pablogonz12 reports that #1225 and #1231 clear the same programs as this PR in their dump. This PR adds the non-uniform case (first-lane semantics under the read's lane guard). That case is only needed where the uniformity proof fails.

I'd suggest landing #1225 / #1231 first. I'll then restack this PR on them, so the wave op is used only for addresses they cannot prove uniform. If nothing in the title needs that case, I'll close this PR.

@catminou4. first_lane_tests ran on Windows 11 with an RTX 5070 (passes). WARP is skipped because it creates no WaveSize(32) pipeline. The details are in the body.

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Scalar loads from a code table at a first-lane address. Main traces these since commit 27c3dc9 (resource.rs:816-865, reaches_pc follows v_readfirstlane_b32 back to s_getpc_b64); the world list has no program left with 'an address not from a root parameter'. Same topic as #1225.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants