perf: v_med3_f32 clamp between constants as max then min on every host (3-6x on a 4060 Laptop) #581
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/581"
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?
Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T12:54:15Z.
Stacked on #570. Merge #570 first; this PR is then its last commit.
What
v_med3_f32of a value between two constants (clamp(x, lo, hi), the console compiler's clamp idiom,v_med3_f32 v, v, 0, 1.0for a saturate) now lowers on every host as the hardware's maximum then minimum,min(max(x, lo), hi). Until now that short form was taken only on AMD hosts (rdna::MED3); NVIDIA, Intel and WARP got the general median: 4 FMin/FMax, 3 NaN tests and a select.The maximum and minimum it uses (
max_min, also everyv_max_f32,v_min_f32,v_max3_f32,v_min3_f32) get two constant forms. Both give exactly what the general form gives:and/orthat gives a zero result that zero's sign. The result can never have the other sign.So the median between constants keeps RDNA 2's rules: a NaN gives the lower constant, and -0 orders below +0. The general form did not keep the second rule on WARP:
v_med3_f32(+0, -0, 1.0)gave -0 there, where RDNA 2 gives +0. The new test caught it.Bench (RTX 4060 Laptop, synthetic programs only)
op_loop_benchruns 8 dependent chains of one console instruction (each followed by an add) for 256 rounds, in 8192 groups of 32 threads.op_loop_ideal_benchruns the same chains written in HLSL (dxc -O3). Before and after were built as two test executables and run interleaved, 3 rounds each. The table gives the median GPU time per dispatch:v_med3_f32 x, 0, 1.0+ addsaturate)v_med3_f32 x, -1.0, 1.0+ addclamp)v_max_f32 x, 0+ addv_max_f32 x, 0.5+ addv_min_f32 x, 1.0+ addv_med3_f32 x, a, b(registers) + addThat is 3-6× on a clamp between constants and 1.3× on a maximum with 0. The nonzero-constant max/min form gains nothing measurable on NVIDIA, because the driver already folds that fix-up, but it writes 48 fewer instructions per chain. The "before" spread is the 4060 Laptop's clock, which runs at two levels between rounds. "After" was stable.
Whether the title's slow skinning shaders use these is unknown. #570's census now counts
c_minmaxandc_minmax_kper shader hash, so a tester's census line will tell.The same file holds the benches behind that decision:
skinning_bench: a skinning-shaped program (4 influences, current and previous bone matrices, 24 multiply-adds per influence, loop and unrolled). It runs as fast translated as in HLSL (0.210 vs 0.212 ms, ratio 0.99-1.00) and is checked against a CPU reference.op_census: the module instructions each console instruction costs.Tests
max_min_and_med3_with_constants_write_no_select: a max or min with 0.5, +0 or -2, a min with -0, and a median between 0 and 1.0 or -1.0 and 1.0 write no select. It fails before (op 0x110 [257, 240, 0] writes 1 selects) and passes after.max_min_and_med3_with_a_constant_match_rdna2: 20 forms (max, min, max3, min3 and med3 with the constant in every operand place, including -0 through an SGPR) × 16 inputs (both zeros, both infinities, NaNs of both signs, the constants and their neighbours), compared bit for bit with RDNA 2's rules on the hardware adapter and WARP. It fails before on WARP (v_med3_f32 [128, 257, 242] of 0x80000000 = 0x80000000, want 0x00000000) and passes after. It also passes on the RTX 4060 Laptop and the Intel UHD (VICTORY_D3D12_ADAPTER=Intel).v_med3_f32_matches_rdna2_with_nan,max_and_min_order_negative_zero_below_positive_zero, the f16 and integer forms) pass on RTX, WARP and Intel UHD.cargo test -p victory-shader --lib --releaseon the RTX 4060 Laptop: 371 passed, 0 failed, 24 ignored. Full pytest (WARP): 1454 passed, 529 skipped.cargo xtask policy: 381 files ok.The translator output changes, so
TRANSLATOR_VERSIONgoes to 21 (17-20 are taken by #456, #535, #489 and #571; I will bump it again if one of them lands first).AI use: written, benched and tested by Claude Code (Claude Opus 5.5) for KainanP, on synthetic programs only (no game shader was used).
🤖 Generated with Claude Code
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.