fix(shader): round normalized storage image writes from the exact product #7666
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7666"
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 tibzejoker on 2026-10-18T21:23:15Z.
What
Vulkan lets a device return either neighbour when it converts a float to a normalized texel ("implementations should round to nearest"), and devices differ in practice. For a stored value whose product with 255 is 123.50003, an RTX 4090 and an Intel UHD 770 both store 123 instead of 124. On GTA V's FSR1 RCAS compute pass (PPSA04264, compute 0x25b393000, RGBA8 output), the two vendors stored 112 different bytes for the same inputs.
Writes to unorm and snorm storage images whose components share one width (8_UNORM, 16_16_UNORM, 8_8_8_8_UNORM/SNORM, 16_16_16_16_UNORM, ...) now:
value * scalein the shader to the level nearest the EXACT product, ties to even.fl32(value * scale)can land exactly on a .5 when the exact product does not; the exact error ofp, taken as a Dekker two-product (both factors split with Veltkamp, then correctly roundedOpFMul/OpFAdd/OpFSub, allNoContraction), breaks that tie by its sign. NoFmais used, because drivers are not required to fuse it (llvmpipe does not). RDNA 2 (RX 6700 XT, nativeimage_store, measured by @thuanquang) and Apple M2 (@jotitadev) both round this way.level * c, wherecis the smallest float not below1 / scale.The exact product of that stored float with the scale lies in
[level, level + 0.01)for 8-, 10- and 16-bit unorm and snorm. I checked every level with exact rational arithmetic. A device that rounds to nearest and a device that truncates therefore both storelevel.The format comes from the image descriptor through specialization constants (
StoreQuantizeBase, four per written storage image), so float formats, packed formats and format codes outside the format table keep the plain write. Registration modules, tables and atomics are unchanged.Tested
Windows 11, MinGW GCC 15.2:
agc_driver_image_store_unorm_rounding. It stores values next to the midpoints between levels, edge values and NaN/infinities to 8_UNORM, 16_16_UNORM, 8_8_8_8_UNORM, 8_8_8_8_SNORM and 16_16_16_16_UNORM images.mainit fails on both GPUs:image_store of 8_UNORM: texel (6, 0) component 0 is 0x7b, expected 0x7c.ANYPS5_NO_SHADER_CACHE=1). The expected level isnearbyintof the product computed in double, exact for these inputs.fl32(value * scale)) fails it:texel (10, 0) component 0 is 0xce, expected 0xcd(exact product 205.49999..., RDNA 2 stores 205).ctest -R ^agc_on the RTX 4090: 727/728. The failure isagc_driver_bda_device, which fails onmaintoo.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)61340e24a: passes, the first version's shader fails); rounding rule checked on RDNA 2 hardware (RX 6700 XT, HIP) by @thuanquang and on Apple M2 by @jotitadev; Apple M2 Max (MoltenVK 1.4.2 and main, fast math 0/1/2) rerun on61340e24aby @jotitadev: passes, the first version's shader fails; AMD Radeon RX 7900 XT (RADV NAVI31, Mesa 25.2.8, Linux) by @DavidAmsellem: full suite, no new failure🤖 Generated with Claude Code
https://claude.ai/code/session_01XBXM4pgkMHYEtqN1EQni2P
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-18T21:36:27Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/PipelineSpecialization.hppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/PipelineSpecialization.hppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/PipelineSpecialization.hppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/PipelineSpecialization.hppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppcore/shader/recompiler/PipelineSpecialization.hppcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppcore/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cppImported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T21:44:12Z.
The overlap with #7085 is textual only: both add a constant next to
MipCountBaseinPipelineSpecialization.hpp. The ranges do not collide (LevelsPastViewBase= 8384 there,StoreQuantizeBase= 12288 + 4 per image here), so whichever merges second only needs the two lines kept side by side.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-18T22:33:08Z.
A question for anyone with an RDNA hardware oracle (as used on #7671). This PR picks round-to-nearest-even of
fl32(x * 255)for float-to-UNORM8 storage writes, because Vulkan leaves the choice to the device and NVIDIA and Intel disagree. I have not measured what RDNA itself stores.An
image_storeof the values fromagc_driver_image_store_unorm_rounding(the near-midpoint ones especially) to an 8_8_8_8_UNORM and a 16_16_16_16_UNORM image on native hardware would show whether the console rule is the same. If it differs, I'll change the rounding here to match it.AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T22:33:44Z.
AMD run, since the description covers an RTX 4090. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release,
BUILD_TESTING=ON. Build:main8a456334fwith #7677, #7673, #7671, #7666, #7676, #7675, #7672, #7668, #7664 and #7658 merged together (each merge clean, built once).agc_driver_image_store_unorm_roundingandagc_driver_bda_devicepass 3 runs of 3 on AMD's proprietary driver.AI-assisted
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T05:07:15Z.
Review: COMMENTED
Apple GPU / MoltenVK run, with a data point for the vendor question. Without this PR, Apple stores the level nearest to the exact product
x * scale, not tofl32(x * scale). That is a third behaviour next to NVIDIA and Intel. With the PR,agc_driver_image_store_unorm_roundingpasses, and the full^agcsuite is unchanged.Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Head
3e5499310, PR diff applied cleanly. Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704.Tested: the tree is
maina373e85e5+ mugurc's macOS series (ci/macos-series-2) + the PRs needed to build on macOS (#6667, #6668, #7524, #6969, and two files of #7332), not plainmain(it doesn't build on macOS yet). Also in the same build: #6606, #7683, #7696, #7692 and #7415 (they applied cleanly together), plus a test-only patch that lets nine AGC fixtures load the Vulkan loader on macOS (branchjotitadev:fix/agc-tests-vulkan-loader-macos). Base on that tree: 40^agcfailures with MoltenVK 1.4.2 and 26 with MoltenVK main + #2704.With the PR:
agc_driver_image_store_unorm_roundingpasses 3 of 3 on both drivers. It still passes 3 of 3 withMVK_CONFIG_FAST_MATH_ENABLED=1, which makes MoltenVK compile with Metal fast math.With this PR's
DescriptorBindingBuilder.cpp,PipelineSpecialization.hppandSpirvImageEmitter.cppreverted, the test fails on both drivers; the first mismatch is 8_UNORM texel (10, 0), 0xcd where 0xce is expected. I changed the check locally to log every mismatch instead of stopping at the first. 59 of the 736 written components differ, and the results are identical on both drivers:x * scale0x1.9c9c9cp-10x1.159116p-50x1.010102p-9-0x1.7cf9f4p-10x1.cec1cep-7In each of these inputs,
fl32(x * scale)is exactly .5, so round-to-nearest-even of the float product and rounding of the exact product give different levels. I recomputed the test's inputs offline: exactly 59 components fall in that case (4/7/17/16/15 per format), and Apple stored the level nearest to the exact product in all 59. The NaN, infinity, clamp and edge values match the test's expectations without the PR.Since both drivers give the same result, the conversion happens in Metal or the hardware, not in SPIRV-Cross. Apple seems to convert float to unorm/snorm with correct rounding. NVIDIA and Intel, as the description says, store 123 for 123.50003.
On the RDNA question: if the console also rounds the exact product, the inputs above are where this PR's rule (RTNE of
fl32(x * scale)) and the console would differ. The test already exercises them, so a hardware oracle run of it would settle the question.Full
^agcsuite with the stack: the base failures on each driver, plusagc_driver_pooled_buffer_rangeandagc_performance_overlay, which only run because of the loader patch. Nothing from this PR.GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T05:16:36Z.
Thanks @jotitadev, this is a real gap in the PR. I picked "round to nearest even of
fl32(x * scale)" because NVIDIA and Intel both start from the f32 product. Apple rounds the exact product, and that is the more defensible rule: it does not depend on how the product was computed, and it is what "convert to the nearest level" means without a tolerance.Rounding the exact product costs little in the shader.
scale(2^n - 1, n <= 16) is exact in f32, ande = fma(x, scale, -p)gives the exact error ofp = fl32(x * scale). Whenp - floor(p)is exactly 0.5, the sign ofepicks the level (e > 0 -> up, e < 0 -> down, e == 0 -> even). OtherwiseroundEven(p)is already the level nearest to the exact product. That is one fma and one compare per component, and thelevel * cstore stays as it is.What I don't know is what RDNA does, and the console is the reference. @simonbbby, could your RDNA 2 oracle run the stores of
agc_driver_image_store_unorm_rounding, or just the 59 inputs above (image_storeto an R8/RGBA8/RG16/RGBA16 UNORM and RGBA8 SNORM texture)?fl32(x * scale), the PR already matches it, and Apple gets the emulation too.Until there is data, I'll leave the PR as it is, so that it doesn't change twice.
Written with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T07:26:04Z.
@tibzejoker RDNA 2 answer: the hardware rounds the exact product, like Apple, not
fl32(x * scale).RX 6700 XT (gfx1031), native
image_storethrough HIP (the shader runs as written, no Vulkan driver in between). For each format I generated the 128 values ofagc_driver_image_store_unorm_rounding(Value(format, tid, index)for tid 0-31 and index 0-3, the edges, NaN, ±infinity and the near-midpoint values), stored them withimage_store ... dmaskinto a 32x4 linear 2D T# of that format, one value per component, and read the raw bits back through a UINT T# over the same memory:x * scale)fl32(x * scale))0x22c)In every row where the rules differ, the hardware stored the level nearest to the exact product, for example
0x3f4e4e4e(exact product 205.49999...) gives 205 in 8_UNORM, and-0x1.7cf9f4p-1(exact -94.5000004) gives -95 in 8_8_8_8_SNORM. Clamping, NaN to 0 and ±infinity to the end levels match the test's expectations. So the fma rule you described (e = fma(x, scale, -p)to break the.5cases offl32(x * scale)) is the one that matches RDNA 2.My counts per format differ from @jotitadev's because I stored all four values of every slot into every format, where the test's own layout stores fewer components for the one- and two-channel formats. Desktop RDNA 2, not a PS5.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T07:30:08Z.
Thank you @thuanquang, that settles it: RDNA 2 and Apple M2 both store the level nearest to the exact product.
Pushed
61340e24aon top of the first commit:p = fl32(x * scale)and its fma residuale = fma(x, scale, -p)(bothNoContraction). Whenpis exactly a .5,e > 0rounds up,e < 0rounds down,e == 0keeps ties to even;nearbyintof the product computed in double (exact for f32 x 16-bit scale). The first version fails it on your example:texel (10, 0) component 0 is 0xce, expected 0xcd.Passes 3/3 on an RTX 4090 and an Intel UHD 770 with
ANYPS5_NO_SHADER_CACHE=1. The 9agc_driver_image_store|agc_driver_storagetests pass too. On the UHD 770 the residual came out 0 until the fma itself was decoratedNoContraction. Title and body updated.@LeandroLP23 your 860M run was on the first version; if you have a moment, a rerun of
agc_driver_image_store_unorm_roundingon this head would be welcome.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-19T07:35:03Z.
Rerun on
61340e24a, AMD Radeon 860M (RDNA 3.5,1002:1114), Windows 11, WinLibs GCC 15.2.0, Release,ANYPS5_NO_SHADER_CACHE=1, merged intomainfb0dc118f:agc_driver_image_store_unorm_roundingpasses 3 of 3.agc_driver_image_store*andagc_driver_storage*tests pass too.SpirvImageEmitter.cppfrom the first commit and the new test, it fails 3 of 3 on the same texel you quoted:image_store of 8_UNORM: texel (10, 0) component 0 is 0x000000ce, expected 0x000000cd. So on this driver the fma residual is what makes the difference.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T07:41:07Z.
Thank you @LeandroLP23, that is the check I hoped for: on RDNA 3.5 the new head passes and the first version's shader fails on the same texel. I added the 860M rerun to the GPU line.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-19T07:48:37Z.
One more detail for the fma rule's
e == 0case, same GPU: the only exact ties these formats can hit are ±0.5 (x * scale= ±63.5, ±127.5, ±16383.5, ±32767.5), and the hardware stores 0.5 → 64 / -0.5 → -64 in 8_8_8_8_SNORM and ±16384 in 16_16_16_16_SNORM (and 128 / 32768 in the UNORM formats earlier). So ties don't go toward +infinity (-63.5 doesn't give -63); round-to-even, as in your rule, gives the hardware level for every tie these formats can produce. -1.0 stores -127 in SNORM8 and -32767 in SNORM16.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T07:51:31Z.
Thanks @thuanquang, good to have the exact ties pinned down too. ±0.5 giving ±64 / ±16384 in SNORM and 128 / 32768 in UNORM is round-to-even, and -1.0 giving -127 / -32767 matches the clamp to [-1, 1]. So the head's rule agrees with RDNA 2 on every case measured.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-19T09:39:32Z.
Review: COMMENTED
Apple GPU / MoltenVK rerun on
61340e24a. The new rule matches what Apple stores, and the fma residual survives Metal fast math.agc_driver_image_store_unorm_roundingpasses, the first commit's shader fails it on the texel you quoted, and the full^agcsuite is unchanged.Machine: Apple M2 Max, macOS 27.2, x86-64 build under Rosetta. Drivers: MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0) and MoltenVK main + SPIRV-Cross #2704. Head
61340e24a, PR diff applied cleanly.Tested: the tree is
maina373e85e5+ mugurc's macOS series (ci/macos-series-2) + the PRs needed to build on macOS (#6667, #6668, #7524, #6969, and two files of #7332), not plainmain(it doesn't build on macOS yet). Applied together in the same build: #7666, #7811, #7799 and #7803, plus a test-only patch that lets nine AGC fixtures load the Vulkan loader on macOS (branchjotitadev:fix/agc-tests-vulkan-loader-macos). Base on that tree: 40^agcfailures with MoltenVK 1.4.2 and 26 with MoltenVK main + #2704, plusagc_driver_pooled_buffer_rangeandagc_performance_overlay, which only run because of the loader patch.agc_driver_image_store_unorm_roundingpasses 3 of 3 on both drivers, withMVK_CONFIG_FAST_MATH_ENABLEDset to 0, 1 (always fast math) and 2.SpirvImageEmitter.cppfrom the first commit (3e5499310) and the new test, it fails 3 of 3 on both drivers, and also with fast math forced:image_store of 8_UNORM: texel (10, 0) component 0 is 0x000000ce, expected 0x000000cd (level nearest to the exact product). That is the same texel as on the 860M.MVK_CONFIG_DEBUG=1, fast math forced), SPIRV-Cross turns theNoContractionmultiply and subtract into its[[clang::optnone]]helpersspvFMul(fma(l, r, 0)) andspvFSub(fma(-1, r, l)). The residual is a plainfma(x, scale, -p). The compiler can't foldpback into the residual.^agcsuite with the stack: the base failures on each driver, nothing new and nothing gone.GPUs tested: Apple M2 Max (MoltenVK 1.4.2; MoltenVK main + SPIRV-Cross #2704)
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T09:42:16Z.
Thank you @jotitadev. Good to know the residual survives forced fast math through SPIRV-Cross's
optnonehelpers. Noted too that on Apple the test guards against a return to the first rule rather than the removal of the rounding, since Metal already stores the nearest level. Added the rerun to the GPU line.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by DavidAmsellem on 2026-10-19T15:44:20Z.
First AMD-on-Linux (RADV) run of this PR. Merged onto
main43a2abaf7(clean merge), built in full and compared with the samemain, on the same machine.AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 (Ubuntu 24.04 base), kernel 7.0.0-38, GCC 13.3, Release.
Full suite (
ANYPS5_REQUIRE_VULKAN=1 ctest -j8): 1637/1639 with this PR, against 1636/1638 onmain. The failures are the same two on both, so this PR adds none:agc_driver_image_table_vouched_lookups(#7766, fixed by #7916) andagc_driver_guest_memory_lifetime(#7784). The newagc_driver_image_store_unorm_roundingran (not skipped) and passed.GPUs tested: AMD Radeon RX 7900 XT (RADV NAVI31, gfx1100), Mesa 25.2.8, Linux Mint 22.3 / kernel 7.0.0-38
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-19T15:54:12Z.
Thank you @DavidAmsellem, the first Linux/RADV run is a useful one to have: the new test ran and passed on gfx1100, and the full suite shows only the two failures
mainhas too. Added to the GPU line.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T10:44:20Z.
Review: CHANGES_REQUESTED
The full Build (linux, CI's lavapipe: llvmpipe, LLVM 20.1.2) fails the new test on this head, and
maindoes not have that failure:Run 39345976979, test
agc_driver_image_store_unorm_rounding. It is the same texel the first version fails.SpirvImageEmitter.cpp:1034computes the residual withGLSLstd450Fma. The Vulkan spec does not makeFmabit-exact, and llvmpipe splits it into a multiply and an add, so the residual comes out 0 and the tie falls back to round-to-even offl32(x * scale).NoContractionon the result cannot stop that: it only forbids fusing, it does not makeFmafused. The RADV, 860M and M2 runs pass because those drivers do fuse it.p = fl32(x * scale)from correctly rounded ops only (Veltkamp split of both factors and the Dekker two-product, asEmitFPFma64does for f64Fma), every opNoContraction.scaleis at most 16 bits andxis clamped to [-1, 1], so the split is cheap. The sign of that error then picks the level exactly as it does now.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-20T10:52:48Z.
Thanks @adriadam10, you're right:
Fmawas the wrong tool, since nothing requires the driver to fuse it.The fix is in
187b1ab24: the residual is now a Dekker two-product.clampedandscaleare each split with Veltkamp (factor 4097).fl32(clamped * scale)'s error usingOpFMul,OpFAddandOpFSubonly. Vulkan requires those to be correctly rounded, and every one of them is decoratedNoContraction.Fmais left on that path, so the result no longer depends on whether a driver fuses it.Checks:
x * scale - p, for scales 127, 255, 1023, 32767 and 65535. The inputs were 4,000 levels per scale, ±40 ulp around each(k + 0.5) / scale, both signs, about 1.6M products, 40,180 of them exact.5ties. The error was exact on every one.agc_driver_image_store_unorm_roundingand the otheragc_driver_image_store*/agc_driver_storage*tests pass, 9 of 9, on the RTX 4090 and the UHD 770.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T16:24:18Z.
Checked the Dekker two-product replacing
Fma(Veltkamp split, onlyOpFMul/OpFAdd/OpFSub, allNoContraction), the test and the third-party RDNA 2, 860M, Apple and RADV runs; merged.