fix(shader): convert unorm and snorm buffer format loads with correct rounding #546

Merged
adriadam10 merged 1 commit from upstream-pr/546 into main 2026-10-07 05:58:13 +00:00
adriadam10 commented 2026-10-04 16:50:22 +00:00 (Migrated from github.com)

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 with OpConvertUToF + OpFDiv by 2^b-1. The Vulkan precision table only bounds OpFDiv to 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: 0x3e60e0e2 instead 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)

  • Unorm/snorm loads are correctly rounded (all 8/16-bit values); snorm -2^(b-1) is -1.0.
  • buffer_store_format_x/xy/xyz on a 4-component format writes the whole element, with the missing components written as 0 (not preserved).
  • Constant DST_SEL 0/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 through tbuffer with the format in the instruction (same encoding on gfx1013 and gfx1035 in llvm-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 covers buffer_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 constant DST_SEL, and buffer_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 this OpFDiv correctly, so on lavapipe only the bit-level mutations fail. Full Release ctest: only agc_driver_pixel_helper_lanes fails, as it does on main on local RADV since #305.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4736017203 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/546). Originally posted by [adriadam10](https://github.com/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 with `OpConvertUToF` + `OpFDiv` by 2^b-1. The Vulkan precision table only bounds `OpFDiv` to 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: `0x3e60e0e2` instead 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) - Unorm/snorm loads are correctly rounded (all 8/16-bit values); snorm -2^(b-1) is -1.0. - `buffer_store_format_x/xy/xyz` on a 4-component format writes the whole element, with the missing components written as 0 (not preserved). - Constant `DST_SEL` 0/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 through `tbuffer` with the format in the instruction (same encoding on gfx1013 and gfx1035 in `llvm-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 covers `buffer_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 constant `DST_SEL`, and `buffer_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 this `OpFDiv` correctly, so on lavapipe only the bit-level mutations fail. Full Release ctest: only `agc_driver_pixel_helper_lanes` fails, as it does on `main` on local RADV since #305. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:19:27 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984495841 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/546#issuecomment-5984495841). Originally posted by [oneandonlydean](https://github.com/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.
github-actions[bot] commented 2026-10-06 08:58:35 +00:00 (Migrated from github.com)

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#832 core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp L16-23
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6012888451 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/546#issuecomment-6012888451). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-06T08:58:35Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #832 | `core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp` [L16-23](https://github.com/boykopovar/AnyPS5/blob/9ab937b261a5bfdf253dd0f0d3bf371ffbdfb1fa/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvFormatConvert.cpp#L16-L23) | | |
adriadam10 commented 2026-10-07 05:58:10 +00:00 (Migrated from github.com)

Imported 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6031964543 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/546#issuecomment-6031964543). Originally posted by [adriadam10](https://github.com/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.
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
boykopovar/AnyPS5!546
No description provided.