Skip to content

d16 image results round like typed buffer loads, and 32_FLOAT truncates (SHADER-308) - #4318

Open
BetterJdue wants to merge 1 commit into
ItsNotRexed:mainfrom
BetterJdue:shader-308-d16-image
Open

BetterJdue wants to merge 1 commit into
ItsNotRexed:mainfrom
BetterJdue:shader-308-d16-image

Conversation

@BetterJdue

Copy link
Copy Markdown
Contributor

SHADER-308. Depends on: #1759.

docs/design.md ("d16 and f16 format narrowing") said that d16 image results round to nearest even, and said
plainly that this was inferred from #489, not measured. The buffer side has been measured twice since
(BUILD-161 for the normalized formats, SHADER-358 for the 32-bit floats). This measures the image side and
finds the same two rules apply there — and that the translator was getting one of them wrong.

How

tools/d16_image_probe.py, built on tools/hw_oracle, puts every value of a format in a texture and reads each
texel in its own lane, six ways: image_load as f32 and as d16, image_sample_lz with a point filter as f32 and
d16, and image_sample_lz with a bilinear filter as f32 and d16, offset --frac texels toward the right
neighbour. The textures are GFX10 T#s built from tools/hw_oracle/tsharp.py (#1566) with the FORMAT field set
per format; the samplers are GFX10 S#s with FORCE_UNNORMALIZED and clamp-to-last-texel on every axis (Mesa
src/amd/registers gfx10.json, SQ_IMG_SAMP_WORD0..3). Every format runs in wave32 and wave64.

Controls that have to hold before a sweep counts: the f32 image_load of a UNORM/SNORM texel is the correctly
rounded quotient, a point sample equals the load, a 16_FLOAT texel comes back with its own bits, the f32 load of
a 32_FLOAT texel is the texel, and no lane reports a misaligned texture. All of them held.

Result

AMD Radeon RX 6700 (gfx1031), AMD Windows driver 32.0.21045.29003.

read formats what the hardware does
image_load d16, point sample d16 UNORM8/16, SNORM8/16 the exact quotient rounded once to nearest even: 65,536 of 65,536 for both 16-bit formats
the same, through f32 first UNORM16, SNORM16 1 ulp high on exactly four values: 0xffaf, 0xffef, 0x7ff7, 0x8009 — the same four as the buffer path
image_load d16 16_FLOAT its own bits, all 65,536 encodings
image_load d16 32_FLOAT toward zero, not to nearest even
bilinear d16 UNORM/SNORM the exact two-texel mix rounded once: 65,534 of 65,536
bilinear f32 UNORM/SNORM the correctly rounded mix: 65,536 of 65,536
bilinear d16 16_FLOAT nearest: 63,488 of 63,488 (the rest infinities and NaNs)
bilinear d16 32_FLOAT toward zero: 1,024 of 1,024

The 32_FLOAT case is the one that decides between the two readings, so it was built to be decisive: of 52 f32
patterns sitting exactly on an f16 halfway point, 28 went toward zero, 24 landed where both readings agree,
and none went to the even neighbour
. Of the other 972 values, none matched nearest-even-only and 481 matched
toward-zero-only. Bilinear 16_FLOAT rounding to nearest shows the truncation belongs to the 32-bit float
channel, not to float formats in general.

wave32 and wave64 agreed on every field of every format. The whole sweep was repeated with the bilinear sample a
quarter of a texel from the centre instead of half, with identical counts, so none of this is an artefact of
sampling exactly between two texels.

What changes

Gen.image_read_typed narrowed every float-class d16 image result with f16_bits / pack_f16_pair_int,
which is nearest even. It now goes through Gen.d16_norm_f16 — the helper the buffer path already uses — with
the format class taken from the T# at run time by a new Gen.fmt_float_flags, built on two 512-bit tables over
the GFX10 FORMAT field in the same shape as the existing Gen.fmt_int_flags. So a normalized format gets the
single-rounding correction and a 32-bit float channel is narrowed toward zero.

The tables cover the 32-bit-channel float formats as a class (32_FLOAT, 32_32_FLOAT, 32_32_32_FLOAT,
32_32_32_32_FLOAT); 32_FLOAT is the one measured here, and the others share the channel and the conversion. The
buffer path measured 32_32_FLOAT and 32_32_32_32_FLOAT directly in SHADER-358.

