Shaders: f16 arithmetic in native half on hosts with 16-bit operations (port of Marvisak/VIctory#2825) #502
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Belobug--VIctory-Belobug-AMD!502
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-502"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@0xkn0wh3r3 on GitHub · 2026-11-27 07:42 UTC · original
0xkn0wh3r3:port/native-f16→a-publier· +826 −28 · 16 files · 1 commits · openThis ports Marvisak/VIctory#2825 to this tree. That PR was written against Marvisak's
main, which this tree's emitter has moved away from, so this is a re-implementation rather than a cherry-pick. It keeps the original's intent and its gate on the host's 16-bit support.The problem
The translator does every console f16 operation in f32 and converts each result back with the host's own conversion (
legacyF32ToF16, orf16_nearestin integer code withVICTORY_F16_NEAREST=1). Marvisak/VIctory#2806 measured that path at 34x the host's ownhalf2math on an RTX 5070 Ti. RDNA runs 16-bit floats natively, so on the cards this tree targets that work is wasted.The change
On a host that runs native 16-bit operations and rounds a 16-bit
FMadonce, these become DXILhalfarithmetic:v_add/sub/subrev/mul_f16,v_fma/fmac_f16,v_min/max/min3/max3/med3_f16v_pk_add/mul/fma/fmac/min/max_f16Sources are read as bits: the abs/neg modifiers flip the sign bit, then
truncandbitcastgive ahalf. Results are bitcast back and written into the destination half as before. The -0/+0 order of min/max and the clamp (+0 for NaN and -0) are decided on the bits. On RDNA (rdna::MAX_MIN) min/max are the host's own, as this tree's f32 path already does there.Sums, differences and products round once to nearest even, the console's MODE default. Fused multiply-adds round once, as the console's
v_fma_f16andv_pk_fma_f16do. The f32 path rounds them twice.These stay on the f32 path: conversions, interpolation,
v_mad/mac_f16,v_fma_mix*, instructions with an output modifier, and the transcendental and rounding operations.halftype in the bitcode writer, andUsage::native16for livehalfori16values. The lowering setsLowPrecisionPresent | UseNativeLowPrecisionwhere it makes ahalf, and the clean-up clears them again if it removes every use. SFI0 gets the native 16-bit bit, and the module gets the 16-bit data layout. A module without native 16-bit values is unchanged.emit::host::Host::native16needs two things. The device must reportNative16BitShaderOpsSupported(D3D12 OPTIONS4). A 16-bitFMadmust round once:rounds_f16_fmad_oncedispatches Marvisak/VIctory#2384's case3c01·3e00+8001, lowered natively, and expects3e01. victory-gpu asks once at device creation, after the wave size and RDNA facts, and tells the translator withemit::set_host_native16, as it does those.VICTORY_F16_IN_F32=1keeps the f32 path on any host. WARP rounds theFMadtwice and keeps it too.translation_fingerprint) take the setting only when it is on. Entries from the f32 path keep their key, and one path's entries never serve the other.TRANSLATOR_VERSIONis unchanged, because the f32 path's translations are the same.victory translate --pipelinestranslates as for its adapter, as it already does for the wave size and RDNA.VICTORY_F16_IN_F32=1gives f32.emit::host's dispatch helpers are no longer test-only, because the probe uses them.How it differs from the original
mode16gate. This tree does not model the MODE register, so a program that rounds toward zero also gets nearest even. The f32 path here already uses the host's rounding whatever MODE says.emit::Options, so the setting is a host global likeset_host_rdna, set by victory-gpu when the device is created.semantics_tests.rs, which this tree does not have. New ones are invalu_tests.rs, checked against an exact f16 model.translated_f16_math_costs.Testing
New tests:
native_f16_arithmetic_rounds_once_to_nearest_even: add, sub, subrev, mul, fma and fmac, plusv_pk_add/mul/fma_f16on both halves. Inputs are random finite values (denormals included) and Marvisak/VIctory#2384's three FMA cases, checked against an exact single-rounding model.native_f16_zeros_nans_and_clamp_are_the_consoles: the zero order of min/max/min3/max3, med3 with NaNs, and the clamp. On an RDNA adapter it also runs in the RDNA form.native_16bit_operations_are_declared_where_used: the SFI0 bit is set for native arithmetic. It is not set for the f32 path, or for a conversion.hosts_that_round_f16_fmad_twice_keep_the_f32_path: WARP is refused.emit::tests::the_fingerprint_says_whether_f16_is_native.Results:
VICTORY_D3D12_ADAPTER). Both passHost::native16.dxil.dllas validator, the native modules validate. As a control, dropping the 16-bit flags fails validation ("Bitcast on minprecison types is not allowed"). This validator does not check the data layout.cargo test -p victory-shader: 308 passed, 0 failed, 4 ignored.cargo test -p victory-gpu: 396 passed, 0 failed, 34 ignored.cargo clippy --workspace --all-targets --locked -- -D warnings, with and without--all-features: clean.cargo xtask policy: ok.Not verified
v_add_f16and the like are not known. The native path takes the host's.VICTORY_F16_IN_F32=1turns it off.@gw1337x on GitHub · 2026-11-27 09:59 UTC · original
Tested in the title on RDNA 3: RX 7800 XT 16 GB, driver 32.0.32015.7029, Ryzen 7 9800X3D, 32 GB RAM, Windows 11 26200 (Memory Integrity off). Diagnostics builds (
--profile diag --features diagnostics) of37b25ccand its base4eb6cfd, same boot.The path comes on here:
host native 16-bit float operations native16=true.Settings:
scripts/play.ps1settings plusVICTORY_PORT_CONSTANTS/DESCRIPTORS/CBV/GUARD/JOURNAL=1, Series X profile, ray tracing off. Method: 270 s hands-off from a Chapter I boat save; fps is the layer's 1 s figure from the window title, sampled every second, 45-265 s with 1000+ draws ("steady" from 205 s); per-frame figures fromVICTORY_TIMELINEwindows of 30 frames at 200 s and 240 s. A first run of the PR filled the shader cache under the new key and is left out.37b25cc4eb6cfd37b25ccNo change I can tell apart from noise in this scene, where the card is the limit (about 31-32 ms of a 34 ms frame). The picture at 241 s matches the base. No device loss. I did not count how many of this scene's shaders have f16 arithmetic.
@adrianmjim on GitHub · 2026-11-27 11:11 UTC · original
Data from one real frame on a discrete RDNA 3 card. RX 7900 XT, driver 32.0.32029.2010, Windows 11 26300,
diagprofile. PR head37b25ccagainst a-publier4eb6cfd.A disclosure first. I used
victory replay --retranslateon a recorded frame, and that path never turns native half on. The PR callsset_host_native16at device creation in victory-gpu and invictory translate, not inretranslate, so there it translates in f32. To get native-half shaders I added a local env switch (six lines, not in the PR) that callsemit::set_host_native16(true)next toset_host_rdna(true)there. That forces the path and does not ask the card. I did not runHost::native16on this card, the PR's GPU tests, or the game with this PR, so I do not know whether the runtime turns it on with this card and driver. The comment above reportsnative16=trueon an RX 7800 XT.Setup: a world frame recorded on
4eb6cfd(1,024 pipelines). 1,162 of its 1,183 title shaders translate on every build (--view-limit 2097151 --raw-views 2097152). The other 21 containers were not found and keep the recorded shaders. Thenreplay --compare --shaders <A> --shaders-b <B> --loops 11in both orders, on4eb6cfdplus the replay fix in #550.Measured:
--retranslate): the folder is byte-identical to base's. 1,162 files, samesha256sum * | sha256sum.994 of 1024 pipelines made with shaders of the folder given, 0 refused. By the replay code every file in the folder belongs to at least one recorded pipeline and all pipelines are made at load, so this driver made pipelines from the 17 native-half shaders.--timing detailed), 17.46 / 17.42 ms with #502 first. Loops after each run's first span 17.28-18.07 ms. A control (recorded shaders against base-retranslated) gives 17.44 / 17.41 ms. No difference I can measure.Limits:
A question: should
replay --retranslateset native16 from the card as the runtime does, or take a flag for it as #388 proposes for the wave size and RDNA? As it is, a folder retranslated with this PR cannot show the PR's change.@MamaLeVrai on GitHub · 2026-11-27 11:40 UTC · original
Tested on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release. At
37b25cc:victory-gpu396 passed, 0 failed, 34 ignored;victory-shader308 passed, 0 failed, 4 ignored;victorybins 25 passed.AI-assisted (Claude Code); no game data.
@gut0to on GitHub · 2026-11-27 13:26 UTC · original · reviewed
AI-assisted review: I focused on the new native-half bitcode path and the validator limitation noted in the PR description.
@ -610,2 +619,4 @@// Native 16-bit types are laid out in 16 bits, minimum precision in// 32 (the validator wants the layout that matches the flags).w.record(3,@gut0to on GitHub · 2026-11-27 13:26 UTC · original
Could we add a bitcode-level regression test for both values of
native16that inspects the emitted data-layout string (and confirms the default layout remains byte-for-byte unchanged)? The PR notes that the available DXIL validator does not check this layout, and this is the core ABI change for nativehalftypes.@MamaLeVrai on GitHub · 2026-11-27 15:15 UTC · original
Checked on an RX 6950 XT (RDNA 2), driver 32.0.21045.29003, Windows 11 build 26300, Rust 1.98.1, release (native half supported), at the PR head: a hardware test builds each case twice, once on the f32 path and once with native half, runs both on the card, and compares them bit for bit and against an exact console model (one rounding to nearest even, denormals kept). 1,412 input triples per run: all pairs of 30 edge values (±0, denormals, smallest normal, 1±ulp, 65504, ±inf, quiet/signalling/negative NaN), random triples, and hand-picked ties and overflow/underflow cases.
Ops:
v_add/sub/subrev/mul/max/min_f16,v_fma_f16,v_max3/min3/med3_f16,v_fmac_f16, thev_pk_*forms,v_cvt_f16_f32andv_cvt_f32_f16, with neg/abs/clamp/omod, op_sel (including writing the high half) and SDWA word selects.legacyF32ToF16rounds toward zero, and FMA there rounds twice). Example:v_add_f16 0x0001 + 0xBC00gives0xBBFFon f32,0xBC00native and in the model. With clamp, no case differed by more than one ulp across 25 runs. So the PR moves results only toward the console's.Things to know: the key of every shader changes on native-16 hosts (
translation_fingerprint,shader_cache.rs), so the whole cache is translated again once after merge; and device creation now runs one compute dispatch as a probe (host.rs~1057-1063), keeping the f32 path if it fails. The MODE register is not modelled, so a program that switches to round-toward-zero still gets nearest even (the PR documents it).AI-assisted (Claude Code); no game data.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.