Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
adriadam10:feat/mimg-coarse-g16
Oct 5, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
adriadam10:feat/mimg-coarse-g16

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

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: fix(shader): decode image_sample_cd as a derivative sample #395
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

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

@github-actions github-actions Bot added shader core/shader agc libSceAgc* labels Oct 3, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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
adriadam10 force-pushed the feat/mimg-coarse-g16 branch from 6acd319 to d73164a Compare October 4, 2026 06:37
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
MichonGoddijn231849 and others added 3 commits October 5, 2026 03:39
image_sample_cd takes its derivatives in the image_sample_d address
layout; only the hardware LOD selection differs (one LOD per quad instead
of per pixel). The decoder refused it. It now samples with the given
gradients per lane, which matches whenever the lanes of a quad pass equal
derivatives.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…dient layout

Co-Authored-By: Claude <noreply@anthropic.com>
ReadLane counts as a wave-uniform source, so the two-lane wave64 path
shared the first half's gradients with lanes 32 to 63. The test also
runs every case as wave64 with different quads in the upper half.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the feat/mimg-coarse-g16 branch from d73164a to 559aeac Compare October 5, 2026 01:40
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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 github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@github-actions

github-actions Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

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

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator

Gated on PPSA21564: no visual or FPS change.

@oneandonlydean
oneandonlydean merged commit bdd10f1 into boykopovar:main Oct 5, 2026
3 checks passed
@adriadam10
adriadam10 deleted the feat/mimg-coarse-g16 branch October 5, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants