Skip to content

Shaders: reads of uninitialized pixel SGPRs read as zero - #1176

Closed
Giiy0 wants to merge 2 commits into
Marvisak:mainfrom
Giiy0:fix/ps-uninitialized-sgprs
Closed

Giiy0 wants to merge 2 commits into
Marvisak:mainfrom
Giiy0:fix/ps-uninitialized-sgprs

Conversation

@Giiy0

@Giiy0 Giiy0 commented Nov 24, 2026 •

Copy link
Copy Markdown

Extends #432 (ComputeIo::initialized_sgprs()) to pixel shaders.

Changes

  • Read SPI_SHADER_PGM_RSRC2_PS from pixel stage data (E+0x128) into StageIo::Ps::rsrc2.
  • Add ps_initialized_sgprs(rsrc2) counting GFX10.3/RDNA2 pixel hardware-initialized SGPRs: USER_SGPR (bits 1-5 plus USER_SGPR_MSB at bit 27; bits 28-31 are SHARED_VGPR_CNT), prim_mask (always present), WAVE_CNT_EN (bit 7), LOAD_COLLISION_WAVEID (bit 25), LOAD_INTRAWAVE_COLLISION (bit 26), and SCRATCH_EN (bit 0).
  • Add StageIo::initialized_sgprs() and wire it into io::raise so general SGPRs (0..=105) at or above the initialized count read as zero in emit::lower, while hardware-initialized SGPRs (such as prim_mask, POPS collision wave ID, and the scratch wave offset) remain refused with "a hardware SGPR input".
  • Add synthetic unit tests in native.rs and an end-to-end emission + dxil.dll validation regression test in emit/mod.rs.

Validation

  • cargo xtask policy — 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings — clean
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings — clean
  • cargo test --workspace --locked — 344 passed, 0 failed, 6 ignored (Windows 11, RTX 3080, Rust 1.98.1)
  • Offline victory translate --validate on local shader archive (26,084 programs, SDK 10.0.26100.0 dxil.dll): emit: a hardware SGPR input drops from 17 to 14 (the 3 programs reading an uninitialized SGPR advance to a later unsupported reason; the remaining 14 read hardware-initialized prim_mask or POPS collision wave ID and stay refused). Validated counts unchanged (12,155 VS/PS, 1,417 CS, 8,152 MS), 0 regressions. Not tested in gameplay.

AI-assisted (Antigravity).

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Two facts for bec4bfa, from reading the diff and Mesa's register database (not run):

  1. ps_initialized_sgprs and its test take USER_SGPR_MSB from RSRC2 bit 28. That is GFX9; on GFX10.3 it is bit 27 (28 to 31 are SHARED_VGPR_CNT): Mesa src/amd/registers/gfx103.json, SPI_SHADER_PGM_RSRC2_PS. With bit 28, a program with shared VGPRs gets 32 user SGPRs too many.
  2. The SGPR right after the primitive mask that 10 of the refused programs read (s18 there) is, as far as I can tell, the POPS collision wave id: the code takes the packer id from bits 28 to 29, wave ids from bits 0 to 9 and 16 to 25 and the overlap bit 31, sets POPS_PACKER and polls src_pops_exiting_wave_id (Mesa 24.2 radv_shader_args.c, aco_instruction_selection.cpp). Read as zero it means "no overlap", so the wait is skipped; without rasterizer ordered views the accesses it ordered can then race (overlapping cutout surfaces). Worth a line in the comment, or a refusal, instead of a general "uninitialized reads as zero".

AI-assisted (Claude Code).

@shortselow-byte

Copy link
Copy Markdown

This PR has no CI run yet, so I ran the CI's checks at head bec4bfa (on main ffe310e): Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT (RDNA2).

  • cargo test --workspace --locked -- --test-threads=1: 344 passed, 0 failed, including pixel_reads_of_uninitialized_sgprs_read_as_zero and ps_initialized_sgprs_covers_user_system_and_scratch.
  • cargo clippy --workspace --all-targets --locked -- -D warnings, and with --all-features: clean.
  • cargo xtask policy: ok.

@Giiy0

Giiy0 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Good catch @TG0000 — pushed a fix moving USER_SGPR_MSB to GFX10.3 bit 27 (and asserting bits 28–31 SHARED_VGPR_CNT don't inflate the SGPR count), plus a doc comment noting why counting the POPS bits keeps collision_waveid refused rather than zeroed.

Thanks @shortselow-byte for running the full CI check on RDNA2!

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at bec4bfa: cargo test -p victory-shader passes, 122 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@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.

Uninitialised pixel SGPRs are handled in main (io.rs initialized_sgprs; lower.rs inputs(): the primitive mask and the ordered-section collision id read 0, others as in compute).

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.

5 participants