Skip to content

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

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
pandather:feat/shader-decode-gather-h-pck
Oct 8, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
pandather:feat/shader-decode-gather-h-pck

Conversation

@pandather

Copy link
Copy Markdown
Contributor

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

@adriadam10

Copy link
Copy Markdown
Collaborator

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 (feat(shader): implement basic f64 VALU ops #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

github-actions Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

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

@adriadam10
adriadam10 force-pushed the feat/shader-decode-gather-h-pck branch 2 times, most recently from eb4cb7d to 7a5ffa3 Compare October 8, 2026 17:55
@adriadam10

Copy link
Copy Markdown
Collaborator

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.

pandather and others added 3 commits October 8, 2026 20:42
Decodes IMAGE_GATHER4H_PCK (MIMG opcode 98) and IMAGE_GATHER8H_PCK
(opcode 99), implements gather4h_pck through translation, the SPIR-V
backend and a GPU execution test, and documents gather8h_pck as a
desktop null-op.

Semantics were measured on gfx1031 hardware with hand-encoded MIMG
words (LLVM rejects the PCK gather mnemonics on every RDNA1/RDNA2
target). The measured rule for gather4h_pck: the four-texel horizontal
window is floor(u*width-0.5)+{-1,0,+1,+2} clamped to the width, the row
is floor(v*height) clamped, the raw bits of the window texels are
concatenated into a little-endian bitstream (texel k at bit offset
k*texelBits), and output register j - the j-th set DMASK bit in
ascending order - receives stream word j, zero past the end of the
stream. Exactly popcount(dmask) consecutive VGPRs are written from vdst
ascending; texels wider than 32 bits write zeros (hardware limit); rows
past the height read zeros; negative coordinates clamp to zero. The
window anchor matches the repo's existing PS5-validated HorizontalOffsets
rule, so this reuses the established convention.

The decoder accepts multi-bit DMASKs for these two opcodes only and
sizes their data span as popcount(dmask) (measured), unlike gather4h
which always writes four components. Translation carries the packed
flag through imageMemoryInfoFromInstruction; the SPIR-V emitter
reassembles the bitstream per lane with per-texel OpImageFetch and
byte assembly. D16 data throws until measured.

gather8h_pck never returns texel data on this hardware: across dmasks
0x1-0xFF including the upper nibble, RGBA8/RGBA16/RGBA32 formats, 1D/2D
and 3D descriptors, normalized and unnormalized sampling, sentinel-
prefilled destination registers confirm it only ever zero-fills
popcount(dmask) registers. It decodes but throws at translation.

The measured semantics and the gather8h_pck null-op are recorded in
docs/dev/TechnicalDebt.md.
…rdinates

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the feat/shader-decode-gather-h-pck branch from 7a5ffa3 to 028bea7 Compare October 8, 2026 18:43
@adriadam10
adriadam10 merged commit b858fdc into boykopovar:main Oct 8, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants