Skip to content

Translator: pack d16 image sample/gather results with integer f32->f16 RTE - #489

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
eIBart0:d16-pack-workaround
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
eIBart0:d16-pack-workaround

Conversation

@eIBart0

@eIBart0 eIBart0 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Works around an amdvlk64 pipeline-creation crash (0xC0000005 on AMD RX 9070 XT) triggered when GLSL.std.450 PackHalf2x16 on an OpImageSampleExplicitLod d16 result feeds a structured loop (OpLoopMerge inside OpSelectionMerge) using OpGroupNonUniformAny.

Replaces PackHalf2x16 only at the two d16 image_gather4* / image_sample* packing sites in tools/rdna2spirv.py with a branchless, pure-integer IEEE-754 binary16 round-to-nearest-even (RTE) helper (Gen.f32_to_f16_int / Gen.pack_f16_pair_int). All other PackHalf2x16 sites remain untouched.

Rounding

The integer helper implements IEEE-754 binary16 RTE (preserving ±0.0, preserving f16 subnormals, overflowing |x| >= 65520 to ±Inf, and canonicalizing NaNs to sign-preserving quiet NaNs). RTE is assumed for the sampler's d16 conversion, in line with D3D f32→f16 conversion rules — this is inferred, not yet measured on hardware.

Previously, PackHalf2x16 lowered to v_cvt_pkrtz_f16_f32 (RTZ) in both AMDVLK/LLPC (SPIRVReader.cpp:10609 + ArithBuilder.cpp:106) and Mesa ACO (aco_select_nir_alu.cpp:594). Compared to that prior lowering, there is a known difference of ≤1 ULP on values that are not exact in f16, and finite overflow produces ±Inf instead of ±65504. Measuring actual RDNA2 d16 sampler rounding on hardware is left as a follow-up.

Changes

  • tools/rdna2spirv.py: Add branchless pure-integer Gen.f32_to_f16_int and Gen.pack_f16_pair_int; use pack_f16_pair_int only in the d16 image_gather4* and image_sample* packing paths.
  • tests/test_d16_pack.py: Pure-stdlib unittest suite checking exact edge cases (±0, subnormal/normal tie-to-even boundaries, max finite 65504.0, ±65520.0 → ±Inf, ±Inf, quiet/signaling NaNs), a 58,368-float sweep across both signs and all 256 exponents against struct.pack('<e', ...), and emitted SPIR-V op checks.
  • tests/test_translate.py: Add test_image_sample_and_gather_d16_avoid_packhalf2x16 verifying d16 image_sample and image_gather4 emit no PackHalf2x16 and pass spirv-val.
  • runtime/test/shaders/ps_texture_sample_d16.asm & scripts/run_gpu_tests.py: Synthetic GPU test sampling the 2D gradient texture with d16, unpacking all 4 f16 halves via v_cvt_f32_f16_e32, and exporting to mrt0 via vk_texture_test.

Verification

  • python3 -m compileall -q make_game.py tools — OK
  • All stdlib tests/test_*.py (including tests/test_d16_pack.py) — OK (tests.test_d16_pack + tests.test_shader_checks: 18 tests OK)
  • LLVM_MC=/usr/lib/llvm-22/bin/llvm-mc python3 -m pytest tests -q --ignore=tests/semantic — 1334 passed, 6 skipped, 1 xfailed, 1018 subtests passed (verified on 18333ff before the 1-line DRAWN_WITH rebase)
  • python3 -m unittest discover -s tests/semantic -v — 440 tests OK
  • Existing non-d16 runtime/test/shaders/*.asm .spvasm diff vs origin/main — 60/60 comparable shaders byte-identical (1 skipped without synthetic DXBC); ps_texture_sample_d16.asm translates, passes spirv-val, and emits zero PackHalf2x16.
  • GPU test (ps_texture_sample_d16) on hardware: PASS on AMD Radeon RX 9070 XT (Windows 11, amdvlk64; center(32,32)=(0.501953, 0.501953, 0.501953, 1.0), exact binary16 RTE 0x3804 / 0x3c00 for UNORM8 128/255 and 1.0).
  • Corpus / amdvlk64 RX 9070 XT pipeline-creation check: Across the 26,503-shader corpus, all 600 shaders with d16 image_sample*/image_gather4* (507 MS, 45 VS, 32 CS, 16 PS, 0 GS) translate and pass spirv-val (600/600); on AMD Radeon RX 9070 XT (amdvlk64, AMD_VK_USE_PIPELINE_CACHE=0, --jobs 1), the previously crashing compute shader (5,009 RDNA2 instructions, 1.42 MB SPIR-V; 3/3 0xC0000005 on main and on a 160-line synthetic repro) now compiles cleanly in 1.81 s (rc=0), completing 1,243/1,243 CS in [0, 1243) without crashes.

