Recompiler: D16 reads of UNORM/SNORM data are one ULP low for about half the codes (f32 then truncating f32tof16) #2026
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl#2026
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@upipibi on GitHub · 2026-11-25 01:10 UTC · original
What happens
A D16 read of a UNORM or SNORM channel (
image_load/image_samplewithD16=1,buffer_load_format_d16_*) packs the f32 value withf32tof16. On D3D12 that rounds toward zero, and the value was already rounded once to f32. RDNA2 instead rounds the exactcode / (2^w − 1)once, to nearest even (AnyPS5#22750 on gfx1036 for 8- and 16-bit codes; XCL-BT-020 in #1488 on gfx1031). So about half of all codes come back one unit in the last place low.Measured on an RTX 3070 (driver 616.56) on
mainde36462, identical withXCL_DIRECT_DXIL=0and1:image_loadD16 of an 8_8_8_8_UNORM texture, all 256 codes: 123 differ. For example, code 9 gives0x2884where RDNA2 gives0x2885.buffer_load_format_d16_xof 16_UNORM and 16_SNORM, 128 sampled codes: 57 differ. For example, 16_SNORM0x5b5bgives0x39b5, want0x39b6.Counting exhaustively in exact arithmetic, today's path is off for 123/256 codes at 8-bit UNORM, 510/1024 at 10-bit, 15358/32768 at 16-bit SNORM and 31740/65536 at 16-bit UNORM. A round-to-nearest of the f32 isn't enough either: the f32 is already rounded, so
0xFFAF(16_UNORM) would give0x3bfeinstead of0x3bfd.Plan
code / (2^w − 1)has a repeating binary expansion: thew-bit code, over and over. So the correctly rounded f16 needs only 32-bit integer operations. Fill a word with the repeated code, find the leading one, take 11 bits, and round on the next bit; a tie can't happen because the tail never ends. I checked this against exact rounding for every code atw= 7, 8, 10, 11, 15 and 16, with no mismatches. SNORM uses|v| / (2^(w−1) − 1)and adds the sign.Two PRs, each covering both paths, with the GPU tests above:
dxil/lower_mem.rs,recomp/emit.rsFormatLoad): XCL-BT-020's third item;dxil/lower_image.rs, the MIMG part ofrecomp/emit.rs), for UNORM/SNORM texels.Float formats stay as they are: there, D3D's truncation already matches RDNA2, which rounds 32-bit float texels toward zero (AnyPS5#22750). Integer saturation is #1977.
AI-assisted (Claude Code). Synthetic tests only.