feat(shader): implement image_sample_cd/_g16 variants and fix a16 gradient layout #475

Merged
adriadam10 merged 3 commits from upstream-pr/475 into main 2026-10-05 05:10:20 +00:00
adriadam10 commented 2026-10-03 18:04:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T18:04:22Z.


What

Found by the ISA sweep: 24 MIMG sample opcodes were refused, and the existing derivative path read the wrong address dwords when a16 is set.

  • image_sample_cd* (0x68-0x6f) and image_sample_{c_}d*_g16 / image_sample_{c_}cd*_g16 (0xa2-0xbb, 0xe8-0xef) are now decoded and translated (_cl, _o and c_ variants included).
  • Bug: with a16, image_sample_d packed the gradients as 16-bit. On gfx10, a16 only packs the coordinates (and bias/LOD/clamp): the gradients stay 32-bit unless the opcode is _g16. For example, 2D image_sample_d a16 is 4 gradient dwords + 1 coordinate dword, not 3 dwords. Before this fix, every such sample returned 0 on hardware-correct inputs.
  • _g16 packs two gradients per dword, and each direction starts a new dword. 1D is dsdh | dsdv | s. 3D is dsdh,dtdh | drdh,pad | dsdv,dtdv | drdv,pad | s | t | r.
  • _cd passes its gradients in the same layout as _d. The hardware computes one LOD per quad, from the gradients of the quad's first active lane. The translation reads the gradient dwords from that lane (ReadLane with quad base + ctz(EXEC bits of the quad)), then samples with them.
  • The decoder carries the gradient count and G16 in the address flags, so GetRdnaImageAddressComponentLayout stays a function of the flags alone.

Behaviour measured on HW (RDNA2 iGPU, raw kernels)

Setup: a single-level R32F texture whose texel value is x + 100y + 1000z, a sampler with mag = point and min = linear, and coordinates at a quarter texel. The returned value shows which dwords were read as coordinates, and its fraction (.25 or .0) gives the sign of the LOD.

  • The LLVM layouts above are confirmed. The "packed a16 gradients" layout returns 0, and so does the "no padding" _g16 layout.
  • _cd: if only one lane of a quad has large gradients, the whole quad minifies only when that lane is the quad's lane 0. With lane 0 off in EXEC, the quad's next active lane decides.

Builds on #395, which removes the _cd refusal (its commit is included here; the conflict on the same lines is resolved in this branch). This PR keeps that change and adds the per-quad LOD.

Tested

Arch Linux, Radeon 660M. New execution test ImageSampleDerivatives: 24 sample variants over 1D/2D/3D textures, expected values captured on hardware. It passes on RADV, lavapipe (Mesa 26) and the CI's lavapipe (Mesa 25.2.8). The two EXEC-masked _cd cases are skipped when the host subgroup is smaller than 32 (lavapipe), as in the other cross-lane tests.

The test fails without each piece of the fix:

  • packed a16 gradients;
  • _g16 without per-direction padding;
  • _cd treated as _d;
  • _cd reading lane 0 of the quad instead of the first active lane (RADV).

ctest (Release): everything passes except agc_driver_pixel_helper_lanes on local RADV, which also fails on main.

