shader: use bitwise IUGE in scrub_nan and zero NaN in clamp_hdr #2432

Open
qlfu wants to merge 2 commits from upstream-pr/2432 into main
qlfu commented 2026-11-29 12:59:58 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-29T12:59:58Z.


What it changes Fixes two NaN-handling bugs on pixel float colour outputs and float image stores in crates/victory-shader/src/emit/lower.rs: 1. Bitwise NaN/Inf check in Lower::scrub_nan (VICTORY_NAN_SCRUB=1): Replaces FP fcmp uge(fabs(f), +inf) with integer (as_int(f) & 0x7FFF_FFFF) >= 0x7F80_0000 (pred::IUGE), matching is_nan_f32 directly below it so NVIDIA fast-math cannot fold away NaN detection while still catching both NaN and +/-Inf. 2. Zero NaN in Lower::clamp_hdr (VICTORY_HDR_CLAMP): Changes the NaN branch from max (+65024.0) to 0.0 so a NaN channel in a pixel shader colour export does not turn into a blazing +65024.0 neon HDR flash when VICTORY_NAN_SCRUB is off or bypassed. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): unit/compiler verification on Windows x64 (RTX 4070 Ti host); no gameplay FPS claims for this correctness fix. - For shader changes, the wave sizes tested (32 and/or 64 lanes): applies identically to Wave32/Wave64 pixel & compute float outputs; new unit test emit::lower::tests::scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero checks emitted bitcode predicates (pred::IUGE, no pred::UGE) and select operands (0.0 instead of 65024.0). - cargo test passed for the crates it touches: yes (cargo test -p victory-shader --lib --locked lower::tests -- --test-threads=1: 26 passed, 0 failed, 1 ignored; cargo clippy -p victory-shader --lib --tests --locked: 0 new warnings). ## Checked for overlap - [x] I searched open pull requests for scrub_nan, clamp_hdr, HDR_CLAMP, and NAN_SCRUB; no overlapping open PR found.

Fixes #2057

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5550945247 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-29T12:59:58Z. --- ## What it changes Fixes two NaN-handling bugs on pixel float colour outputs and float image stores in `crates/victory-shader/src/emit/lower.rs`: 1. **Bitwise NaN/Inf check in `Lower::scrub_nan` (`VICTORY_NAN_SCRUB=1`):** Replaces FP `fcmp uge(fabs(f), +inf)` with integer `(as_int(f) & 0x7FFF_FFFF) >= 0x7F80_0000` (`pred::IUGE`), matching `is_nan_f32` directly below it so NVIDIA fast-math cannot fold away NaN detection while still catching both `NaN` and `+/-Inf`. 2. **Zero NaN in `Lower::clamp_hdr` (`VICTORY_HDR_CLAMP`):** Changes the NaN branch from `max` (`+65024.0`) to `0.0` so a NaN channel in a pixel shader colour export does not turn into a blazing `+65024.0` neon HDR flash when `VICTORY_NAN_SCRUB` is off or bypassed. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): unit/compiler verification on Windows x64 (RTX 4070 Ti host); no gameplay FPS claims for this correctness fix. - For shader changes, the wave sizes tested (32 and/or 64 lanes): applies identically to Wave32/Wave64 pixel & compute float outputs; new unit test `emit::lower::tests::scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero` checks emitted bitcode predicates (`pred::IUGE`, no `pred::UGE`) and `select` operands (`0.0` instead of `65024.0`). - `cargo test` passed for the crates it touches: yes (`cargo test -p victory-shader --lib --locked lower::tests -- --test-threads=1`: 26 passed, 0 failed, 1 ignored; `cargo clippy -p victory-shader --lib --tests --locked`: 0 new warnings). ## Checked for overlap - [x] I searched open pull requests for `scrub_nan`, `clamp_hdr`, `HDR_CLAMP`, and `NAN_SCRUB`; no overlapping open PR found. Fixes #2057
Dukestar69 commented 2026-11-29 13:05:11 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-29T13:05:11Z.

Review: APPROVED


Tested locally merged onto main (829188e9).

  • Merge: Clean merge across bitcode.rs, lower.rs, and lower/tests.rs.
  • Tests (RTX 5090):
    • cargo test -p victory-shader --lib emit::tests:: passed (26 passed; 0 failed; 1 ignored).
    • cargo test -p victory-shader --lib emit::lower::tests:: passed (26 passed; 0 failed; 1 ignored, including the new scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero test).
  • 0 new warnings.

