Skip to content

rdna2spirv: support 1D, 3D, CUBE, and a16 in image_get_lod - #5338

Open
DenRakEiw wants to merge 1 commit into
ItsNotRexed:mainfrom
DenRakEiw:feat/rdna2-image-get-lod-dims
Open

DenRakEiw wants to merge 1 commit into
ItsNotRexed:mainfrom
DenRakEiw:feat/rdna2-image-get-lod-dims

Conversation

@DenRakEiw

Copy link
Copy Markdown
Contributor

Claim: #5256 (comment)

Summary

Extends Gen.image_get_lod in tools/rdna2spirv.py to support standard AMD RDNA2 1D, 3D, and CUBE dimensions alongside 2D and 2D_ARRAY, including a16 packed half-float coordinate unpacking for all five dimensions and explicit address VGPR count validation:

  • dim:SQ_RSRC_IMG_1D: binds %images_1d / %img1d / %simg1d (uses_tex_1d = True, OpCapability Sampled1D), emits OpImageQueryLod %v2float <si> <u> (%float), and unpacks u from the low f16 of addr[0] under a16.
  • dim:SQ_RSRC_IMG_3D: binds %images_3d / %img3d / %simg3d (uses_tex_3d = True), emits OpImageQueryLod %v2float <si> <uvw> (%v3float), and unpacks (u, v) from addr[0] + w from the low f16 of addr[1] under a16.
  • dim:SQ_RSRC_IMG_CUBE: binds %images_arr / %img2da / %simg2da (uses_tex_array = True), maps (s, t, face) through self.cube_coords(f) to (u, v, layer), and passes %v2float (u, v) to OpImageQueryLod (per SPIR-V Dim=2D, Arrayed=1 rules); a16 unpacks (s, t) from addr[0] and face from the low f16 of addr[1].
  • dim:SQ_RSRC_IMG_2D / dim:SQ_RSRC_IMG_2D_ARRAY: preserves a16 UnpackHalf2x16 lowering (ignoring the array slice for LOD) and validates len(addr) >= ((ncoord + 1) // 2 if a16 else ncoord), raising Unsupported("image_get_lod coords") instead of IndexError on short address ranges.
  • Guards: preserves non-PS stage, non-VGPR destination, and tfe / lwe / d16 rejections.

Verification

  • Semantic + spirv-val (vulkan1.2) + llvm-mc round-trip (python -m unittest discover -s tests/semantic -p test_image_get_lod_dims.py -v): 7/7 passed (every generated SPIR-V module validated with spirv-as + spirv-val --target-env vulkan1.2, plus llvm-mc -arch=amdgcn -mcpu=gfx1030 assemble/disassemble round-trip across all 10 dim x a16 forms).
  • Translation tests (python -I tests/test_image_query_translate.py -v): 10/10 passed.
  • Typed image load / LOD tests (python -I tests/test_image_load_typed.py -v): 23/23 passed.
  • Line endings check (python -I tests/test_text_write_line_endings.py): 3/3 passed.
  • RTX 5090 GPU tests (LEONIDA_W64_EMULATE=1; python -I scripts/run_gpu_tests.py --build H:\leonida\Leonida\runtime\build --filter get_lod): 2/2 passed (ps_get_lod, ps_get_lod_array; 3/3 shaders translated and spirv-val clean).
  • git diff --check: clean.

AI-assisted (Gemini 4 Neon).

@Flyander

Copy link
Copy Markdown
Contributor

Reproduced a documentation contract mismatch on head 6d8047d6:

  1. Run python -I -m unittest discover -s tests/semantic -p test_image_get_lod_dims.py -v; it passes all 7 cases, including image_get_lod for 1D, 3D, and CUBE (with and without a16).
  2. Read the Image atomics, size and LOD queries row in docs/design.md: it still describes image_get_lod as supporting only 2D and 2D-array images. The Half-precision coordinates for LOD queries row also limits a16 to those dimensions.

Those contract rows now understate the accepted forms and leave the new translator behavior undocumented. Please update the relevant row(s) to cover the added dimensions and state the verification level for them, as CONTRIBUTING.md requires for translator rules. python -I tests/test_image_query_translate.py -v also passes (10/10). GPU/driver not used; no game files used.

@CryingLaughing

Copy link
Copy Markdown
Contributor

Windows RTX 4080 coverage of the new image_get_lod paths at 6d8047d6. Windows 11 build 26200, NVIDIA 621.01, Khronos validation 1.4.357.

Each synthetic pixel shader exports two LOD queries as (clamped, unclamped, clamped, unclamped). Each draw matched 4,096/4,096 pixels within the existing 1/16 tolerance.

Texture and coordinates Draws Expected RGBA
8-wide 1D, full and packed a16 2 (0, -3, 0, -4)
8×8×16 3D, full coordinates 1 (0, -3, 0, -2)
8×8×16 3D, packed a16 1 (0, -4, 0, -3)
Cube, full and packed a16 (X and Y controls) 3 (0, 2, 0, -3)
Existing 2D-array control 1 (0, 2, 0, -3)

The cube and array draws used stock vk_texture_test. The 1D and 3D draws used local variants binding real sampled views at bindings 5 and 6. The 3D test varies X and Z independently. The 1D packed case deliberately puts different derivatives in the two halves to detect a wrong half selection. All modules passed spirv-val; the eight draws emitted no Vulkan validation warnings or errors. Synthetic inputs only. AI-assisted with Codex.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head 6d8047d6 — tools/rdna2spirv.py (+97/−29), new tests/semantic/test_image_get_lod_dims.py, tests/test_image_query_translate.py.

  • $env:LLVM_MC='wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc'; python tests/semantic/test_image_get_lod_dims.py → Ran 7 tests, OK. Without LLVM_MC the round-trip test skips (6 OK, 1 skipped), as it should.
  • python -I -m pytest tests/test_image_query_translate.py -q → 10 passed, 14 subtests; tests/test_image_load_typed.py → 23 passed, 53 subtests; tests/test_text_write_line_endings.py → 3 OK.
  • GPU: LEONIDA_VK_DEVICE='RTX 5070 Ti' python -I scripts/run_gpu_tests.py --build <build> --filter get_lod → 2/2 PASS (ps_get_lod, ps_get_lod_array; 3/3 shaders translated, spirv-val clean).
  • Code checked: cube binds the same 6-layer 2D-array view and cube_coords() as image_op; a16 reads (n+1)//2 address VGPRs and a short range raises Unsupported("image_get_lod coords") instead of IndexError; tfe/lwe/d16 rejected. The non-a16 2D_ARRAY form now requires 3 address VGPRs (test updated) — an intentional tightening to the ISA width.
  • Gap: the two GPU fixtures are 2D / 2D-array only, so 1D, 3D, CUBE and their a16 forms are covered by translation + spirv-val, not by a hardware run.
    Verdict: LGTM.

@Razuri0

Razuri0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 6d8047d6 on AMD Radeon RX 9070 (RDNA4, RADV GFX1201), Mesa 26.2.6, Vulkan 1.4.354, Arch Linux (kernel 7.2.5), LLVM 23.1.5 llvm-mc: no regressions

AI-assisted (Claude Code). Synthetic inputs only, no game files.

@sovljanskii

Copy link
Copy Markdown
Contributor

Apple M4 run of 6d8047d6 (on main 3faac557): pass, no regressions. Setup: macOS 27.2.1, MoltenVK 1.4.2, AppleClang 21.0.0, Homebrew LLVM 23.1.6, Python 3.15.2 and 3.12.16. The test programs are built from main, since this PR changes no C++.

  • CPU: test_image_get_lod_dims 7/7, plus test_image_query_translate and test_image_load_typed, all OK on both Pythons.
    • Mutations: taking the 3D a16 w or the 1D a16 u from the high f16 fails test_image_get_lod_dims.
    • The cube face half can't be observed: only (s - 1, t - 1) reaches OpImageQueryLod.
  • Semantic suite (6,252 tests): the same 32 failures and 2 errors as main on this machine (the 29 arm64 NaN rows from spirv_mini_interp: pin the sign of a NaN made from non-NaN operands (semantic suite on arm64) #5208 plus test_loop_ballot_barrier). Nothing new fails.
  • GPU suite: 152/217 passed, 58 skipped, identical to main per test. ps_get_lod and ps_get_lod_array pass (4,096 pixels). No in-repo GPU fixture reaches the new 1D/3D/CUBE/a16 paths; the RTX 4080 run above covers those.
  • Corpus (my own copy, counts only):
    • Shard 0/64: 415/415 translated and spirv-val-valid on both sides, and 415/415 modules byte-identical.
    • Across all 25,734 listings, 653 use image_get_lod: 715 instructions with 2D, 3 with 2D_ARRAY, none with 1D, 3D, CUBE or a16. So the PR changes no shipped shader; it completes the instruction for other inputs.

AI-assisted.

@lunarminus

Copy link
Copy Markdown

Tested 6d8047d6 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • Merge. d0835dd9 is an ancestor of the PR head, so the merge fast-forwarded and the tree tested is exactly 6d8047d6 (main 3faac557 + this PR). For that reason I also ran a clean 3faac557 worktree as the control. runtime/ is the same in both trees, so I built it fresh once (RelWithDebInfo) and used that build for the PR runs and the control runs.
  • PR tests. spirv-as/spirv-val and llvm-mc 23.1.5 were on PATH, so none of these skipped.
    • tests/semantic/test_image_get_lod_dims.py: 7/7 OK, 0 skipped, including test_llvm_mc_binary_round_trip.
    • tests/test_image_query_translate.py: 10/10.
    • tests/test_image_load_typed.py: 23/23.
    • tests/test_text_write_line_endings.py: 3/3.
  • Semantic suite.
    • -p 'test_image*': 49 OK, against 42 on 3faac557.
    • Full suite: 6,252 tests, with the same 3 failures + 2 errors (all in test_loop_ballot_barrier) as 3faac557 (6,245 tests). Nothing new comes from this PR.
  • GPU suite (run_gpu_tests.py, with LEONIDA_W64_EMULATE unset and =1):
    • 202/217 passed, 15 skipped in both modes, with 221/221 shaders translated and spirv-val clean.
    • Compared test by test with 3faac557 without the PR, run on the same build: identical in both modes (0 differences over 216 test names; cs_fmt_d16_xyz reports twice).
    • Against the older d0835dd9 baseline (196/210): no test changed result. The 7 new names all come from main since then (6 PASS, ps_m2_literal SKIP); this PR adds no GPU fixture.
  • --filter get_lod: ps_get_lod and ps_get_lod_array 2/2 in both modes, the same as on 3faac557. The suite has no 1D/3D/CUBE image_get_lod fixture, so the new paths were not run on this GPU. Only the semantic tests above cover them here; GPU coverage of them is the RTX 4080 draws posted above.

Synthetic inputs only, no game files. AI-assisted (Claude Code).

Extend Gen.image_get_lod to handle dim:SQ_RSRC_IMG_1D, dim:SQ_RSRC_IMG_3D, and dim:SQ_RSRC_IMG_CUBE alongside 2D and 2D_ARRAY, with a16 half-float coordinate unpacking for all five dimensions and explicit address-length validation.

- 1D: binds %images_1d / %img1d / %simg1d (OpCapability Sampled1D) and queries OpImageQueryLod with %float (or low f16 of addr[0] under a16).
- 3D: binds %images_3d / %img3d / %simg3d and queries OpImageQueryLod with %v3float ((u, v) from addr[0] and w from low f16 of addr[1] under a16).
- CUBE: binds %images_arr / %img2da / %simg2da, maps (s, t, face) via cube_coords(), and passes %v2float (u, v) to OpImageQueryLod per SPIR-V Dim=2D Arrayed=1 rules.
- 2D / 2D_ARRAY: keeps a16 half2x16 unpacking and validates address VGPR count for every dim/a16 mode (raising Unsupported instead of IndexError).
- Keeps non-PS stage, tfe/lwe/d16, and non-VGPR destination guards intact.

AI-assisted (Gemini 4 Neon).
@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested b7179dd on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073.

  • pytest tests/semantic/test_image_get_lod_dims.py -q: 7 passed, 40 subtests passed.
  • pytest tests/test_image_query_translate.py -q: 10 passed, 14 subtests passed.

No runtime C++ changes; only translator/tests touched. Full GPU suite not re-run here (focused test run).

AI-assisted (OpenCode).

@sovljanskii

Copy link
Copy Markdown
Contributor

Re-tested b7179dd merged onto main 561b65a on Apple M4, macOS 27.2.1, MoltenVK 1.4.2, Python 3.15.2 and 3.12.16, since my check of 6d8047d: pass for the translator; needs changes in docs/design.md (last bullet)

  • What changed since 6d8047d: Gen.image_get_lod differs between the two heads in exactly two lines. The T# and S# OpAccessChain now go through main's dynamic_sampled_access(...) (tools/rdna2spirv.py:2793 and :2795).
    • That helper sets uses_dynamic_sampled_index, which declares SampledImageArrayDynamicIndexing and the shaderSampledImageArrayDynamicIndexing feature.
    • The arguments match image_op's calls (:11836, :11888). All five dims go through that one call site.
  • Mutating the resolved hunk:
    • Putting back the old raw OpAccessChain for both is caught, but only by main's test_sampled_dynamic_index (test_fetch_and_query_paths, the 2D_ARRAY image_get_lod row: capability missing). test_image_get_lod_dims stays green with it.
    • Reverting just one of the two changes nothing in the output, because the capability is module-wide.
  • Other mutations, caught: taking the 3D a16 w or the 1D a16 u from the high half, cube without cube_coords, cube bound as plain 2D, need ignoring a16, no address-length check, d16 accepted, uses_tex_3d dropped. 8 of 8 caught.
  • Other mutations, survived:
    • 3D non-a16 coordinates reversed (w, v, u) or w replaced by v, and 1D non-a16 reading v1 instead of v0. test_3d_non_a16_and_a16 (tests/semantic/test_image_get_lod_dims.py:180) and test_1d_non_a16_and_a16 (:112) check only the types (OpBitcast %float, a three-operand %v3float), not which VGPR feeds which coordinate.
    • Fix: trace each coordinate operand back to its OpLoad of %vgpr, and assert index 0 for 1D and 0, 1, 2 for 3D. vgpr_stores already parses those access chains.
    • The cube face half still can't be observed, as before.
  • Focused tests, OK on both Pythons: test_image_get_lod_dims 7/7 (the llvm-mc 23.1.6 round trip ran, nothing skipped), test_image_query_translate 10/10, test_image_load_typed 23/23, test_sampled_dynamic_index 6/6, test_text_write_line_endings 3/3, semantic -p 'test_image*' 53. compileall and git diff --check are clean.
  • Semantic suite: 6,157 tests, OK (1 skipped, 1 expected failure). Main at 561b65a8 on this machine: 6,150 tests, OK, with the same skip and expected failure. The 7 extra tests are test_image_get_lod_dims, and nothing new fails.
  • Corpus shard 0/64: 415/415 translated and spirv-val-valid on both sides, nothing unsupported. 415/415 modules byte-identical to main, and identical index entries.
  • GPU: --filter get_lod passes, native and with LEONIDA_W64_EMULATE=1: ps_get_lod and ps_get_lod_array, 4,096 pixels per draw. Their SPIR-V is byte-identical to main's. The PR adds no GPU fixture, so 1D/3D/CUBE/a16 still have only the RTX 4080 draws from the earlier comments.
  • Still open from 6d8047d: docs/design.md:399 ("Half-precision coordinates for LOD queries": "on 2D and 2D-array images") and :401 ("Image atomics, size and LOD queries": "image_get_lod (pixel shaders; 2D and 2D array, the slice ignored)") still describe only the old forms, as Flyander noted.
    • AGENTS.md wants new rules in the contract table with how they were confirmed. That means listing 1D, 3D and CUBE (cube via cube_coords, face/layer ignored), a16 for all five, and the image_get_lod coords rejection. Cite tests/semantic/test_image_get_lod_dims.py plus spirv-val, and the RTX 4080 draws as the GPU check.

AI-assisted.

@CryingLaughing

Copy link
Copy Markdown
Contributor

RTX 4080 retest of the rebased b7179dde on Windows 11 build 26200, NVIDIA 621.01, MSVC 19.44 Release and Khronos validation 1.4.357. I rebuilt the texture harnesses at this revision and repeated the eight synthetic draws above. Both 1D and 3D coordinate widths, all three cube cases, and the 2D-array control passed: 32,768/32,768 pixels within 1/16. All eight fragment modules passed spirv-val and declare SampledImageArrayDynamicIndexing. Validation was active on all eight draws with zero Vulkan validation errors. The 1D and 3D cases use local harness variants with actual 1D and 3D sampled views. Synthetic inputs only. AI-assisted with Codex.

@kapitche

Copy link
Copy Markdown
Contributor

Tested b7179dd merged with main 561b65a on Ubuntu (WSL2) / Python 3.12.16: pass

tests/semantic/test_image_get_lod_dims.py 7 run, 7 passed (1 skipped; same skip before the change); tests/test_image_query_translate.py 10/10. Full semantic suite with the #4357 runner (--jobs 3): 434 files, 6,157 tests, 0 files not ok (730 s); the main baseline in my notes is 433 files, 6,150 tests, 0 not ok, so the only difference is the new file with its 7 tests. Negative control: with tools/rdna2spirv.py from main, test_image_get_lod_dims fails (7 run, 6 failures and 17 errors, 1 skipped) and test_image_query_translate fails 1 of 10 (Unsupported not raised). Not run here: GPU draws (others on this PR report them). Still open from earlier comments, confirmed on this head: docs/design.md (lines 399 and 401) still describes LOD queries as 2D and 2D-array only; the PR does not touch docs.

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.

9 participants