fix(recompiler): zero the high half of 16-bit SDWA results written as a whole dword #214

Merged
oneandonlydean merged 1 commit from upstream-pr/214 into main 2026-10-01 18:27:50 +00:00
oneandonlydean commented 2026-10-01 12:08:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-01T12:08:34Z.


What

An SDWA instruction with dst_sel:DWORD dst_unused:UNUSED_PAD writes its 16-bit result zero-extended to the whole destination register. TranslationContext::write16Bits treated sdwaSel 6 (DWORD) like the plain VOP encoding of a 16-bit instruction, which on GFX10 keeps the destination's high half, so the SDWA form kept stale high bits instead of zeroing them. The decoder already marks SDWA destinations with explicitSdwaDst; write16Bits now sends those through writeRawU32 (zero-padded). Plain 16-bit encodings and dst_sel:WORD_0/WORD_1 with UNUSED_PRESERVE are unchanged.

A common idiom packs two halves this way: v_cvt_f16_f32_sdwa lo ... dst_sel:DWORD dst_unused:UNUSED_PAD, the same for hi, then v_lshl_add_u32 dst, hi, 16, lo. With stale bits left in lo, the integer add carries them into the high half.

In PPSA21564 the FidelityFX depth of field combine pass packs its RG and BA outputs exactly like that, so G and A of the composited scene picked up garbage: a green haze with values up to about 1500 and black tile-shaped blocks over the intro cinematic's crowd and space shots. About 33 other shaders in the title use the same form (GPU skinning computes and a particle shader).

Before (left), with this fix (middle) and the real PS5 (right), in the crowd and space shots:

SDWA dword destination before and after

Tested

  • Linux (CachyOS, RTX 4090): new agc_driver_sdwa_destination_tests runs guest code on the GPU and checks v_cvt_f16_f32_sdwa with DWORD/UNUSED_PAD zeroing the high half, the plain v_cvt_f16_f32_e32 keeping it, WORD_1 with UNUSED_PRESERVE keeping the low half, the pad, pad, v_lshl_add_u32 packing idiom, and v_add_f16_sdwa with DWORD/UNUSED_PAD. On main it fails with "thread 0 result 0 is 0xabcdbf00, expected 0x0000bf00"; with the fix it passes. Like agc_driver_mix_precision_tests it needs a Vulkan device, so it is built but not part of ctest.
  • Exercised in PPSA21564 on our integration branch: over the crowd and space shots (235 frames) the green haze went from 208 frames to none and the tile-shaped black blocks are gone; skinned characters render correctly.
  • ctest --test-dir build: 89/89 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:4703412754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/214). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-01T12:08:34Z. --- ### What An SDWA instruction with `dst_sel:DWORD dst_unused:UNUSED_PAD` writes its 16-bit result zero-extended to the whole destination register. `TranslationContext::write16Bits` treated `sdwaSel` 6 (DWORD) like the plain VOP encoding of a 16-bit instruction, which on GFX10 keeps the destination's high half, so the SDWA form kept stale high bits instead of zeroing them. The decoder already marks SDWA destinations with `explicitSdwaDst`; `write16Bits` now sends those through `writeRawU32` (zero-padded). Plain 16-bit encodings and `dst_sel:WORD_0/WORD_1` with `UNUSED_PRESERVE` are unchanged. A common idiom packs two halves this way: `v_cvt_f16_f32_sdwa lo ... dst_sel:DWORD dst_unused:UNUSED_PAD`, the same for `hi`, then `v_lshl_add_u32 dst, hi, 16, lo`. With stale bits left in `lo`, the integer add carries them into the high half. In PPSA21564 the FidelityFX depth of field combine pass packs its RG and BA outputs exactly like that, so G and A of the composited scene picked up garbage: a green haze with values up to about 1500 and black tile-shaped blocks over the intro cinematic's crowd and space shots. About 33 other shaders in the title use the same form (GPU skinning computes and a particle shader). Before (left), with this fix (middle) and the real PS5 (right), in the crowd and space shots: ![SDWA dword destination before and after](https://raw.githubusercontent.com/oneandonlydean/AnyPS5/ab7e03ebbd015c5b1985d49aeea588229e666f15/sdwa-before-after-ps5.png) ### Tested - Linux (CachyOS, RTX 4090): new `agc_driver_sdwa_destination_tests` runs guest code on the GPU and checks `v_cvt_f16_f32_sdwa` with DWORD/UNUSED_PAD zeroing the high half, the plain `v_cvt_f16_f32_e32` keeping it, `WORD_1` with UNUSED_PRESERVE keeping the low half, the pad, pad, `v_lshl_add_u32` packing idiom, and `v_add_f16_sdwa` with DWORD/UNUSED_PAD. On main it fails with "thread 0 result 0 is 0xabcdbf00, expected 0x0000bf00"; with the fix it passes. Like `agc_driver_mix_precision_tests` it needs a Vulkan device, so it is built but not part of ctest. - Exercised in PPSA21564 on our integration branch: over the crowd and space shots (235 frames) the green haze went from 208 frames to none and the tile-shaped black blocks are gone; skinned characters render correctly. - `ctest --test-dir build`: 89/89 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!214
No description provided.