Skip to content

Shader: preserve the trailing VGPR high half on odd-component d16 image loads - #1336

Closed
freemanovec wants to merge 1 commit into
ItsNotRexed:mainfrom
freemanovec:fix/d16-image-load-preserve-hi16
Closed

freemanovec wants to merge 1 commit into
ItsNotRexed:mainfrom
freemanovec:fix/d16-image-load-preserve-hi16

Conversation

@freemanovec

Copy link
Copy Markdown
Contributor

Claimed in #1218 (#1218 (comment)) from @Vonsek's finding (#1218 (comment)).

Why

On native RDNA2 (gfx1036), a 1-component D16 image_load / image_load_mip preserves the unused high 16 bits of the destination VGPR (a seeded 0x5a5a high half survived all four UINT/SINT cases). Gen.image_load_typed padded odd component counts (1 or 3 in dmask) with 0, zeroing the trailing VGPR's high half.

What changed

  • Updated Gen.image_load_typed (write()) in tools/rdna2spirv.py to write the trailing odd component through self.write_lo16(dest, lo) (matching Shader: preserve the last VGPR high half on packed D16 buffer loads #1275's buffer-load rule): self.f16_bits(self.tof(xs[i])) for the float branch and xs[i] & 0xFFFF for the integer branches. Even component counts continue to write the full packed VGPR.
  • Added unit and spirv-val tests in tests/test_image_load_typed.py verifying high-half preservation for dmask:0x1 and dmask:0x7 across all three typed branches, and full writes for dmask:0x3 and dmask:0xf.
  • Updated the Typed image loads row in docs/design.md.

Testing

  • AI-only implementation and verification. No game files.
  • python3 -m compileall -q tools tests
  • python3 -I tests/test_image_load_typed.py -v: 9/9 OK (including spirv-val v2026.4 on d16 dmask:0x1 and dmask:0x7)
  • GPU (scripts/run_gpu_tests.py --filter imgload on Intel(R) HD Graphics 530 SKL GT2, Mesa 26.2.7-arch1.1 ANV, Vulkan 1.4.354, Arch Linux, llvm-mc 23.1.5): cs_imgload and cs_imgload_d16 2/2 pass.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Ran independent RGBA32F readbacks on 6fc4588 with RTX 5080 and Radeon Graphics on Windows. R8G8B8A8 UINT, SINT and UNORM each used distinguishable per-texel values. The shader seeded v4=0x5a5aaaaa and v5=0xa5a5bbbb, then exported all four halfwords numerically. Dmask 0x8 (A into v4.lo) and 0xd (R/B/A into v4 and v5.lo) passed all 4096 pixels on both GPUs for each format. Replacing the preserved half in the CPU oracle by zero disagreed at all 4096 pixels in every odd-mask case. Dmask 0x3 also passed with v5 entirely unchanged.

At this head image_load_mip D16 still does not produce a fragment module: the PR is based on c0ac5fd before #1308, and GitHub reports a conflict. Please merge current main so the released explicit-mip path uses the same odd-mask handling. I have a two-mip fixture ready for the revised SHA. Related #1340 handles integer saturation independently.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

I also exercised explicit mip on a local integration of the executable changes from 6fc4588 onto f85582e (translator blob 9ea4cea5e0ab648c612e1f5272fbd1793726cd6a). The two code/test files applied without conflict; the conflict was in the design table. The two mip levels have distinct channel patterns. On RTX 5080 and Radeon gfx1036, image_load_mip d16 at mip 1 reproduced the expected packed VGPR contents at all 4096 pixels for each of UINT, SINT and UNORM with masks 0x8, 0xd and 0x3. Zeroing the trailing high half in the reference disagreed at all 4096 pixels in all 12 odd-mask cases. The plain image_load cases still matched at mip 0. This result is for the local combination; the PR head itself still predates D16 explicit mip.

@freemanovec
freemanovec force-pushed the fix/d16-image-load-preserve-hi16 branch from 6fc4588 to be4620e Compare November 22, 2026 10:47
@freemanovec

Copy link
Copy Markdown
Contributor Author

Rebased onto main (f85582e2) at be4620e9; docs/design.md conflict resolved, test_image_load_typed.py (11/11 OK) and Intel ANV cs_imgload/cs_imgload_d16 (2/2 pass) re-verified. Ready for your revised-SHA check. (AI-only.)

ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.md
@freemanovec

Copy link
Copy Markdown
Contributor Author

Already merged into main at cb4e78b0 (before the force-pushed rebase be4620e9); closing.

@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested be4620e on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200: pass. (It conflicts with current main (c2967a5), so this is the PR head.)

  • runtime built from the PR; run_gpu_tests.py: 137 translated, 137 pass spirv-val; 105/133 passed, 28 skipped (wave64, 32-wide GPU)
  • with LEONIDA_W64_EMULATE=1: 133/133 passed
  • tests/test_image_load_typed.py OK; tests/semantic: 1178 OK

Tools: llvm-mc 21.1.8 (WSL), MSVC 19.50, Vulkan SDK 1.4.363.0, Python 3.13.

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.

3 participants