@evilclanker9000

Copy link
Copy Markdown
Contributor

Measured the packed 16-bit words from both image_sample d16 (RGBA) and image_gather4_lz d16 (four red taps) on ca611ae35a5a1c2ffbc60dcbcb1ccc049e68ea6d, RTX 5080, driver 616.92, Windows 11.

A synthetic 8×8 RGBA32_FLOAT texture held 64 distinct binary32 values. For nine binary16 rounding boundaries I uploaded the midpoint and its two immediately adjacent binary32 values, with both signs. Cases included zero/min-subnormal, max-subnormal/min-normal, odd and even normal ties, a power-of-two transition, and the 65520 overflow threshold. The other ten inputs were signed zeros, minimum subnormals, minimum normals, maximum finite halves and infinities. Channel permutations and the gather footprint exercised both packed words and both half positions. The pixel shaders extracted each half as an unsigned 16-bit integer for readback, preserving the word even for negative zero and infinity.

GPU measurement over 4,096 pixels Sample Gather
PR output differing from independent CPU binary16 RTE, out of 16,384 words 0 0
Non-d16 sample/gather followed by explicit v_cvt_pkrtz_f16_f32, differing from RTE 7,168 7,168
Parent 4abd545 output differing from this PR on the same inputs 0 0

The explicit RTZ controls matched a separate toward-zero oracle on every word. For example, input bits 0x3f803000 (halfway between 0x3c01 and 0x3c02) returned 0x3c02 through d16 and 0x3c01 through explicit RTZ. The CPU RTE reference used Python's binary16 packing with finite overflow mapped to signed infinity. This establishes the numerical behavior of the translated Vulkan paths on this NVIDIA driver. Native RDNA2 sampler rounding remains open.

@esteban-dcp

Copy link
Copy Markdown

Tested ca611ae on two GPUs, Linux 7.3.5 (CachyOS): pass.

  • NVIDIA RTX 4050 Laptop (Ada), driver 620.73.04, subgroup 32: 41/67 passed, 26 wave64 skipped; with LEONIDA_W64_EMULATE=1: 67/67 passed
  • AMD Radeon 740M (RDNA3, RADV, Mesa 26.3.1), subgroup 32..64: 67/67 passed
  • New ps_texture_sample_d16: PASS on all three runs
  • CPU: unittest discover -s tests/semantic 462 OK; tests/test_d16_pack.py OK; tests/test_translate.py 170 passed, 2 skipped

Ran python -I scripts/run_gpu_tests.py --build runtime/build/linux with VK_LOADER_DRIVERS_SELECT pinning each GPU (device line checked in each run), VK_LOADER_LAYERS_DISABLE=~implicit~. Baseline on main (4abd545) was the same minus this PR's tests, 0 failures.

@dcastell1

Copy link
Copy Markdown
Contributor

Tested ca611ae on an RTX 5090 (Blackwell), Windows 11 Pro 10.0.26300.9974, NVIDIA 621.24 / Vulkan 1.4.357, MSVC 14.44, llvm-mc 21.1.8 (WSL), SPIRV-Tools 2025.1, with LEONIDA_W64_EMULATE=1 (32-wide GPU):

  • run_gpu_tests.py: 70 shaders: 70 translated, 70 pass spirv-val; 67/67 passed, 0 skipped.
  • New fixture: ps_texture_sample_d16 PASS, centre (32,32) = (0.501953, 0.501953, 0.501953, 1.0). Other texture, draw, wave64-emulated compute and mesh tests unchanged.
    The test programs were built from main at 4abd545; VK_LOADER_LAYERS_DISABLE=~implicit~ was set because ReShade and the Epic/NVIDIA implicit layers are installed. No game files were used.

