Skip to content

Shaders: find the normalized load rounding shift with one bit scan - #2893

Open
Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:perf/unorm-rounding-firstbithi
Open

Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:perf/unorm-rounding-firstbithi

Conversation

@Ingenioeren

Copy link
Copy Markdown
Contributor

What changed

Use a DXIL FirstbitHi scan to compute the rounding shift in Lower::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 specification

Emitted DXIL

A synthetic GFX10 compute shader reads the 11-bit channel of a packed 10_11_11_UNORM buffer at runtime and stores the resulting float bits.

Instructions counted Before After
Whole shader function 78 60
Rounding shift calculation 21 3

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 -- --nocapture on 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 -- --nocapture passed both new tests. The rounding test checked 20 codes on hardware and WARP against an f64 reference rounded to f32.
  • dxc -dumpbin on the two containers confirmed 78 instructions before and 60 after in the temporary synthetic DXIL containers.
  • dxc -O3 -T cs_6_6 retained 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-shader reported 302 passed, 2 failed, and 3 ignored. Both failures reproduced on upstream 842b0cb with the same local dxil.dll Pipeline State Validation rejection. They were emit::rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order.
  • cargo test --locked --offline -p victory-shader --test resource_tracking reported 3 passed and 1 ignored. cargo test --locked --offline -p victory-shader --doc completed with 0 doctests.
  • cargo clippy --locked --offline -p victory-shader --all-targets passed without warnings.
  • cargo run --locked --offline -p xtask -- policy --staged passed for all three staged files.
  • gh pr list --repo Marvisak/VIctory --state open --search '"UNORM" "FirstbitHi"' and searches for exact_norm and norm_quotient found no matching change. Related proposals cover a separate normalized-conversion algorithm and single rounding for normalized d16 loads. PR #1112 PR #1940

Scope 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.

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 fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 u32 domain equivalence: Replacing the 7-iteration for bit in 25..32 select chain in exact_norm (crates/victory-shader/src/emit/lower.rs) with scan = q | (1 << 24) -> leading = FirstbitHi(33)(scan) -> shift = 8 - leading is strictly equivalent across all q in [0, 2^32):
    • Because bit 24 is always set in scan, scan != 0 and msb(scan) in [24, 31], so leading = 31 - msb(scan) in [0, 7] and shift = 8 - leading in [1, 8].
    • For q < 2^25 (msb(scan) == 24), shift == 1 (and a == 0 still selects 0 via is_zero); for 2^k <= q < 2^(k+1) (k in [25, 31]), shift == k - 23 == 1 + (k - 24).
  • File size: emit/lower.rs is 521,282 B (3,006 B 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 (including normalized_11_bit_buffer_loads_keep_exact_rounding and existing 8/10/16-bit normalized buffer load tests).
  • cargo test --locked -p victory-shader --lib emit::lower::tests: 34/34 passed (including an_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 kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

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 the s_flbit_i32_b32 lowering uses). With bit 24 forced on, 8 - leading equals bitpos - 23 for 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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 - leading replaces the 7-step compare/select/add chain cleanly while keeping shift >= 1 even for q == 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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants