Repository navigation
Shaders: find the normalized load rounding shift with one bit scan - #2893
Ingenioeren wants to merge 1 commit into
Conversation
Use FirstbitHi to count the leading zeros in the runtime quotient. A synthetic 11-bit UNORM load emits 60 DXIL instructions, down from 78. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fuzeofficlal
left a comment
There was a problem hiding this comment.
Verified head 35e7e57b cleanly merged onto origin/main (842b0cb7) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0 dxil.dll).
Code & Correctness Review
- Full
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):- Because bit 24 is always set in
scan,scan != 0andmsb(scan) in [24, 31], soleading = 31 - msb(scan) in [0, 7]andshift = 8 - leading in [1, 8]. - For
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).
- Because bit 24 is always set in
- File size:
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.
kotekompotek
left a comment
There was a problem hiding this comment.
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.
|
Automated triage for the maintainers. Merge candidate. This replaces the seven compare/select/add steps in CI: passing. Generated by Claude Code |
Dukestar69
left a comment
There was a problem hiding this comment.
Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):
- Diff review: Computing the normalized-load rounding shift via
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).
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.