Shaders: honor FP32 rounding when packing a byte #1839

Open
AlanQiang0418 wants to merge 3 commits from upstream-pr/1841 into main
AlanQiang0418 commented 2026-11-24 23:13:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AlanQiang0418 on 2026-11-24T23:13:33Z.


A shader that switches FP32 rounding upward expects packing 0.5 into the low byte of 0xAABBCCDD to produce 0xAABBCC01. Main produces 0xAABBCC00 on both an RTX 5080 and WARP. The four rounding modes have been measured on RDNA2. Native hardware measurements

The existing control-flow pass now tracks the four rounding bits in MODE together. Literal bitfield writes update only the selected bits. A join keeps bits on which the incoming paths agree. v_cvt_pk_u8_f32 uses the FP32 field to round before saturating and inserting the result into the destination byte. Unknown rounding at runtime still uses nearest-even. The pack follows the translator's existing assumption of flushed FP32 inputs. Explicit denormal-preserve modes remain outside this change.

Verification

  • Extended the existing packing readback to exercise all four rounding modes, a one-bit register write, a separate FP16/64 write and preservation of the other bytes. Across 250 input tuples per adapter, 85 failed on main on each adapter and all 250 passed with this change on each. Windows 11, RTX 5080 driver 621.14 and Microsoft WARP, with the DXIL validator loaded.
  • All 18 lowering unit tests passed, including the new branch-join case. The existing FP16 mode and rounding readbacks passed on both adapters.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings and the staged asset-policy check passed.

This extends the mode coverage discussed in #1804. Coordination claim. AI-assisted implementation and testing using synthetic programs.

Full-suite comparison

Both Clippy configurations passed in Actions. The new join test, the extended byte-packing test and the existing FP16 mode test passed there. The full shader suite had the same two failures as CI on the exact base commit, in the explicit-level gather and lane permute tests. The PR run had 208 passed, 2 failed and 2 ignored, compared with 207 passed, 2 failed and 2 ignored on the base. PR CI · Base CI