The _cl variants decode with the same layout. Their execution needs #388 (MinLod capability), so the test does not use them.

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: #395
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4728591746 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T18:04:22Z. --- ### What Found by the ISA sweep: 24 MIMG sample opcodes were refused, and the existing derivative path read the wrong address dwords when `a16` is set. - `image_sample_cd*` (0x68-0x6f) and `image_sample_{c_}d*_g16` / `image_sample_{c_}cd*_g16` (0xa2-0xbb, 0xe8-0xef) are now decoded and translated (`_cl`, `_o` and `c_` variants included). - **Bug:** with `a16`, `image_sample_d` packed the gradients as 16-bit. On gfx10, `a16` only packs the coordinates (and bias/LOD/clamp): the gradients stay 32-bit unless the opcode is `_g16`. For example, 2D `image_sample_d a16` is 4 gradient dwords + 1 coordinate dword, not 3 dwords. Before this fix, every such sample returned 0 on hardware-correct inputs. - `_g16` packs two gradients per dword, and each direction starts a new dword. 1D is `dsdh | dsdv | s`. 3D is `dsdh,dtdh | drdh,pad | dsdv,dtdv | drdv,pad | s | t | r`. - `_cd` passes its gradients in the same layout as `_d`. The hardware computes one LOD per quad, from the gradients of the quad's **first active lane**. The translation reads the gradient dwords from that lane (`ReadLane` with `quad base + ctz(EXEC bits of the quad)`), then samples with them. - The decoder carries the gradient count and `G16` in the address flags, so `GetRdnaImageAddressComponentLayout` stays a function of the flags alone. #### Behaviour measured on HW (RDNA2 iGPU, raw kernels) Setup: a single-level R32F texture whose texel value is `x + 100y + 1000z`, a sampler with mag = point and min = linear, and coordinates at a quarter texel. The returned value shows which dwords were read as coordinates, and its fraction (`.25` or `.0`) gives the sign of the LOD. - The LLVM layouts above are confirmed. The "packed `a16` gradients" layout returns 0, and so does the "no padding" `_g16` layout. - `_cd`: if only one lane of a quad has large gradients, the whole quad minifies only when that lane is the quad's lane 0. With lane 0 off in EXEC, the quad's next active lane decides. Builds on #395, which removes the `_cd` refusal (its commit is included here; the conflict on the same lines is resolved in this branch). This PR keeps that change and adds the per-quad LOD. ### Tested Arch Linux, Radeon 660M. New execution test `ImageSampleDerivatives`: 24 sample variants over 1D/2D/3D textures, expected values captured on hardware. It passes on RADV, lavapipe (Mesa 26) and the CI's lavapipe (Mesa 25.2.8). The two EXEC-masked `_cd` cases are skipped when the host subgroup is smaller than 32 (lavapipe), as in the other cross-lane tests. The test fails without each piece of the fix: - packed `a16` gradients; - `_g16` without per-direction padding; - `_cd` treated as `_d`; - `_cd` reading lane 0 of the quad instead of the first active lane (RADV). `ctest` (Release): everything passes except `agc_driver_pixel_helper_lanes` on local RADV, which also fails on `main`. The `_cl` variants decode with the same layout. Their execution needs #388 (MinLod capability), so the test does not use them. ### 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: #395 - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-03 18:04:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-03T18:04:43Z.


Progress report (ebf29f8 → 559aeac)

📈 GPU shader instructions: 89.37% (+2.06%, +24 implemented instructions)

✅ 24 implemented
Encoding Name
MIMG IMAGE_SAMPLE_CD
MIMG IMAGE_SAMPLE_CD_CL
MIMG IMAGE_SAMPLE_CD_CL_G16
MIMG IMAGE_SAMPLE_CD_CL_O
MIMG IMAGE_SAMPLE_CD_CL_O_G16
MIMG IMAGE_SAMPLE_CD_G16
MIMG IMAGE_SAMPLE_CD_O
MIMG IMAGE_SAMPLE_CD_O_G16
MIMG IMAGE_SAMPLE_C_CD
MIMG IMAGE_SAMPLE_C_CD_CL
MIMG IMAGE_SAMPLE_C_CD_CL_G16
MIMG IMAGE_SAMPLE_C_CD_CL_O
MIMG IMAGE_SAMPLE_C_CD_CL_O_G16
MIMG IMAGE_SAMPLE_C_CD_G16
MIMG IMAGE_SAMPLE_C_CD_O
MIMG IMAGE_SAMPLE_C_CD_O_G16
MIMG IMAGE_SAMPLE_C_D_CL_G16
MIMG IMAGE_SAMPLE_C_D_CL_O_G16
MIMG IMAGE_SAMPLE_C_D_G16
MIMG IMAGE_SAMPLE_C_D_O_G16
MIMG IMAGE_SAMPLE_D_CL_G16
MIMG IMAGE_SAMPLE_D_CL_O_G16
MIMG IMAGE_SAMPLE_D_G16
MIMG IMAGE_SAMPLE_D_O_G16
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5971968429 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5971968429). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-03T18:04:43Z. --- <!-- progress-report --> ### Progress report (ebf29f8 → 559aeac) 📈 **GPU shader instructions**: 89.37% (+2.06%, +24 implemented instructions) <details> <summary>✅ 24 implemented</summary> | Encoding | Name | | - | - | | MIMG | `IMAGE_SAMPLE_CD` | | MIMG | `IMAGE_SAMPLE_CD_CL` | | MIMG | `IMAGE_SAMPLE_CD_CL_G16` | | MIMG | `IMAGE_SAMPLE_CD_CL_O` | | MIMG | `IMAGE_SAMPLE_CD_CL_O_G16` | | MIMG | `IMAGE_SAMPLE_CD_G16` | | MIMG | `IMAGE_SAMPLE_CD_O` | | MIMG | `IMAGE_SAMPLE_CD_O_G16` | | MIMG | `IMAGE_SAMPLE_C_CD` | | MIMG | `IMAGE_SAMPLE_C_CD_CL` | | MIMG | `IMAGE_SAMPLE_C_CD_CL_G16` | | MIMG | `IMAGE_SAMPLE_C_CD_CL_O` | | MIMG | `IMAGE_SAMPLE_C_CD_CL_O_G16` | | MIMG | `IMAGE_SAMPLE_C_CD_G16` | | MIMG | `IMAGE_SAMPLE_C_CD_O` | | MIMG | `IMAGE_SAMPLE_C_CD_O_G16` | | MIMG | `IMAGE_SAMPLE_C_D_CL_G16` | | MIMG | `IMAGE_SAMPLE_C_D_CL_O_G16` | | MIMG | `IMAGE_SAMPLE_C_D_G16` | | MIMG | `IMAGE_SAMPLE_C_D_O_G16` | | MIMG | `IMAGE_SAMPLE_D_CL_G16` | | MIMG | `IMAGE_SAMPLE_D_CL_O_G16` | | MIMG | `IMAGE_SAMPLE_D_G16` | | MIMG | `IMAGE_SAMPLE_D_O_G16` | </details>
oneandonlydean commented 2026-10-04 05:46:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:46:11Z.


