Skip to content

Shader: saturate unsigned D16 formatted buffer loads - #1442

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
evilclanker9000:codex/mubuf-d16-uint-clamp
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
evilclanker9000:codex/mubuf-d16-uint-clamp

Conversation

@evilclanker9000

@evilclanker9000 evilclanker9000 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed as SHADER-237, now coordinated in #1380. Builds on the integer conversion merged in #1340.

What and why

A D16 unsigned formatted buffer load must saturate each channel to 65535 when packing into a destination half. The low half previously kept only the low 16 bits, and shifting the high channel discarded its overflow. Reuse the existing unsigned saturating conversion for both halves in the shared MUBUF/MTBUF load path. An odd final channel preserves its VGPR's existing high half.

Tested

  • Native RDNA2 gfx1036, both wave sizes, all four destination widths: 96/96 synthetic cases agreed with saturation.
  • Six independent compute shaders translated and passed spirv-val before and after. On Windows RTX 5080 (616.92) and integrated Radeon (32.0.21045.16010), 65,536 invocations verified 262,144 result words per shader. With either MUBUF or MTBUF, the xyz and xyzw D16 cases respectively had 131,070 and 131,071 incorrect words on bb2e184 and zero here. The ordinary 32-bit control passed on both revisions. Input, guard words and the untouched high half were checked.
  • Synthetic 64×64 D3D12 guest through the separate Runtime: compute path and graphics draws on Vulkan (D3D-3, D3D-4, milestone M1) #291 integration runtime, with the base and patched translators. An MTBUF vertex color load produced 2,544 incorrect pixels on each GPU before and zero here. The floating-point control matched 4,096/4,096 pixels on both revisions. No Vulkan validation VUIDs.
  • Both new opcode subcases failed against bb2e184. The updated 27 formatted-load tests passed. The translator plus formatted-load suites passed 435 tests with two skips.

Checklist

  • All fixtures, input words and readbacks were produced from synthetic source.
  • The measured D16 rule is recorded in docs/design.md.

AI-assisted.

Native RDNA2 clamps 32-bit unsigned contents to the 16-bit range before packing both halves. Reuse the integer narrowing helper for MUBUF and MTBUF while preserving full-width fetches and the unused high half.

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

lgtm, saturating instead of masking matches the hw (your 96/96 gfx1036 cases are convincing), and reusing saturate_int16 keeps one path. semantic tests pass merged with main ^^ AI-assisted review (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (MSVC 14.51, Windows SDK 10.0.26100) with llvm-mc 23.1.6: no regression. tests/test_translate.py gives 408 passed, the same with main's translator (the new cases are in tests/semantic). --filter cs_fmt gives 2/2 on both the RTX 4080 SUPER and the Intel UHD 770.

@bobcatchris

Copy link
Copy Markdown
Contributor

Independent CPU verification, at head 85cb97ee against main bb2e1842 (Windows, Python 3.12, ROCm 5.7 llvm-mc + Vulkan SDK spirv-as/spirv-val present, no GPU):

  • tests/semantic/test_buffer_load_format.py: 27 passed.
  • Negative control with tools/rdna2spirv.py reverted to main (the test and the contract row kept): 1 failed, 26 passed — D16DestinationHalves::test_uint32_saturates_before_packing, so the new test pins the saturation rather than the packing that already worked.

Not checked: no GPU here, so the translated result was not compared against hardware — this is the semantic suite only.

@kacper-serewis

Copy link
Copy Markdown
Contributor

Tested 85cb97e on RTX 5070 (Blackwell, subgroup 32..32), NVIDIA 616.56, Windows 11 Pro 26200: pass.

  • runtime built from the PR; run_gpu_tests.py: 144 translated, 144 pass spirv-val; 109/139 passed, 30 skipped (wave64, 32-wide GPU)
  • with LEONIDA_W64_EMULATE=1: 139/139 passed
  • tests/semantic: 1221 OK (test_buffer_load_format OK)

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

@ItsNotRexed
ItsNotRexed merged commit 5c9e983 into ItsNotRexed:main Nov 22, 2026
10 of 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.

6 participants