Checked

  • tests/test_d16_image_probe.py: 14 tests — the textures and samplers, the kernels assembling for gfx1031, the
    comparison and its exit codes through a fake oracle, the exit-77 contract, and a hardware class that runs when
    an oracle is present. It also guards the zip() truncation bug reported as BUILD-733 against the buffer probe:
    a short runner result is a harness error, not a quiet undercount.
  • the full tests/semantic/ suite on aa2988cc, with and without the change: 7,456 tests each, and the same 9
    fail on both (7 failures and 2 errors in test_cmp_class_f64, test_loop_ballot_barrier and test_mutations),
    so the change adds none. tests/test_translate.py under pytest: 581 passed, 4 skipped, with and without it.
  • hand-written d16 image_load and image_sample_lz programs, alone and next to a buffer_load_format (so the
    new tables share %_ptr_Private_uint with %fmt_size_tab), translate and pass spirv-val (vulkan1.3).
  • cs_imgload, cs_imgload_d16 and cs_sample_lz_int_d16 translate, pass spirv-val and pass check.py on
    lavapipe with the change in (on 16282101, before the rebase).
  • the probe exits 77 when the oracle is unavailable or the GPU is not GFX10, and 1 when wave32 and wave64 differ,
    so it is safe to run anywhere.

Nothing here reads game files: every texture is generated, and the shaders are assembled from the probe's own
kernel text.

AI-assisted verification using synthetic inputs.

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

Ran this at 7d68fba9 on Windows 11: python -I tests/test_d16_image_probe.py -> 13 tests, OK (2 skipped); git diff --check clean.

The translator now classifies the T# format with the 512-bit UNORM/SNORM and 32-bit-float tables and applies the d16 image-result rule: normalized formats round the exact quotient once, and a 32-bit float channel truncates toward zero. The probe tests cover the halfway and 32_FLOAT cases. The hardware measurement is gfx1031 and I cannot reproduce it here; the CPU side passes. No blocking issue.

@tylerstraub

Copy link
Copy Markdown
Contributor

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

At 7d68fba9 I checked the translated image_sample ... d16 on a Linux RTX 5090 (595.99.02). The stock ps_texture_sample_d16 UNORM8 gradient matched all 4,096 RGBA pixels. I then kept the same translator and shader and changed the local fixture to bind a constant VK_FORMAT_R32_SFLOAT texture, with its T# generated from that format. Each input sits halfway between two binary16 values.

Input texel (f32 bits) Observed red after widening Nearest-even would yield Exact RGBA pixels
0x3f803000 0x3f802000 0x3f804000 4,096/4,096
0xbf803000 0xbf802000 0xbf804000 4,096/4,096

Both signs rounded toward zero. Green and blue were zero and alpha was one throughout. This checks the translated Vulkan sampling path for two discriminating R32_FLOAT values; the native gfx1031 measurement is the author's.

AI-assisted (Codex). Synthetic textures only.

AI-assisted verification.

@throny

throny commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7d68fba on NVIDIA RTX 4080, driver 610.88, Windows 11: pass

Ran (MSVC 19.44 / VS 2022 BuildTools, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.0, native llvm-mc 23.1.6; merge base 03b09a08):

  • python -I scripts/run_gpu_tests.py --build <build> at this head: 164/210 passed, 46 skipped, 0 failed. The d16 image fixtures cs_imgload_d16, cs_sample_lz_int_d16 and ps_texture_sample_d16 (4096/4096 pixels exact) pass, and so do cs_fmt_d16_xyz and cs_w32_lds_u8_d16.
  • Corpus subset: every shader in my 1.0.2.1 carve with a d16 image_load/image_sample/image_gather (1,827 shaders), translated with --no-asm at the merge base and at this head:
    • base: 1,827 translated, 1,827 pass spirv-val;
    • head: 1,827 translated, 1,827 pass spirv-val.
    • All 1,827 modules differ between the two, as expected: every float-class d16 image read now picks its rounding from the T#'s format at run time (Gen.fmt_float_flags).

Not run here: the hardware side (tools/d16_image_probe.py needs an RDNA2 GPU with hw_oracle). The GPU suite has no fixture that reads a 32_FLOAT texel through d16 at an f16 halfway point, so the round-toward-zero branch isn't checked on this card, only that it translates and validates.

AI-assisted.

@akimfomin

Copy link
Copy Markdown

I checked the normalized 16-bit rounding boundary on Windows 11 28000.3501 with an RTX 4070 Ti SUPER / 617.14. I ran both image_load ... d16 and point image_sample_lz ... d16 translated at 03b09a08 and 7d68fba9. The fixture binds a synthetic constant texture and uses the regular harness T# encoding for its format. The output contains the two packed half words and a separate D32 read of the same texel.

Texture and texel words Base D16 PR D16 D32 unchanged on both
R16G16_UNORM (ffaf, ffef) (3bfe, 3c00) (3bfd, 3bff) (3f7fb000, 3f7ff000)
R16G16_SNORM (7ff7, 8009) (3c00, bc00) (3bff, bbff) (3f7ff000, bf7ff000)

