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

Open
github-bot wants to merge 1 commit from github-bot/gh-2893 into main AGit
Owner

@⁠Ingenioeren on GitHub · 2026-11-25 23:16 UTC · original
Ingenioeren:perf/unorm-rounding-firstbithi → main · +53 −8 · 3 files · 1 commits · open

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.

**@⁠Ingenioeren** on GitHub · 2026-11-25 23:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2893) `Ingenioeren:perf/unorm-rounding-firstbithi` → `main` · +53 −8 · 3 files · 1 commits · open ## 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://git.gtahub.st/github/Marvisak--VIctory/pulls/1112) [PR #1940](https://git.gtahub.st/github/Marvisak--VIctory/pulls/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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:16:54 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:28 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-26 04:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2893#issuecomment-7182626924) <!-- 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)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2893/head:github-bot/gh-2893'
git switch 'github-bot/gh-2893'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2893'
git switch 'github-bot/gh-2893'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2893'
git switch 'github-bot/gh-2893'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2893'
git switch 'main'
git merge --squash 'github-bot/gh-2893'
git switch 'main'
git merge --ff-only 'github-bot/gh-2893'
git switch 'main'
git merge 'github-bot/gh-2893'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2893
No description provided.