fix(shader): convert unorm and snorm buffer format loads with correct rounding #546
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/546"
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?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T16:50:22Z.
What
Unorm and snorm buffer format loads (
buffer_load_format_*,tbuffer_load_format_*, any 2/8/10/11/16-bit unorm/snorm component) converted withOpConvertUToF+OpFDivby 2^b-1. The Vulkan precision table only boundsOpFDivto 2.5 ULP, and RADV doesn't return the correctly rounded quotient for many values (e.g. 8-bit unorm, lane 4 of the new test:0x3e60e0e2instead of the hardware's value).The hardware returns the correctly rounded f32 of v/(2^b-1) for every 8- and 16-bit unorm/snorm value (measured exhaustively on RDNA2). The conversion is now built on the bits, with no float op: v/(2^b-1) in binary is v's b bits repeated forever, so the mantissa is v rotated so its leading one is on top, replicated to 32 bits, and rounded on the next bit. There are no ties because the expansion never ends in zeros, and v = 2^b-1 rounds up to exactly 1.0. Snorm is the sign plus the unorm of
min(|v|, 2^(b-1)-1)on b-1 bits, so -2^(b-1) gives -1.0 as before. A Python mirror of the emitted ops matches exact rational rounding for every value of widths 1, 2, 8, 10, 11 and 16.Format stores were already exact (#330) and are unchanged; the new test also covers them.
Behaviour measured on HW (RDNA2 gfx1035)
buffer_store_format_x/xy/xyzon a 4-component format writes the whole element, with the missing components written as 0 (not preserved).DST_SEL0/1 on float formats give 0 / 1.0.Left out of the test on purpose because gfx1035 may not match the PS5's gfx10.1 here: on the oracle, the V# format table puts the 2-bit field in x for
2_10_10_10(the MTBUF field of the same number doesn't). 2-component V# formats also read back as (x, y, x, y) and take y from w on stores. The test only uses 4-component V# formats; packed formats go throughtbufferwith the format in the instruction (same encoding on gfx1013 and gfx1035 inllvm-mc).Tested
Linux, RADV + lavapipe (local and CI's Mesa 25.2.8): new
agc_driver_buffer_format(32 lanes, edge rows plus random rows; expected values from the hardware). It coversbuffer_load_format_x/xy/xyz/xyzw,tbuffer_load_format_x/xy/xyz(16 unorm, 10_11_11 float, 2_10_10_10 snorm), a permuted and a constantDST_SEL, andbuffer_store_format_x/xy/xyz/xyzw. On RADV the test fails without the fix (unorm and snorm paths each), and also fails if the round bit or the snorm clamp is dropped. Both lavapipe builds happen to round thisOpFDivcorrectly, so on lavapipe only the bit-level mutations fail. Full Release ctest: onlyagc_driver_pixel_helper_lanesfails, as it does onmainon local RADV since #305.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:19:27Z.
Thanks, the bit construction is neat. I checked it with a Python mirror of the emitted ops against exact rational round-to-nearest-even, for every value of the unorm widths 1, 2, 8, 10, 11 and 16 and the snorm widths 2, 8, 10, 11 and 16. There are no mismatches: the 2^b-1 carry gives exactly 1.0, -2^(b-1) gives -1.0, and 0 gives +0. All shifts stay below 32 for these widths, and the snorm input is already sign-extended by both callers.
I also recomputed the 24 load columns of the test from the raw rows with exact rounding, decoding the V# formats from the gfx10 format table, and all 768 values match. For 8- and 16-bit values the result equals IEEE division, so main's OpFDiv was only right on drivers that divide exactly. A reciprocal multiply, which the Vulkan precision rules allow, differs on 126 of the 256 8-bit values. On my NVIDIA machine the new test fails against main's code ("lane 4 buffer_load_format_x 8_8_8_8_UNORM x is 0x3e60e0e2, expected 0x3e60e0e1") and passes with this PR.
This changes every unorm/snorm format load, which vertex fetch goes through, so I'll run it through my PPSA21564 check before merging, keeping an eye on frame time (about 25 integer ops per component instead of two float ops). Otherwise it's ready to merge from my side.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-06T08:58:35Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cppL16-23Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T05:58:10Z.
Rebuilt on current main: full ctest green, the new test fails without the change on RADV and passes on CI's lavapipe (0.9 s cold). @oneandonlydean merging now since it was ready from your side; if your PPSA21564 run shows a frame-time regression from the integer conversion I'll follow up in a new PR.