Shaders: find the normalized load rounding shift with one bit scan #2891
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2893"
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 Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-25T23:16:44Z.
What changed
Use a DXIL
FirstbitHiscan to compute the rounding shift inLower::exact_norm. The previous sequence emitted seven comparisons, seven selects, and seven adds to count the powers of two reached by the runtime quotient. Setting bit 24 for the scan preserves the minimum shift of one. Higher bits still determine the shift up to eight, and the original quotient still goes into rounding. DXIL specificationEmitted DXIL
A synthetic GFX10 compute shader reads the 11-bit channel of a packed
10_11_11_UNORMbuffer at runtime and stores the resulting float bits.The whole function is 18 instructions smaller (23.1%). The regression counts live instructions selected for serialization. A DXC disassembly of both containers confirmed the counts.
How tested
cargo test --locked --offline -p victory-shader an_11_bit_unorm_load_has_a_short_rounding_sequence -- --nocaptureon the upstream emitter with the regression applied failed as expected. It reported 78 instructions against the budget of 60.cargo test --locked --offline -p victory-shader 11_bit -- --nocapturepassed both new tests. The rounding test checked 20 codes on hardware and WARP against an f64 reference rounded to f32.dxc -dumpbinon the two containers confirmed 78 instructions before and 60 after in the temporary synthetic DXIL containers.dxc -O3 -T cs_6_6retained six unsigned comparisons and emitted zero bit scans when compiling an equivalent synthetic HLSL version of the original shift calculation.cargo test --locked --offline -p victory-shaderreported 302 passed, 2 failed, and 3 ignored. Both failures reproduced on upstream842b0cbwith the same localdxil.dllPipeline State Validation rejection. They wereemit::rect::tests::rectangles_get_their_fourth_cornerandemit::tests::elements_sharing_a_row_keep_their_order.cargo test --locked --offline -p victory-shader --test resource_trackingreported 3 passed and 1 ignored.cargo test --locked --offline -p victory-shader --doccompleted with 0 doctests.cargo clippy --locked --offline -p victory-shader --all-targetspassed without warnings.cargo run --locked --offline -p xtask -- policy --stagedpassed for all three staged files.gh pr list --repo Marvisak/VIctory --state open --search '"UNORM" "FirstbitHi"'and searches forexact_normandnorm_quotientfound no matching change. Related proposals cover a separate normalized-conversion algorithm and single rounding for normalized d16 loads. PR #1112 PR #1940Scope of measurement
Verification used synthetic shaders. Hardware GPU time, driver-generated instruction counts, pipeline compilation time, and game behavior were unmeasured. No game files or assets were used.
AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:31:27Z.
Review: COMMENTED
Verified head
35e7e57bcleanly merged ontoorigin/main(842b0cb7) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0dxil.dll).Code & Correctness Review
u32domain equivalence: Replacing the 7-iterationfor bit in 25..32select chain inexact_norm(crates/victory-shader/src/emit/lower.rs) withscan = q | (1 << 24)->leading = FirstbitHi(33)(scan)->shift = 8 - leadingis strictly equivalent across allq in [0, 2^32):scan,scan != 0andmsb(scan) in [24, 31], soleading = 31 - msb(scan) in [0, 7]andshift = 8 - leading in [1, 8].q < 2^25(msb(scan) == 24),shift == 1(anda == 0still selects0viais_zero); for2^k <= q < 2^(k+1)(k in [25, 31]),shift == k - 23 == 1 + (k - 24).emit/lower.rsis521,282B (3,006B below the 512 KiB policy limit).Local Verification
cargo test --locked -p victory-shader --lib valu_tests: 36/36 passed on RTX 5090 + WARP +dxil.dll(includingnormalized_11_bit_buffer_loads_keep_exact_roundingand existing 8/10/16-bit normalized buffer load tests).cargo test --locked -p victory-shader --lib emit::lower::tests: 34/34 passed (includingan_11_bit_unorm_load_has_a_short_rounding_sequence).cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--all-features): clean.cargo xtask policy:191 files ok.Looks clean and ready to merge.
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:34:42Z.
Review: COMMENTED
Scoped review: the written_instructions counter gives the test a way to assert the single-bit-scan shape rather than comparing whole DXIL, and the bias select refactor reads consistently. I didn't verify the bit-scan emission itself line-by-line, but the instrumentation is the right kind of observable for this optimization.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:28:30Z.
Automated triage for the maintainers.
Merge candidate. This replaces the seven compare/select/add steps in
Lower::exact_norm(emit/lower.rs) with one DXIL FirstbitHi (op 33, as thes_flbit_i32_b32lowering uses). With bit 24 forced on,8 - leadingequalsbitpos - 23for bit lengths 25 to 32 and 1 below, matching the old bounds of 1 and 8, and rounding still uses the original quotient. The new test checks 11-bit UNORM codes against an f64 reference.CI: passing.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T05:52:26Z.
Review: APPROVED
Tested locally merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):Or(q, 1 << 24)+FirstbitHi+8 - leadingreplaces the 7-step compare/select/add chain cleanly while keepingshift >= 1even forq == 0.cargo test -p victory-shader an_11_bit_unorm_load_has_a_short_rounding_sequence: 1 passed, 0 failed.cargo test -p victory-shader normalized_buffer_formats_load_correctly_rounded_quotients -- --test-threads=1(RTX 5090 & WARP): 1 passed, 0 failed on both.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T17:13:32Z.
I tested #2893 together with #3007 on
63bcca4. Combined branch227937epreserves both original commits. The identical DXIL instruction counter and its test field merge into one copy.I expanded the 11-bit UNORM fixture locally to run all 2,048 encodings through the translated buffer load. Every returned f32 matched the rounded binary64 reference
code / 2047.0bit for bit on app-local WARP 1.0.22 with DXIL validation. The branch retains the original short regression and passes its 60-instruction limit.All three
mul_hitests passed. The four 32-bit forms emitted 18 DXIL instructions each and the two 24-bit forms emitted 22 each. SFI0 leftINT64clear in all six. Strict shader Clippy and the asset policy passed.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.