Skip to content

feat(rdna2spirv): implement image_store_pck - #3359

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

klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-image-store-pck

Conversation

@klarnet2009

Copy link
Copy Markdown
Contributor

What and why

Implements image_store_pck (MIMG pre-packed storage image write) in tools/rdna2spirv.py:

  • Writes pre-packed raw texel dwords directly to the %stv1_<di> (%uint) storage image view without format conversion or fmt_int_flags branching.
  • Supports 1D, 2D, 2D_ARRAY, and 3D dimensions, dmask component packing, a16 packed 16-bit coordinates, glc (Coherent), and active-lane predication; rejects d16, insufficient coordinates, and unsupported dims with Unsupported.
  • Documents image_store_pck in docs/design.md (CPU/structural-checked, not yet hardware-measured on RDNA2) and adds structural + spirv_asm.assemble() tests in tests/semantic/test_image_store_pck.py (3 tests).

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_store_pck.py: passed (exit 0)
  • py -3.12 -I tests/semantic/test_image_store_pck.py -v: Ran 3 tests in 0.100s, OK
  • py -3.12 -I tests/test_isa_coverage.py -v: Ran 18 tests in 9.441s, OK (skipped=3 for LEONIDA_RDNA2_ISA_XML)
  • py -3.12 -I tests/test_ctest_registration.py -v: Ran 4 tests in 0.051s, OK
  • Deliberate-breakage check: reverted tools/rdna2spirv.py to HEAD and confirmed test_image_store_pck.py failed (5 Unsupported errors), then restored and re-ran 3/3 passing.

Checklist

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

@dcastell1

Copy link
Copy Markdown
Contributor

Code read (no test run: this machine is in a no-background-work window).

The doc row is the part I would keep honest: image_store_pck is a pre-packed store, so the SINT/UINT/SNORM formats must go to the numeric-type view with no float conversion, and only the float formats should reach the unpacked write path. The test list says "no fmt_int_flags branching", which is right for the packed form, but it also means a _pck store into a *_SINT view is going through %v4uint_st: worth one case that stores a value with the sign bit set into an 8_SINT view and checks the debug words, so the identity is pinned and not only the shape.

d16 and CUBE being Unsupported matches the unpacked path (d16 needs the trailing half rule), so nothing there is a surprise.

@Dom-in

Dom-in commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested c55c670 merged onto main f847748 (clean merge) on Windows 11, Python 3.13: pytest tests/semantic/test_image_store_pck.py: 3 passed, 8 subtests passed. Negative control: the same tests against main's copies of the changed sources give 5 failed, so the tests cover the change.

AI-assisted (Claude Code); runs on my machine.

@whattheflop

Copy link
Copy Markdown
Contributor

AI-assisted synthetic CPU review at c55c670dd3b7bb4a56ca03948a0a5734aa4a0bd3. Packed-only shaders fail SPIR-V validation because the format-mask composites reference undefined constants.

A minimal case uses v_mov_b32 v0, 0x80, v_mov_b32 v4, 1, v_mov_b32 v5, 2, then image_store_pck v0, v[4:5], s[8:15] dmask:0x1 dim:SQ_RSRC_IMG_2D unorm and s_endpgm. LLVM 21.1.8 (gfx1030) assembles it. Both SPIR-V assemblers produce a binary, then spirv-val rejects it on Vulkan 1.2 and 1.3 with an undefined-ID error. All 12 LLVM-assembled packed-only fixtures behaved the same way. The unpacked and mixed packed/unpacked controls passed. CPython 3.12.13, SPIRV-Tools v2026.4, CPU only.

The packed early return at tools/rdna2spirv.py:9812 skips registering the format-mask constants. uses_store_views still causes assemble() to emit the mask composites at line 11352, after the constants have already been emitted at 11314. For example, %fmt_uint_init references %c8140820 with no definition. A packed-only spirv-val regression case would catch this. The scratch composition with #3187 emits the dynamic storage capability and still hits the same undefined IDs.

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

AI-assisted independent CPU review at c55c670 confirms the packed-only validation failure reported by @whattheflop. All six positive fixtures fail spirv-val Vulkan 1.2 on undefined constants in OpConstantComposite. The packed early return skips format-mask registration while assemble still emits the tables (tools/rdna2spirv.py around 9804 and 11352). Ordinary-store and mixed-store controls validate.

The runtime integration also needs correction: the packed path always selects %stv1, but ordinary UNORM and SINT UAVs populate as_float/as_sint, leaving the unsigned slot as fallback_info (runtime/shims/d3d12_vulkan_device.cpp:926; runtime/src/vulkan_bindless.h:912). A valid mixed module can therefore address the fallback image.

Raw packed semantics need more than a uint component view. For a four-byte RGBA texel, writing the synthetic word 0x44332211 as component R and zero for G/B/A cannot preserve the packed bytes through an R8G8B8A8_UINT view. Please establish the appropriate raw view/unpacking contract and verify legal packed-width/dmask combinations against primary ISA or hardware evidence; the existing sparse-mask shape test does not establish those semantics.

