feat(shader): bit-exact RDNA v_rcp_f32 behind APS5_EXACT_RCP #9305

Merged
tibzejoker merged 3 commits from upstream-pr/9305 into main 2026-10-24 00:47:44 +00:00
tibzejoker commented 2026-10-21 17:56:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-21T17:56:45Z.


What

Follow-up to #7671, which was closed because the console's v_rcp_f32 is not correctly rounded, and a target that differs from the console is not wanted. This PR emits the RDNA reciprocal itself, bit for bit, behind an opt-in switch. The default output is unchanged.

  • With APS5_EXACT_RCP set, v_rcp_f32 and v_rcp_iflag_f32 on a normal input with a normal result compute the hardware's datapath in 32-bit integer arithmetic:
    • a cubic over 32 segments of the mantissa (m >> 18), with its linear term rounded to nearest even (shift 14, or 15 when the product reaches 2^38);
    • then a table of 2,761 one-ULP corrections. The table is searched in 4 steps in a DontInline function, which only runs when the 6 dropped bits are within 2 of the rounding cut, so 6.25 % of inputs.
  • Other inputs and results keep the driver's 1/x, as now.
  • The datapath shape and coefficients follow AMD's rocjitsu emulator (ROCm/rocm-systems), which describes the reciprocal it captured on gfx1100 and gfx1201. The correction table was rebuilt here from @thuanquang's exhaustive gfx1031 sweep (all 2^23 mantissas, posted on #7671). The result matches that sweep on all 8,388,608 mantissas. So RDNA 2, 3 and 4 return the same reciprocal.
  • Why opt-in. The coefficient and correction tables cost about 5 words per entry in SPIR-V. On the 79 GTA V compute requests I have, each shader that uses v_rcp_f32 gains at least 13.6k words, and the total grows by 28 %. That is too much to pay by default for a difference no title has shown visibly. It suits comparisons against hardware and titles that depend on the exact bits.
  • The variable is read in the recompiler, so it is part of the shader disk cache key (Generated::RecompilerSwitches). It is documented in ENVIRONMENT.md, and there is an entry in docs/dev/debt/shader.md.

Tested

Windows 11 26200, WinLibs GCC 15.2.0 r7, Release.

  • New test agc_driver_rcp_hardware. It checks 256 measured RDNA 2 (input, output) pairs, 128 of them where the correction table applies, each at 6 exponents and both signs.
    • The default ctest entry skips itself (exit 77) when the switch is unset.
    • agc_driver_rcp_hardware_exact sets APS5_EXACT_RCP=1 and passes on the RTX 4090 and the UHD 770.
    • With main's recompiler and the same switch, it fails on both GPUs: v_rcp_f32 of 0x00813df5 is 0x7e7d8a33, expected the RDNA 2 value 0x7e7d8a32 on NVIDIA, and 0x00802922 is 0x7e7fadd7, expected 0x7e7fadd6 on Intel.
  • ^agc suite. 879 of 883 pass. The four left fail the same way on main: agc_driver_sample_order, agc_driver_sample_order_grid_reference and agc_driver_sample_order_recipe_depth_locations fail with main's recompiler too, and agc_driver_image_array_window_past_layer_limit does not compile with MinGW (requested alignment 65536 exceeds 8192).
  • Model check offline. A Python model of the same integer arithmetic gives 0 mismatches against the full gfx1031 sweep.
  • Not tested: a title running with the switch on.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none (follow-up to #7671)
  • Unblocks: none
  • GPUs tested: NVIDIA GeForce RTX 4090 (616.92, Windows 11 26200); Intel UHD Graphics 770 (Windows 11 26200)
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

https://claude.ai/code/session_017EjjFU1FjMR11oWDYs8UMo

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4956022874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9305). Originally posted by [tibzejoker](https://github.com/tibzejoker) on 2026-10-21T17:56:45Z. --- ### What Follow-up to #7671, which was closed because the console's `v_rcp_f32` is not correctly rounded, and a target that differs from the console is not wanted. This PR emits the RDNA reciprocal itself, bit for bit, **behind an opt-in switch**. The default output is unchanged. - With `APS5_EXACT_RCP` set, `v_rcp_f32` and `v_rcp_iflag_f32` on a normal input with a normal result compute the hardware's datapath in 32-bit integer arithmetic: - a cubic over 32 segments of the mantissa (`m >> 18`), with its linear term rounded to nearest even (shift 14, or 15 when the product reaches 2^38); - then a table of 2,761 one-ULP corrections. The table is searched in 4 steps in a `DontInline` function, which only runs when the 6 dropped bits are within 2 of the rounding cut, so 6.25 % of inputs. - Other inputs and results keep the driver's `1/x`, as now. - The datapath shape and coefficients follow AMD's rocjitsu emulator (ROCm/rocm-systems), which describes the reciprocal it captured on gfx1100 and gfx1201. The correction table was rebuilt here from @thuanquang's exhaustive gfx1031 sweep (all 2^23 mantissas, posted on #7671). The result matches that sweep on **all 8,388,608 mantissas**. So RDNA 2, 3 and 4 return the same reciprocal. - **Why opt-in.** The coefficient and correction tables cost about 5 words per entry in SPIR-V. On the 79 GTA V compute requests I have, each shader that uses `v_rcp_f32` gains at least 13.6k words, and the total grows by 28 %. That is too much to pay by default for a difference no title has shown visibly. It suits comparisons against hardware and titles that depend on the exact bits. - The variable is read in the recompiler, so it is part of the shader disk cache key (`Generated::RecompilerSwitches`). It is documented in `ENVIRONMENT.md`, and there is an entry in `docs/dev/debt/shader.md`. ### Tested Windows 11 26200, WinLibs GCC 15.2.0 r7, Release. - **New test `agc_driver_rcp_hardware`.** It checks 256 measured RDNA 2 (input, output) pairs, 128 of them where the correction table applies, each at 6 exponents and both signs. - The default ctest entry skips itself (exit 77) when the switch is unset. - `agc_driver_rcp_hardware_exact` sets `APS5_EXACT_RCP=1` and passes on the RTX 4090 and the UHD 770. - With `main`'s recompiler and the same switch, it fails on both GPUs: `v_rcp_f32 of 0x00813df5 is 0x7e7d8a33, expected the RDNA 2 value 0x7e7d8a32` on NVIDIA, and `0x00802922 is 0x7e7fadd7, expected 0x7e7fadd6` on Intel. - **`^agc` suite.** 879 of 883 pass. The four left fail the same way on `main`: `agc_driver_sample_order`, `agc_driver_sample_order_grid_reference` and `agc_driver_sample_order_recipe_depth_locations` fail with `main`'s recompiler too, and `agc_driver_image_array_window_past_layer_limit` does not compile with MinGW (requested alignment 65536 exceeds 8192). - **Model check offline.** A Python model of the same integer arithmetic gives 0 mismatches against the full gfx1031 sweep. - **Not tested:** a title running with the switch on. ### Checklist - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none (follow-up to #7671) - [x] Unblocks: none - [x] GPUs tested: NVIDIA GeForce RTX 4090 (616.92, Windows 11 26200); Intel UHD Graphics 770 (Windows 11 26200) - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_017EjjFU1FjMR11oWDYs8UMo
jotitadev commented 2026-10-21 23:21:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-21T23:21:49Z.

Review: COMMENTED


Confirmed red→green on Linux, and on lavapipe as a third device (the PR's runs are an RTX 4090 and an Intel UHD 770, both on Windows). lavapipe is a software rasterizer, so it is a useful reference here: its native v_rcp_f32 also diverges from the RDNA 2 value, and the correction table makes it bit-exact too.

Tested

Ubuntu 26.04, Intel i7-8700, GCC 15.2.0, Release, BUILD_TESTING=ON, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1, device selected with VK_ICD_FILENAMES, run as APS5_EXACT_RCP=1 ./agc_driver_rcp_hardware_tests (the agc_driver_rcp_hardware_exact entry). Head 662dc031c; it applies cleanly on main a6dcea927.

agc_driver_rcp_hardware_exact NVIDIA RTX 3070 (595.99.02) lavapipe (Mesa 26.0.3, LLVM 21.1.8)
main's recompiler + the PR's test only fail fail
this PR pass pass

With the recompiler reverted to main (SpirvAlu*, SpirvModuleEmitter.cpp, the two SpirvEmitter* headers and the new SpirvAluEmitterReciprocal.cpp removed) and the PR's test kept, it exits non-zero on both devices, each at a different input and in the opposite ULP direction:

NVIDIA  : AGC graphics: v_rcp_f32 of 0x00813df5 is 0x7e7d8a33, expected the RDNA 2 value 0x7e7d8a32
lavapipe: AGC graphics: v_rcp_f32 of 0x00810c47 is 0x7e7debcd, expected the RDNA 2 value 0x7e7debce

The NVIDIA line is the same input and values reported in the PR body. lavapipe's native reciprocal diverges on a different mantissa and rounds the other way. With the full PR both devices return the exact RDNA 2 reciprocal for every measured pair and pass. The default agc_driver_rcp_hardware entry (switch unset) still skips with exit 77.

GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02, Linux) and lavapipe (Mesa 26.0.3, LLVM 21.1.8), Ubuntu 26.04.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607429319 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9305#pullrequestreview-5607429319). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-21T23:21:49Z. **Review:** COMMENTED --- Confirmed `red→green` on Linux, and on lavapipe as a third device (the PR's runs are an RTX 4090 and an Intel UHD 770, both on Windows). lavapipe is a software rasterizer, so it is a useful reference here: its native `v_rcp_f32` also diverges from the RDNA 2 value, and the correction table makes it bit-exact too. ### Tested Ubuntu 26.04, Intel i7-8700, GCC 15.2.0, Release, `BUILD_TESTING=ON`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`, device selected with `VK_ICD_FILENAMES`, run as `APS5_EXACT_RCP=1 ./agc_driver_rcp_hardware_tests` (the `agc_driver_rcp_hardware_exact` entry). Head `662dc031c`; it applies cleanly on `main` `a6dcea927`. | `agc_driver_rcp_hardware_exact` | NVIDIA RTX 3070 (595.99.02) | lavapipe (Mesa 26.0.3, LLVM 21.1.8) | |---|---|---| | `main`'s recompiler + the PR's test only | **fail** | **fail** | | this PR | pass | pass | With the recompiler reverted to `main` (`SpirvAlu*`, `SpirvModuleEmitter.cpp`, the two `SpirvEmitter*` headers and the new `SpirvAluEmitterReciprocal.cpp` removed) and the PR's test kept, it exits non-zero on both devices, each at a different input and in the opposite ULP direction: ``` NVIDIA : AGC graphics: v_rcp_f32 of 0x00813df5 is 0x7e7d8a33, expected the RDNA 2 value 0x7e7d8a32 lavapipe: AGC graphics: v_rcp_f32 of 0x00810c47 is 0x7e7debcd, expected the RDNA 2 value 0x7e7debce ``` The NVIDIA line is the same input and values reported in the PR body. lavapipe's native reciprocal diverges on a different mantissa and rounds the other way. With the full PR both devices return the exact RDNA 2 reciprocal for every measured pair and pass. The default `agc_driver_rcp_hardware` entry (switch unset) still skips with exit 77. GPUs tested: NVIDIA GeForce RTX 3070 (595.99.02, Linux) and lavapipe (Mesa 26.0.3, LLVM 21.1.8), Ubuntu 26.04. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!9305
No description provided.