Skip to content

feat(rdna2spirv): implement image_sample_cd, image_sample_c_d, and image_sample_c_cd variants - #3300

Open
klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-image-sample-cd
Open

klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-image-sample-cd

Conversation

@klarnet2009

Copy link
Copy Markdown
Contributor

What and why

Implements the 12 derivative and depth-compare image_sample* variants in tools/rdna2spirv.py:

  • image_sample_cd, image_sample_cd_o, image_sample_cd_cl, image_sample_cd_cl_o -> OpImageSampleExplicitLod %v4float
  • image_sample_c_d, image_sample_c_d_o, image_sample_c_d_cl, image_sample_c_d_cl_o, image_sample_c_cd, image_sample_c_cd_o, image_sample_c_cd_cl, image_sample_c_cd_cl_o -> OpImageSampleDrefExplicitLod %float
  • Handles Grad, Grad|Offset, Grad|MinLod, and Grad|Offset|MinLod in exact SPIR-V ImageOperands order, a16 (keeping offset, z-compare, and gradients 32-bit while unpacking f16 coords/minlod), and d16 packing.
  • Documents all 12 ops in docs/design.md (CPU/structural verification only; unmeasured on GPU) and adds tests in tests/semantic/test_image_sample_cd.py (5 tests, assembling every emitted SPIR-V module via spirv_asm).

Claim: #3232 (comment)

Tested

  • OS: Windows (Python 3.12.0), GPU: n/a (CPU translator + spirv_asm)
  • py -3.12 -I -m compileall -q tools/rdna2spirv.py tests/semantic/test_image_sample_cd.py: passed (exit 0)
  • py -3.12 -I tests/semantic/test_image_sample_cd.py -v: Ran 5 tests in 0.232s, OK
  • py -3.12 -I tests/test_isa_coverage.py -v: Ran 13 tests in 3.652s, OK (skipped=3 for LEONIDA_RDNA2_ISA_XML)
  • py -3.12 -I tests/test_ctest_registration.py -v: Ran 4 tests in 0.048s, OK
  • Deliberate-breakage check: reverted tools/rdna2spirv.py to HEAD and confirmed test_image_sample_cd.py failed (15 Unsupported errors), then restored and re-ran 5/5 passing.

Checklist

  • No game data / keys / decryption
  • Standard library only (python -I compatible)
  • AI-assisted: yes

@klarnet2009
klarnet2009 force-pushed the feat/rdna2spirv-image-sample-cd branch from 8569d1b to 382f0ee Compare November 22, 2026 20:25
@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

At 382f0ee7, the new image_sample_cd with a16 is rejected for a valid instruction. tools/rdna2spirv.py:9251 counts an extra full-width z-compare word whenever extras.startswith("_c"). That matches _cd and _cd_cl, but in those the c means coarse derivatives, not a compare. For a 2D _cd ... a16, all five address VGPRs are then kept whole and the packed (u,v) word is never split, so the operand count comes up short and translation raises Unsupported.

LLVM accepts the instruction (gfx1030):

image_sample_cd v[4:7], v[10:14], s[8:15], s[16:19] dmask:0xf dim:SQ_RSRC_IMG_2D a16 ; encoding: [0x08,0x0f,0xa0,0xf1,0x0a,0x04,0x82,0x40]

Translating it with the semantic-test helpers, on the head and on its merge with main:

image_sample_d translated True
image_sample_cd Unsupported: image_sample_cd coords

The equivalent image_sample_d ... a16, with the same five-register range, translates fine. Counting the compare word only for _c_* (e.g. extras.startswith("_c_") or extras in ("_c", "_c_b", "_c_l", ...), or simply not extras.startswith("_cd")) fixes the _cd cases. The PR's tests (5 passed) only cover non-a16 and _c_cd, so they don't catch it. Main doesn't implement _cd at all, so this is a gap in the new op, not a regression.

AI-assisted (Devin); Linux, Python 3.12, llvm-mc 15, CPU translation only, no GPU or game files.

@klarnet2009