All 26 focused CPU tests passed, and full semantic discovery ran 7,325 tests with one expected failure. These passes do not cover the validation and descriptor defects above. No GPU or game data used.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested c55c670: its tests pass (test_image_store_pck.py, 3/3), and I measured image_store_pck on an AMD Radeon RDNA2 iGPU (gfx1036, driver 26.10.1, raw hw_oracle kernels, a linear T# texture; each texel stored with image_store_pck, then read back with image_load_pck, whose bytes-as-dwords result I had measured before).

Measured on the hardware:

  • The store writes the whole texel as raw bytes, whatever the FORMAT's numeric type (UINT and UNORM the same). dmask bit k selects dword k of the texel, the selected dwords take the VGPRs in ascending bit order, and every dword of the texel that is not selected is written as 0, not kept. A dword past the texel's size is dropped.
    • 32_32_32_32_UINT, data 0x11223344 0x55667788: dmask 0x1 -> 11223344 0 0 0; 0x2 -> 0 11223344 0 0; 0xA -> 0 11223344 0 55667788; 0x5 -> 11223344 0 55667788 0; 0xE with three VGPRs -> 0 d0 d1 d2.
    • 16_16_16_16_UINT (8 bytes, two dwords): dmask 0x1 -> d0 0; 0x2 -> 0 d0; 0x3 -> d0 d1. One dword holds two 16-bit components, so dmask 0x1 sets R and G.
    • 8_8_8_8_UINT and _UNORM (4 bytes): dmask 0x1 -> dword 0 = 0x11223344 (all four bytes); 0x3 -> the second VGPR is dropped; 0x2 -> the texel becomes 0.
    • 8_UINT, 8_8_UINT, 16_UINT: only the texel's own 1 or 2 low bytes of the VGPR are written (0x44, 0x3344), and neighbouring texels are not touched (eight lanes storing 0x...40+lane each read back their own value).

Compared with #3359: the translation writes component c = VGPR data (dmask bit c) or 0 into a uvec4 and OpImageWrites it to the storage image's own format (%v4uint_st of d0,0,0,0 for dmask 1, 0,d0,0,0 for 2, 0,d0,0,d1 for 0xA). That matches the hardware for formats whose components are 32 bits (R32, R32G32, R32G32B32A32 UINT: the dword-per-component layout is the same), including the zero fill. It does not for formats narrower than 32 bits, which is the usual case for pck: on 8_8_8_8 / 16_16_16_16 the hardware writes the dword as 4 / 2 packed components (dmask 1 on 16_16_16_16 sets R and G), the translation writes only R truncated to 8 / 16 bits with the others 0; and 8_8_8_8 dmask 0x2 gives 0 on the hardware but the translation writes d0 into G. For those the dwords need to be split into components by the format's widths before the write (the reverse of #3152's packing; the format table there has the widths).

Not done: a GPU fixture of #3359 on both cards. vk_store_test has no storage image of a narrow integer format yet, so I only compared the emitted SPIR-V with the measured words. The measurement script is a local scratch one, not pushed; the words above are all I ran. Nothing else in the PR tested. AI-assisted.

@klarnet2009

Copy link
Copy Markdown
Contributor Author

Thank you @whattheflop, @Vonsek, and @dcastell1 for the bug report, gfx1036 hardware measurement, and format-width context!

Updated in 36db52e6:

  • Moved for w in [16] + sum(self.int_format_masks(), []): self.cu(w) in tools/rdna2spirv.py image_store() before the if pck: early return so packed-only shaders define every %c... constant referenced by %fmt_uint_init / %fmt_sint_init.
  • Updated image_store's docstring and docs/design.md to reflect the gfx1036 measurement (32_UINT, 32_32_UINT, 32_32_32_32_UINT match bit-for-bit including zero-fill of unselected dwords; sub-32-bit packed formats and non-UINT format unpacking remain a follow-up once #3152's format width table lands).
  • Added test_packed_only_shader_defines_all_constant_composite_operands in tests/semantic/test_image_store_pck.py (including the minimal dmask:0x1 dim:SQ_RSRC_IMG_2D unorm case), verifying every OpConstantComposite operand is defined earlier in the .spvasm and assembles with spirv_asm.assemble().

Verified (py -3.12 -I):

  • tests/semantic/test_image_store_pck.py -v: 4/4 OK (0.168s)
  • tests/test_isa_coverage.py -v: 18 tests OK (7.940s, 3 skipped for LEONIDA_RDNA2_ISA_XML unset)
  • tests/test_ctest_registration.py -v: 4/4 OK (0.526s)
  • compileall -q tools/rdna2spirv.py tests/semantic/test_image_store_pck.py: exit 0

@klarnet2009

Copy link
Copy Markdown
Contributor Author

@kacper-serewis Thanks for the review! Just a quick ping: commit 36db52e6 (pushed after c55c670d) already fixed the spirv-val undefined format-mask constant error by calling int_format_masks() before the if pck: early return, added a packed-only regression test, and updated docs/design.md with @Vonsek's gfx1036 hardware scope. Please take another look when you have a chance!

@klarnet2009
klarnet2009 force-pushed the feat/rdna2spirv-image-store-pck branch from 36db52e to a6e12e0 Compare November 22, 2026 22:10
@JRamos1993

Copy link
Copy Markdown
Contributor

Tested a6e12e0 merged onto main aa2988c on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed. main aa2988c gives 164/210, 46 skipped, 0 failed. All 214 test shaders translate and pass spirv-val.
  • tests/semantic/test_image_store_pck.py: ran 4 tests in 0.204s; OK.

AI-assisted (Claude Code).

@mrbean56

Copy link
Copy Markdown

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

  • python -m pytest over the 12 image test files (tests/semantic/test_image*.py and tests/test_image*.py): 190 passed, 3 skipped, 146 subtests passed in 5.47s.
  • Beyond the structural test's spirv_asm assembly, I ran spirv-val --target-env vulkan1.2 on 33 translated image_store_pck modules: 1D/2D/2D-array/3D x dmask 0x1/0x3/0xf x plain/a16/glc where legal. All 33 are Vulkan 1.2 valid, and the a16 unpacking keeps the coordinates 32-bit as the structural test expects.

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.

8 participants