@eIBart0

eIBart0 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @Vonsek, @esteban-dcp and @dcastell1 for the GPU tests and the 16,384-word binary16 RTE measurement! Rebased onto current main after #240 merged (code and fixture identical to ca611ae).

@kacper-serewis

Copy link
Copy Markdown
Contributor

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

  • run_gpu_tests.py: 70 translated, 70 pass spirv-val; 41/67 passed, 26 skipped (wave64, 32-wide GPU)
  • with LEONIDA_W64_EMULATE=1: 67/67 passed (including the new ps_texture_sample_d16)
  • tests/test_d16_pack.py + tests/test_translate.py: 173 passed, 2 skipped; tests/semantic: 462 OK

Tools: llvm-mc 21.1.8 (WSL), SPIRV-Tools v2026.4.

@aslan2142

Copy link
Copy Markdown
Contributor

Tested 2efd471 on two AMD GPUs with RADV (Mesa 26.3.1, Linux): pass.

GPU scripts/run_gpu_tests.py with LEONIDA_W64_EMULATE=1
Radeon RX 7900 XTX (RDNA3), subgroup 32..64 72/72 72/72
Ryzen 9 7950X3D integrated GPU (RDNA2), subgroup 32..64 72/72 72/72
  • LEONIDA_VK_VALIDATION=1 in all four runs.
  • ps_texture_sample_d16 gives center(32,32)=(0.501953, 0.501953, 0.501953, 1.0) in each.
  • The amdvlk crash itself is not covered: it is specific to the Windows AMD driver, and these runs are RADV.

CPU side

  • compileall, tests/test_d16_pack.py and tests/semantic (708 tests) pass.
  • pytest tests --ignore=tests/semantic: 1835 passed, 1 failed. The failure is tests/test_make_game_dry_run.py::SkipAssetsAndStageIsolationTest::test_skip_assets_preflight_bypasses_dotnet_sdk_and_submodule_checks. That file fails the same way on main (91b36cf) on Linux, and this PR doesn't touch it.

Three notes from reading it

  1. The GPU test cannot tell the new packing from the old one. 128/255 becomes 0x3804 under round to nearest even and under truncation alike, and red and green have a tolerance of 0.05. 123 of the 256 UNORM8 values differ between the two roundings (9/255 is 0x2885 against 0x2884), so one pixel with such a value, compared exactly, would pin the rule.
  2. The comment in ps_texture_sample_d16.asm and the description say 0x3808. 0.501953125 is 0x3804.
  3. Mesa is no reference for the hardware's d16 rounding: ac_nir_opt_16bit_tex_image folds conversions with nir_rounding_mode_undef only. So the rule stays inferred, as the description says. docs/design.md ("Known approximations") already lists d16 results as round to nearest.

AI-assisted (Claude Code, Claude Fable 5.2).

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @aslan2142.

@aslan2142, I checked the rest of the stock texture on 2efd471. Its red and green texel codes are 0, 32, 64, …, 224, blue is 128, and alpha is 255. For every one of those UNORM8 codes, converting the normalized f32 texel value to binary16 gives the same bits under nearest-even and toward-zero. Moving the check to another stock texel would therefore have the same blind spot.

For a fixture-specific texel with your 9/255 input, the shader already expands the packed half back to f32 before export. The exact RGBA32F readback word to assert for that channel is 0x3d10a000 (half 0x2885); truncation gives 0x3d108000 (half 0x2884). These are CPU-derived expectations for the translated path. Native RDNA2 sampler rounding remains open. Texture initialization at this head

AI-assisted.

@aslan2142

Copy link
Copy Markdown
Contributor

@tylerstraub you're right, thanks for checking. The stock texture's codes (0, 32, …, 224, 128, 255) all convert the same under both roundings, so it takes a texel of its own. I get the same two words for 9/255: 0x3d10a000 for nearest even and 0x3d108000 for truncation.

One update to my test comment: the test_make_game_dry_run.py failure on Linux is fixed on main since ee97b84.

AI-assisted (Claude Code, Claude Fable 5.2).

