Repository navigation
Round v_fma_f16 once in DXIL lowering - #1387
Conversation
catminou4
left a comment
There was a problem hiding this comment.
v_fma_f16 now rounds once: TwoSum recovers the f32 addition error (exact, since the f16 product is exact in f32), then a manual round-to-nearest-even f16 pack handles subnormals, overflow-to-inf at the 65520 midpoint, and falls back to the legacy op for NaN. The sign trick (XOR error bits with the sum sign so 'away from zero' is uniform in magnitude space) is correct, and the 9 hardware cases include exact ties, subnormal ties, and the overflow midpoint. Two caveats for the Windows run: (1) this expands to ~50 DXIL ops per fma — fine for correctness, but check shader compile times on fma-heavy titles; (2) the tiny -> 0 flush relies on the 2^-25 boundary, please confirm the halfway cases on hardware. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host/diff_tests) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 96 passed, 0 failed on macOS; the valu test is Windows-gated.
|
Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head
Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the test fails without the fix on this host. AI-assisted (Claude Code, Claude Opus 5.5); no game data. |
|
Second, independent NVIDIA confirmation. No game files were used. Machine: Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1, DXC
Not tested: the in-game effect; AMD. |
|
Tested on a Radeon 860M (RDNA 3.5) and WARP, at |
|
Automated triage for the maintainers. Merge candidate. It now merges cleanly with CI: passing ( Generated by Claude Code |
Offline measurement on a retail dump, and the shader tests on AdaResult: no count change against Offline. I merged head c77b253 onto No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on Tests: with the same merge, No shader names or data are included, per the asset policy. AI-assisted (Claude Code). |
c77b253 to
db78277
Compare
The translator did every console f16 operation in f32 and rounded each result to f16 in integer code (`narrow16`, about 40 operations), which costs about 34x the host's own half math (Marvisak#2806). With the new `emit::Options::native16`, `v_add/sub/subrev/mul_f16`, `v_fma/fmac_f16`, `v_min/max/min3/max3/med3_f16` and `v_pk_add/mul/fma/fmac/min/max_f16` become DXIL `half` arithmetic: 16-bit types, the native low precision shader flags, the 16-bit data layout and SFI0's native 16-bit bit, set only when a live instruction uses them. The option is for hosts that report `Native16BitShaderOpsSupported` (D3D12 OPTIONS4; SM 6.6 is required anyway) and round a 16-bit FMad once, as the console's v_fma_f16 does (`Host::rounds_f16_fmad_once`, a dispatch of Marvisak#2384's case, run once as `fuses_fmad` is). The RTX 5070 Ti does. WARP rounds in f32 first and keeps the f32 path, which since Marvisak#1387 rounds v_fma_f16 once itself. Results: sums, differences and products are the same as the f32 path's (rounding in f32 first never changes the f16 result), as are minima, maxima and medians (exact; the -0/+0 order and the clamp, which gives +0 for NaN and -0, are done on the bits). NaN payloads are the host's where the f32 path quiets them. Fused multiply-adds round once; the f32 path still rounds the packed ones twice. Programs that round toward zero (the title's around its 16-bit interpolation) or toward an infinity keep the f32 path, as do conversions, interpolation, v_mad/mac_f16, v_fma_mix* and the transcendental and rounding operations. victory-gpu asks the host once (`host::native16`, off with `VICTORY_F16_IN_F32=1`) and puts the answer in `shader_cache::options()` and the cache key, so one path's entries never serve the other. Tests (RTX 5070 Ti, driver 621.24; WARP is refused and skips them): the 16-bit probes natively; the fused inputs the f32 path rounds twice, Marvisak#2384's three cases and 8,192 random finite triples (denormals included) rounded once natively; every 16-bit probe and the random triples lowered both ways, native never further from the model (they differ on 1 input, a packed one); the toward-zero mode natively; the host check (WARP refused); flags and validation of the lowered modules (dxil.dll 1.6). A translated benchmark (ignored; 262,144 threads, dependent chains of 512 instructions, best of five, GPU timestamps): v_pk_fma_f16 f32 path 0.221 ms native 0.006 ms 37x v_fma_f16 f32 path 0.129 ms native 0.006 ms 23x v_mul_f16 + v_add_f16 f32 path 0.221 ms native 0.011 ms 21x
The DXIL lowering of
v_fma_f16can round twice when the exact result lands just above or below a binary16 midpoint. For1.5 * 4108 + 2^-14, the binary32 intermediate loses the addend and the baseline produces0x6e04(6160). The correctly rounded binary16 result is0x6e05(6164).Use the residual of the binary32 sum to determine which side of the midpoint the exact result lies on. Keep ties-to-even for exact midpoints and preserve the destination register's other half.
Independent Windows x64 QA checked nine finite operand triples against an integer reference and compared both complete output DWORDs on a GeForce RTX 5070 (32-lane waves). The targeted regression failed on the baseline and passed on the candidate. Both synthetic Wave32 shaders passed the Windows SDK 10.0.26100.0 DXIL validator and created and dispatched their D3D12 compute pipelines. The shader capability flags matched, and the
victorypackage test passed. Validation covered one adapter. Wave64, NaNs, and game scenes were outside the test scope.AI-assisted.