tombox-xshader: keep the RDNA2 NaN payload in buffer_load_format_d16 (follow-up to #263) #382

Open
github-bot wants to merge 1 commit from github-bot/gh-382 into main AGit
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-25 15:30 UTC · original
SchmashinPrunes:mubuf-d16-rtz → main · +32 −2 · 2 files · 1 commits · open

This is a follow-up to #263 that also corrects my review there.

Correction to my #263 review

I wrote that buffer_load_format_d16_x doesn't round the way f32tof16 does, and that 1,014 of 2,048 recorded cases differ. That was wrong. My comparison used a round-to-nearest-even model of f32tof16, but D3D doesn't define it that way. The D3D11.3 functional spec, §3.2.2, says:

  • round-to-zero must be used when converting to another float format;
  • finite overflow gives the largest representable value, not infinity;
  • a NaN only has to become a NaN.

That is the rounding and overflow rule gfx1036 follows. So #263 as applied already rounds the same as the hardware, on any conformant host. Both hosts here confirm it: the recorded rounding and overflow cases below pass on WARP and on an RX 9060 XT with today's f32tof16. Sorry for the noise.

What is still different: NaN payloads

The spec leaves the NaN payload open, and hosts differ there:

source (32_FLOAT) gfx1036 WARP f32tof16 RX 9060 XT f32tof16
0xffbfffff 0xfdff 0xffff 0xfdff
0x7f800001 0x7c01 0x7fff 0x7c01
0x7fc00000 0x7e00 0x7fff 0x7e00
0x7f810000 0x7c08 0x7fff 0x7c08

The hardware keeps the truncated payload, sign | 0x7c00 | max(mant >> 13, 1). This is the same rule the MTBUF d16 path already implements in tb_f16_rtz (hlsl/mtbuf.rs), and it matched all 40 NaN inputs in the 2,048-case recording. WARP collapses every NaN to 0x7fff/0xffff.

Change

The MUBUF format d16 loads now use tb_f16_rtz(asuint(x)) instead of f32tof16(x), and set uses_d16 so the helper is emitted. For non-NaN values the result is the same as before.

The rule is measured for 32_FLOAT elements only. For other source formats this keeps today's approach (the view converts to f32, then f32 → f16), and the NaN handling now follows the hardware rule.

Tests

New buffer_mubuf_format_d16_load_keeps_the_rdna2_nan_payload, with 16 elements:

  • 8 NaNs: two taken from the gfx1036 recording, the rest from the rule above, which the recording confirms on all 40 of its NaN inputs;
  • 8 recorded non-NaN values: 6 that a round-to-nearest-even model would get wrong (rounding, overflow to ±65504, subnormals), plus ±infinity.

Each case also checks that vdst[31:16] is kept.

Results (Windows 11, DXC from TOMBOX_DXC_DIR):

run result
WARP, without the hlsl.rs change the new test fails on 7 of its 8 NaN cases (all read 0x7fff/0xffff); the 8 rounding, overflow and infinity cases pass
WARP, full tombox-xshader suite all suites ok, tests/exec.rs 190/190
Radeon RX 9060 XT (TOMBOX_EXEC_HW=1), exec 190/190; this host already keeps the payload, so the test passes there with or without the change

The PR gate is clean.

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-25 15:30 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/382) `SchmashinPrunes:mubuf-d16-rtz` → `main` · +32 −2 · 2 files · 1 commits · open This is a follow-up to #263 that also corrects my review there. ### Correction to my #263 review I wrote that `buffer_load_format_d16_x` doesn't round the way `f32tof16` does, and that 1,014 of 2,048 recorded cases differ. That was wrong. My comparison used a round-to-nearest-even model of `f32tof16`, but D3D doesn't define it that way. The D3D11.3 functional spec, §3.2.2, says: - round-to-zero must be used when converting to another float format; - finite overflow gives the largest representable value, not infinity; - a NaN only has to become *a* NaN. That is the rounding and overflow rule gfx1036 follows. So #263 as applied already rounds the same as the hardware, on any conformant host. Both hosts here confirm it: the recorded rounding and overflow cases below pass on WARP and on an RX 9060 XT with today's `f32tof16`. Sorry for the noise. ### What is still different: NaN payloads The spec leaves the NaN payload open, and hosts differ there: | source (32_FLOAT) | gfx1036 | WARP `f32tof16` | RX 9060 XT `f32tof16` | |---|---|---|---| | `0xffbfffff` | `0xfdff` | `0xffff` | `0xfdff` | | `0x7f800001` | `0x7c01` | `0x7fff` | `0x7c01` | | `0x7fc00000` | `0x7e00` | `0x7fff` | `0x7e00` | | `0x7f810000` | `0x7c08` | `0x7fff` | `0x7c08` | The hardware keeps the truncated payload, `sign | 0x7c00 | max(mant >> 13, 1)`. This is the same rule the MTBUF d16 path already implements in `tb_f16_rtz` (`hlsl/mtbuf.rs`), and it matched all 40 NaN inputs in the 2,048-case recording. WARP collapses every NaN to `0x7fff`/`0xffff`. ### Change The MUBUF format d16 loads now use `tb_f16_rtz(asuint(x))` instead of `f32tof16(x)`, and set `uses_d16` so the helper is emitted. For non-NaN values the result is the same as before. The rule is measured for 32_FLOAT elements only. For other source formats this keeps today's approach (the view converts to f32, then f32 → f16), and the NaN handling now follows the hardware rule. ### Tests New `buffer_mubuf_format_d16_load_keeps_the_rdna2_nan_payload`, with 16 elements: - 8 NaNs: two taken from the gfx1036 recording, the rest from the rule above, which the recording confirms on all 40 of its NaN inputs; - 8 recorded non-NaN values: 6 that a round-to-nearest-even model would get wrong (rounding, overflow to ±65504, subnormals), plus ±infinity. Each case also checks that vdst[31:16] is kept. **Results** (Windows 11, DXC from `TOMBOX_DXC_DIR`): | run | result | |---|---| | WARP, without the `hlsl.rs` change | the new test fails on 7 of its 8 NaN cases (all read `0x7fff`/`0xffff`); the 8 rounding, overflow and infinity cases pass | | WARP, full `tombox-xshader` suite | all suites ok, `tests/exec.rs` 190/190 | | Radeon RX 9060 XT (`TOMBOX_EXEC_HW=1`), exec | 190/190; this host already keeps the payload, so the test passes there with or without the change | The PR gate is clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The MUBUF format d16 loads converted with f32tof16. D3D float-to-float
conversion already rounds toward zero and clamps finite overflow to the
largest finite value, which is what gfx1036 does (2,048 recorded
buffer_load_format_d16_x cases from 32_FLOAT), but it only requires some
NaN: WARP turns every NaN into 0x7fff/0xffff. The hardware keeps the
truncated payload (sign | 0x7c00 | max(mant >> 13, 1)). Use the existing
tb_f16_rtz helper, which implements the measured rule exactly, so every
host gives the hardware bits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:21:00 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/382/head:github-bot/gh-382'
git switch 'github-bot/gh-382'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-382'
git switch 'github-bot/gh-382'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-382'
git switch 'github-bot/gh-382'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-382'
git switch 'main'
git merge --squash 'github-bot/gh-382'
git switch 'main'
git merge --ff-only 'github-bot/gh-382'
git switch 'main'
git merge 'github-bot/gh-382'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.