Skip to content

Shaders: in halves, a first lane is read only of a value alike in both halves - #1

Closed
justrealyme wants to merge 1 commit into
chippy01:shaders/wave64-in-32-lane-halvesfrom
justrealyme:halves-first-lane-v2
Closed

justrealyme wants to merge 1 commit into
chippy01:shaders/wave64-in-32-lane-halvesfrom
justrealyme:halves-first-lane-v2

Conversation

@justrealyme

Copy link
Copy Markdown

A fix for Marvisak#2633: in halves, v_readfirstlane_b32 read the host wave's first lane. For console lanes 32..63 that is lane 32, not lane 0, so a value that differs between the halves comes out per half.

How it showed up

A test program in halves (v_mbcnt for console lanes, then v_readfirstlane_b32 s12, v0), dispatched on an RTX 5070 Ti (driver 621.40): all 32 upper lanes read 32; the console reads 0. The Intel iGPU on the same machine got 48 of 64 lanes wrong.

The change

  • A first-lane read in halves is kept only for a value proven the same in every lane of the console wave (halves::lane_uniform). That covers what the console compiler reads first lanes for:
    • values made from scalars, constants and such values;
    • writes under conditions that are themselves alike;
    • LDS reads at alike addresses;
    • the thread id shifted right past the console wave (id >> 6 and up, for groups whose console waves are runs of 64 along x).
  • Any other first-lane read is refused with its reason. One whose result nothing uses costs nothing, like the other cross-half reads.
  • The analysis is ported from the one in fxceless/victory-fork (GPU: keep what shaders may write by page, in reused page buffers Marvisak/VIctory#497 there), where it was written for the same split.
  • New code is in emit/lower/halves.rs. lower.rs grows by 24 lines (494,791 bytes).

Evidence

victory translate --list --wave32 over the title's shader pack, executable, BVH builder DLL and upscaler DLL:

Marvisak#2633 with this
compute programs emitted 2,372 2,360
  • The 12 programs no longer emitted give the new reason. They read a first lane of something the analysis cannot show is alike, so they could have computed per-half values.
  • No other program changes.

Tests

  • reads_across_32_lane_halves_are_refused_with_their_reason:
    • a first-lane read of the thread id in halves is refused;
    • an unused one still translates;
    • the existing no-console-lanes case is unchanged.
  • first_lanes_of_alike_values_match_the_console_on_every_gpu (new, runs on every hardware adapter): a group of 128 (two console waves) reads the first lane of tid >> 6 in halves. 0 of 128 lanes differ from the console on the RTX 5070 Ti and the Intel iGPU.
  • the_first_lane_program_is_what_its_comments_say: the hand-encoded words are checked against the disassembler.

Checks on Windows 11, RTX 5070 Ti, driver 621.40:

Not covered

Without console lanes, a wave64 program's first-lane reads stay as on main: the host wave's first lane, whatever wave size the driver picks. That is the existing shortcut and is not changed here.

AI-assisted (Claude Code). No game data in this PR.

🤖 Generated with Claude Code

…h halves

A wave64 program in halves (two 32-lane host waves per console wave) read
v_readfirstlane_b32 as the host wave's first lane. For lanes 32..63 that
is console lane 32, not lane 0: a value that differs between the halves
comes out per half. On an RTX 5070 Ti, the first lane of the thread id
read 32 in all 32 upper lanes (the console: 0).

Now the read is kept only for a value proven the same in every lane of
the console wave (`halves::lane_uniform`: built from scalars, constants
and such values, under conditions that are themselves alike, LDS reads at
alike addresses, the thread id shifted past the console wave), which is
what the console compiler reads first lanes for. Any other first-lane
read is refused with its reason; one whose result is unused costs
nothing, as with the other cross-half reads.

Offline over the title's shaders (translate --list --wave32): 2,372
compute programs emitted before, 2,360 after; the 12 now refused say
"a first-lane read of a value that may differ between 32-lane halves".
No other program changes.

The analysis is ported from the one in fxceless/victory-fork (Marvisak#497 there).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
chippy01 added a commit that referenced this pull request Nov 25, 2026
…sole wave

What was wrong: two results in halves were wrong in the upper host wave
(console lanes 32-63). Measured before this change on an RTX 3070 Ti
(driver 32.0.16.2124) and the Radeon iGPU of a Ryzen 9 7900X (RDNA2),
32 of 64 lanes wrong on both, while the Radeon's own wave64 form gave the
console's results:

- v_readfirstlane_b32 read the host wave's first lane, which in the upper
  half is console lane 32 or later: a first-lane read of the thread id gave
  32 in lanes 32-63, the console 0 (reported on Marvisak#2633 by justrealyme).
- A DPP row mask was applied by the host lane's row, 0 or 1: with
  row_mask:0xc, lanes 32-63 (the console's rows 2 and 3) kept their old
  value instead of taking the DPP result.

What changed:

- A first-lane read in halves is kept for a value proven the same in every
  lane of the console wave (halves::lane_uniform: values made from scalars,
  constants and such values, under conditions that are themselves alike;
  LDS reads, and buffer loads through a scalar descriptor, at alike
  addresses; the thread id shifted past the console wave) and in groups of
  32 threads or fewer, which one host wave holds whole. Any other is
  refused with its reason; one nothing uses costs nothing. The analysis is
  justrealyme's (#1), ported from fxceless/victory-fork
  (Marvisak#497 there). Added here: buffer loads (a typed buffer's element the
  compiler loads with a vector load, then reads the first lane of), the
  dwords a waterfall loop reads (each lane's own, so not alike), and the
  small-group exception.
- DPP row masks take the row from the console lane.
- The GPU tests share one dispatch helper.

Evidence, translate --list --wave32 over shaders.rpf, the executable,
XboxBvhBuilder.dll and the FidelityFX DLL: compute programs emitted
2,372 -> 2,369. The 3 no longer emitted read the first lane of a value the
analysis cannot show is alike (scattering_transmittance_system's cloud
multiple-scattering cache: a value joined from a branch on a per-lane
compare; two ptx_prerender light-atlas programs: a select on a mask made
from EXEC, in a loop), so in halves they could compute per-half values. 14
programs refused before give this reason first now; no other outcome
changes. The analysis as #1 has it refuses 12: the
small-group exception keeps 5 of them, buffer loads 4. No program of the title uses a DPP row mask
other than 0xF in halves, so that fix changes none of its translations.

Tests (emit::halves_tests):

- dpp_row_masks_name_the_console_rows_on_every_gpu (new): row_mask:0xc;
  0 of 64 lanes differ on both GPUs (32 before).
- first_lanes_kept_in_halves_match_the_console_on_every_gpu (new): the
  thread id's first lane in a group of 32 and l >> 6 in a group of 128;
  0 lanes differ on both GPUs.
- reads_across_32_lane_halves_are_refused_with_their_reason: the thread
  id's first lane is refused in a group of 64 and kept in a group of 32;
  an unused one costs nothing; a buffer dword loaded at an offset alike in
  every lane is kept, at the thread id's offset refused.
- the_test_programs_are_what_their_comments_say: every hand-encoded
  program checked against the disassembler.

Checks: cargo clippy --workspace --all-targets --locked -- -D warnings,
and with --all-features: clean. cargo xtask policy: ok. cargo test -p
victory-shader --lib -- --test-threads=1: 273 passed, 3 failed, the same 3
as on main on this NVIDIA machine (two stride_tests,
gathers_at_a_level_match_the_hardware);
heap_direct_texture_reads_mark_their_heap_index skipped as before. lower.rs
is 495,122 bytes.

Co-Authored-By: justrealyme <82029927+justrealyme@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chippy01

Copy link
Copy Markdown
Owner

Thanks! I reproduced the bug on an RTX 3070 Ti and the Radeon iGPU, and folded this into Marvisak#2633 with you as co-author. I added two things on top of your analysis:

  • groups of 32 threads or fewer keep the host wave's first lane, since one host wave holds the whole console wave (5 of the 12 programs were such groups);
  • buffer loads at alike addresses through a scalar descriptor count as alike, while the dwords a waterfall loop reads don't (4 more of the 12 come back).

Closing this one in favour of the commit on Marvisak#2633.

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.

2 participants