Skip to content

Shaders: lower binary64 fractional parts - #2728

Open
andrewpozdnakov7-jpg wants to merge 2 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/fract-f64
Open

andrewpozdnakov7-jpg wants to merge 2 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/fract-f64

Conversation

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

Summary

Lower RDNA 2 V_FRACT_F64 in VOP1 and VOP3. The lowering uses two 32-bit words, rounds ties to even and keeps finite results below one. It writes both destination words under the lane guard and requires neither host doubles nor int64. VOP3 source abs and neg are supported.

The RDNA 2 ISA defines the operation as x minus floor(x). RDNA 2 and RDNA 4 measurements confirm that tiny negative inputs return the largest binary64 value below one.

Verification

  • On base 13f91ca, both synthetic tests fail during shader translation with Unsupported v_fract_f64.
  • With the patch, the tests check results for 32 inputs through VGPR and SGPR pairs, including a rounding carry between words, signed zeros, subnormals and nonfinite inputs. VOP3 literals exercise abs and neg separately and verify that the optional double and int64 DXIL feature flags are clear.
  • All 32 project VALU tests pass on app-local D3D12 WARP. Strict shader Clippy and the staged asset policy pass.

Coordination claim

Please retain the Git author if cherry-picking or rebasing this commit. If folding it into another commit, add Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com.

AI-assisted. The regression uses synthetic inputs.

@kotekompotek

Copy link
Copy Markdown

Impressive lowering. Verified the hard parts:

  • shift_fraction_words correctly avoids any IR shift count reaching 32 (carry_shift masked to 31, needs_carry guards offset==0) - no poison.
  • The guard/sticky/parity combine implements round-to-even on the 53-bit significand before the 1-|x| complement for negatives - this is what produces the 'largest f64 below 1' for tiny negatives (KytyPS5-measured behavior), and the empty/limit select handles it explicitly.
  • Operand details all match the ISA: f64 literal = high word + zero low (the test's 0x402a8000 -> 13.25 confirms), const 248 uses the true f64 1/(2pi) not the f32 widening, inline float consts convert f32->f64, int consts sign-extend, and clamp/omod/sdwa/dpp reject to skip rather than mis-emit.
  • Positive |x|<1 shortcut (incl. subnormals), exponent>=1075 -> +0, nonfinite -> quiet NaN all correct.

Test matrix is genuinely exhaustive. LGTM.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Independent Windows 11 + NVIDIA GeForce RTX 5090 + WARP + dxil.dll verification and IEEE-754 review of 325fda6 (on 13f91ca):

Verification (rustc 1.98.1 MSVC, Windows SDK 10.0.26100.0 dxil.dll)

  • cargo xtask policy: 184 files ok.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings and --features host: 0 warnings.
  • cargo test --locked -p victory-shader --lib -- fract_f64 emit::tests emit::lower emit::valu_tests (skipping the 4 long exhaustive 16-bit/3-op tests; RTX 5090 hardware + WARP + dxil.dll active): 72 passed, 0 failed.
  • Combined merge test with Shaders: lower binary64 ceiling and floor #2713 (0af08f5): clean zero-conflict merge; all 4 binary64 tests pass together on RTX 5090 + WARP + dxil.dll.

Review Notes

  • Pure 32-bit integer lowering + firstbitLo/firstbitHi + safe shift_fraction_words avoids SFI0 DOUBLE/INT64 flags and shift UB, while accurately implementing RDNA2 [0.0, 1.0) semantics: fract(-0.0) = +0.0, exact integers and |x| >= 2^52 -> +0.0, positive subnormals preserved without FTZ, negative small/subnormal inputs (-2^-54, -3*2^-54, -2^-1074) rounded-to-nearest-even and clamped to next_down(1.0), and ±Inf/sNaN/qNaN quietened via | 0x0008_0000.
  • Clean and ready to merge (minor follow-up: share the ~40-line f64 source decode helper with Shaders: lower binary64 ceiling and floor #2713 if both land).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This adds a lowering for v_fract_f64 (VOP1 and VOP3) in the new crates/victory-shader/src/emit/lower/fract.rs, dispatched from Lower in lower.rs. It works on two 32-bit words, so it needs no double or int64 DXIL flags.

What I checked:

  • operand decoding: the f64 literal as the high word, the 1/(2π) constant and sign-extended integer constants;
  • that the shift helper keeps every shift count below 32;
  • the round-to-even path for small negative inputs, which gives the largest value below one;
  • the NaN, infinity and exact-integer cases.

The behaviour cites the public RDNA2 ISA document and published hardware measurements. The tests cover 32 inputs through VGPR and SGPR pairs, plus abs and neg. CI: check passes. This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

@HAL9000RELOADED

Copy link
Copy Markdown

First Radeon run of 7b0550c: RX 7900 XTX (RDNA3), driver 32.0.32015.7029, Windows 10 22H2 (19045), Agility SDK 1.620 in the shader test binaries through #486. Base: 481d699 (main 842b0cb + #486).

  • victory-shader: 303 passed, 1 failed. Both new tests pass on the card: fract_f64_vop1_returns_both_words_below_one and fract_f64_vop3_literals_obey_abs_and_neg.
  • The one failure is pops_wait_loop_exits_immediately_for_inactive_lanes, which wants an SM 6.6 WARP device (this Windows 10 WARP reports SM 6.2). It fails the same way on the base without this PR.

So the lowered binary64 fract matches on RDNA3 hardware as well as on the RTX 5090 and WARP.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::valu_tests::fract_f64: 2 passed, 0 failed (fract_f64_vop1_returns_both_words_below_one and fract_f64_vop3_literals_obey_abs_and_neg pass on both RTX 5090 hardware and WARP).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

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

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…arts

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…arts

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants