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

Closed
github-bot wants to merge 2 commits from gh-pr/1176/head into gh-pr/1176/base
Owner

@⁠Giiy0 on GitHub · 2026-11-24 16:20 UTC · original
Giiy0:fix/ps-uninitialized-sgprs → main · +158 −7 · 3 files · 2 commits · closed without merging

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

**@⁠Giiy0** on GitHub · 2026-11-24 16:20 UTC · [original](https://github.com/Marvisak/VIctory/pull/1176) `Giiy0:fix/ps-uninitialized-sgprs` → `main` · +158 −7 · 3 files · 2 commits · closed without merging 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).*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:22:27 +00:00
github-bot closed this pull request 2026-11-26 04:22:27 +00:00
Author
Owner

@⁠TG0000 on GitHub · 2026-11-24 17:21 UTC · original

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

**@⁠TG0000** on GitHub · 2026-11-24 17:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7139067638) 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)._
Author
Owner

@⁠shortselow-byte on GitHub · 2026-11-24 17:23 UTC · original

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.
**@⁠shortselow-byte** on GitHub · 2026-11-24 17:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7139128082) 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.
Author
Owner

@⁠Giiy0 on GitHub · 2026-11-24 17:53 UTC · original

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!

**@⁠Giiy0** on GitHub · 2026-11-24 17:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7139843453) 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!
Author
Owner

@⁠Zipettte on GitHub · 2026-11-24 18:00 UTC · original

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

**@⁠Zipettte** on GitHub · 2026-11-24 18:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7140028524) 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)._
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:33 UTC · original

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** on GitHub · 2026-11-25 00:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7148642182) 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.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1176
No description provided.