shader: round f32 to f16 conversions to nearest even #1204
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1204
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1204/head"
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?
@Nawid3333 on GitHub · 2026-10-08 12:02 UTC · original
Nawid3333:shader/f16-pack-rounding→main· +56 −38 · 8 files · 2 commits · closed without mergingProblem
On AMD GPUs, every inexact 16-bit float result the recompiler produces is one ulp toward zero. On an
RX 9070 XT (Windows 11, driver 26.8.1) three GPU-harness modes fail because of it:
--buffer-float16-store-only,--buffer-format-store-only:BufferStoreFormatXyzwFloat16ConvertsComponentsstores
0x3c03where the test expects0x3c04.--cmpx-class-f16-only:FloatInlineConstantF16UsesNumericValueturns 1/(2π) into0x3117instead of0x3118.--fract-f16-onlychanges too. With this change it passes, because three of its edge results now round to1.0 as the test expects (on AMD they truncated to
0x3bff). WhetherV_FRACT_F16may return 1.0 at all isa separate question, raised in #1209.
Cause
The recompiler computes 16-bit float results in f32 and packs them with GLSL.std.450
PackHalf2x16.The Vulkan spec's precision table defines
packHalf2x16as "correctly rounded with implementationdefined rounding mode", and the float-controls rounding modes don't cover it. The AMD driver I tested
truncates. The tests above expect round to nearest even (
0x3c04,0x3118), the default rounding ofRDNA2's MODE register.
Fix
Convert in integer arithmetic, the way
V_CVT_PKRTZ_F16_F32already does. Its round-toward-zerohelper gets a round-to-nearest-even mode: after truncating it adds the rounding increment (ties to
even) and overflows to infinity. Values between 2^-25 and 2^-24 round up to the smallest subnormal,
values at or below 2^-25 to zero.
PackHalf2x16,ConvertF16F32, formatted 16-bit float buffer storesand 16-bit (D16) float image data use the new mode. The result no longer depends on the host driver,
and no Float16 capability or device feature is needed. Round-toward-zero results (PKRTZ) don't change.
The SPIR-V checks in
shaderCfgTeststhat requiredPackHalf2x16now check for its absence or for theconversion itself.
Two things to be aware of:
extended instruction, on every host (PKRTZ already works this way). I haven't measured the effect on
real shaders.
existing test, which expects round to nearest even there too.
Testing
Windows 11, clang-cl 19.1.5, Release, AMD RX 9070 XT, this branch on
main5a880808:--fract-f16-only) fail onmainand pass with this change; with the sixbackend/spirvfiles reset tomain(tests kept) they fail again.shader_cfgpasses.--*-onlymodes againstmain(420bb51d): 94/109 pass instead of 90/109; the only changes arethose four modes, and the other 15 failing modes fail exactly as on
main.PackHalf2x16now forbids it (secondcommit, after CodeRabbit's review).
MakeCases()-only cases that no--*-onlymode reaches, run through a temporary local selector onboth builds: Pkrtz (2), ImageD16 (2), F16 (27), Half (12) and D16 (36) all pass with this change. On
mainthe same set passes except F16, which stops atVectorFractF16CapturedAndEdges.inputs covering every f32 exponent, including the subnormal and overflow ranges; the PKRTZ path is
bit-identical to before.
Not tested on NVIDIA. I expect the same results there, except possibly NaN payload bits and
subnormals where its native pack differed, plus the instruction cost above.
AI use (per the README's AI policy): Claude (Anthropic) found the failing modes with a nightly test run on my PC, traced the cause, wrote the change and ran the tests above on my machine.
🤖 Generated with Claude Code
@coderabbitai[bot] on GitHub · 2026-10-08 12:09 UTC · original · edited 2026-10-08 13:03 UTC
📝 Walkthrough
Walkthrough
The SPIR-V emitter now supports F32-to-F16 conversion with selectable rounding and explicit packing of two half values. Scalar conversion, formatted stores, and image-result packing use these paths. Tests update their emitted-instruction expectations.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🔵 Low · up to
2cff8The conversion paths appear correct, but no checked-in output assertion protects ties-to-even at halfway values. Add the two focused cases to catch a future rounding regression; the existing driver-dependent packing guards are in place.
🚥 Pre-merge checks | ✅ 4 | ❓ 1
❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 6 files. (1 skipped: 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-10-08 12:09 UTC · original · reviewed
🧹 Nitpick comments (1)
ℹ️ Review info
⚙️ Run configuration
2cdeab01-8a89-43be-a331-efc3413c9f95📥 Commits
Reviewing files that changed from the base of the PR and between
3c62359e05andbab897a444.📒 Files selected for processing (8)
src/graphics/shader/recompiler/backend/spirv/spirvEmitterAlu.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterAluHelpers.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterImage.cppsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInstructions.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterInternal.hsrc/graphics/shader/recompiler/backend/spirv/spirvEmitterMemory.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-08 12:36 UTC · original · reviewed
🧹 Nitpick comments (1)
ℹ️ Review info
⚙️ Run configuration
00088c21-71f0-4878-a1b3-3e170eee3241📥 Commits
Reviewing files that changed from the base of the PR and between
bab897a444and2cff8e60d7.📒 Files selected for processing (1)
tests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 1 remain after this review.
Pull request closed