feat(shader): implement image_gather4h_pck end to end #1701

Merged
pandather merged 3 commits from upstream-pr/1701 into main 2026-10-08 19:05:28 +00:00
pandather commented 2026-10-08 17:01:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by pandather on 2026-10-08T17:01:18Z.


What

Decodes the two packed horizontal gathers from the AMD machine-readable ISA specs — IMAGE_GATHER4H_PCK (MIMG opcode 98 = 0x62) and IMAGE_GATHER8H_PCK (opcode 99 = 0x63) — implements gather4h_pck fully through translation, the SPIR-V backend and a GPU execution test measured against RDNA2 hardware, and documents gather8h_pck as a desktop null-op. Shader instructions: 1143/1166 -> 1145/1166 decoded; gather4h_pck additionally implemented (the progress table counts implementation separately once the op reaches titles).

No open PR covers these two opcodes: #1314 (packed load/store pck2/pck4, a different opcode family) was closed without merging, and #1053 maps other gather variants. The branch rebases onto current main with no conflicts in the decoder (the only conflict during rebase was the TechnicalDebt.md tail, both sides appending entries).

  • Decoding: both opcodes share the gather4h operand layout (VDATA 128 bits, VADDR 96 bits, SRSRC, SSAMP) per amdgpu_isa_rdna1.xml/amdgpu_isa_rdna2.xml. The decoder accepts multi-bit DMASKs for these two only and sizes the data span as popcount(dmask) — measured on gfx1031 with sentinel-prefilled destination registers: exactly popcount(dmask) consecutive VGPRs are written from vdst ascending, unlike gather4h which always writes four components. A zero DMASK is rejected at decode (measured: the instruction still executes and writes a single zero dword, but every real compiler emission uses a non-zero mask; rejecting matches the other MIMG mask rules).
  • Semantics of gather4h_pck (measured on gfx1031, hand-encoded MIMG words — LLVM rejects the PCK gather mnemonics on gfx908/gfx1010/gfx1031/gfx1035, so encodings came from bit surgery on the assembler's gather4h base word with the ISA opcode values): the four-texel horizontal window is floor(u*width-0.5)+{-1,0,+1,+2} clamped to [0,width); the row is floor(v*height), negative rows clamp to 0 and rows past the height read zeros; the raw bits of the window texels are concatenated into a little-endian bitstream (texel k at bit offset k*texelBits, components little-endian); output register j — the j-th set DMASK bit in ascending order — receives stream word j, zero past the end of the stream. Formats with more than 32 bits per texel write zeros (hardware limit; measured for RGBA16/RGBA32). The window is clamped regardless of the sampler addressing mode (wrap and mirror measured identical to clamp on the validated normalized-sampler recipe). The anchor rule matches the repo's existing PS5-validated HorizontalOffsets (floor(x-0.5), offsets -1..+2), so no new convention is introduced.
  • Implementation: translation carries the packed flag through imageMemoryInfoFromInstruction; the SPIR-V emitter's new EmitPackedHorizontalGatherOp fetches each window texel with per-lane OpImageFetch and reassembles the bitstream with byte assembly, then selects stream word j into destination register j. D16 (lformat-packed 16-bit) data throws "packed horizontal gather with D16 data is not measured" — sub-32-bit packing was measured on hardware (R8 packs four texel dwords into vdst only; RG8 pairs) but the repo's resource model exposes those as distinct formats whose per-texel bit width needs its own emitter path, so it stays an explicit throw rather than a guess.
  • gather8h_pck is a desktop null-op: across DMASKs 0x1-0xFF including the upper nibble, RGBA8/RGBA16/RGBA32 formats, 1D/2D and 3D descriptors, normalized and unnormalized sampling, sentinel-prefilled (0xDEADBEEF / 0xCAFEBABE) destination registers prove it never writes texel data — it only ever zero-fills popcount(dmask) registers. It decodes (so progress counts it) but throws at translation; implementing its eight-texel semantics would be an unverified guess on this hardware, and LLVM rejects the mnemonic on every RDNA1/RDNA2 target.

Sources:

  • Encodings: AMD machine-readable ISA specs (amdgpu_isa_rdna1.xml/amdgpu_isa_rdna2.xml, opcodes 98/99) cross-checked against LLVM's assembler base word for gfx1031 (image_gather4h v[16:19], v[4:5], s[0:7], s[8:11] dmask:0x1 dim:SQ_RSRC_IMG_2D -> F1840108 00401004; PCK word0s F1880108/F18C0108).
  • Behavior: measured on RDNA2 hardware (gfx1031, RX 6700 XT) with the project's hardware oracle over roughly thirty probe configurations; not derived from documentation.

Tested

Oracle measurements: RDNA2 hardware (gfx1031, AMD RX 6700 XT) via SSH/HSA as described above. Test-runner side: Windows 11 on an RTX Blackwell Series GPU (driver 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7): ninja clean, agc_driver_image_sample_gather, agc_driver_image_packed_horizontal_gather and the full suite report 100% tests passed out of 458. Conventions check (tools/check_conventions.py) passes.

  • Extended agc_driver_image_sample_gather: checks both PCK gathers decode from hand-encoded words, multi-bit DMASKs are accepted with dataComponents == popcount(dmask), and zero DMASK is rejected.
  • New agc_driver_image_packed_horizontal_gather: GPU execution test through the recompiler and SPIR-V backend — a 16x4 RGBA8 linear image whose texel markers equal (texelIndex*4+channel+1) & 0xFF, 32 lanes covering interior anchors, both left-edge clamps, row clamping past the height (zero rows), and DMASKs 0x1/0x5/0xF selecting different stream words; all four output registers per lane are checked against the measured bitstream rule. The test passes on the Blackwell runner device, reproducing the gfx1031-measured semantics exactly.

Not tested: no title or homebrew (no compiler in the toolchain emits these mnemonics; real PS5 shaders using them would come from GNM-proven games, not available here). Sub-32-bit-per-texel formats throw explicitly rather than being exercised. gather8h_pck behavior beyond desktop is unknown.

Checklist

  • Based on current main; no other open PR implements the same opcodes (#1314 and #1053 checked — different opcode families)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (imageGather D16 throw, gather8h_pck translation throw); 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 (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4790187126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1701). Originally posted by [pandather](https://github.com/pandather) on 2026-10-08T17:01:18Z. --- ### What Decodes the two packed horizontal gathers from the AMD machine-readable ISA specs — `IMAGE_GATHER4H_PCK` (MIMG opcode 98 = 0x62) and `IMAGE_GATHER8H_PCK` (opcode 99 = 0x63) — implements gather4h_pck fully through translation, the SPIR-V backend and a GPU execution test measured against RDNA2 hardware, and documents gather8h_pck as a desktop null-op. Shader instructions: 1143/1166 -> 1145/1166 decoded; gather4h_pck additionally implemented (the progress table counts implementation separately once the op reaches titles). No open PR covers these two opcodes: #1314 (packed load/store `pck2`/`pck4`, a different opcode family) was closed without merging, and #1053 maps other gather variants. The branch rebases onto current `main` with no conflicts in the decoder (the only conflict during rebase was the TechnicalDebt.md tail, both sides appending entries). - Decoding: both opcodes share the gather4h operand layout (VDATA 128 bits, VADDR 96 bits, SRSRC, SSAMP) per `amdgpu_isa_rdna1.xml`/`amdgpu_isa_rdna2.xml`. The decoder accepts multi-bit DMASKs for these two only and sizes the data span as `popcount(dmask)` — measured on gfx1031 with sentinel-prefilled destination registers: exactly `popcount(dmask)` consecutive VGPRs are written from vdst ascending, unlike gather4h which always writes four components. A zero DMASK is rejected at decode (measured: the instruction still executes and writes a single zero dword, but every real compiler emission uses a non-zero mask; rejecting matches the other MIMG mask rules). - Semantics of gather4h_pck (measured on gfx1031, hand-encoded MIMG words — LLVM rejects the PCK gather mnemonics on gfx908/gfx1010/gfx1031/gfx1035, so encodings came from bit surgery on the assembler's gather4h base word with the ISA opcode values): the four-texel horizontal window is `floor(u*width-0.5)+{-1,0,+1,+2}` clamped to `[0,width)`; the row is `floor(v*height)`, negative rows clamp to 0 and rows past the height read zeros; the raw bits of the window texels are concatenated into a little-endian bitstream (texel k at bit offset `k*texelBits`, components little-endian); output register j — the j-th set DMASK bit in ascending order — receives stream word j, zero past the end of the stream. Formats with more than 32 bits per texel write zeros (hardware limit; measured for RGBA16/RGBA32). The window is clamped regardless of the sampler addressing mode (wrap and mirror measured identical to clamp on the validated normalized-sampler recipe). The anchor rule matches the repo's existing PS5-validated `HorizontalOffsets` (`floor(x-0.5)`, offsets `-1..+2`), so no new convention is introduced. - Implementation: translation carries the packed flag through `imageMemoryInfoFromInstruction`; the SPIR-V emitter's new `EmitPackedHorizontalGatherOp` fetches each window texel with per-lane `OpImageFetch` and reassembles the bitstream with byte assembly, then selects stream word j into destination register j. D16 (`lformat`-packed 16-bit) data throws "packed horizontal gather with D16 data is not measured" — sub-32-bit packing was measured on hardware (R8 packs four texel dwords into vdst only; RG8 pairs) but the repo's resource model exposes those as distinct formats whose per-texel bit width needs its own emitter path, so it stays an explicit throw rather than a guess. - gather8h_pck is a desktop null-op: across DMASKs 0x1-0xFF including the upper nibble, RGBA8/RGBA16/RGBA32 formats, 1D/2D and 3D descriptors, normalized and unnormalized sampling, sentinel-prefilled (0xDEADBEEF / 0xCAFEBABE) destination registers prove it never writes texel data — it only ever zero-fills `popcount(dmask)` registers. It decodes (so progress counts it) but throws at translation; implementing its eight-texel semantics would be an unverified guess on this hardware, and LLVM rejects the mnemonic on every RDNA1/RDNA2 target. Sources: - Encodings: AMD machine-readable ISA specs (`amdgpu_isa_rdna1.xml`/`amdgpu_isa_rdna2.xml`, opcodes 98/99) cross-checked against LLVM's assembler base word for gfx1031 (`image_gather4h v[16:19], v[4:5], s[0:7], s[8:11] dmask:0x1 dim:SQ_RSRC_IMG_2D` -> `F1840108 00401004`; PCK word0s `F1880108`/`F18C0108`). - Behavior: measured on RDNA2 hardware (gfx1031, RX 6700 XT) with the project's hardware oracle over roughly thirty probe configurations; not derived from documentation. ### Tested Oracle measurements: RDNA2 hardware (gfx1031, AMD RX 6700 XT) via SSH/HSA as described above. Test-runner side: Windows 11 on an RTX Blackwell Series GPU (driver 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7): `ninja` clean, `agc_driver_image_sample_gather`, `agc_driver_image_packed_horizontal_gather` and the full suite report `100% tests passed out of 458`. Conventions check (`tools/check_conventions.py`) passes. - Extended `agc_driver_image_sample_gather`: checks both PCK gathers decode from hand-encoded words, multi-bit DMASKs are accepted with `dataComponents == popcount(dmask)`, and zero DMASK is rejected. - New `agc_driver_image_packed_horizontal_gather`: GPU execution test through the recompiler and SPIR-V backend — a 16x4 RGBA8 linear image whose texel markers equal `(texelIndex*4+channel+1) & 0xFF`, 32 lanes covering interior anchors, both left-edge clamps, row clamping past the height (zero rows), and DMASKs 0x1/0x5/0xF selecting different stream words; all four output registers per lane are checked against the measured bitstream rule. The test passes on the Blackwell runner device, reproducing the gfx1031-measured semantics exactly. Not tested: no title or homebrew (no compiler in the toolchain emits these mnemonics; real PS5 shaders using them would come from GNM-proven games, not available here). Sub-32-bit-per-texel formats throw explicitly rather than being exercised. gather8h_pck behavior beyond desktop is unknown. ### Checklist - [x] Based on current `main`; no other open PR implements the same opcodes (#1314 and #1053 checked — different opcode families) - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`imageGather` D16 throw, gather8h_pck translation throw); silent stubs are listed in TechnicalDebt - [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 (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
adriadam10 commented 2026-10-08 17:18:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T17:18:27Z.


Thanks, the measurements are solid. I pushed one commit on top (fix(shader): fetch packed horizontal gather texels and cover edge coordinates) and rebased on main, so it can go in:

  • The emitter now reads each window texel with OpImageFetch at LOD 0 and packs it with the existing PackedImageTexel (the image_load_pck path), as the description says. The previous version sampled through the title's sampler with OpImageSampleExplicitLod, so a linear filter, LOD bias or min LOD in the S# could change the raw bits. It also duplicated PackedImageTexel's exactness check and unorm rebuild.
  • u * size is decorated NoContraction, so a driver can't fuse it with the - 0.5 into an FMA and move the anchor (#446).
  • The test now covers what the description listed. It runs DMASKs 0xF, 0x1, 0x5, 0xA and 0x8, and adds rows next to the anchor boundary ((k+0.5)/16 and the float just below), negative u, u > 1, negative v (clamps to row 0, which the old expected value got wrong), and v = 1.0 and 2.0 (zeros). Before, it only ran DMASK 0xF with rows in [0, height). Only the first popcount(dmask) registers are compared.
  • Removed the code comments (CONTRIBUTING). Moved the TechnicalDebt line next to the MIMG tfe/lwe entry instead of the end of the section, and updated it to say the sampler state isn't used.

Tested on the merge with main: RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8). agc_driver_image_packed_horizontal_gather and image_sample_gather pass, and the full ctest passes. Negative check: moving the window by one texel fails at dmask 15 thread 0 register 2.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065254544 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1701#issuecomment-6065254544). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T17:18:27Z. --- Thanks, the measurements are solid. I pushed one commit on top (`fix(shader): fetch packed horizontal gather texels and cover edge coordinates`) and rebased on `main`, so it can go in: - The emitter now reads each window texel with `OpImageFetch` at LOD 0 and packs it with the existing `PackedImageTexel` (the `image_load_pck` path), as the description says. The previous version sampled through the title's sampler with `OpImageSampleExplicitLod`, so a linear filter, LOD bias or min LOD in the S# could change the raw bits. It also duplicated `PackedImageTexel`'s exactness check and unorm rebuild. - `u * size` is decorated `NoContraction`, so a driver can't fuse it with the `- 0.5` into an FMA and move the anchor (#446). - The test now covers what the description listed. It runs DMASKs 0xF, 0x1, 0x5, 0xA and 0x8, and adds rows next to the anchor boundary (`(k+0.5)/16` and the float just below), negative `u`, `u > 1`, negative `v` (clamps to row 0, which the old expected value got wrong), and `v = 1.0` and `2.0` (zeros). Before, it only ran DMASK 0xF with rows in `[0, height)`. Only the first `popcount(dmask)` registers are compared. - Removed the code comments (CONTRIBUTING). Moved the TechnicalDebt line next to the MIMG `tfe`/`lwe` entry instead of the end of the section, and updated it to say the sampler state isn't used. Tested on the merge with `main`: RADV (gfx1035) and CI's lavapipe (Mesa 25.2.8). `agc_driver_image_packed_horizontal_gather` and `image_sample_gather` pass, and the full ctest passes. Negative check: moving the window by one texel fails at `dmask 15 thread 0 register 2`.
github-actions[bot] commented 2026-10-08 17:18:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T17:18:47Z.


Progress report (55e8959 → 028bea7)

📈 GPU shader instructions: 98.37% (+0.17%, +2 implemented instructions)

✅ 2 implemented
Encoding Name
MIMG IMAGE_GATHER4H_PCK
MIMG IMAGE_GATHER8H_PCK
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065260067 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1701#issuecomment-6065260067). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T17:18:47Z. --- <!-- progress-report --> ### Progress report (55e8959 → 028bea7) 📈 **GPU shader instructions**: 98.37% (+0.17%, +2 implemented instructions) <details> <summary>✅ 2 implemented</summary> | Encoding | Name | | - | - | | MIMG | `IMAGE_GATHER4H_PCK` | | MIMG | `IMAGE_GATHER8H_PCK` | </details>
adriadam10 commented 2026-10-08 17:56:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T17:56:29Z.


Rebased on main after #639. The new test needed raw bounds (OOB_SELECT 3) in its buffer descriptors, as #639 did for the existing execution tests. Retested on the merge: packed horizontal gather on RADV and CI's lavapipe, full ctest 483/483, and the negative check still fails.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065900703 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1701#issuecomment-6065900703). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T17:56:29Z. --- Rebased on `main` after #639. The new test needed raw bounds (`OOB_SELECT` 3) in its buffer descriptors, as #639 did for the existing execution tests. Retested on the merge: packed horizontal gather on RADV and CI's lavapipe, full ctest 483/483, and the negative check still fails.
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!1701
No description provided.