Both instructions gave those results at every pixel. The exact-rational binary16 expectations matched 0/16,384 boundary pixels on the base and 16,384/16,384 on the PR. The endpoint controls matched 16,384/16,384 on each revision. Six generated modules passed spirv-val --target-env vulkan1.2. The Vulkan validation layer was off for the draws. Synthetic textures only. AI-assisted (Codex).

@tunczzz

tunczzz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 7d68fba9 on NVIDIA GeForce RTX 5070 Ti (Blackwell, driver 616.56, subgroup 32..32), Windows 11 10.0.26300; translator from the PR head, runtime test programs built from main aa2988cc (MSVC 19.44, Ninja RelWithDebInfo): pass. scripts/run_gpu_tests.py: 164/210 passed, 46 skipped (wave64 fixtures without emulation on a 32-wide GPU), 0 failed: the same result as main aa2988cc on this machine for every test (diffed test by test). No game files. AI-assisted (Claude Code).

@BetterJdue

Copy link
Copy Markdown
Contributor Author

Thanks @Kam471, @tylerstraub, @throny and @akimfomin for checking this.

On the test count: tests/test_d16_image_probe.py at 7d68fba9 defines 14 tests. Without hw_oracle and the ROCm clang, python -I tests/test_d16_image_probe.py prints Ran 13 tests and OK (skipped=2). OnHardware skips in setUpClass, so its one test never runs and is not counted. Kernels.test_kernels_assemble_for_rdna2 runs and skips itself, so it is one of the 13. Those are the two skips. I got the same output here with the oracle and clang made unavailable; with both present on the RX 6700 the file prints Ran 14 tests.

Your results agree with the gfx1031 measurement: R32_FLOAT halfway inputs go toward zero for both signs, and the four UNORM16/SNORM16 values 0xffaf, 0xffef, 0x7ff7, 0x8009 now come out exactly rounded, on NVIDIA through the translator.

AI-assisted verification using synthetic inputs.

@tupacfan04

Copy link
Copy Markdown
Contributor

The translator change looks correct, but nothing tests it.

On the positive side: the d16 probe 12 passed / 2 hardware skips, test_translate.py -k d16 18 pass, the semantic d16/buffer tests pass, docs tests pass, regen/compileall pass, merge clean, and a manual d16 image_load translates and validates.

The problem: with tools/rdna2spirv.py reverted to base, every test stays green. tests/test_d16_image_probe.py never imports the translator (or spirv_mini_interp), so tools/rdna2spirv.py:10570-10603 (fmt_float_flags / d16_norm_f16) has no CPU regression test - and docs/design.md:381 says it is "checked by tests/test_d16_image_probe.py", which is not true.

Add a translating case (a hand-written d16 image_load through support.translate_lines + spirv_mini_interp, asserting the round/truncate and 32_FLOAT truncation), or fix the doc claim.

AI-assisted review, no game files, no GPU.

@FZNull

FZNull commented Nov 23, 2026

Copy link
Copy Markdown

Tested 7d68fba9 on NVIDIA GeForce RTX 4060 (Windows driver 32.0.16.2124 / 621.24, Vulkan 1.4.357), Windows 11 build 26300: pass for the translation and GPU-suite checks; the PR's AMD GFX10 hardware sweeps skip here, they do not pass.

Clean worktree at 7d68fba9 on main 03b09a08; the PR only changes Python and docs, so there is nothing to build.

  • python -I tests/test_d16_image_probe.py -> OK, 13 tests, 2 skipped (Kernels.test_kernels_assemble_for_rdna2 and OnHardware: "no AMD clang ..."), 216.9 s. tools/d16_image_probe.py --formats unorm8 exits 77 "hardware oracle unavailable".
  • python -m pytest tests/test_translate.py -q -> 581 passed, 4 skipped in 314.95 s, as in the PR body.
  • python -I scripts/run_gpu_tests.py --build runtime/build --llvm-mc llvm-mc --work out/gpu-tests/imgload --filter imgload -> 2/2 passed on the RTX 4060 (cs_imgload, cs_imgload_d16); both translate and pass spirv-val.
  • Full GPU suite: native 164/210 passed, 46 skipped, exit 0; wave64 emulated (LEONIDA_W64_EMULATE=1) 195/210 passed, 15 skipped, exit 0. Clean main 03b09a08 gives the same counts, so there is no GPU-suite regression.
  • python -m unittest discover -s tests/semantic on the PR and on clean main: 7456 tests each, both ending failures=7, errors=2, skipped=1, expected failures=1 with the identical failing set (test_cmp_class_f64, test_loop_ballot_barrier.*, test_mutations.*). No new semantic failures.

Not verified here: the AMD d16 image rounding and 32_FLOAT truncation measurements need a GFX10 GPU and the HIP oracle; they skip with exit 77 on this machine.

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