Copy link
Copy Markdown
Contributor Author

Thank you @ray3242 for catching that! Fixed in 5497f4ce:

  • Updated a16 unpacking in tools/rdna2spirv.py to check extras == "_c" or extras.startswith("_c_") instead of extras.startswith("_c"), so _cd and _cd_cl (coarse derivatives, no z-compare) do not consume an extra full-width z-compare dword.
  • Added regression tests in tests/semantic/test_image_sample_cd.py for image_sample_cd ... a16 (5 address VGPRs) and image_sample_cd_cl ... a16 (6 address VGPRs), assembling both with spirv_asm.assemble().

Verified with py -3.12 -I:

  • tests/semantic/test_image_sample_cd.py -v: 5/5 OK
  • tests/test_isa_coverage.py -v: 18 tests OK (3 skipped for LEONIDA_RDNA2_ISA_XML unset)
  • tests/test_ctest_registration.py -v: 4/4 OK
  • compileall -q tools/rdna2spirv.py tests/semantic/test_image_sample_cd.py: exit 0

@ttd0x

ttd0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 382f0ee7 on NVIDIA GeForce RTX 2080 Ti (Turing, subgroup 32..32, driver 610.88), Windows 11: pass, no regression.

  • tests/semantic/test_image_sample_cd.py: 5 tests OK.
  • scripts/run_gpu_tests.py (default, no emulation), 204 shaders translated, all pass spirv-val: 152/199 passed, 46 skipped (native wave64, expected on a 32-wide device), 1 failed. Main f847748c gives the same result lines; the one failure (ps_sdwa_f16_const) also fails on main.
  • Limit: no GPU fixture uses the new image_sample_cd / _c_d / _c_cd forms, so the GPU run shows only that nothing else broke, not that the new forms sample correctly.

Synthetic fixtures only, no game data. AI-assisted (Claude Code).

@sperm

sperm commented Nov 22, 2026

Copy link
Copy Markdown