Clean bitwise IUGE NaN/Inf scrubbing and NaN-safe HDR clamping. LGTM.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6102995776 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#pullrequestreview-6102995776). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-29T13:05:11Z. **Review:** APPROVED --- Tested locally merged onto `main` (`829188e9`). - **Merge**: Clean merge across `bitcode.rs`, `lower.rs`, and `lower/tests.rs`. - **Tests (RTX 5090)**: - `cargo test -p victory-shader --lib emit::tests::` passed (`26 passed; 0 failed; 1 ignored`). - `cargo test -p victory-shader --lib emit::lower::tests::` passed (`26 passed; 0 failed; 1 ignored`, including the new `scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero` test). - 0 new warnings. Clean bitwise `IUGE` NaN/Inf scrubbing and NaN-safe HDR clamping. LGTM. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fainty0 commented 2026-11-29 14:45:55 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by fainty0 on 2026-11-29T14:45:55Z.

Review: CHANGES_REQUESTED


Review: Request changes (coverage)

The semantics look right: both wave widths share the same lowering. The masked unsigned >= in scrub_nan scrubs infinities and NaNs; the > in clamp_hdr zeroes only NaNs. FTZ doesn't change classification. DXIL min/max alone would yield −65024 for NaN, so the fix is needed.

However, the test at crates/victory-shader/src/emit/lower/tests.rs:1184 passes even without the sign mask. Please make it check the select condition against signed NaNs, infinities and subnormals.

Checks: Linux shader tests 295 → 296 passed. Windows Clippy unchanged (workspace 5 → 5 / 4 → 4; shader-only 3 → 3 / 4 → 4). No asset violations. NVIDIA driver behaviour and runtime not verified.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6103365332 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#pullrequestreview-6103365332). Originally posted by [fainty0](https://github.com/fainty0) on 2026-11-29T14:45:55Z. **Review:** CHANGES_REQUESTED --- ## Review: Request changes (coverage) The semantics look right: both wave widths share the same lowering. The masked unsigned `>=` in `scrub_nan` scrubs infinities and NaNs; the `>` in `clamp_hdr` zeroes only NaNs. FTZ doesn't change classification. [DXIL min/max](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#fmax) alone would yield −65024 for NaN, so the fix is needed. However, the test at `crates/victory-shader/src/emit/lower/tests.rs:1184` passes even without the sign mask. Please make it check the select condition against signed NaNs, infinities and subnormals. **Checks:** Linux shader tests 295 → 296 passed. Windows Clippy unchanged (workspace 5 → 5 / 4 → 4; shader-only 3 → 3 / 4 → 4). No asset violations. NVIDIA driver behaviour and runtime not verified.
remie1529 commented 2026-11-29 14:56:12 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by remie1529 on 2026-11-29T14:56:12Z.


Tested on RTX 4070 (driver 616.92, Windows 11, Series X 2560x1440, DLSS Performance, DLSS FG 2x, release build on \829188e). Built with this PR applied (unit test \scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero\ passed) and the white single-pixel HDR sparkles indoors are gone.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7257690741 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#issuecomment-7257690741). Originally posted by [remie1529](https://github.com/remie1529) on 2026-11-29T14:56:12Z. --- Tested on RTX 4070 (driver 616.92, Windows 11, Series X 2560x1440, DLSS Performance, DLSS FG 2x, release build on \829188e\). Built with this PR applied (unit test \scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero\ passed) and the white single-pixel HDR sparkles indoors are gone.
dyshay commented 2026-11-29 15:40:13 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by dyshay on 2026-11-29T15:40:13Z.


