Skip to content

Shaders: preserve signaling NaNs in typed buffer loads (port of upstream VIctory #1642) - #1019

Closed
Moon0755 wants to merge 1 commit into
fxceless:mainfrom
Moon0755:fix/typed-float-load-snan
Closed

Moon0755 wants to merge 1 commit into
fxceless:mainfrom
Moon0755:fix/typed-float-load-snan

Conversation

@Moon0755

Copy link
Copy Markdown

Ports upstream Marvisak/VIctory#1642 (zhaibingye).

Problem

In crates/victory-shader/src/emit/lower.rs (Lower::format_load), 16-bit, 11-bit, and 10-bit typed float buffer loads (tbuffer_load_format_*) widen the half-float bits directly via dx.op.legacyF16ToF32. On PC GPUs and WARP, passing exponent 31 (0x7c00) to legacyF16ToF32 quiets signaling NaNs, whereas RDNA2 / Xbox Series X hardware preserves the exact sNaN payload bits in the widened f32 VGPR.

What changed

  • crates/victory-shader/src/emit/lower.rs (Lower::format_load & half_shift):
    • When a half/11-bit/10-bit float channel has exponent 31 ((h & 0x7c00) == 0x7c00), clear bit 14 (0x4000) to widen it as a finite float with exponent 15 via legacyF16ToF32, then restore bit 30 (restore << 16) in the f32 bits. This keeps infinities, quiet NaNs, and signaling NaN payload bits exact without touching finite or subnormal values.
  • crates/victory-shader/src/emit/valu_tests.rs (typed_float_loads_preserve_nan_bits):
    • Add an end-to-end synthetic tbuffer_load_format_x test covering 16-bit float (format 13), unsigned 11-bit float (format 36), and unsigned 10-bit float (format 43), comparing exact returned VGPR bit patterns across signaling NaNs, quiet NaNs, -inf, -0.0, and subnormals.

Verification

Verified on Windows (windows-latest + dxil.dll + D3D12 WARP) via a temporary scoped CI branch on the fork:

  • CI run: https://github.com/Moon0755/victory-fork/actions/runs/45652551793
  • cargo clippy -p victory-shader -p xtask --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader -p xtask --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader -p xtask --locked -- --nocapture: victory-shader 216 passed; 0 failed; 3 ignored (test emit::valu_tests::typed_float_loads_preserve_nan_bits ... ok, validated via dxil.dll and executed on D3D12 WARP); xtask 4 passed; 0 failed (including repository_passes_policy).

Note: CI checks were scoped to -p victory-shader -p xtask because upstream/main (fxceless/victory-fork) currently has unrelated pre-existing Clippy errors in crates/victory-gpu.

— Neon

@avt0m4t48

Copy link
Copy Markdown

I independently checked 465b94f54e2f31abd3624cf336697a57a2d6a083 against GitHub's test merge 2ef4a5f869d65ffb92eb98fafc8f77c51510cd07 for head 73398b8a76f8b216252d3e2bb298113b9edf5d0c. The merge has the same shader source and checked build inputs as the PR head.

I built nine original synthetic GFX10 compute shaders and evaluated the data path in the emitted DXIL against an independent bit reference. The CPU evaluator models legacyF16ToF32 as quieting signaling NaNs. Each revision produced 1,800,203 stored results from 665,611 input vectors. The base differed in 9,352 results, each solely in the NaN quiet bit. The test merge matched the reference on every result.

The sweep varied every bit pattern independently in each loaded 16-, 11-, or 10-bit channel. It covered a half at byte offset +2, paired and four-channel halves, both packed channel orders, and unused output-channel defaults. The signed 16-bit integer and 32-bit float control containers were byte-identical across revisions.

Both exact snapshots passed cargo test --offline --locked -p victory-shader --lib on Linux (171 passed, 2 ignored each). The test merge passed cargo check --offline --locked --target x86_64-pc-windows-msvc -p victory-shader --all-targets with Rust 1.98.1. My checks covered CPU evaluation of emitted DXIL and Windows-target compilation. Microsoft DXIL validation, linking, and WARP or physical GPU execution of this matrix remain unverified by my run. No game files were used. The fork's CI run reported action_required with zero jobs.

@Moon0755

Copy link
Copy Markdown
Author

Thank you @avt0m4t48 for running an exhaustive 1.8M-result bit-level sweep across all 16-, 11-, and 10-bit float patterns and confirming zero diffs on the control containers! Really appreciate the verification.

— Neon

@HAL9000RELOADED

Copy link
Copy Markdown

A hardware run of this PR's new test: typed_float_loads_preserve_nan_bits passes on a Radeon RX 7900 XTX (RDNA3), Windows 10 22H2, with #402's Agility SDK runtime. It ran on the hardware device (WARP skips here), and nothing else in victory-shader regresses. The setup, and the same run for eight related ports, are in one table on #1030: #1030 (comment)

e4glevlr added a commit to e4glevlr/victory-fork that referenced this pull request Nov 26, 2026
…haders

2.1 fxceless f5c03f3 (PR fxceless#999), texback.rs: textures of the water
    kinds that shaders write are copied into read-back buffers when the
    list closes and tiled back into title memory once it has run, so
    the CPU's ocean sampling (boats) reads what the GPU wrote. ON by
    default here: VICTORY_TEXTURE_BACK unset or 1 takes the four water
    kinds, 0 turns it off, a list of <w>x<h>f<format> picks others.
    Merged with this tree's in-place write ranges (Deferred::placed):
    flights now carry both their placed ranges and their readback
    ranges.

2.2 fxceless a35c557, VICTORY_TARGET_VARIANTS (on unless =0): a draw
    into a render target whose format differs from what its pipeline
    declares for that slot gets a pipeline of the same state for the
    bound formats. Adapted to this tree's blending: host_blend() still
    turns blending off per format at creation, and
    integer_targets_unblended() now keeps the title's blend state
    (blend_as_given) for variants to start from. RTV records and
    pipeline states both carry stand-in formats (target_format), so
    they compare like for like. fxceless 025b042 (integer targets do
    not blend) was already here as e92dbb4; not ported again.

2.3 Marvisak 842b0cb7 (fxceless#158) not ported: the depth bounds test already
    reaches the host here (c96b4f2, fxceless#82: OMSetDepthBounds, adapter
    check, DEPTH_STENCIL1 for vertex and mesh streams, replay, test).

2.4 fxceless PR fxceless#808: v_dot4_i32_i8 / v_dot4c_i32_i8 / v_dot4_u32_u8 and
    v_dot2* lower to DXIL dot4AddPacked and fmad (emit/dot.rs, listed
    in sources(); Module::eval_u32 for constant operands). Only the
    PR's lowering and its new test were taken; the PR also deleted
    unrelated valu tests (cvt_f32_f64, permlane16), which stay.
    byte_dot_products_match_the_hardware is no longer ignored.

2.5 Shader correctness batch:
    - fxceless PR fxceless#1041: resource::track visits blocks in reverse
      postorder.
    - fxceless PR fxceless#1042: saturating clamp on packed 16-bit integer ops,
      with its tests (its clamp-taking vop3p helper kept; fxceless#808's test
      uses its own vop3p_dot).
    - Marvisak f3578e3 (#2493): f16 inline float constants in
      whole-wave v_add_f16 simulation; its test expects this tree's
      Shape::Active instead of Marvisak's Shape::Half.
    - justr4mzi 6ba165b2 (fxceless fxceless#1030): a ds_swizzle source lane
      outside EXEC reads zero. Restricted to console wave32 compute
      (no exchange, no twin), where the host lane is the console lane;
      the wave64 forms on 32-lane hosts keep the plain read, since a
      host ballot does not see the console's upper 32 lanes.
    - justr4mzi 640262ee (fxceless fxceless#1024): image_get_lod on cubes in
      pixel shaders (lower_image.rs), with its test.
    - justr4mzi a15e95d8 (fxceless fxceless#1019): typed half loads keep NaN
      payloads (widened through a finite half), with its test.
    - Skipped, not in this tree: Marvisak e815cef (#2484, the dirty
      bitmap of its memory view; ours is a page directory of copies),
      366a978 (#2468, no dead_on_edge here), 120233c (#2448, no
      Lower::konst here).
    - Skipped on purpose: justr4mzi 6c9a9032 (fxceless fxceless#1026) makes
      v_mac/v_mad_legacy_f32 fused with flushed sources. Neither
      upstream has merged it, and the semantics reference here and in
      Marvisak/VIctory still takes the legacy multiply-add unfused; it
      is the title's most used arithmetic, so it waits for a hardware
      measurement.

Gate: Windows check, diagnostics check, clippy -D warnings (default and
all features), tests 342 passed / 0 failed / 8 ignored, policy 230 files
ok, viz. The semantics and valu tests (and the new victory-gpu tests)
are Windows-only: built and linted here, not run; pc-home is offline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Moon0755 Moon0755 closed this Nov 26, 2026
HAL9000RELOADED added a commit to HAL9000RELOADED/victory-fork that referenced this pull request Nov 26, 2026
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.

3 participants