Work around an amdvlk64 pipeline-creation crash (0xC0000005 on RX 9070 XT)
triggered when GLSL.std.450 PackHalf2x16 on an OpImageSampleExplicitLod d16
result feeds a structured loop (OpLoopMerge inside OpSelectionMerge) using
OpGroupNonUniformAny.

Replace PackHalf2x16 at the two d16 image_gather4*/image_sample* sites in
tools/rdna2spirv.py with a branchless pure-integer IEEE-754 binary16 RTE
packer (Gen.f32_to_f16_int / Gen.pack_f16_pair_int). Leave all other
PackHalf2x16 call sites unchanged.

Add unit tests covering edge cases and a 58,368-value sweep across all 256
exponents (tests/test_d16_pack.py), SPIR-V translation/validation tests for
d16 image_sample and image_gather4 (tests/test_translate.py), and a synthetic
GPU sample shader (runtime/test/shaders/ps_texture_sample_d16.asm).
@eIBart0
eIBart0 force-pushed the d16-pack-workaround branch from 2efd471 to dbb3319 Compare November 22, 2026 01:24
@eIBart0

eIBart0 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @kacper-serewis, @aslan2142 and @tylerstraub for the GPU tests and the review notes! Rebased onto c432cdc (now that #551 is on main, tools/rdna2spirv.py is down to the two d16 call-site changes) and corrected the 0x3804 half-float comment.

@tylerstraub

Copy link
Copy Markdown
Contributor

Replying to @eIBart0.

There’s a landing-order dependency with #575 after both rebases. At dbb3319, the d16 sample and gather paths both call Gen.pack_f16_pair_int, and the new structural test calls it directly too. At 6f4bd2a, #575 removes that method and replaces it with Gen.f16_pack2(lo, hi), which packs the low and high halves using integer round-to-nearest-even.

When combining the heads, please update all three callers together (or retain a forwarding method), then run the d16 pack tests and translate both a sample and a gather fixture. Otherwise those paths would call a missing Python method even if Git accepts the merge. I verified the two diffs; this is a source-level finding. #489 changes · #575 changes

AI-assisted.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested dbb33196812b5ba6faf7779139811754c71ac656 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

I measured the 9/255 rounding case discussed above on this rebased head. The stock ps_texture_sample_d16 draw passed with center RGBA (0.501953, 0.501953, 0.501953, 1.0); both shaders translated and passed spirv-val.

For a second draw I reused those same SPIR-V modules and changed only the synthetic RGBA8 texture initialization in a local copy of the harness: every red texel held code 9. All 4,096 red framebuffer words were exactly 0x3d10a000 (half 0x2885, nearest-even). Zero were 0x3d108000 (half 0x2884, truncation).

This measures translated Vulkan image_sample d16 for one distinguishing input. The gather path, native RDNA2 sampler rounding and the Windows AMD pipeline-creation issue were outside this run.

AI-assisted verification.

@ItsNotRexed
ItsNotRexed merged commit e5d9f28 into ItsNotRexed:main Nov 22, 2026
9 checks passed
ItsNotRexed added a commit that referenced this pull request Nov 22, 2026
- rdna2spirv: restore f32_to_f16_int / pack_f16_pair_int, which #489's call sites need
- spirv_mini_interp: OpFDiv by zero is undefined, so refuse it again in the shared float branch
- test_run_gpu_tests: take the wave32 lane-dump list from check.py rather than restating it
- vulkan_replay: pass the ResourceBinding to round_trip in the clear path, as its new signature expects

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fearkov added a commit to fearkov/Leonida that referenced this pull request Nov 22, 2026
An OpULessThan and OpSelect give the same shift (the same bits on 30,648 inputs). With no GLSL.std.450 left, f16_bits can narrow the d16 memory results, which ItsNotRexed#489 keeps free of extended instructions.
fearkov added a commit to fearkov/Leonida that referenced this pull request Nov 22, 2026
…t goes

main had two integer round-to-nearest-even narrowers: ItsNotRexed#489's f32_to_f16_int for the d16 image results and f16_bits for everything else. They gave the same bits on 30,648 inputs, so pack_f16_pair_int now uses f16_bits and there is one path to keep right.
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