fix(shader): give the a16 bias of image samples its own address dword #581

Merged
DotDebian merged 1 commit from upstream-pr/581 into main 2026-10-04 23:50:05 +00:00
DotDebian commented 2026-10-04 23:30:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-04T23:30:57Z.


What

With a16, the MIMG address layout packed the first coordinate into the upper half of the bias dword. On hardware the bias is a half that occupies a full dword, and the coordinates start in the next one:

main hardware
image_sample_b 2D a16 bias,s | t bias,pad | s,t
image_sample_b_cl 2D a16 bias,s | t,clamp bias,pad | s,t | clamp
image_sample_b_o 2D a16 offset | bias,s | t offset | bias,pad | s,t

So image_sample_b, image_sample_b_cl, image_sample_b_o, image_sample_b_cl_o and the biased gathers read their coordinates from the wrong halves whenever a16 is set. With a compare the layout was already right, because the 32-bit reference realigned it.

GetRdnaImageAddressComponentLayout now advances a full dword past the bias. The bias itself is still read as a half from the low 16 bits. Without a16 nothing changes.

Source: LLVM AMDGPULegalizerInfo.cpp, packImage16bitOpsToDwords ("Special handling of bias when A16 is on. Bias is of type half but occupies full 32-bit."), and the expected code in llvm/test/CodeGen/AMDGPU/llvm.amdgcn.image.sample.a16.dim.ll: sample_b_2d is image_sample_b v[0:3], v[0:1] with v1 = {s, t}, sample_b_cl_2d is [v0, v1, v3].

This touches the same helper as #475 (a16 gradients), which leaves the bias packed as on main. The two changes are independent; whichever lands second needs a small rebase.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA RTX 5080. No title run.

New execution test agc_driver_image_sample_bias_a16: a compute shader samples a 16x16 mip chain with image_sample_b, image_sample_b_o (two offsets) and image_sample_b_cl, all a16, on 32 threads with distinct coordinates. The unused upper half of the bias dword holds another coordinate, so reading it shows. The bias is 0: outside pixel shaders the recompiler samples at an explicit LOD, so the bias value is not what this test covers. image_sample_b_cl runs only on devices with shaderResourceMinLod.

  • on main the test fails: image_sample_b a16: thread 0 component 1 is 8.000000, expected 3
  • with the fix it passes; ctest (Release): 214/214 passed

Not tested on AMD hardware.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4738425243 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/581). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-04T23:30:57Z. --- ### What With `a16`, the MIMG address layout packed the first coordinate into the upper half of the bias dword. On hardware the bias is a half that occupies a full dword, and the coordinates start in the next one: | | main | hardware | |-|-|-| | `image_sample_b` 2D a16 | `bias,s \| t` | `bias,pad \| s,t` | | `image_sample_b_cl` 2D a16 | `bias,s \| t,clamp` | `bias,pad \| s,t \| clamp` | | `image_sample_b_o` 2D a16 | `offset \| bias,s \| t` | `offset \| bias,pad \| s,t` | So `image_sample_b`, `image_sample_b_cl`, `image_sample_b_o`, `image_sample_b_cl_o` and the biased gathers read their coordinates from the wrong halves whenever `a16` is set. With a compare the layout was already right, because the 32-bit reference realigned it. `GetRdnaImageAddressComponentLayout` now advances a full dword past the bias. The bias itself is still read as a half from the low 16 bits. Without `a16` nothing changes. Source: LLVM `AMDGPULegalizerInfo.cpp`, `packImage16bitOpsToDwords` ("Special handling of bias when A16 is on. Bias is of type half but occupies full 32-bit."), and the expected code in `llvm/test/CodeGen/AMDGPU/llvm.amdgcn.image.sample.a16.dim.ll`: `sample_b_2d` is `image_sample_b v[0:3], v[0:1]` with `v1 = {s, t}`, `sample_b_cl_2d` is `[v0, v1, v3]`. This touches the same helper as #475 (a16 gradients), which leaves the bias packed as on main. The two changes are independent; whichever lands second needs a small rebase. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, NVIDIA RTX 5080. No title run. New execution test `agc_driver_image_sample_bias_a16`: a compute shader samples a 16x16 mip chain with `image_sample_b`, `image_sample_b_o` (two offsets) and `image_sample_b_cl`, all `a16`, on 32 threads with distinct coordinates. The unused upper half of the bias dword holds another coordinate, so reading it shows. The bias is 0: outside pixel shaders the recompiler samples at an explicit LOD, so the bias value is not what this test covers. `image_sample_b_cl` runs only on devices with `shaderResourceMinLod`. - on main the test fails: `image_sample_b a16: thread 0 component 1 is 8.000000, expected 3` - with the fix it passes; `ctest` (Release): 214/214 passed Not tested on AMD hardware. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-04 23:44:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T23:44:12Z.


Thanks, good find. I checked it with LLVM's own code generation for gfx1013: sample_b_2d with a16 puts the bias alone in v0 and {s, t} in v1, sample_b_cl_2d uses [v0, v1, v3], and sample_b_o_2d uses offset, bias, then {s, t}, so the bias always takes a whole dword. With a compare the layout comes out the same as before, as you say, and without a16 nothing changes. The fix covers every opcode with the bias flag, the gathers included.

The test shaders disassemble (llvm-mc, gfx1013) to the loads, the three image_sample_b forms with offsets (1, 2) and (-1, -2), and image_sample_b_cl, and putting a different coordinate in the upper half of the bias dword is a nice way to make the old packing visible.

agc_driver_image_sample_bias_a16 passes here on NVIDIA, and on main's code it fails exactly as in the description ("thread 0 component 1 is 8.000000, expected 3").

PPSA21564 has no a16 image samples, so this doesn't change anything in my runs. The overlap with #475 is only textual, since bias and gradients never appear together. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985703886 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/581#issuecomment-5985703886). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T23:44:12Z. --- Thanks, good find. I checked it with LLVM's own code generation for gfx1013: `sample_b_2d` with a16 puts the bias alone in v0 and `{s, t}` in v1, `sample_b_cl_2d` uses `[v0, v1, v3]`, and `sample_b_o_2d` uses offset, bias, then `{s, t}`, so the bias always takes a whole dword. With a compare the layout comes out the same as before, as you say, and without a16 nothing changes. The fix covers every opcode with the bias flag, the gathers included. The test shaders disassemble (llvm-mc, gfx1013) to the loads, the three `image_sample_b` forms with offsets (1, 2) and (-1, -2), and `image_sample_b_cl`, and putting a different coordinate in the upper half of the bias dword is a nice way to make the old packing visible. `agc_driver_image_sample_bias_a16` passes here on NVIDIA, and on main's code it fails exactly as in the description ("thread 0 component 1 is 8.000000, expected 3"). PPSA21564 has no a16 image samples, so this doesn't change anything in my runs. The overlap with #475 is only textual, since bias and gradients never appear together. Ready to merge from my side.
github-actions[bot] commented 2026-10-04 23:46:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T23:46:08Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#475 core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp L241
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985717662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/581#issuecomment-5985717662). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-04T23:46:08Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #475 | `core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp` [L241](https://github.com/boykopovar/AnyPS5/blob/b5d54c9db89650ccf6e7260f7a44388cbf9b5692/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp#L241) | | |
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!581
No description provided.