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

Open
SchmashinPrunes wants to merge 1 commit from upstream-pr/382 into main
SchmashinPrunes commented 2026-11-25 15:30:38 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by SchmashinPrunes on 2026-11-25T15:30:38Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:5494328130 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/382). Originally posted by [SchmashinPrunes](https://github.com/SchmashinPrunes) on 2026-11-25T15:30:38Z. --- 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)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/382:upstream-pr/382'
git switch 'upstream-pr/382'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/382'
git switch 'upstream-pr/382'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/382'
git switch 'upstream-pr/382'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/382'
git switch 'main'
git merge --squash 'upstream-pr/382'
git switch 'main'
git merge --ff-only 'upstream-pr/382'
git switch 'main'
git merge 'upstream-pr/382'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
omonkeyz/xbox-emulator!382
No description provided.