Thanks, the per-quad LOD for _cd and the a16 gradient layout are great finds, and the hardware captures make the layout easy to follow. One problem with the lane read for _cd: SpirvAnalysis.cpp treats every ReadLane as a wave-uniform source (UniformSource), because v_readlane takes its lane from an SGPR. Here the lane differs per quad, and in the two-lane emission (a wave64 compute program on a host with 32-wide subgroups, state.laneCount == 2 in SpirvEmitter.cpp) SpirvFlowEmitter reuses the first half's result for every value in sharedLaneValues, so lanes 32 to 63 would sample with the gradients of the quads in lanes 0 to 31. ImageSampleDerivatives.cpp only runs wave32, so it can't catch this. Reading the gradients through a lane-varying shuffle that SpirvAnalysis does not count as uniform (or a small dedicated quad-read op) would fix it, and a wave64 compute case in the test would cover it. Also, since #389 merged, the branch conflicts with main in RdnaImageOpDecoder.cpp (the new image_sample_d_cl_o row sits next to image_sample_cd), so it needs a rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977042699 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5977042699). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:46:11Z. --- Thanks, the per-quad LOD for _cd and the a16 gradient layout are great finds, and the hardware captures make the layout easy to follow. One problem with the lane read for _cd: SpirvAnalysis.cpp treats every ReadLane as a wave-uniform source (UniformSource), because v_readlane takes its lane from an SGPR. Here the lane differs per quad, and in the two-lane emission (a wave64 compute program on a host with 32-wide subgroups, state.laneCount == 2 in SpirvEmitter.cpp) SpirvFlowEmitter reuses the first half's result for every value in sharedLaneValues, so lanes 32 to 63 would sample with the gradients of the quads in lanes 0 to 31. ImageSampleDerivatives.cpp only runs wave32, so it can't catch this. Reading the gradients through a lane-varying shuffle that SpirvAnalysis does not count as uniform (or a small dedicated quad-read op) would fix it, and a wave64 compute case in the test would cover it. Also, since #389 merged, the branch conflicts with main in RdnaImageOpDecoder.cpp (the new image_sample_d_cl_o row sits next to image_sample_cd), so it needs a rebase.
adriadam10 commented 2026-10-04 06:37:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:37:04Z.


Thanks, you're right about ReadLane. Fixed and rebased onto main (the image_sample_d_cl_o row from #389 is kept next to the _cd rows):

  • The quad read now uses BpermuteU32 with the source lane's byte address. It is lane-varying and already listed in LaneSource, and the source lane is always inside the lane's own quad, so it never crosses a 32-lane half.
  • ImageSampleDerivatives also runs every 2D/3D/1D case as a wave64 compute program when the device's subgroups are at least 32 wide. Lanes 32 to 63 get the input rows of lanes 16 to 31 and 0 to 15 (whole quads), so a second half that reused the first half's gradients would sample at the wrong LOD. The EXEC-masked cases are checked only in the lower half, since their masks only set exec_lo.

