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

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

@klarnet2009
klarnet2009 force-pushed the feat/rdna2spirv-image-sample-cd branch from 5497f4c to 0d11b47 Compare November 23, 2026 07:05
@memorypressure

Copy link
Copy Markdown

Verified at the PR head against main 3faac55. All five tests in tests/semantic/test_image_sample_cd.py pass under python -I, compileall is clean, and the nearest existing suites on the sample dispatch (test_cube_sample 18/18, test_desc_cache, test_shader_meta) stay green. Main's rewritten sample-name grammar (kind.endswith(_cl) plus an allowed set — verified) has no _cd/_c_d/_c_cd form, so nothing here is superseded — but the PR and main now edit the same name-dispatch code with different shapes, so this needs a rebase onto main's current grammar before it can land. CI currently fails linux-gpu-tools and windows. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Stalkerino

Copy link
Copy Markdown

Found a rendering difference on head 0d11b473d. With nearest-mip sampling, the native texel offset gives the expected shadow. With --texel-offset-fallback, the right half of the same 64 × 64 draw compares against the neighboring texel. This reproduces for both image_sample_c_d_o and image_sample_c_cd_o.

Exact same RDNA2 shader and input Linux Vulkan Windows build under Wine 11.22 Windows build under Proton-CachyOS 11.0-20261015 SLR
Native SPIR-V Grad|Offset, with VK_KHR_maintenance8 4,096/4,096 correct 4,096/4,096 4,096/4,096
--texel-offset-fallback 2,048/4,096 correct 2,048/4,096 2,048/4,096

Counts are per instruction. Both the f32 result and the widened d16 result show the same split. All three runs used the same integrated Radeon, RADV RAPHAEL_MENDOCINO, Mesa 26.3.1, CachyOS kernel 7.3.5-2. The fallback was forced on this driver to compare both paths on identical hardware.

The explicit gradient selects mip 1 of a 128-wide image. A two-texel offset should therefore shift U by 2/64. At this head, texel_offset_fold has no gradient input and selects level 0 for this instruction, shifting by 2/128. At U = 30.5/64 the native path reads column 32, while the fallback reads column 31. This case uses an integral LOD, nearest filtering and zero sampler bias.

Synthetic reproducer

Use the two-mip vk_texture_test dref_c_cl fixture from #5325 at b74109969. Keep mip 0 at depth 0.25. In the mip-1 upload loop set columns 0–31 to 0.25 and 32–63 to 0.75. Its existing readback expects 0 on the left and 1 on the right in both channels. Keep its vs_ids vertex shader. Build the fragment shader below with #3300's runtime/test/shaders/build.py as ps_sample_c_cl.asm, once normally and once with LEONIDA_TEXOFF_FALLBACK=1. Use VKT_NEED_MAINTENANCE8=1 on the native-offset run. Substituting image_sample_c_cd_o for the two image_sample_c_d_o instructions reproduces the second case.

v_interp_p1_f32_e32 v2, v0, attr0.x
v_interp_p2_f32_e32 v2, v1, attr0.x
s_load_dwordx8 s[8:15], s[0:1], 0x0
s_waitcnt lgkmcnt(0)
s_mov_b32 s16, 0x3092
s_mov_b32 s17, 0xc00000
s_mov_b32 s18, 0x14500000
s_mov_b32 s19, 0
v_cmp_le_f32_e32 vcc_lo, 0.5, v2
v_mov_b32_e32 v4, 2
v_cndmask_b32_e64 v4, 0, v4, vcc_lo
v_mov_b32_e32 v5, 0.5
v_mov_b32_e32 v6, 0x3c800000
v_mov_b32_e32 v7, 0
v_mov_b32_e32 v8, 0
v_mov_b32_e32 v9, 0
v_mov_b32_e32 v10, 0x3ef40000
v_mov_b32_e32 v11, 0.25
image_sample_c_d_o v14, v[4:11], s[8:15], s[16:19] dmask:0x1 dim:SQ_RSRC_IMG_2D
image_sample_c_d_o v15, v[4:11], s[8:15], s[16:19] dmask:0x1 dim:SQ_RSRC_IMG_2D d16
s_waitcnt vmcnt(0)
v_cvt_f32_f16_e32 v15, v15
exp mrt0 v14, v15, v2, v4 done vm
s_endpgm

The no-offset controls passed on all three runtimes. Explicit shadow gradients and their min-LOD-clamped forms selected the expected mip at all 4,096 pixels with both ordinary and packed a16 coordinates. The color image_sample_cd a16 fixture also produced the expected blue/cyan split on this head. Parent 367875359 rejected that same fixture with image_sample_cd coords.

Hand-written shaders only. AI-assisted.

@thehollowedone

Copy link
Copy Markdown

The RADV shadow-offset difference also reproduces on native Windows at 0d11b473. Windows 11 x64, RTX 3080 Ti / 621.35, MSVC 19.44, LLVM MC 23.1.6, Vulkan SDK 1.4.363.0.

Using the same synthetic two-mip texture and hand-written shaders, both image_sample_c_d_o and image_sample_c_cd_o matched all 4,096 pixels with native offsets and VK_KHR_maintenance8. With LEONIDA_TEXOFF_FALLBACK=1, both matched only 2,048 pixels. The right half returned zero in both f32 and d16. At columns 31 and 32, native sampling returned 0 and 1, while the fallback returned 0 and 0. An offset-free control using explicit coordinates matched all 4,096 pixels.

All five fragment variants and the vertex shader passed spirv-val. Vulkan core and synchronization validation reported zero errors. Synthetic inputs only. 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.