Merged into our fork's integration branch for testing (dyshay/VIctory-NVIDIA, branch janus-prs). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with --profile diag --features diagnostics, and the CPU tests of the touched crates pass. Runs on an RTX 4090 are queued (Series X 1440p, and Series S with ray tracing, DLSS Quality). Frame times will follow here.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7258272369 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#issuecomment-7258272369). Originally posted by [dyshay](https://github.com/dyshay) on 2026-11-29T15:40:13Z. --- Merged into our fork's integration branch for testing ([dyshay/VIctory-NVIDIA](https://github.com/dyshay/VIctory-NVIDIA/tree/janus-prs), branch `janus-prs`). I read the full diff first: no network access, process spawning, binaries, build or dependency changes, or game data. It builds with `--profile diag --features diagnostics`, and the CPU tests of the touched crates pass. Runs on an RTX 4090 are queued (Series X 1440p, and Series S with ray tracing, DLSS Quality). Frame times will follow here.
Robotniq commented 2026-11-29 21:56:39 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Robotniq on 2026-11-29T21:56:39Z.


Tested in-game: this fixes #2057 (red speckles on the police officers' dark uniforms in the opening). I bisected that issue to #534's clamp_hdr, which turns a NaN into +65024.

RTX 3070 Ti 8 GB, Windows 11, Series S profile, DLSS Quality, scripts/play-foxhound.ps1 (so VICTORY_NAN_SCRUB is off). Diagnostics build for the frame captures, with the build fix from #2974's first item. Same save and the same two shots (the officers by the car, then in the coffee shop):

  • f889bb07: speckles in both shots.
  • This PR merged into fbe3dc20: gone in both.
  • The clamp_hdr change alone (NaN to 0.0) on f889bb07: gone too.

If you add Fixes #2057 to the description, that issue closes when this is merged.

Measured with AI assistance (Claude Code); the runs were made on my machine.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7263278207 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#issuecomment-7263278207). Originally posted by [Robotniq](https://github.com/Robotniq) on 2026-11-29T21:56:39Z. --- Tested in-game: this fixes #2057 (red speckles on the police officers' dark uniforms in the opening). I bisected that issue to #534's `clamp_hdr`, which turns a NaN into +65024. RTX 3070 Ti 8 GB, Windows 11, Series S profile, DLSS Quality, `scripts/play-foxhound.ps1` (so `VICTORY_NAN_SCRUB` is off). Diagnostics build for the frame captures, with the build fix from #2974's first item. Same save and the same two shots (the officers by the car, then in the coffee shop): - f889bb07: speckles in both shots. - This PR merged into fbe3dc20: gone in both. - The `clamp_hdr` change alone (NaN to 0.0) on f889bb07: gone too. If you add `Fixes #2057` to the description, that issue closes when this is merged. Measured with AI assistance (Claude Code); the runs were made on my machine.
qlfu commented 2026-11-29 23:54:48 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-29T23:54:48Z.


Rebased onto 41adb551 and updated the unit test per @fainty0's review: eval_i32_cond now evaluates the emitted scrub_nan and clamp_hdr select conditions directly across signed zeros (±0.0), signed subnormals (±0x0000_0001, ±0x007F_FFFF), finite extremes (±f32::MAX), ±Inf, and signed sNaN/qNaN payloads (fails immediately if the 0x7FFF_FFFF sign mask is dropped). Also added Fixes #2057 per @Robotniq's confirmation.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7264821065 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#issuecomment-7264821065). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-29T23:54:48Z. --- Rebased onto `41adb551` and updated the unit test per @fainty0's review: `eval_i32_cond` now evaluates the emitted `scrub_nan` and `clamp_hdr` select conditions directly across signed zeros (`±0.0`), signed subnormals (`±0x0000_0001`, `±0x007F_FFFF`), finite extremes (`±f32::MAX`), `±Inf`, and signed sNaN/qNaN payloads (fails immediately if the `0x7FFF_FFFF` sign mask is dropped). Also added `Fixes #2057` per @Robotniq's confirmation.
qlfu commented 2026-11-30 14:30:29 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by qlfu on 2026-11-30T14:30:29Z.


Updated scrub_nan_value and pixel colour export ordering based on live gameplay testing with VICTORY_NAN_SCRUB=1:

  • Previously, scrub_nan ran before clamp_hdr and mapped both NaN and ±Inf (IUGE 0x7F80_0000) to 0.0, turning bright HDR light highlights into black spots on lit surfaces.
  • scrub_nan_value now distinguishes NaN (IUGT 0x7F80_0000 -> 0.0) from ±Inf (EQ 0x7F80_0000 -> ±HDR_MAX preserving sign).
  • Pixel shader colour exports now call clamp_hdr directly (which clamps ±Inf/overflows to [-HDR_MAX, HDR_MAX], maps NaN to 0.0, and falls back to scrub_nan when VICTORY_HDR_CLAMP=0), eliminating the redundant pass.
  • Verified with victory-shader unit tests and clean merge-tree onto 41adb551.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7281483211 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2432#issuecomment-7281483211). Originally posted by [qlfu](https://github.com/qlfu) on 2026-11-30T14:30:29Z. --- Updated `scrub_nan_value` and pixel colour export ordering based on live gameplay testing with `VICTORY_NAN_SCRUB=1`: - Previously, `scrub_nan` ran before `clamp_hdr` and mapped both `NaN` and `±Inf` (`IUGE 0x7F80_0000`) to `0.0`, turning bright HDR light highlights into black spots on lit surfaces. - `scrub_nan_value` now distinguishes `NaN` (`IUGT 0x7F80_0000` -> `0.0`) from `±Inf` (`EQ 0x7F80_0000` -> `±HDR_MAX` preserving sign). - Pixel shader colour exports now call `clamp_hdr` directly (which clamps `±Inf`/overflows to `[-HDR_MAX, HDR_MAX]`, maps `NaN` to `0.0`, and falls back to `scrub_nan` when `VICTORY_HDR_CLAMP=0`), eliminating the redundant pass. - Verified with `victory-shader` unit tests and clean `merge-tree` onto `41adb551`.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2432:upstream-pr/2432'
git switch 'upstream-pr/2432'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2432'
git switch 'upstream-pr/2432'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2432'
git switch 'upstream-pr/2432'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2432'
git switch 'main'
git merge --squash 'upstream-pr/2432'
git switch 'main'
git merge --ff-only 'upstream-pr/2432'
git switch 'main'
git merge 'upstream-pr/2432'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
trustno1-coder/VIctory-NVIDIA!2432
No description provided.