fix(recompiler): select v_fma_mix source precision by OP_SEL_HI and the half by OP_SEL #205

Merged
oneandonlydean merged 1 commit from upstream-pr/205 into main 2026-10-01 08:32:45 +00:00
oneandonlydean commented 2026-10-01 03:24:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-01T03:24:00Z.


What

v_fma_mix_f32 and v_fma_mixlo_f16/v_fma_mixhi_f16 take each source as an f32 or as one half of a packed f16 register. Per source, OP_SEL_HI set means the source is f16 and OP_SEL then picks its low or high half; OP_SEL_HI clear means the source is an f32 (the encoding LLVM uses for these instructions).

TranslationContext::readMixF32 had the two bits swapped: it used opSel as the f16 switch and opSelHi as the half. So a low-half f16 source (op_sel_hi set, op_sel clear) was read as the whole register's 32 bits as an f32, a tiny or garbage value, while a high-half source happened to come out right. The fix swaps them; v_dot2c and the cube ops that also go through readMixF32 never set op_sel on their operands and are unaffected.

In PPSA21564 the material shaders lerp packed f16 colours with exactly these instructions (e.g. v_fma_mix_f32 v10, 1.0, v11, -v8 op_sel_hi:[0,1,1] for the low half, the same with op_sel:[0,1,1] for the high half). Red and blue live in the low halves and collapsed to near zero in the G-buffer albedo (a grey moon came out as (12, 180, 12)), while green and alpha survived, so every lit surface had a green cast and red details went black. With the fix the albedo reads (178, 178, 178).

Before (left), with this fix (middle) and the real PS5 (right):

v_fma_mix before and after

Tested

  • Linux (CachyOS, RTX 4090): new agc_driver_mix_precision_tests recompiles hand-assembled mix instructions and runs them on the GPU against a model of the guest instructions: low and high f16 halves, the 1.0 inline constant with negation, mixed f32 and f16 sources, the abs modifier, mixlo/mixhi keeping the other half, and op_sel set on an f32 source. On main it fails with "thread 0 result 0 is -0.007825, expected 3.250000"; with the fix it passes. Like the other GPU tests (agc_driver_recorder_tests, agc_driver_mesh_tests) it is built but not part of ctest, since it needs a Vulkan device.
  • Exercised in PPSA21564 on our integration branch: the green cast on lit surfaces is gone (moon, asteroids, the mothership hull and its red stripes).
  • ctest --test-dir build: 79/79 pass on current main with this branch.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4698835580 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/205). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-01T03:24:00Z. --- ### What `v_fma_mix_f32` and `v_fma_mixlo_f16`/`v_fma_mixhi_f16` take each source as an f32 or as one half of a packed f16 register. Per source, `OP_SEL_HI` set means the source is f16 and `OP_SEL` then picks its low or high half; `OP_SEL_HI` clear means the source is an f32 (the encoding LLVM uses for these instructions). `TranslationContext::readMixF32` had the two bits swapped: it used `opSel` as the f16 switch and `opSelHi` as the half. So a low-half f16 source (`op_sel_hi` set, `op_sel` clear) was read as the whole register's 32 bits as an f32, a tiny or garbage value, while a high-half source happened to come out right. The fix swaps them; `v_dot2c` and the cube ops that also go through `readMixF32` never set `op_sel` on their operands and are unaffected. In PPSA21564 the material shaders lerp packed f16 colours with exactly these instructions (e.g. `v_fma_mix_f32 v10, 1.0, v11, -v8 op_sel_hi:[0,1,1]` for the low half, the same with `op_sel:[0,1,1]` for the high half). Red and blue live in the low halves and collapsed to near zero in the G-buffer albedo (a grey moon came out as `(12, 180, 12)`), while green and alpha survived, so every lit surface had a green cast and red details went black. With the fix the albedo reads `(178, 178, 178)`. Before (left), with this fix (middle) and the real PS5 (right): ![v_fma_mix before and after](https://raw.githubusercontent.com/oneandonlydean/AnyPS5/80b5425668c5ca3466d02feb7392894cf299ad15/fma-mix-before-after-ps5.png) ### Tested - Linux (CachyOS, RTX 4090): new `agc_driver_mix_precision_tests` recompiles hand-assembled mix instructions and runs them on the GPU against a model of the guest instructions: low and high f16 halves, the 1.0 inline constant with negation, mixed f32 and f16 sources, the abs modifier, `mixlo`/`mixhi` keeping the other half, and `op_sel` set on an f32 source. On main it fails with "thread 0 result 0 is -0.007825, expected 3.250000"; with the fix it passes. Like the other GPU tests (`agc_driver_recorder_tests`, `agc_driver_mesh_tests`) it is built but not part of ctest, since it needs a Vulkan device. - Exercised in PPSA21564 on our integration branch: the green cast on lit surfaces is gone (moon, asteroids, the mothership hull and its red stripes). - `ctest --test-dir build`: 79/79 pass on current main with this branch. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
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
boykopovar/AnyPS5!205
No description provided.