Skip to content

Avoid double rounding in shader float-to-integer conversion - #2678

Open
masteraapjes wants to merge 1 commit into
trustno1-coder:mainfrom
masteraapjes:codex/shader-rpi-rounding
Open

masteraapjes wants to merge 1 commit into
trustno1-coder:mainfrom
masteraapjes:codex/shader-rpi-rounding

Conversation

@masteraapjes

Copy link
Copy Markdown

What it changes

v_cvt_rpi_i32_f32 adds 0.5 in f32 and then floors the result. The first
operation 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

  • Windows, Rust 1.98.1, RTX 3090 and D3D12 WARP; synthetic wave32 compute
    shaders, with no game files or vendor SDKs.
  • Before: the new GPU regression fails on hardware for 0x3effffff,
    returning 1 instead of 0.
  • After: cargo test --locked -p victory-shader --lib v_cvt_rpi_i32_f32_rounds_without_an_intermediate_f32_sum -- --nocapture
    passes. Both backends exercise 27 bit-exact cases: half-value neighbours,
    positive/negative ties, large odd integers, signed zero, saturation bounds,
    finite overflows and infinities.
  • An independent CPU model agreed with exact-sum rounding over 500,015
    float bit patterns, including the existing conversion's special cases.
  • Full shader library suite: cargo test --locked -p victory-shader --lib:
    463 passed, 0 failed, 9 ignored.
  • Staged asset policy: both changed files passed. git diff --check passed.
  • Strict Clippy has existing unused-import, immutable-condition and
    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

@github-actions github-actions Bot added the pr-fix Pull request: fixes a bug label Nov 29, 2026
@github-actions github-actions Bot changed the title [pr-fix] Avoid double rounding in shader float-to-integer conversion Avoid double rounding in shader float-to-integer conversion Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-fix Pull request: fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant