Avoid double rounding in shader float-to-integer conversion #2678
No reviewers
Labels
No labels
AMD
audio
bug
confirmed
crash
cutscene
dlss
documentation
duplicate
enhancement
fixed-on-main
frame-generation
good first issue
help wanted
input
INTEL
invalid
lighting
linux-proton
memory
needs-info
needs-logs
needs-rebase
NVIDIA
performance
pr-diagnostics
pr-docs
pr-feature
pr-fix
pr-performance
pr-refactor
pr-tests
question
RAYTRACING
rendering
report
series-s
shaders
spam
startup
upstream
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/trustno1-coder--VIctory-NVIDIA!2678
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2678"
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?
@masteraapjes on GitHub · 2026-11-29 15:55 UTC · original
masteraapjes:codex/shader-rpi-rounding→main· +57 −5 · 2 files · 1 commits · merged 2026-11-30 14:12 UTCWhat it changes
v_cvt_rpi_i32_f32adds 0.5 in f32 and then floors the result. The firstoperation rounds before the conversion: 0x3effffff (just below 0.5) becomes
1 instead of 0, and 8388609 becomes 8388610 despite already being integral.
Floor the input first, then increment only if its fractional part is at least
0.5. This gives nearest-integer rounding with ties towards positive infinity,
without an intermediate f32 rounding of x+0.5. The existing conversion and
NaN handling remain in place; the other conversion opcodes are unchanged.
This adapts the rounding-only correction from upipibi's upstream PR
#605, supported by
Lyrnc's native RDNA 2 measurements.
The separate NaN-sign saturation change discussed there is outside this patch.
How it was tested
shaders, with no game files or vendor SDKs.
returning 1 instead of 0.
cargo test --locked -p victory-shader --lib v_cvt_rpi_i32_f32_rounds_without_an_intermediate_f32_sum -- --nocapturepasses. Both backends exercise 27 bit-exact cases: half-value neighbours,
positive/negative ties, large odd integers, signed zero, saturation bounds,
finite overflows and infinities.
float bit patterns, including the existing conversion's special cases.
cargo test --locked -p victory-shader --lib:463 passed, 0 failed, 9 ignored.
git diff --checkpassed.too-many-arguments failures elsewhere in this crate. With just those
existing lint categories allowed on the command line, all-targets,
all-features Clippy passed. No source or CI lint settings were changed.
In-game rendering has not been measured.
Checked for overlap
#2018 corrects the opposite uint-to-float conversion, not this opcode.
@Lyrnc on GitHub · 2026-11-30 06:11 UTC · original
Checked this against a Radeon gfx1036 (RDNA 2) running the same kernels natively, and ran the translator's output for main and for this PR on an RTX 5080 and on that Radeon through D3D12.
Setup
mainat41adb551with this PR's head (4355f38) merged locally, no conflict. The victory-shader test binary was built from that merged tree (own tree and target directory).super::emit(32-lane host),dxil.dllvalidation, dispatched on the 5080 and on the Radeon (VICTORY_ADAPTER=radeon).Case
cvt_rpi_flr: 64 inputs (ties at ±0.5, ±1.5 .. ±100.5, the neighbours of 0.5, odd integers from 2^23 to 2^24 and their negatives, 2^31 and its neighbours, ±inf, ±qNaN, sNaN, ±denormal, ±max). Outputs: v10v_cvt_rpi_i32_f32 x, v11v_cvt_flr_i32_f32 x, v12v_cvt_i32_f32 x, v13v_cvt_u32_f32 x, v14v_cvt_rpi_i32_f32 -x(VOP3 neg), v15v_cvt_flr_i32_f32 |x|(VOP3 abs).Results (values that differ from the gfx1036, out of 64 lanes x 6 outputs)
rpi xflr xi32 x, v13u32 xrpi -xflr abs(x)The 8 rounding lanes main gets wrong, gfx1036 / main (v10):
With this PR all eight are exact, and so are the ties (±0.5 .. ±100.5 round up: -0.5 gives 0, -2.5 gives -2), the values next to ±0.5, ±0, the 2^31 boundary (0x4F000000 saturates to 0x7FFFFFFF, 0xCF000000 gives 0x80000000) and ±inf. Both cards give the same values.
Verdict: correct for what it changes. Every rounding lane is exact on both cards in both the plain and the
negform, and nothing else moved (flr,i32,u32give the same values as main).What still differs, both cards, main and this PR alike (outside this patch, as you say):
rpiandflr, it does not give 0. +qNaN 0x7FC00000 and sNaN 0x7F800001 give 0x7FFFFFFF, -qNaN 0xFFC00000 gives 0x80000000 (lanes 40, 42 / 41;rpi -xthe other way round;flr abs(x)0x7FFFFFFF for all three).v_cvt_i32_f32andv_cvt_u32_f32do give 0 for NaN on the hardware, so only therpi/flrpair differs. The same values with IEEE mode off.flrof -denormal 0x80000001: -1 on the gfx1036 with denormals kept, 0 with them flushed. The translation gives 0 (the host flushes), so this one depends on the console's MODE.AI use: Claude Code (Claude Opus 5.5) ran the oracle and host cases and wrote this comment.