Skip to content

rdna2spirv: support 2D_ARRAY and CUBE for image_gather4_l[_o] (+ a16/d16) - #7097

Open
DenRakEiw wants to merge 1 commit into
ItsNotRexed:mainfrom
DenRakEiw:feat/rdna2-gather4-l-dims
Open

DenRakEiw wants to merge 1 commit into
ItsNotRexed:mainfrom
DenRakEiw:feat/rdna2-gather4-l-dims

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Claim: #6910 (comment)

Summary

Adds SQ_RSRC_IMG_2D_ARRAY and SQ_RSRC_IMG_CUBE support (including a16 and d16) for image_gather4_l and image_gather4_l_o:

  • tools/rdna2spirv.py:
    • Relax the image_gather4_l guard to accept 2D, 2D_ARRAY, and CUBE (ncoord in (2, 3) and not is3d, requiring len(f) >= ncoord + 1), keeping 1D and 3D refused as Unsupported.
    • Remap CUBE (s, t, face) via cube_coords(f) and read LOD from f[ncoord].
    • For ncoord == 3, query OpImageQuerySizeLod %v3int, round and clamp f[2] to [0, sz.z - 1] via RoundEven / SMax / SMin, emit %v3int texel coordinates for OpImageFetch, and pass dim:SQ_RSRC_IMG_2D_ARRAY as view_dim to image_read_typed for CUBE.
    • Keep existing LOD-rounding lines untouched.
  • tools/rdna2_decode.py:
    • Fix _mimg_addr_count under a16 to pack coordinates and trailing lod/clamp/mip continuously (fixed32 + (packable + 1) // 2), verified against llvm-mc -mcpu=gfx1030 (e.g., image_gather4_l on 2D_ARRAY/CUBE with a16 uses 2 address VGPRs; image_gather4_l_o uses 3).
  • tests/semantic/test_image_gather4_l_dims.py:
    • Matrix test covering image_gather4_l / image_gather4_l_o x {2D_ARRAY, CUBE} x {normal, a16, d16, a16+d16} x {dmask:0x1, 0x2, 0x4, 0x8} x {ps, cs} with spirv-val --target-env vulkan1.2.
    • llvm-mc -mcpu=gfx1030 binary decode -> rdna2_decode -> rdna2spirv -> spirv-val round-trip tests.
    • Negative tests confirming 1D, 3D, invalid dmask, and under-sized address ranges still raise Unsupported.

Verification (Windows 11 + NVIDIA GeForce RTX 5090)

  • python -m unittest discover -s tests/semantic -p test_image_gather4_l_dims.py -v (with llvm-mc 21 -mcpu=gfx1030 & spirv-val Vulkan 1.2): 3/3 passed (128 matrix cases + 16 binary round-trips + 8 negative cases).
  • python -m compileall -q tools tests: OK.
  • Related semantic tests (test_sampled_dynamic_index.py, test_image_coherent.py, test_format_decode_branch.py, test_norm_decode.py): all passed.
  • scripts/run_gpu_tests.py --filter gather --require-pass with LEONIDA_VK_VALIDATION=1 and LEONIDA_W64_EMULATE=1 on RTX 5090: 3/3 passed (ps_gather_border, ps_gather_sampler_lod, ps_gather_wrap, 4096/4096 pixels exact).
  • git diff --check: clean.

AI-assisted (Gemini 4 Neon).

@kapitche

Copy link
Copy Markdown
Contributor

Tested fb32681 merged with main 8575d36 on Windows/Python 3.13.1 (llvm-mc 23.1.6, spirv-as/spirv-val from the leonida-tools folder) and on Linux/Python 3.12.16: pass

Merge was clean (tools/rdna2_decode.py, tools/rdna2spirv.py, tests/semantic/test_image_gather4_l_dims.py).

Windows/3.13.1 (all three tests need llvm-mc and spirv-val, so this is the run that counts):

  • python -I -m unittest discover -s tests/semantic -p test_image_gather4_l_dims.py -v: Ran 3 tests, OK, including test_decode_and_translate_round_trip. (On Windows LLVM_MC has to be given with forward slashes, because the test splits it with shlex; with a backslash path the round-trip test silently skips.)
  • Negative control: git checkout upstream/main -- tools/rdna2spirv.py tools/rdna2_decode.py, same command: FAILED (failures=8, errors=136). Restored afterwards.
  • python -I tests\test_rdna2_decode.py: 1 failure, test_parity_with_llvm_mc_on_shader_fixtures (op_sel:[0,1,0,0] against llvm-mc's op_sel:[0,1]). It fails the same way with tools/rdna2_decode.py taken from main, so it is not from this PR.

Linux/3.12.16 (no llvm-mc, spirv-as or spirv-val here, so the round trip skips and the matrix test only translates, without validating the SPIR-V):

  • .venv/bin/python -I -m unittest discover -s tests/semantic -p test_image_gather4_l_dims.py: Ran 3, OK (skipped=1).
  • tests/test_rdna2_decode.py OK (skipped=1); tests/test_image_load_typed.py, tests/test_image_query_translate.py, tests/test_ps_oracle.py, tests/test_sampler_source_census.py, tests/test_translate.py/test_cube_sample.py/test_texel_offsets.py (pytest): no failures. tests/semantic/test_image*.py gives 48 failures on this Linux box with and without the PR (Ran 68 with it, 65 on main; same 48), and test_shader_checks.py 15 errors with and without it.

Not run: any GPU, so the cube-as-2D-array view, the layer clamp (RoundEven then clamp to size-1) and the new a16 address count are checked by translation and validation only, not against hardware.

@wasonidas

Copy link
Copy Markdown

AI-assisted independent GPU verification of fb32681d on Windows. Eight synthetic fragment fixtures matched all four expected gather taps at 32,768/32,768 pixels per GPU on RTX 5080 (621.24) and Radeon Graphics (32.0.21042.62). The checks covered 2D-array layer selection and interior cube faces across two slices, fractional mip inputs, signed offsets, both gather forms, and a16/d16 separately and together. All four unpacked d16 halves matched exactly. The focused suite on the clean integration with e0816d6a passed 128 translation/validation cases, 16 binary round trips and eight refusal cases. A separate check against LLVM 23.1.5 matched 288/288 decoded address lists on gfx1030 and gfx1036.

Please record the new array/cube lowering and verification scope in the translator contract table in docs/design.md. These GPU readbacks cover translated UNORM fragment behavior. Native RDNA2 cube seams, fractional layer selection, and compute or typed-integer readback remain unverified. No game data. Vulkan validation layers were unavailable.

@moffington

Copy link
Copy Markdown

I reproduced a seamless cube discrepancy and an ignored instruction bit on exact head fb32681 using an RTX 3060 with Khronos validation. Plain seamless cube _l clamped taps to the selected face on two independent edges. At +Z left, native Vulkan gather returned [29,49,30,10]/255, while the translated shader returned [49,49,30,30]/255. A scratch guard rejected the seamless cases and preserved the clamped controls.

For 2D-array _l, flipping instruction UNRM alone produced identical SPIR-V at both tested sizes. Bypassing the coordinate multiply made both draws agree with equivalent normalized coordinates and explicit texel reads. The parent rejected both forms. Native RDNA2 behavior of the exact array UNRM combination remains unverified.

AI-assisted.
leonida-7097-repro.zip

@SquishyGithub

Copy link
Copy Markdown

Tested fb32681dc merged into main 8575d36ed on NVIDIA GeForce RTX 3070 (Ampere), driver 620.70.06, Vulkan 1.4.357, subgroup 32..32, Linux (Bazzite 45; Fedora 45 distrobox, GCC 16.2.1, LLVM 23.1.5, SPIRV-Tools 2026.2, Python 3.15.2): pass

No GPU fixture in the suite runs image_gather4_l[_o] on a 2D_ARRAY or CUBE view, so on hardware this checks for regressions only.

@duudiis

duudiis commented Nov 23, 2026

Copy link
Copy Markdown

Tested fb32681 merged onto main 37f2151 on Windows 11 Pro, Intel i7-14700KF, CPython 3.11.0 (venv: pytest 9.2.0, numpy 2.4.6, Pillow 13.0.0; numpy is below the 2.5.5 pin because 2.5 needs Python 3.12), CPU only: pass, with notes.

  • The merge was clean (git merge --no-ff, auto-merged tools/rdna2_decode.py and tools/rdna2spirv.py).
  • python -m pytest -q tests/semantic/test_image_gather4_l_dims.py -rs: 2 passed, 1 skipped, 136 subtests passed. The skip is TestImageGather4LBinaryDecodeRoundTrip, which needs llvm-mc with the AMDGPU target (not installed here).
  • In an earlier run on main 511e382, putting back main's tools/rdna2spirv.py and tools/rdna2_decode.py with the new test kept gave 128 failed, 2 passed. So the test does exercise the change.
  • python -m pytest -q tests/test_rdna2_decode.py tests/test_cube_sample.py tests/test_image_load_typed.py tests/test_texel_offsets.py tests/semantic/test_image_coherent.py tests/semantic/test_sampled_dynamic_index.py tests/semantic/test_mem_crash_forms.py tests/semantic/test_image_sample_bias_nonps.py: 120 passed, 20 skipped, 673 subtests passed. The earlier run had the same pass/skip counts on main and on the merge.

Review notes:

  • tests/semantic/test_image_gather4_l_dims.py:62-64: _validate_spirv returns without doing anything when spirv-as or spirv-val is missing. On a machine without them, the matrix test passes but validates nothing. A skip, or a recorded note, would make that visible.
  • tools/rdna2_decode.py:386-388: the new a16 formula fixed32 + (packable + 1) // 2 affects every MIMG op whose coordinate count is odd and has a trailing lod, clamp or mip, not just gather4_l. For example, image_sample_l or image_load_mip with a16 on 1D, 3D, 2D_ARRAY or CUBE: 2D_ARRAY + lod goes from 3 VGPRs to 2, and 1D + lod goes from 2 to 1. If that matches llvm-mc, good. But the only test of it is the llvm-mc round-trip, which skips without llvm-mc, and no CPU-only test covers the other ops.
  • tools/rdna2spirv.py:12574-12576 (CUBE path of image_gather4_l): after cube_coords, the 2x2 footprint is resolved on one face layer through the sampler's CLAMP_X/CLAMP_Y. Taps at a face edge do not cross to the neighbouring face. The GPU check posted earlier in this thread covered only interior texels. Could you confirm what the hardware does at face edges? I could not check it on CPU.

AI-assisted (Claude Code).

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.

6 participants