Skip to content

Shaders: lower v_div_scale_f32 and v_div_fmas_f32 - #1747

Open
ESAT586 wants to merge 2 commits into
Marvisak:mainfrom
ESAT586:shader/div-scale-and-fmas-f32
Open

ESAT586 wants to merge 2 commits into
Marvisak:mainfrom
ESAT586:shader/div-scale-and-fmas-f32

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

AI Authorship Disclosure: Written and verified with Gemini 4 Neon (Extra High Mode). Strictly clean-room: zero game files or title data were used; all tests are synthetic RDNA2 compute shaders validated with dxil.dll and executed on real hardware and WARP.

Summary

Lowers RDNA2's f32 IEEE-754 division pre-scale and post-scale instructions — �_div_scale_f32 (VOP3B

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

A note on denormals, from reading the diff (not run).

scale_f32_pow2 keeps f32 denormals exactly in both directions. It scales a subnormal input up through FirstbitHi, and a value scaled down into the subnormal range gets its rounded subnormal. That is RDNA2's behaviour only when the shader's f32 denormal mode keeps denormals.

  • VIctory reads f32 denormals as the console's float mode does elsewhere: ftz on main (lower.rs around 2067) makes them a zero of their sign.
  • AnyPS5 measured v_div_scale_f32 on RDNA2 hardware (gfx1036, 2,304 operand rows, every f32 denormal mode, IEEE 0 and 1; recorded in their docs/dev/debt/shader.md). In mode 0, which the common FLOAT_MODE 0xc0 selects, and in mode 2, it flushes its inputs. In mode 1 it keeps the inputs and flushes a denormal result to a zero of its sign. Only mode 3 keeps both.

So for the usual float mode, passing the operands through ftz and flushing a subnormal result to a signed zero would match the hardware and the rest of the lowering. The current form matches mode 3. A test with a subnormal numerator, and one with a quotient that scales into the subnormal range, would show the difference.

AI-assisted (Claude Code). No game files.

Lowers RDNA2's IEEE-754 f32 division pre-scale and post-scale FMA helpers
(�_div_scale_f32 VOP3B 0x16d and �_div_fmas_f32 VOP3A 0x16f) in pure
32-bit DXIL (no INT64/F64 flags) with FTZ-immune exponent scaling, and
wires implicit VCC tracking for �_div_fmas_f32 through IR and lane-mask
structure analysis.

AI-assisted with Gemini 4 Neon (Extra High Mode); synthetic tests only,
zero game data used.
@ESAT586
ESAT586 force-pushed the shader/div-scale-and-fmas-f32 branch from fbf1dc3 to 5cea8dd Compare November 24, 2026 22:22
@rminzao

rminzao commented Nov 24, 2026

Copy link
Copy Markdown

Double rounding on denormal quotients

The denormal-mode point is already raised above. One more: in v_div_fmas_f32 (lower.rs ~6084/6094) the 2^±64 scale is applied after an already-rounded fmad. When the quotient ends up denormal it is rounded twice, so it can be 1 ULP off the hardware's single rounding. valu_tests.rs ~1792 allows max_ulp = 1 for denormal results, which would hide exactly this.

Related: this uses FMad (DXIL op 46), which isn't guaranteed to be fused. That's the existing v_fma_f32 shortcut, but here the final correction step relies on the fused result, so it might deserve a shortcut: note like the others.

Static review (AI-assisted), read against main at 70564ae; nothing was run on a GPU.

…v_fmas_f32

Addresses review feedback on Marvisak#1747:
- Flushes f32 inputs via `Self::ftz` and flushes subnormal outputs to signed zero
  in `scale_f32_pow2`, `div_scale_f32`, and `v_div_fmas_f32`, matching RDNA2's
  default f32 denormal mode (`FLOAT_MODE = 0xc0`, mode 0) and the rest of
  `emit::lower`.
- Eliminates double rounding on denormal quotients (subnormal scaled outputs flush
  to signed zero; normal quotients match IEEE-754 at 0 ULP) and removes the
  `max_ulp = 1` allowance from `rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas`.
- Documents the existing `shortcut:` for DXIL `FMad` (op 46) in `v_div_fmas_f32`.
- Updates tests to verify FTZ behavior on subnormal numerator/denominator inputs
  and quotients scaling into the subnormal range.

Clean-room synthetic tests only; zero game data used.
AI-assisted with Gemini 4 Neon (Extra High Mode).
@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Thanks @upipibi and @rminzao for the sharp RDNA2 denormal-mode and rounding feedback!

Updated this branch to match VIctory's default f32 FTZ mode (FLOAT_MODE = 0xc0, mode 0) and address both notes:

  • div_scale_f32 & v_div_fmas_f32: flush inputs with Self::ftz and flush subnormal outputs (exp <= 64 under 2^-64, or unscaled subnormal FMad) to a zero of their sign in scale_f32_pow2, matching lower.rs's ftz behavior and RDNA2 mode 0.
  • No double-rounding tolerance: because scaled subnormal quotients flush to signed zero and normal results scale by an exact power of two, rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas now asserts exact bit equality (0 ULP) across all cases (removing max_ulp = 1).
  • shortcut: note: documented that v_div_fmas_f32 uses DXIL FMad (op 46), matching v_fma_f32.
  • Tests: added explicit FTZ test cases for subnormal numerator/denominator inputs (qNaN, flag = 0), negative/positive subnormal outputs in v_div_fmas_f32 (±0.0), and subnormal-range quotients in the full Newton-Raphson pipeline (±0.0).

Re-verified on Windows 11 with NVIDIA GeForce RTX 3060 Ti + Microsoft D3D12 WARP + dxil.dll:

  • v_div_scale_f32_matches_rdna2, v_div_fmas_f32_matches_rdna2, rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas: 3 passed, 0 failed
  • cargo clippy --workspace --all-targets -- -D warnings & --all-features: 0 warnings
  • cargo xtask policy: ok

(Clean-room synthetic tests only, zero game data. AI-assisted with Gemini 4 Neon (Extra High Mode).)

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/structure.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Shaders: lower v_div_scale_f32 and v_div_fmas_f32 — merge conflict with main (940d32f), verified locally. Also overlaps #200 (same v_div_scale/v_div_fmas lowering, plus v_div_fixup). Rebase needed; will re-test.

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.

5 participants