Tested 5497f4c on AMD Radeon RX 9070 XT (RADV GFX1201) and the Ryzen 7 9800X3D iGPU (RADV RAPHAEL_MENDOCINO, gfx1036), Mesa 26.2.6, Linux 7.3.4: pass, no regressions.

  • Full python -I scripts/run_gpu_tests.py (Linux build of this branch's runtime/, LEONIDA_VK_DEVICE per GPU): 185/199 passed, 14 skipped, 0 failed on both GPUs. Compared with the same suite at the merge base f847748 on the same machine, every test has the same result.
  • python -m unittest discover -s tests/semantic: 7327 run, OK (expected failures=1)

No game files. AI-assisted.

@ray3242

ray3242 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Rechecked at 5497f4ce: tools/rdna2spirv.py:9251 now counts the z-compare word only for _c / _c_*, and python -I tests/semantic/test_image_sample_cd.py 5/5 OK on Python 3.12.16, including the a16 cases. My point is resolved. CPU only. AI-assisted (Devin).

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (head merged with main f847748, MSVC 14.51, Python 3.13.1):

  • tests/semantic/test_image_sample_cd.py passes.
  • Negative control: with tools/rdna2spirv.py reverted to main, the tests error out (1D/2D-array, 3D gradients and short coordinates).
  • GPU: the PR adds no GPU fixture (its description says the ops are unmeasured on GPU), so I ran the whole run_gpu_tests.py suite with the PR's translator and compared it with main, program by program. RTX 4080 SUPER: 153/199 passed, 46 skipped, identical to main. Intel UHD 770 (VK_LOADER_DRIVERS_DISABLE=*nv*): 136/199 passed, 63 skipped, identical to main.

So nothing regresses, but image_sample_cd, image_sample_c_d and their _cl/_o forms still have no execution check on either GPU. The vk_* programs came from a runtime built from main.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested 5497f4c 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: pass

  • python3 -m pytest tests/semantic/test_image_sample_cd.py -q -> 5 passed, 18 subtests passed
  • python3 -I scripts/run_gpu_tests.py --build <build> -> 184/199 passed, 14 skipped, 0 failed (expected D3D12-owned skips)

No runtime C++ changes; GPU suite run from the PR worktree against the existing main runtime build.

AI-assisted (OpenCode).

@bielfreedom

Copy link
Copy Markdown

Tested 5497f4ce (on its base f847748c) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass, no regression.

  • python -m unittest discover -s tests/semantic (includes this PR's test_image_sample_cd.py): 7327 tests OK (1 expected failure).
  • scripts/run_gpu_tests.py: 204 shaders, 204 translated, 204 pass spirv-val; 185/199 passed, 14 skipped, 0 failures. The skips are the shaders run by the d3d12 tests and ps_w64x_readlane.
  • Full ctest: 129/129, 0 validation errors.

The new image_sample_cd / _c_cd forms are checked on the CPU only. No GPU fixture uses them, so this run shows nothing else regressed.

AI-assisted (Claude Code). No game files.

@jeremygold02 jeremygold02 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.

Direct GPU execution at 5497f4ce1c082b163f794ce79569987838103d98: image_sample_cd, image_sample_c_d, and image_sample_c_cd selected the expected mip on an RTX 5070 with both A32 and packed A16 coordinates. This includes the corrected A16 color form from the last commit. The corresponding A16 and A32 RGBA32F frames were byte-identical for both color and depth.

Five synthetic 16×16 draws used an 8×8 base mip, a 4×4 second mip, nearest filtering, and sampler LOD range [0,1]. UV was constant at (0.6875, 0.5625), so the mip change had to come from the supplied gradients. I held dUV/dx at (1/16,0) and changed dUV/dy from (0,1/8) to (0,1/4), selecting mip 0 and mip 1 respectively.

Input and instructions Expected and observed result Pixels
RGBA8 UNORM, image_sample_cd, A32 and A16 Mip 0 texel (5,4), mip 1 texel (2,2). R/G encoded both coordinates and mip, yielding byte codes (5,4,18,34). 256/256 each
D32, image_sample_c_d then image_sample_c_cd, A32 and A16 Mip depths 0.25 and 0.75, reference 0.5, LESS_OR_EQUAL. Both instructions returned (0,1) across the gradient change, giving RGBA (0,1,0,1). 256/256 each
Same A16 depth shader with the two mip values exchanged RGBA reversed to (1,0,1,0). 256/256

LLVM assembled each of the three opcodes in both address widths for gfx1013 and gfx1030. The draw shaders used the gfx1013 listings. All five original translated modules passed Vulkan 1.2 spirv-val. For execution I added the sampled-array dynamic-indexing capability from #3045 to the four pixel modules and enabled the matching device feature. Removing that one declaration reproduces each head-generated SPIR-V text exactly. The scratch vk_texture_test kept DrawParameters in the device feature chain as in #3347 and supplied the synthetic color/depth mips. The checks cover the three 2D gradient forms with f32 results. The offset, MinLod and d16 variants remain outside this run.

GPU execution used NVIDIA GeForce RTX 5070, driver 621.01 (32.0.16.2101), with Intel Core Ultra 7 265K on Windows 11 Home 25H2 build 26200.9974.

AI-assisted by GPT-6.2 Astra.

@mrbean56

Copy link
Copy Markdown

Tested 5497f4ce1c on Linux (Debian 13), Python 3.13.5: pass.

  • python -I tests/semantic/test_image_sample_cd.py: Ran 5 tests in 0.082s, OK.
  • I translated 17 modules through the real pipeline (the 12-op 2D matrix, 1D cd and c_d_cl_o, 2D-array c_cd_cl_o, 2D c_cd_cl_o with a16, cd_cl_o with d16) and ran spirv-val --target-env vulkan1.2: the 7 without an _o suffix validate. The 10 _o modules hit VUID-RuntimeSpirv-Offset-10213 (dynamic Offset on a non-gather sample), exactly as main's existing image_sample_o does; that is the VK_KHR_maintenance8/--texel-offset-fallback path, not something this PR adds.

No GPU, no game data. AI-assisted.

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