Skip to content

Shader: preserve the last VGPR high half on packed D16 buffer loads - #1275

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:codex/d16-buffer-partial-write
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vonsek:codex/d16-buffer-partial-write

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as D16 buffer destination writes.

What and why

Packed buffer_load_format_d16_x writes the low half of its destination. For _xyz, x and y fill the first VGPR and z replaces the low half of the second VGPR. The previous translation also cleared the unused high half. This destroyed a live value even when the descriptor bounds made the loaded components zero.

The odd final component now uses the existing low-half register writer after integer truncation or floating-point narrowing. The fixed-format MTBUF opcodes are outside the scope of this change.

Tested

  • On native RDNA2 gfx1036 (AMD Radeon Graphics, Windows 32.0.21045.16010), 64 MUBUF kernels matched independent expected register bits. The cases cross wave32/64, two destination seeds, RGBA8_UINT/UNORM, x/xy/xyz/xyzw and valid/OOB addresses. Each kernel returned the two destination registers. This also corroborates the earlier gfx1030 x observation.
  • For example, from bytes 44 33 22 11, UINT _xyz gives 00330044 a9bc0022. Moving just the descriptor offset out of bounds gives 00000000 a9bc0000. The even-width _xyzw control gives 00330044 00110022 or two zero words.
  • For the translated path, 16 source-built compute shaders assembled with LLVM, translated and passed spirv-val before and after. On an RTX 5080 (616.92) and the same Radeon, 64 lanes × two words × 16 shaders produced exactly 512 wrong words per GPU on e7584c5. With this change, all 2,048 words matched on each GPU, including the adjacent untouched register in the x/xy cases. Khronos validation reported no VUID.
  • python -I tests/semantic/test_buffer_load_format.py passed all 22 tests. On the parent the new or strengthened assertions produced five failures. tests/test_d16_pack.py and both relevant tests/test_translate.py narrowing cases passed.

The full semantic suite ran 1,149 tests with one skip. The unmodified runner was built from this checkout and used the normal output descriptor for the GPU comparison.

Checklist

  • All fixtures and hardware inputs were source-built. No game data, keys or decryption.
  • The shader contract and native check are recorded in docs/design.md.

AI-assisted.

The RDNA2 buffer unit updates only the addressed components even on an out-of-bounds load. Clearing the last high half clobbers unrelated values held across an x or xyz load.
@kapitche

Copy link
Copy Markdown
Contributor

Tested 6e17ca8 on Linux (WSL2 Ubuntu), Python 3.12, CPU only: pass.

  • python -m unittest discover -s tests/semantic: 1149 OK. With main's tools/rdna2spirv.py, test_buffer_load_format.py gives 5 failures (odd component counts at offsets 0 and 256, and unorm8_x).
  • For an odd component count, the last component goes through write_lo16, which keeps the destination's high half and respects the active-lane rule. Even pairs are unchanged.
  • Merging Fetch explicit 2D mips with the image view's numeric type #1276 is clean, and both test sets pass.

@Rainmaker33

Copy link
Copy Markdown

Tested 6e17ca8 on NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass. This is the first NVIDIA run; the PR was measured on AMD.

The PR's diff applied cleanly to main e7584c5. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8.

  • tests/semantic: 1149 run, OK (1 skipped).
  • scripts/run_gpu_tests.py: 91/118 pass, 27 skipped (wave64), identical per test to main's translator on the same harness. That includes cs_imgload_d16, cs_w32_lds_u8_d16 and ps_texture_sample_d16.
  • valu_fuzz --family mem --seed 1 --programs 200: 200/200 that ran match native, 400/400 with --emulate.

No game files.

@Pixel-Katana

Copy link
Copy Markdown

Code review of 6e17ca8: looks correct, no changes requested.

What I checked in buffer_load_format (tools/rdna2spirv.py):

  • The odd final component now goes through write_lo16(dest, r), which keeps old & 0xFFFF0000 and writes through self.dst, so EXEC handling is the same as the even-pair path. Both inputs to the OpSelect are already 16-bit (il is masked, f16_bits returns the low 16 bits), so nothing leaks into the kept half.
  • Rounding is unchanged for the low half: pack_f16_pair_int narrows each half with f16_bits, which is what the new odd path calls directly. The only behaviour change is the high half, as intended.
  • tbuffer_load_format_d16_* is not in the dispatch list, so it raises Unsupported (checked: tbuffer_load_format_d16_x → Unsupported), not a silent wrong result. Since tbuffer_* forwards to buffer_load_format(op[1:], ...), the MTBUF d16 forms will pick up this fix once they are added.

Extra cases through the repo's own harness (test_buffer_load_format.load_format + spirv_mini_interp), beyond the PR's UINT _xyz and UNORM8 _x:

Load v2 v3
FMT_UNORM8X4 _d16_xyz, in bounds 0x32663444 0xa9bc3044 (high half kept; z = f16(0x22/255) = 0x3044)
FMT_UNORM8X4 _d16_xyz, offset 0x100 (OOB) 0x00000000 0xa9bc0000

Suites on 6e17ca8: tests/semantic 1149 OK, test_buffer_load_format.py 22 OK, test_d16_pack.py OK, test_translate.py 332 passed / 2 skipped. run_gpu_tests.py with a runtime built from this head on an RTX 5080 (621.24, Windows 11): 91/118 passed, 27 skipped (wave64); LEONIDA_W64_EMULATE=1: 118/118.

AI-assisted review (Claude Code).

@ItsNotRexed
ItsNotRexed merged commit 8ed4dbc into ItsNotRexed:main Nov 22, 2026
11 checks passed
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.

5 participants