Shaders: fix v_div_fixup_f32 turning 0.0/x and x/Inf into +/-Infinity #349

Open
github-bot wants to merge 1 commit from github-bot/gh-349 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-24 23:49 UTC · original
AscilCH:fix/shader-v-div-fixup-zero-and-inf → main · +64 −21 · 2 files · 1 commits · open

Problem

In crates/victory-shader/src/emit/lower.rs, v_div_fixup_f32 ORed q_nan into make_inf and evaluated make_inf after make_zero.

In the console's IEEE f32 division sequence (v_div_scale_f32 -> v_rcp_f32 -> v_fma_f32 -> v_div_fmas_f32 -> v_div_fixup_f32):

  • 0.0 / x: v_div_scale_f32 sets r = qNaN when any_zero holds, so q_nan is true alongside n_zero.
  • x / ±Inf: v_rcp_f32(±Inf) = 0.0, then v_fma_f32(-den, 0.0, 1.0) = qNaN, so q_nan is true alongside d_inf.

Because make_inf ran after make_zero, both 0.0 / x and x / ±Inf returned ±Infinity instead of ±0.0.

Fix

  • Remove q_nan from make_inf (n_inf || d_zero) and apply select(q_nan, signed_inf, signed_q) before make_zero (d_inf || n_zero), matching the RDNA2/GFX10 V_DIV_FIXUP_F32 priority table (n_nan > d_nan > invalid > make_inf > make_zero > q_nan > signed_q).
  • Extend emit::tests::valu_div_f32_sequence_executes_on_gpu with 0.0/1.0, -0.0/1.0, 0.0/-2.0, 1.0/+Inf, -1.0/+Inf, +Inf/+Inf, and ±1e38 / 1e-38 across both hardware D3D12 and WARP (plus dxil.dll validation).

Verification

  • cargo test -p victory-shader --locked valu_div_f32_sequence_executes_on_gpu: passed (hardware D3D12 + WARP + dxil.dll)
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic tests only, no game data.)

**@⁠AscilCH** on GitHub · 2026-11-24 23:49 UTC · [original](https://github.com/fxceless/victory-fork/pull/349) `AscilCH:fix/shader-v-div-fixup-zero-and-inf` → `main` · +64 −21 · 2 files · 1 commits · open ## Problem In `crates/victory-shader/src/emit/lower.rs`, `v_div_fixup_f32` ORed `q_nan` into `make_inf` and evaluated `make_inf` *after* `make_zero`. In the console's IEEE f32 division sequence (`v_div_scale_f32` -> `v_rcp_f32` -> `v_fma_f32` -> `v_div_fmas_f32` -> `v_div_fixup_f32`): - `0.0 / x`: `v_div_scale_f32` sets `r = qNaN` when `any_zero` holds, so `q_nan` is true alongside `n_zero`. - `x / ±Inf`: `v_rcp_f32(±Inf) = 0.0`, then `v_fma_f32(-den, 0.0, 1.0) = qNaN`, so `q_nan` is true alongside `d_inf`. Because `make_inf` ran after `make_zero`, both `0.0 / x` and `x / ±Inf` returned `±Infinity` instead of `±0.0`. ## Fix - Remove `q_nan` from `make_inf` (`n_inf || d_zero`) and apply `select(q_nan, signed_inf, signed_q)` *before* `make_zero` (`d_inf || n_zero`), matching the RDNA2/GFX10 `V_DIV_FIXUP_F32` priority table (`n_nan > d_nan > invalid > make_inf > make_zero > q_nan > signed_q`). - Extend `emit::tests::valu_div_f32_sequence_executes_on_gpu` with `0.0/1.0`, `-0.0/1.0`, `0.0/-2.0`, `1.0/+Inf`, `-1.0/+Inf`, `+Inf/+Inf`, and `±1e38 / 1e-38` across both hardware D3D12 and WARP (plus `dxil.dll` validation). ## Verification - `cargo test -p victory-shader --locked valu_div_f32_sequence_executes_on_gpu`: passed (hardware D3D12 + WARP + `dxil.dll`) - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic tests only, no game data.)*
In `Lower::valu` for `v_div_fixup_f32`, `q_nan` was ORed into `make_inf`
and applied after `make_zero`. Because `v_div_scale_f32` outputs `qNaN`
whenever either operand is zero (`0.0 / x`), and `v_fma_f32(-Inf, 0, 1)`
produces `qNaN` when the denominator is infinite (`x / +/-Inf`), both
`make_zero` and `q_nan` are true for those cases — causing `make_inf` to
overwrite `+/-0.0` with `+/-Infinity`.

Match the RDNA2/GFX10 `V_DIV_FIXUP_F32` priority order: apply
`q_nan -> signed_inf` below `make_zero` (`d_inf || n_zero`) so `0.0 / x`
and `x / +/-Inf` evaluate to signed zero while finite quotient overflow
(`q_nan` with finite non-zero inputs) still produces signed infinity.

Extend `valu_div_f32_sequence_executes_on_gpu` to test `0/1`, `-0/1`,
`0/-2`, `1/+Inf`, `-1/+Inf`, `+Inf/+Inf`, and `+/-1e38 / 1e-38` on both
hardware D3D12 and WARP.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:41:24 +00:00
Author
Owner

@⁠cafeoh on GitHub · 2026-11-25 12:15 UTC · original

Tested in the game: this fixes a visible bug.

Setup: RTX 2080 Ti, Windows 10, diagnostics build of fork f85101b plus other open PRs, warm shader cache.

  • Before: in the prologue, from the bar scene on, the lens effect that softens and colour-splits the screen edges also covered a hard-edged circle in the middle of the screen. It was there in every run (8 runs).
  • After merging this PR (no other change): the circle is gone, and the effect at the edges is unchanged.

Guess at why: the effect's strength looks like max(r - r0, 0) / width, so inside r0 it is 0 / x, which came out as infinity.

Not tested: a second run, other scenes, other cards.

AI-assisted (Claude Code); the picture was checked by a person.

**@⁠cafeoh** on GitHub · 2026-11-25 12:15 UTC · [original](https://github.com/fxceless/victory-fork/pull/349#issuecomment-7162140118) Tested in the game: this fixes a visible bug. Setup: RTX 2080 Ti, Windows 10, diagnostics build of fork `f85101b` plus other open PRs, warm shader cache. - Before: in the prologue, from the bar scene on, the lens effect that softens and colour-splits the screen edges also covered a hard-edged circle in the middle of the screen. It was there in every run (8 runs). - After merging this PR (no other change): the circle is gone, and the effect at the edges is unchanged. Guess at why: the effect's strength looks like `max(r - r0, 0) / width`, so inside `r0` it is `0 / x`, which came out as infinity. Not tested: a second run, other scenes, other cards. AI-assisted (Claude Code); the picture was checked by a person.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/349/head:github-bot/gh-349'
git switch 'github-bot/gh-349'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-349'
git switch 'github-bot/gh-349'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-349'
git switch 'github-bot/gh-349'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-349'
git switch 'main'
git merge --squash 'github-bot/gh-349'
git switch 'main'
git merge --ff-only 'github-bot/gh-349'
git switch 'main'
git merge 'github-bot/gh-349'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.