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

Closed
Giiy0 wants to merge 2 commits from upstream-pr/1176 into main
Giiy0 commented 2026-11-24 16:20:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Giiy0 on 2026-11-24T16:20:15Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6586357655 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1176). Originally posted by [Giiy0](https://github.com/Giiy0) on 2026-11-24T16:20:15Z. --- 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 commented 2026-11-24 18:05:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by TG0000 on 2026-11-24T17:21:17Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139067638 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7139067638). Originally posted by [TG0000](https://github.com/TG0000) on 2026-11-24T17:21:17Z. --- 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 commented 2026-11-24 18:05:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by shortselow-byte on 2026-11-24T17:23:43Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7139128082 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7139128082). Originally posted by [shortselow-byte](https://github.com/shortselow-byte) on 2026-11-24T17:23:43Z. --- 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 commented 2026-11-24 18:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Giiy0 on 2026-11-24T17:53:04Z.


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!

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139843453 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7139843453). Originally posted by [Giiy0](https://github.com/Giiy0) on 2026-11-24T17:53:04Z. --- 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 commented 2026-11-24 18:06:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Zipettte on 2026-11-24T18:00:45Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140028524 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7140028524). Originally posted by [Zipettte](https://github.com/Zipettte) on 2026-11-24T18:00:45Z. --- 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)._
upstream-sync closed this pull request 2026-11-25 01:05:49 +00:00
Marvisak commented 2026-11-25 01:08:03 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T00:33:53Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148642182 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1176#issuecomment-7148642182). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T00:33:53Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1176
No description provided.