Skip to content

Shaders: NaN guard on colour exports and typed stores (VICTORY_NAN_GUARD) - #837

Open
PastiiJay wants to merge 2 commits into
fxceless:mainfrom
PastiiJay:amd-nan-guard
Open

PastiiJay wants to merge 2 commits into
fxceless:mainfrom
PastiiJay:amd-nan-guard

Conversation

@PastiiJay

@PastiiJay PastiiJay commented Nov 25, 2026 •

Copy link
Copy Markdown

Asked for on #3: this ports the NaN guard from KLBF Lab, our AMD-focused build (commit 0e5f4b2, test builds in KLBF Lab Beta), onto current main, on its own.

What it does. A float a pixel program exports to a colour target has NaN turned into 0 and an infinity into ±65504. Typed image and buffer stores get the same treatment, but only for the NaN the GPU itself produces (0x7FC00000, either sign) and infinities, so integer bits written through a typed store pass untouched. Raw buffer stores are left alone (there is no type to go by). The idea: a NaN produced by a 32-lane program running on a 64-lane AMD wave can no longer stay in a temporal buffer the title blends into every frame and spread as purple.

Switch. VICTORY_NAN_GUARD, on by default; VICTORY_NAN_GUARD=0 turns it off. On current main the translation-cache key picks up every "VICTORY_*" literal in victory-shader through build.rs, so the switch joins the key without editing a list (the manual list mentioned on #3 is no longer there), and shader_cache.rs is unchanged.

Measured. On an RX 9070 XT, by us and by IxMxAMAR (see #3; related: #318). This was the change that removed the remaining purple in both cases.

Caveats. This probably hides the symptom rather than fixing the root cause (whatever produces the NaN in the first place), so it may be worth keeping only until that is found. We have a single machine, so results on other cards are unknown.

Checks. cargo build --release, cargo clippy (no new warnings), cargo test --release -p victory-shader: 215 passed, 0 failed.

A float a pixel program exports to a colour target gets NaN as 0 and an
infinity as +-65504; typed image and buffer stores get the same for the NaN
the GPU makes (0x7FC00000) and infinities, so integer bits pass. One bad pixel
no longer stays in a history blended into every frame. VICTORY_NAN_GUARD=0
turns it off; part of the shader cache fingerprint.
@avt0m4t48

Copy link
Copy Markdown

I independently checked 23da1ac38c9826d07bd3e14f8d415f5c037953c5 against its parent and the listed base, 9376fd60c0ba0e76a74c39b49b1407dbac0a64c7. I inspected 80 scalar operands in the emitted DXIL from 20 original synthetic shaders.

  • Float color exports zeroed every tested NaN payload and saturated ±infinity to ±65504. Typed image and buffer stores each changed only four of the 16 input patterns: 7fc00000, ffc00000, 7f800000, and ff800000. Noncanonical NaNs, signed zeros, subnormals, and finite controls passed through. Unsigned color exports and raw buffer stores preserved all 16 patterns.
  • The typed path cannot distinguish those four patterns from integer payloads passed through a float view. For example, a typed store carrying the integer word 0x7fc00000 now emits zero. The four exceptions are worth spelling out in the PR description because they qualify the statement that integer bits pass through.
  • With VICTORY_NAN_GUARD=0, all 20 emitted containers were byte-identical to base. The translation fingerprint contained the switch and distinguished default from opt-out.
  • The untouched Linux shader suite passed at both revisions (171 passed, 2 ignored). The untouched head passed cargo check --locked --offline -p victory-shader --tests --target x86_64-pc-windows-msvc. The operand checks used LLVM disassembly. Windows execution, Microsoft DXIL validation, and GPU readback remain unverified.

AI-assisted using Codex.

@eazyves

eazyves commented Nov 25, 2026

Copy link
Copy Markdown

Two notes; I haven't run this in the game.

From reading the diff, integer bits do not always pass untouched. Typed stores all go out as textureStore.f32 / bufferStore.f32 here, whatever the view's format, so nan_guard(_, false) also sees integers. By its arithmetic, these four 32-bit values change:

stored bits become
0x7F800000 0x477FE000
0xFF800000 0xC77FE000
0x7FC00000, 0xFFC00000 0

They can be ordinary data: 0xFF800000 as packed RGBA8 is an opaque mid blue. If a uint or packed store reaches this path, it would be a wrong value rather than a NaN. I did not check whether the title's stores do. If the format isn't known at that point, limiting the guard to the colour exports (strict) would avoid it.

Tests on an RX 9070 XT (driver 32.0.32029.2010, Windows 11 26200), 23da1ac: cargo test --workspace --locked --no-fail-fast gives 539 passed, 1 failed, 10 ignored, with the hardware device in use. The failure is modules::display::tests::sdr_and_display_deferrals (0x80070032), which fails on main here too because I run from a service session. The PR adds no test of the guard itself.

AI-assisted (Claude Code); run on my machine.

…; colour exports stay guarded by default

Typed stores go out as .f32 whatever the view's format, so the integer words
0x7F800000, 0xFF800000, 0x7FC00000 and 0xFFC00000 were changed too (found in
review by avt0m4t48 and eazyves). Until the view's format is known at that point,
only float colour exports are guarded by default.
@PastiiJay

Copy link
Copy Markdown
Author

Thank you both @avt0m4t48 and @eazyves, very careful reviews.

You are right: typed stores go out as .f32 whatever the view's format, so the four words 0x7F800000, 0xFF800000, 0x7FC00000 and 0xFFC00000 were changed even when they were integer data. Pushed 45c5924:

  • by default only float colour exports are guarded (the strict path, where the target is known to be float);
  • typed stores are guarded only with VICTORY_NAN_GUARD=stores, with the four changed words spelled out in the code comment;
  • VICTORY_NAN_GUARD=0 is unchanged (off, byte-identical output).

We will re-run our AMD tests with the default and with stores to check whether the colour-export guard alone keeps the image clean, and report back here.

@avt0m4t48

Copy link
Copy Markdown

I reran the original 20 synthetic shader fixtures on 45c59247c37d61fa77df96f2188cb9f58c0a6e8e and compared the emitted DXIL with the same 9376fd60c0ba0e76a74c39b49b1407dbac0a64c7 baseline.

  • Unset: the eight non-finite float colour operands were handled as expected. Typed image stores, typed buffer stores, raw buffer stores and unsigned colour exports all produced byte-identical containers to the base. That includes the four typed-store words from the earlier report.
  • VICTORY_NAN_GUARD=stores: float colour output matched the default byte for byte. The typed image and buffer paths each changed exactly 0x7F800000, 0xFF800000, 0x7FC00000 and 0xFFC00000 out of 16 tested words. Raw stores and unsigned colour exports still matched the base.
  • VICTORY_NAN_GUARD=0: all 20 containers matched the base byte for byte. The shader translation fingerprint distinguished all three settings.

The untouched PR head passed the Linux shader library suite (171 passed, 2 ignored) and Windows MSVC cross-target test compilation. The fixture check inspects emitted operands and container bytes. Windows execution, DXIL validation, and GPU readback are still open.

@PastiiJay

Copy link
Copy Markdown
Author

Thanks @avt0m4t48 for re-running the fixtures on 45c5924, that confirms the three settings behave as intended. The in-game A/B on our RX 9070 XT (default vs stores) is next; we'll post the result here.

@MamaLeVrai

MamaLeVrai commented Nov 25, 2026 •

Copy link
Copy Markdown

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head 45c5924 into main f85101b in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails with the errors main itself has on this machine (placed_filler_twins, depth_written, a sort, and manual implementation of .is_multiple_of() at victory-gpu/src/shadow.rs:2675; PR #841 fixes the first three), also with --all-features; this PR adds none. (Correction: my first version of this report said that the is_multiple_of one was extra here. It is in main too: my first baseline run of main did not list it, and a run with --keep-going on a clean main does.) cargo test --workspace --locked: run 1: stopped at indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields (the one that fails on main itself most runs here (PR #849 and #847 fix its assertion)) and watch::tests::a_write_is_reported_at_every_checked_view (not a test I have seen fail before; I did not look into it); run 2: 565 passed, 0 failed, 12 ignored (main alone: 555 passed, 0 failed, 11 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

@PastiiJay

PastiiJay commented Nov 25, 2026 •

Copy link
Copy Markdown
Author

In-game check on our RX 9070 XT (Adrenalin 32.0.32029.2010, Series S mode with AMD FSR, 45c5924 on top of our AMD branch): two 14 min runs, one with the default (colour exports only) and one with VICTORY_NAN_GUARD=stores. Both ran without a device removal, at a median of 6.6 and 6.8 fps in the world. With the default, the cutscenes and the boat section render cleanly: no purple blocks stuck on screen; some small purple patches remain on vegetation, as in our earlier builds. So the colour-export guard alone looks enough for what it was meant to fix here. One machine, two runs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants