shader: avoid duplicate NaN check when NAN_SCRUB and HDR_CLAMP are both active #439

Open
Paliton wants to merge 2 commits from Paliton/Darner-fork:pr/shader-dedup-scrub-clamp into Main
First-time contributor

Contributor role (optional)

  • Fluxer ID:

What it changes

  • The problem on main: read from the code (crates/darner-shader/src/emit/lower.rs). On every pixel shader float colour export, emit_export called { let f = self.scrub_nan(f); self.clamp_hdr(f) }. In play mode both DARNER_NAN_SCRUB=1 and DARNER_HDR_CLAMP (default on) are enabled, so scrub_nan_value already replaced NaN/Inf with 0.0 via integer bit-test + select, and then clamp_hdr_value ran a second redundant is_nan_f32 bitcast/mask/compare/select on the already-finite value for every RGBA channel.

Replaces the chained call with scrub_and_clamp_colour(f): when both nan_scrub() and hdr_clamp() are active, it runs scrub_nan_value(f) once followed by FMax/FMin clamping to [-HDR_MAX, HDR_MAX]. When only one or neither switch is active, behaviour is identical to before.

How it was tested

  • Tested revision: 7e24699 (based on f3840d7)

  • Commands and results:

    • cargo xtask policy -> asset policy: 555 files ok
    • cargo clippy --workspace --all-targets --locked -- -D warnings -> ok (0 warnings)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings -> ok (0 warnings)
    • cargo test -p darner-shader --lib --locked scrub -- --test-threads=1 -> 1 passed; 0 failed (scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero)
    • cargo test --workspace --locked -> all non-flaky tests pass (1772/1773 in darner-gpu; the single failure is the pre-existing gpu_owned::tests::concurrent_faults_on_same_block_serialize_single_readback flake fixed by #261)
  • Changes a default: yes (eliminates 4 redundant DXIL instructions per pixel colour export channel when both switches are on; bit-exact equivalence).

  • A/B result: 2 runs each of 240 s automated opening sequence from the same snapshot, --profile diag --features diagnostics (generic x86-64 build), pure play-defaults (DARNER_NAN_SCRUB=1 and default DARNER_HDR_CLAMP on), RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Both B runs beat both A runs across Window 2 and Window 3:

    Metric (2 runs each) A (off, f3840d7) B (on, 7e24699)
    Total frames (240 s) 5,367 / 5,537 (mean 5,452) 5,486 / 5,554 (mean 5,520, +68)
    Window 1 (90-130 s) fps / med ms 21.23 / 22.43 fps (46.42 / 44.97 ms) 21.43 / 22.87 fps (44.70 / 43.45 ms)
    Window 2 (130-180 s) fps / med ms 21.24 / 21.86 fps (45.77 / 44.46 ms) 22.06 / 22.55 fps (44.12 / 43.24 ms)
    Window 3 (180-235 s) fps (mean) 21.52 / 22.16 (21.84 fps) 22.26 / 22.71 (22.49 fps, +0.65)
    Window 3 median frame time 45.26 / 43.89 (44.58 ms) 43.70 / 42.90 (43.30 ms, -1.28 ms)
    Window 3 p95 / p99 frame time 47.1 / 94.3 ms (mean) 45.7 / 88.3 ms (mean, p99 -6.0 ms)
    Window 3 GPU (card_lists_ms) 43.63 / 42.43 (43.03 ms) 42.14 / 41.37 (41.76 ms, -1.28 ms)
    Host pipeline library size 167,601,528 B 167,129,078 B (-472 KB)
  • Gates run on that revision: all three AGENTS.md gates + cargo xtask policy on 7e24699.

  • Gameplay, when relevant: RTX 5070 12 GB, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Clean visuals with zero NaN/HDR artefacts.

  • Shader wave sizes tested, when relevant: 32 lanes (RTX 5070).

  • Hardware dependence: general (translator IR only).

  • Coverage gaps: tested on NVIDIA RTX 5070 (32-lane) only; untested on AMD/Intel or 64-lane wave size.

Checklist

  • I ran this change in the game, or say below why it can't be (docs, tests only).
  • This PR contains one change based on main.
  • The diff follows docs/asset-policy.md.
  • I recorded this PR's dependencies below.
    • Depends on: none

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches.
  • Related PRs: #425, #422, #394, #304, #292, #223 (also touch crates/darner-shader/src/emit/lower.rs, non-overlapping functions)
  • Replaces or overlaps: none found
## Contributor role (optional) - Fluxer ID: ## What it changes - The problem on main: read from the code (`crates/darner-shader/src/emit/lower.rs`). On every pixel shader float colour export, `emit_export` called `{ let f = self.scrub_nan(f); self.clamp_hdr(f) }`. In play mode both `DARNER_NAN_SCRUB=1` and `DARNER_HDR_CLAMP` (default on) are enabled, so `scrub_nan_value` already replaced NaN/Inf with `0.0` via integer bit-test + select, and then `clamp_hdr_value` ran a second redundant `is_nan_f32` bitcast/mask/compare/select on the already-finite value for every RGBA channel. Replaces the chained call with `scrub_and_clamp_colour(f)`: when both `nan_scrub()` and `hdr_clamp()` are active, it runs `scrub_nan_value(f)` once followed by `FMax`/`FMin` clamping to `[-HDR_MAX, HDR_MAX]`. When only one or neither switch is active, behaviour is identical to before. ## How it was tested - Tested revision: `7e24699` (based on `f3840d7`) - Commands and results: - `cargo xtask policy` -> `asset policy: 555 files ok` - `cargo clippy --workspace --all-targets --locked -- -D warnings` -> ok (0 warnings) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` -> ok (0 warnings) - `cargo test -p darner-shader --lib --locked scrub -- --test-threads=1` -> `1 passed; 0 failed` (`scrub_nan_uses_bitwise_iuge_and_clamp_hdr_replaces_nan_with_zero`) - `cargo test --workspace --locked` -> all non-flaky tests pass (`1772/1773` in `darner-gpu`; the single failure is the pre-existing `gpu_owned::tests::concurrent_faults_on_same_block_serialize_single_readback` flake fixed by #261) - Changes a default: yes (eliminates 4 redundant DXIL instructions per pixel colour export channel when both switches are on; bit-exact equivalence). - A/B result: 2 runs each of 240 s automated opening sequence from the same snapshot, `--profile diag --features diagnostics` (generic x86-64 build), pure `play-defaults` (`DARNER_NAN_SCRUB=1` and default `DARNER_HDR_CLAMP` on), RTX 5070 12 GB + i7-14700KF, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Both B runs beat both A runs across Window 2 and Window 3: | Metric (2 runs each) | A (off, `f3840d7`) | B (on, `7e24699`) | |---|---|---| | Total frames (240 s) | 5,367 / 5,537 (mean 5,452) | 5,486 / 5,554 (mean **5,520**, +68) | | Window 1 (90-130 s) fps / med ms | 21.23 / 22.43 fps (46.42 / 44.97 ms) | 21.43 / 22.87 fps (44.70 / 43.45 ms) | | Window 2 (130-180 s) fps / med ms | 21.24 / 21.86 fps (45.77 / 44.46 ms) | 22.06 / 22.55 fps (44.12 / 43.24 ms) | | Window 3 (180-235 s) fps (mean) | 21.52 / 22.16 (**21.84 fps**) | 22.26 / 22.71 (**22.49 fps**, +0.65) | | Window 3 median frame time | 45.26 / 43.89 (**44.58 ms**) | 43.70 / 42.90 (**43.30 ms**, -1.28 ms) | | Window 3 p95 / p99 frame time | 47.1 / 94.3 ms (mean) | 45.7 / 88.3 ms (mean, p99 -6.0 ms) | | Window 3 GPU (`card_lists_ms`) | 43.63 / 42.43 (**43.03 ms**) | 42.14 / 41.37 (**41.76 ms**, -1.28 ms) | | Host pipeline library size | 167,601,528 B | 167,129,078 B (-472 KB) | - Gates run on that revision: all three `AGENTS.md` gates + `cargo xtask policy` on `7e24699`. - Gameplay, when relevant: RTX 5070 12 GB, Series X, 1440p -> 4K, DLSS off, FG off, RT off. Clean visuals with zero NaN/HDR artefacts. - Shader wave sizes tested, when relevant: 32 lanes (RTX 5070). - Hardware dependence: general (translator IR only). - Coverage gaps: tested on NVIDIA RTX 5070 (32-lane) only; untested on AMD/Intel or 64-lane wave size. ## Checklist - [x] I ran this change in the game, or say below why it can't be (docs, tests only). - [x] This PR contains one change based on `main`. - [x] The diff follows `docs/asset-policy.md`. - [x] I recorded this PR's dependencies below. - Depends on: none ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. - Related PRs: #425, #422, #394, #304, #292, #223 (also touch `crates/darner-shader/src/emit/lower.rs`, non-overlapping functions) - Replaces or overlaps: none found
Contributor

Test report on an RTX 4080, together with #438: no measurable change in card time here.

PC: RTX 4080 16 GB (driver 621.40), i7-13700K, 64 GB, Windows 11 26300. Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on, Reflex off.
Builds: A = main 47d21cd + #334, #337, #457, #274, #397, #312, #304, #303. B = A + #438 (9483e65) + #439 (77add49); both change the translations, so B had two warm-up runs first (the process building pipelines at about 22 logical processors) and the table has the third.
Method: the opening scene of one save, entered the same way every run (no input after the start screen); medians of per-second values from the host log in three 45-second windows from the moment the world appears.

A (warm) B (warm)
card time in the title's lists, ms a frame 23.5 / 23.5 / 23.5 23.4 / 23.3 / 23.4
frame time, ms 26.2 / 25.8 / 25.7 25.6 / 25.5 / 25.6

0.1 to 0.2 ms, within the run-to-run spread (about 0.3 ms); the driver's compiler may already fold the repeated handles and checks. Picture 60 s in matches A; no errors in the log besides Streamline's presentCommon() was not observed once at start, which A logs too.

Not tested: each PR on its own, more than one run each, other cards.

Test report on an RTX 4080, together with #438: no measurable change in card time here. **PC:** RTX 4080 16 GB (driver 621.40), i7-13700K, 64 GB, Windows 11 26300. Series X profile, 1440p with DLSS Quality, frame generation off, hardware ray tracing on, Reflex off. **Builds:** A = main 47d21cd + #334, #337, #457, #274, #397, #312, #304, #303. B = A + #438 (9483e65) + #439 (77add49); both change the translations, so B had two warm-up runs first (the process building pipelines at about 22 logical processors) and the table has the third. **Method:** the opening scene of one save, entered the same way every run (no input after the start screen); medians of per-second values from the host log in three 45-second windows from the moment the world appears. | | A (warm) | B (warm) | |---|---|---| | card time in the title's lists, ms a frame | 23.5 / 23.5 / 23.5 | 23.4 / 23.3 / 23.4 | | frame time, ms | 26.2 / 25.8 / 25.7 | 25.6 / 25.5 / 25.6 | 0.1 to 0.2 ms, within the run-to-run spread (about 0.3 ms); the driver's compiler may already fold the repeated handles and checks. Picture 60 s in matches A; no errors in the log besides Streamline's `presentCommon() was not observed` once at start, which A logs too. Not tested: each PR on its own, more than one run each, other cards.
First-time contributor

Merged onto c8258f9, passed cargo xtask policy, clippy --all-features --all-targets -- -D warnings (0 warnings), and 1,819 darner-gpu + 219 darner unit tests, then verified through a full 16-minute release shader preload (DARNER_RDNA_FLOAT=2) and two 30,000+ frame Series X sessions (DLSS Quality + MFG 3x) on RTX 5070 Ti / 64 GB DDR5-6400 with zero color/NaN artifacts, crashes, or shader regressions.

Merged onto `c8258f9`, passed `cargo xtask policy`, `clippy --all-features --all-targets -- -D warnings` (0 warnings), and `1,819 darner-gpu` + `219 darner` unit tests, then verified through a full 16-minute release shader preload (`DARNER_RDNA_FLOAT=2`) and two 30,000+ frame Series X sessions (DLSS Quality + MFG 3x) on RTX 5070 Ti / 64 GB DDR5-6400 with zero color/NaN artifacts, crashes, or shader regressions.
TrustNo1 changed title from [pr-performance][shaders] shader: avoid duplicate NaN check when NAN_SCRUB and HDR_CLAMP are both active to shader: avoid duplicate NaN check when NAN_SCRUB and HDR_CLAMP are both active 2026-12-06 03:29:02 +00:00
Owner

Test needed — commit 77add497; what to check:

  • The problem on main: read from the code (crates/darner-shader/src/emit/lower.rs). On every pixel shader float colour export, emit_export called { let f = self.scrub_nan(f); self.clamp_hdr(f) }. In play mode both DARNER_NAN_SCRUB=1 and DARNER_HDR_CLAMP (default on) are enabled, so scrub_nan_value already replaced NaN/Inf with 0.0 via integer bit-test + select, and then clamp_hdr_value ran a second redundant is_nan_f32 bitcast/mask/compare/select on the already-finite value for every RGBA channel.

Replaces the chained call with scrub_and_clamp_colour(f): when both nan_scrub() and hdr_clamp() are active, it runs scrub_nan_value(f) once followed by FMax/FMin clamping to [-HDR_MAX, HDR_MAX]. When only one or neither switch is active, behaviour is identical to before.

Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture.

Reply testing to take this run; it's free again after 24 h with no results.

**Test needed** — commit `77add497`; what to check: - The problem on main: read from the code (`crates/darner-shader/src/emit/lower.rs`). On every pixel shader float colour export, `emit_export` called `{ let f = self.scrub_nan(f); self.clamp_hdr(f) }`. In play mode both `DARNER_NAN_SCRUB=1` and `DARNER_HDR_CLAMP` (default on) are enabled, so `scrub_nan_value` already replaced NaN/Inf with `0.0` via integer bit-test + select, and then `clamp_hdr_value` ran a second redundant `is_nan_f32` bitcast/mask/compare/select on the already-finite value for every RGBA channel. Replaces the chained call with `scrub_and_clamp_colour(f)`: when both `nan_scrub()` and `hdr_clamp()` are active, it runs `scrub_nan_value(f)` once followed by `FMax`/`FMin` clamping to `[-HDR_MAX, HDR_MAX]`. When only one or neither switch is active, behaviour is identical to before. Evidence to post: card + driver, settings, commit tested, before/after (switch off vs on, two runs each), fps median and p99, and one line saying you looked at the picture. Reply `testing` to take this run; it's free again after 24 h with no results.
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 'pr/shader-dedup-scrub-clamp:Paliton-pr/shader-dedup-scrub-clamp'
git switch 'Paliton-pr/shader-dedup-scrub-clamp'

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 'Paliton-pr/shader-dedup-scrub-clamp'
git switch 'Paliton-pr/shader-dedup-scrub-clamp'
git rebase 'Main'
git switch 'Main'
git merge --ff-only 'Paliton-pr/shader-dedup-scrub-clamp'
git switch 'Paliton-pr/shader-dedup-scrub-clamp'
git rebase 'Main'
git switch 'Main'
git merge --no-ff 'Paliton-pr/shader-dedup-scrub-clamp'
git switch 'Main'
git merge --squash 'Paliton-pr/shader-dedup-scrub-clamp'
git switch 'Main'
git merge --ff-only 'Paliton-pr/shader-dedup-scrub-clamp'
git switch 'Main'
git merge 'Paliton-pr/shader-dedup-scrub-clamp'
git push origin 'Main'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
TrustNo1/Darner!439
No description provided.