I also ran the full shader crate locally, serially, on both revisions. The same three tests failed on both revisions here: the two heap-buffer stride tests and the explicit-level gather test. This branch had 207 passed, 3 failed and 2 ignored. The base had 206 passed, 3 failed and 2 ignored.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5481921807 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841). Originally posted by [AlanQiang0418](https://github.com/AlanQiang0418) on 2026-11-24T23:13:33Z. --- A shader that switches FP32 rounding upward expects packing 0.5 into the low byte of 0xAABBCCDD to produce 0xAABBCC01. Main produces 0xAABBCC00 on both an RTX 5080 and WARP. The four rounding modes have been measured on RDNA2. [Native hardware measurements](https://github.com/boykopovar/AnyPS5/blob/8170c102d48a39ab76ffd29ea4ffa7ac43bd20e7/docs/dev/debt/shader.md#L135) The existing control-flow pass now tracks the four rounding bits in MODE together. Literal bitfield writes update only the selected bits. A join keeps bits on which the incoming paths agree. `v_cvt_pk_u8_f32` uses the FP32 field to round before saturating and inserting the result into the destination byte. Unknown rounding at runtime still uses nearest-even. The pack follows the translator's existing assumption of flushed FP32 inputs. Explicit denormal-preserve modes remain outside this change. ## Verification - Extended the existing packing readback to exercise all four rounding modes, a one-bit register write, a separate FP16/64 write and preservation of the other bytes. Across 250 input tuples per adapter, 85 failed on main on each adapter and all 250 passed with this change on each. Windows 11, RTX 5080 driver 621.14 and Microsoft WARP, with the DXIL validator loaded. - All 18 lowering unit tests passed, including the new branch-join case. The existing FP16 mode and rounding readbacks passed on both adapters. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` and the staged asset-policy check passed. This extends the mode coverage discussed in #1804. [Coordination claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7147032546). AI-assisted implementation and testing using synthetic programs. ## Full-suite comparison Both Clippy configurations passed in Actions. The new join test, the extended byte-packing test and the existing FP16 mode test passed there. The full shader suite had the same two failures as CI on the exact base commit, in the explicit-level gather and lane permute tests. The PR run had 208 passed, 2 failed and 2 ignored, compared with 207 passed, 2 failed and 2 ignored on the base. [PR CI](https://github.com/Marvisak/VIctory/actions/runs/45508123158) · [Base CI](https://github.com/Marvisak/VIctory/actions/runs/45492970391) I also ran the full shader crate locally, serially, on both revisions. The same three tests failed on both revisions here: the two heap-buffer stride tests and the explicit-level gather test. This branch had 207 passed, 3 failed and 2 ignored. The base had 206 passed, 3 failed and 2 ignored.
catminou4 commented 2026-11-24 23:52:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T23:52:16Z.

Review: APPROVED


Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 119 passed, 0 failed. GPU execution tests are #[cfg(all(test, windows))] and run only on Windows.

FP32 rounding honored when packing a byte: cvt/pack rounding-mode fix — matches DXBC pack semantics.

<!-- greyfinch-sync:Marvisak/VIctory:review:6049333384 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841#pullrequestreview-6049333384). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T23:52:16Z. **Review:** APPROVED --- Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (70564ae), cargo test -p victory-shader --locked → 119 passed, 0 failed. GPU execution tests are #[cfg(all(test, windows))] and run only on Windows. FP32 rounding honored when packing a byte: cvt/pack rounding-mode fix — matches DXBC pack semantics.
upipibi commented 2026-11-25 00:06:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T23:20:47Z.


Ampere run alongside your RTX 5080 one: RTX 3070 8 GB, driver 616.56, plus WARP, Windows 11, debug, ee2cf26 (on main 70564ae). cargo test -p victory-shader --locked: 207 passed, 3 failed, 2 ignored. The new rounding cases in semantics_tests pass, and the 3 failures are #1744's NVIDIA heap-cap failures on main.

AI-assisted (Claude Code). Synthetic tests only.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7147295440 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841#issuecomment-7147295440). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T23:20:47Z. --- Ampere run alongside your RTX 5080 one: RTX 3070 8 GB, driver 616.56, plus WARP, Windows 11, debug, `ee2cf26` (on `main` `70564ae`). `cargo test -p victory-shader --locked`: 207 passed, 3 failed, 2 ignored. The new rounding cases in `semantics_tests` pass, and the 3 failures are #1744's NVIDIA heap-cap failures on `main`. AI-assisted (Claude Code). Synthetic tests only.
Marvisak commented 2026-11-25 08:06:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T07:25:28Z.


Automated triage for the maintainers.

Needs rebase. This PR no longer merges cleanly into main: main has moved since the last triage, and the branch now conflicts in crates/victory-shader/src/emit/lower.rs. It will be reviewed again once it is rebased onto current main.

Previous verdict for this head: merge candidate.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7155844190 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841#issuecomment-7155844190). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T07:25:28Z. --- <!-- victory-triage v1 head=ea1f55f4be3ef7697fd7ca821b7ddc73c4a90797 verdict=needs-rebase at=2026-11-25T22:26:00Z --> Automated triage for the maintainers. **Needs rebase.** This PR no longer merges cleanly into `main`: `main` has moved since the last triage, and the branch now conflicts in `crates/victory-shader/src/emit/lower.rs`. It will be reviewed again once it is rebased onto current `main`. Previous verdict for this head: merge candidate. --- _Generated by [Claude Code](https://claude.ai/code)_
sinan-cmyk commented 2026-11-25 13:02:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T12:14:52Z.


Offline measurement on a retail dump

Result: no count change against main.

I merged head ea1f55f4 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7162115100 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841#issuecomment-7162115100). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T12:14:52Z. --- ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head ea1f55f4 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
fuzeofficlal commented 2026-11-26 23:55:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T23:55:13Z.

Review: APPROVED


LGTM — verified rustfmt on the changed hunks, cargo xtask policy, cargo clippy (--all-targets and --all-features), emit::lower::tests (34 passed, including a_join_preserves_the_rounding_field_its_paths_agree_on), emit::valu_tests on hardware (35 passed), and emit::semantics_tests on both hardware and WARP (34 passed, 1 pre-existing ignored, including the expanded byte_packing_matches_the_hardware).

Checked RoundModes (MODE[3:0] per-bit join and partial s_setreg_imm32_b32 updates), v_cvt_pk_u8_f32 (round_ne/round_pi/round_ni/round_z + [0.0, 255.0] clamp + NaN->0), and the updated test assertions: existing nearest-even and NaN/saturation checks are intact (r as u8 saturates and maps NaN to 0), and all four FP32 rounding modes plus partial MODE writes now match hardware.

Two small non-blocking notes:

  • RoundModes::after (lower.rs:327) only treats Operand::Literal as constant, so a s_setreg_b32 with an inline constant (Operand::Const) clears the written field's known bits and falls back to nearest-even (same documented fallback as conflicting joins).
  • byte_packing_matches_the_hardware currently leaves src0 abs/neg modifiers untested on GPU.
<!-- greyfinch-sync:Marvisak/VIctory:review:6081739948 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841#pullrequestreview-6081739948). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T23:55:13Z. **Review:** APPROVED --- LGTM — verified `rustfmt` on the changed hunks, `cargo xtask policy`, `cargo clippy` (`--all-targets` and `--all-features`), `emit::lower::tests` (34 passed, including `a_join_preserves_the_rounding_field_its_paths_agree_on`), `emit::valu_tests` on hardware (35 passed), and `emit::semantics_tests` on both hardware and WARP (34 passed, 1 pre-existing ignored, including the expanded `byte_packing_matches_the_hardware`). Checked `RoundModes` (`MODE[3:0]` per-bit join and partial `s_setreg_imm32_b32` updates), `v_cvt_pk_u8_f32` (`round_ne`/`round_pi`/`round_ni`/`round_z` + `[0.0, 255.0]` clamp + NaN->0), and the updated test assertions: existing nearest-even and NaN/saturation checks are intact (`r as u8` saturates and maps NaN to `0`), and all four FP32 rounding modes plus partial `MODE` writes now match hardware. Two small non-blocking notes: - `RoundModes::after` (`lower.rs:327`) only treats `Operand::Literal` as constant, so a `s_setreg_b32` with an inline constant (`Operand::Const`) clears the written field's known bits and falls back to nearest-even (same documented fallback as conflicting joins). - `byte_packing_matches_the_hardware` currently leaves `src0` `abs`/`neg` modifiers untested on GPU.
andrewpozdnakov7-jpg commented 2026-11-27 20:15:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T20:15:17Z.


I tested this PR at 2e06840 with #3592 9a62538 on 63bcca4. The merge keeps the FP32 rounding analysis and the Bfi insertion from their original commits. Integration branch 2137695

An additional synthetic compute matrix checked 1,024 inputs per WARP path across the four rounding modes, all four byte positions with and without the high selector bits set, and two destination words. Both harness paths used app-local WARP 1.0.22 and the CPU reference checked the whole destination dword.

Revision Nearest-even mismatches Upward Downward Toward zero
main 0 96 32 32
#3592 alone 0 96 32 32
Combined 0 0 0 0

The original 250-tuple byte-pack test, rounding-field join test, FP16 rounding test and all three tests from #3592 passed. The default-mode fixture still emits 25 DXIL instructions. Strict shader Clippy and the asset policy passed. The extra matrix was local. AI-assisted with Codex.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7227324673 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1841#issuecomment-7227324673). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-27T20:15:17Z. --- I tested this PR at `2e06840` with #3592 `9a62538` on `63bcca4`. The merge keeps the FP32 rounding analysis and the Bfi insertion from their original commits. [Integration branch `2137695`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/review/byte-pack-rounding-and-bfi) An additional synthetic compute matrix checked 1,024 inputs per WARP path across the four rounding modes, all four byte positions with and without the high selector bits set, and two destination words. Both harness paths used app-local WARP 1.0.22 and the CPU reference checked the whole destination dword. | Revision | Nearest-even mismatches | Upward | Downward | Toward zero | | --- | ---: | ---: | ---: | ---: | | `main` | 0 | 96 | 32 | 32 | | #3592 alone | 0 | 96 | 32 | 32 | | Combined | 0 | 0 | 0 | 0 | The original 250-tuple byte-pack test, rounding-field join test, FP16 rounding test and all three tests from #3592 passed. The default-mode fixture still emits 25 DXIL instructions. Strict shader Clippy and the asset policy passed. The extra matrix was local. AI-assisted with Codex.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1841:upstream-pr/1841'
git switch 'upstream-pr/1841'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1841'
git switch 'upstream-pr/1841'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1841'
git switch 'upstream-pr/1841'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1841'
git switch 'main'
git merge --squash 'upstream-pr/1841'
git switch 'main'
git merge --ff-only 'upstream-pr/1841'
git switch 'main'
git merge 'upstream-pr/1841'
git push origin 'main'
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!1839
No description provided.