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

Open
Ingenioeren wants to merge 1 commit from upstream-pr/2893 into main
Ingenioeren commented 2026-11-25 23:16:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-25T23:16:44Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5500840109 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2893). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-25T23:16:44Z. --- ## 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](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#firstbithi) ## 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](https://github.com/Marvisak/VIctory/pull/1112) [PR #1940](https://github.com/Marvisak/VIctory/pull/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.
fuzeofficlal commented 2026-11-25 23:31:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:31:27Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066444960 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2893#pullrequestreview-6066444960). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T23:31:27Z. **Review:** COMMENTED --- 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 commented 2026-11-25 23:34:42 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066474973 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2893#pullrequestreview-6066474973). Originally posted by [kotekompotek](https://github.com/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.
Marvisak commented 2026-11-26 05:04:11 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182626924 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2893#issuecomment-7182626924). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:28:30Z. --- <!-- victory-triage v1 head=35e7e57bf234b13666baa0a7d4827936230ef3ed verdict=merge-candidate at=2026-11-26T04:28:25Z --> 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](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 05:52:26 +00:00 (Migrated from github.com)

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):

  • 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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6069176995 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2893#pullrequestreview-6069176995). Originally posted by [Dukestar69](https://github.com/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): - 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).
andrewpozdnakov7-jpg commented 2026-11-27 17:13:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T17:13:32Z.


I tested #2893 together with #3007 on 63bcca4. Combined branch 227937e preserves 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.0 bit 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_hi tests passed. The four 32-bit forms emitted 18 DXIL instructions each and the two 24-bit forms emitted 22 each. SFI0 left INT64 clear in all six. Strict shader Clippy and the asset policy passed.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7224168061 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2893#issuecomment-7224168061). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-27T17:13:32Z. --- I tested #2893 together with #3007 on `63bcca4`. [Combined branch `227937e`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/review/mul-hi-and-exact-unorm-scan) preserves 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.0` bit 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_hi` tests passed. The four 32-bit forms emitted 18 DXIL instructions each and the two 24-bit forms emitted 22 each. SFI0 left `INT64` clear in all six. Strict shader Clippy and the asset policy passed.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2893:upstream-pr/2893'
git switch 'upstream-pr/2893'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/2893'
git switch 'upstream-pr/2893'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2893'
git switch 'upstream-pr/2893'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2893'
git switch 'main'
git merge --squash 'upstream-pr/2893'
git switch 'main'
git merge --ff-only 'upstream-pr/2893'
git switch 'main'
git merge 'upstream-pr/2893'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Marvisak/VIctory!2891
No description provided.