Passes on RADV, local lavapipe and CI's lavapipe. One limit: this RADV still reports 64-wide subgroups even with RADV_PERFTEST=cswave32, so I couldn't run the two-lane path locally. With ReadLane put back, the test still passes here, so the wave64 case only catches the old version on a 32-wide host like yours.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977370250 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5977370250). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:37:04Z. --- Thanks, you're right about `ReadLane`. Fixed and rebased onto main (the `image_sample_d_cl_o` row from #389 is kept next to the _cd rows): - The quad read now uses `BpermuteU32` with the source lane's byte address. It is lane-varying and already listed in `LaneSource`, and the source lane is always inside the lane's own quad, so it never crosses a 32-lane half. - ImageSampleDerivatives also runs every 2D/3D/1D case as a wave64 compute program when the device's subgroups are at least 32 wide. Lanes 32 to 63 get the input rows of lanes 16 to 31 and 0 to 15 (whole quads), so a second half that reused the first half's gradients would sample at the wrong LOD. The EXEC-masked cases are checked only in the lower half, since their masks only set exec_lo. Passes on RADV, local lavapipe and CI's lavapipe. One limit: this RADV still reports 64-wide subgroups even with `RADV_PERFTEST=cswave32`, so I couldn't run the two-lane path locally. With `ReadLane` put back, the test still passes here, so the wave64 case only catches the old version on a 32-wide host like yours.
adriadam10 commented 2026-10-05 01:40:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:40:49Z.


Rebased on current main (after #581). The address-layout loop now advances by isBiasComponent(flags, index) ? 32u : shape.width, so #581's own dword for the a16 bias is kept alongside this PR's ComponentShape (g16 gradients and a16 coordinates). agc_driver_image_sample_derivatives_tests and #581's agc_driver_image_sample_bias_a16_tests pass on RADV and lavapipe, full ctest green on RADV apart from the known agc_driver_pixel_helper_lanes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986665710 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5986665710). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T01:40:49Z. --- Rebased on current main (after #581). The address-layout loop now advances by `isBiasComponent(flags, index) ? 32u : shape.width`, so #581's own dword for the a16 bias is kept alongside this PR's `ComponentShape` (g16 gradients and a16 coordinates). `agc_driver_image_sample_derivatives_tests` and #581's `agc_driver_image_sample_bias_a16_tests` pass on RADV and lavapipe, full ctest green on RADV apart from the known `agc_driver_pixel_helper_lanes`.
github-actions[bot] commented 2026-10-05 02:02:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T02:02:29Z.


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
#426 core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp L131-133
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986847645 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5986847645). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T02:02:29Z. --- <!-- 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 | |---|---|---|---| | #426 | `core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp` [L131-133](https://github.com/boykopovar/AnyPS5/blob/71f248ad54f4afa481708d44dc68547d1fcaf4ea/core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp#L131-L133) | | |
oneandonlydean commented 2026-10-05 02:47:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:06Z.


Thanks, and sorry I didn't come back to this after your fix on the 4th. BpermuteU32 with the address and EXEC is lane varying, so the wave64 path no longer shares the first half's gradients with lanes 32 to 63, and the source lane is still the first active lane of the quad (or the lane itself when the quad is empty). Running every case as wave64 with different quads in the upper half covers exactly what I was worried about.

The rebase onto #581 looks right too: the bias component still gets its own dword under a16 (isBiasComponent advances 32), and for every non-a16 layout componentShape gives the same offsets as main. agc_driver_image_sample_derivatives and agc_driver_image_sample_bias_a16 pass here on the merged branch.

PPSA21564 has no _cd, _g16 or a16 samples, but the address layout code is shared by every image sample, so I'll put this through my PPSA21564 check before merging. Ready to merge from my side otherwise.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987227524 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5987227524). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T02:47:06Z. --- Thanks, and sorry I didn't come back to this after your fix on the 4th. `BpermuteU32` with the address and EXEC is lane varying, so the wave64 path no longer shares the first half's gradients with lanes 32 to 63, and the source lane is still the first active lane of the quad (or the lane itself when the quad is empty). Running every case as wave64 with different quads in the upper half covers exactly what I was worried about. The rebase onto #581 looks right too: the bias component still gets its own dword under a16 (`isBiasComponent` advances 32), and for every non-a16 layout `componentShape` gives the same offsets as main. `agc_driver_image_sample_derivatives` and `agc_driver_image_sample_bias_a16` pass here on the merged branch. PPSA21564 has no `_cd`, `_g16` or a16 samples, but the address layout code is shared by every image sample, so I'll put this through my PPSA21564 check before merging. Ready to merge from my side otherwise.
oneandonlydean commented 2026-10-05 05:03:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:03:58Z.


Gated on PPSA21564: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5988441757 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/475#issuecomment-5988441757). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T05:03:58Z. --- Gated on PPSA21564: no visual or FPS change.
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!475
No description provided.