shader: follow known 16-bit VGPR halves across half ops, phis, and exports (#455) #464

Open
Sheffy007 wants to merge 1 commit from upstream-pr/464 into main
Sheffy007 commented 2026-11-27 21:24:32 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-27T21:24:32Z.


Closes #455
Refs #4, #258

Problem

When v_fma_mixlo_f16 or v_fma_mixhi_f16 writes one 16-bit half over a VGPR whose other half has no host form (such as a v_interp_p1ll_f16 scratch register or a barycentric input), Lower records the known half in low16 or high16 while leaving Val::Missing in vals. However:

  1. high16 had no source lookup helper, and low16/high16 were not consulted by v_fma_mix* (op_sel_hi high half), v_pack_b32_f16, v_pk_*, 16-bit VALU high-half sources, SDWA selects, half-shifts/half-masks/v_bfi_b32/v_bfe_*/v_perm_b32, a16/d16 image and LDS half accesses, or unguarded v_mov_b32/v_cndmask_b32 copies.
  2. write_half and low_half zeroed the kept half instead of preserving low16/high16 when completing a register, and reset_for_rect_corner did not clear high16.
  3. Lower::export neither clipped Target::Color channels to OSG1's cols nor consulted low16/high16 on compressed exports (exp ... compr on GFX10 enables register pairs via en 0x3 or 0xf even for 1- or 3-channel targets).
  4. fill_phis refused joins of known-half values whose transitive readers only read that half, and overwritten_movreld_edge only matched "barycentrics read directly" and not "the first half of an interpolation".

Changes

  • crates/victory-shader/src/emit/lower.rs, lower/rect.rs:
    • Add half16_src, value_half, src_half, and phi_half_only / only_reads_half, and clear high16 in reset_for_rect_corner.
    • Follow low16/high16 across v_fma_mix*, v_pack_b32_f16, v_pk_*, half_src, v_fmac_f16/v_mac_f16, SDWA selects, v_lshlrev_b32/v_lshrrev_b32/v_ashrrev_i32 (>= 16), half-mask v_and_b32/v_and_or_b32/v_lshl_add_u32/v_lshl_or_b32/v_bfi_b32/v_bfe_*/v_perm_b32, a16/d16 image and LDS half accesses, unguarded v_mov_b32/v_cndmask_b32 copies, and half-only fill_phis joins.
    • Preserve low16/high16 in write_half and low_half, and extend overwritten_movreld_edge to "the first half of an interpolation".
    • Restrict Target::Color exports to [col, col + cols), read value_half for half exports, and pad the trailing odd half of guessed 1-/3-channel exp ... compr pairs (// Guess:, documented in docs/native-host.md).
  • Tests:
    • export_tests::compressed_exports_and_half_ops_follow_known_16bit_halves
    • export_tests::half_only_phis_and_overwritten_movreld_loops_translate_while_other_half_reads_refuse
    • shader_tests::known_16bit_halves_draw_across_mix_pack_joins_and_odd_compressed_exports (Metal GPU execution)

Verification

  • cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy

cc @Olima34

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5533274550 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/464). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-27T21:24:32Z. --- Closes #455 Refs #4, #258 ## Problem When `v_fma_mixlo_f16` or `v_fma_mixhi_f16` writes one 16-bit half over a VGPR whose other half has no host form (such as a `v_interp_p1ll_f16` scratch register or a barycentric input), `Lower` records the known half in `low16` or `high16` while leaving `Val::Missing` in `vals`. However: 1. `high16` had no source lookup helper, and `low16`/`high16` were not consulted by `v_fma_mix*` (`op_sel_hi` high half), `v_pack_b32_f16`, `v_pk_*`, 16-bit VALU high-half sources, SDWA selects, half-shifts/half-masks/`v_bfi_b32`/`v_bfe_*`/`v_perm_b32`, `a16`/`d16` image and LDS half accesses, or unguarded `v_mov_b32`/`v_cndmask_b32` copies. 2. `write_half` and `low_half` zeroed the kept half instead of preserving `low16`/`high16` when completing a register, and `reset_for_rect_corner` did not clear `high16`. 3. `Lower::export` neither clipped `Target::Color` channels to `OSG1`'s `cols` nor consulted `low16`/`high16` on compressed exports (`exp ... compr` on GFX10 enables register pairs via `en` `0x3` or `0xf` even for 1- or 3-channel targets). 4. `fill_phis` refused joins of known-half values whose transitive readers only read that half, and `overwritten_movreld_edge` only matched `"barycentrics read directly"` and not `"the first half of an interpolation"`. ## Changes - **`crates/victory-shader/src/emit/lower.rs`, `lower/rect.rs`:** - Add `half16_src`, `value_half`, `src_half`, and `phi_half_only` / `only_reads_half`, and clear `high16` in `reset_for_rect_corner`. - Follow `low16`/`high16` across `v_fma_mix*`, `v_pack_b32_f16`, `v_pk_*`, `half_src`, `v_fmac_f16`/`v_mac_f16`, SDWA selects, `v_lshlrev_b32`/`v_lshrrev_b32`/`v_ashrrev_i32` (`>= 16`), half-mask `v_and_b32`/`v_and_or_b32`/`v_lshl_add_u32`/`v_lshl_or_b32`/`v_bfi_b32`/`v_bfe_*`/`v_perm_b32`, `a16`/`d16` image and LDS half accesses, unguarded `v_mov_b32`/`v_cndmask_b32` copies, and half-only `fill_phis` joins. - Preserve `low16`/`high16` in `write_half` and `low_half`, and extend `overwritten_movreld_edge` to `"the first half of an interpolation"`. - Restrict `Target::Color` exports to `[col, col + cols)`, read `value_half` for `half` exports, and pad the trailing odd half of guessed 1-/3-channel `exp ... compr` pairs (`// Guess:`, documented in `docs/native-host.md`). - **Tests:** - `export_tests::compressed_exports_and_half_ops_follow_known_16bit_halves` - `export_tests::half_only_phis_and_overwritten_movreld_loops_translate_while_other_half_reads_refuse` - `shader_tests::known_16bit_halves_draw_across_mix_pack_joins_and_odd_compressed_exports` (Metal GPU execution) ## Verification - `cargo test -p victory-hle -p victory -p victory-gpu -p victory-metal -p victory-shader -p xtask` - `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo clippy --workspace --all-targets --all-features --target x86_64-pc-windows-gnullvm -- -D warnings` - `cargo xtask policy` cc @Olima34
Olima34 commented 2026-11-27 23:02:33 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-27T23:02:33Z.


Thanks for this PR! An offline check of f880ac60, with no game run:

On current main (b998bb65) + #464: tests pass (victory-gpu on arm64, plus victory-hle, victory, xtask and victory-shader). Windows clippy (x86_64-pc-windows-gnullvm, -D warnings) is clean, and cargo xtask policy passes.

Corpus sweep (examples/sweep + tools/corpus/compare.sh, main → main + #464), on two local shader sets:

set newly translated output changed regressions other reason now
A (4,921 shaders) 1 pixel (was: barycentrics read directly) 52 pixel 0 0
B (4,129 shaders) 28 pixel: 22 barycentrics read directly, 4 "lane mask … not written by a scalar ALU instruction", 2 "first half of an interpolation" 90 pixel 0 0

Compute and vertex shaders are unchanged. All changed outputs are in pixel shaders, which fits the export and half-op changes.

Merging:

  • #464 merges cleanly with #409 and with the preview/graphics-2026-11-28 branch (b3c1a1b0).
  • On that preview branch the counts are the same as on main: +1 / 52 changed on A, +28 / 90 changed on B, 0 regressions.
  • With #409, the gains mostly overlap. #409 + #464 adds 3 shaders over #409 alone, all on B, with 0 regressions: pixel_4ced2d9a4b14e6cc, pixel_ef85b8efcc24a3b6 and pixel_ecc384d4a7ffd3e0. Outputs changed for 53 shaders on A and 124 on B.

I haven't checked the rendering of the changed outputs, since no game run was done.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229823699 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/464#issuecomment-7229823699). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-27T23:02:33Z. --- Thanks for this PR! An offline check of f880ac60, with no game run: **On current main (b998bb65) + #464:** tests pass (victory-gpu on arm64, plus victory-hle, victory, xtask and victory-shader). Windows clippy (`x86_64-pc-windows-gnullvm`, `-D warnings`) is clean, and `cargo xtask policy` passes. **Corpus sweep** (`examples/sweep` + `tools/corpus/compare.sh`, main → main + #464), on two local shader sets: | set | newly translated | output changed | regressions | other reason now | |---|---|---|---|---| | A (4,921 shaders) | 1 pixel (was: barycentrics read directly) | 52 pixel | 0 | 0 | | B (4,129 shaders) | 28 pixel: 22 barycentrics read directly, 4 "lane mask … not written by a scalar ALU instruction", 2 "first half of an interpolation" | 90 pixel | 0 | 0 | Compute and vertex shaders are unchanged. All changed outputs are in pixel shaders, which fits the export and half-op changes. **Merging:** - #464 merges cleanly with #409 and with the `preview/graphics-2026-11-28` branch (b3c1a1b0). - On that preview branch the counts are the same as on main: +1 / 52 changed on A, +28 / 90 changed on B, 0 regressions. - With #409, the gains mostly overlap. #409 + #464 adds 3 shaders over #409 alone, all on B, with 0 regressions: `pixel_4ced2d9a4b14e6cc`, `pixel_ef85b8efcc24a3b6` and `pixel_ecc384d4a7ffd3e0`. Outputs changed for 53 shaders on A and 124 on B. I haven't checked the rendering of the changed outputs, since no game run was done.
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/464:upstream-pr/464'
git switch 'upstream-pr/464'

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/464'
git switch 'upstream-pr/464'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/464'
git switch 'upstream-pr/464'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/464'
git switch 'main'
git merge --squash 'upstream-pr/464'
git switch 'main'
git merge --ff-only 'upstream-pr/464'
git switch 'main'
git merge 'upstream-pr/464'
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
LakshanSivakumar/Native-Victory-macOS!464
No description provided.