Shaders: normalized buffer codes load as the float nearest their quotient (port of upstream da82e44) #200

Open
github-bot wants to merge 3 commits from github-bot/gh-200 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:27 UTC · original
lcg007xd-c:port/norm-quotient → main · +385 −20 · 3 files · 3 commits · open

On the host, a typed buffer load of a normalized format (UNORM, SNORM) converts each code with one DXIL division, and the driver decides how that division rounds. The comment above format_load already says so: "Norm formats divide, which the AMD driver does 1 ULP off the hardware's correctly rounded conversion for some values; correct it with an integer check if a title compares converted values exactly."

Upstream found that this title does compare converted values exactly. On an RX 6700 XT, 125 of the 256 8-bit codes loaded one ulp high, and some 16-bit codes (257, 261, … 32896) loaded one ulp low. The title's mesh programs read a material index from a vertex colour's alpha as v_mul_legacy_f32 255.0 then v_cvt_u32_f32. A quotient one ulp low truncates to the code below, and the draw uses the wrong material. WARP was exact. See the upstream commit message for the measurements.

What

  • Port of upstream da82e44 (Marvis), cherry-picked with -x. For formats up to 16 bits a channel, the quotient a / (2^n - 1) is computed in integers and rounded to the nearest float, so it no longer depends on the host's division. The 11- and 12-bit widths are rounded fully in integers. Wider formats still divide. It picked cleanly onto this tree.
  • This PR is stacked on #190 (the ends of the range load as exactly 1.0 and -1.0). The exact quotient needs that change, because a code equal to the top of the range would wrap. The first commit here is #190's change, unchanged. Once #190 is merged, only the last two commits remain.
  • New here: a Linux check of the emitted arithmetic. The two emitting functions now take the module and block instead of &mut self, and the method calls them. A test-only Module::eval runs the instructions they emit: 32-bit integer ops and compares, selects, bitcasts, uitofp and fmul. The test normalized_quotients_as_emitted_are_the_nearest_floats runs that for every code of every width the loads use (1 to 16 bits, 131,054 codes). It compares the result with the host's own f32 division, which IEEE 754 rounds correctly. Every code agrees, and no shift reaches 32. With the rounding step removed, the test fails at 1 / 3.

Cost: about 18 to 21 DXIL operations a channel instead of 2. Upstream measured no change in frame time in the title (within run-to-run noise).

Tested (Linux, no GPU)

  • cargo test -p victory-shader --lib, with the branch merged into 4f62c2a: 197 passed, 0 failed (main: 196 + the new test).
  • cargo clippy -p victory-shader --all-targets for Linux and for x86_64-pc-windows-msvc: the same findings as main, none new. cargo check --tests --target x86_64-pc-windows-msvc builds the GPU tests.
  • rustfmt: no new differences in the touched files.

Not tested

  • The two hardware tests from upstream (normalized_buffer_formats_load_correctly_rounded_quotients, normalized_codes_truncate_back_to_themselves). They need a Windows GPU, and I have none here. On an RX 9070 XT they should fail on main and pass with this PR.
  • The title: I have no Windows machine or game files. A world run would show whether any material looks different.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:27 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/200) `lcg007xd-c:port/norm-quotient` → `main` · +385 −20 · 3 files · 3 commits · open On the host, a typed buffer load of a normalized format (UNORM, SNORM) converts each code with one DXIL division, and the driver decides how that division rounds. The comment above `format_load` already says so: *"Norm formats divide, which the AMD driver does 1 ULP off the hardware's correctly rounded conversion for some values; correct it with an integer check if a title compares converted values exactly."* Upstream found that this title does compare converted values exactly. On an RX 6700 XT, 125 of the 256 8-bit codes loaded one ulp high, and some 16-bit codes (257, 261, … 32896) loaded one ulp low. The title's mesh programs read a material index from a vertex colour's alpha as `v_mul_legacy_f32 255.0` then `v_cvt_u32_f32`. A quotient one ulp low truncates to the code below, and the draw uses the wrong material. WARP was exact. See the upstream commit message for the measurements. ## What - **Port of upstream `da82e44` (Marvis), cherry-picked with `-x`.** For formats up to 16 bits a channel, the quotient `a / (2^n - 1)` is computed in integers and rounded to the nearest float, so it no longer depends on the host's division. The 11- and 12-bit widths are rounded fully in integers. Wider formats still divide. It picked cleanly onto this tree. - **This PR is stacked on #190** (the ends of the range load as exactly 1.0 and -1.0). The exact quotient needs that change, because a code equal to the top of the range would wrap. The first commit here is #190's change, unchanged. Once #190 is merged, only the last two commits remain. - **New here: a Linux check of the emitted arithmetic.** The two emitting functions now take the module and block instead of `&mut self`, and the method calls them. A test-only `Module::eval` runs the instructions they emit: 32-bit integer ops and compares, selects, bitcasts, `uitofp` and `fmul`. The test `normalized_quotients_as_emitted_are_the_nearest_floats` runs that for every code of every width the loads use (1 to 16 bits, 131,054 codes). It compares the result with the host's own `f32` division, which IEEE 754 rounds correctly. Every code agrees, and no shift reaches 32. With the rounding step removed, the test fails at 1 / 3. Cost: about 18 to 21 DXIL operations a channel instead of 2. Upstream measured no change in frame time in the title (within run-to-run noise). ## Tested (Linux, no GPU) - `cargo test -p victory-shader --lib`, with the branch merged into `4f62c2a`: 197 passed, 0 failed (main: 196 + the new test). - `cargo clippy -p victory-shader --all-targets` for Linux and for `x86_64-pc-windows-msvc`: the same findings as main, none new. `cargo check --tests --target x86_64-pc-windows-msvc` builds the GPU tests. - rustfmt: no new differences in the touched files. ## Not tested - The two hardware tests from upstream (`normalized_buffer_formats_load_correctly_rounded_quotients`, `normalized_codes_truncate_back_to_themselves`). They need a Windows GPU, and I have none here. On an RX 9070 XT they should fail on main and pass with this PR. - The title: I have no Windows machine or game files. A world run would show whether any material looks different. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Buffer format loads divide a UNORM or SNORM field by its largest value.
A host may divide through a reciprocal and land just below 1.0: on an
RX 9070 (RDNA 4), 16_16_UNORM's 65535 loaded through the d16 path as the
half 0x3BFF instead of 0x3C00. The largest value now selects 1.0, and an
SNORM field's two lowest values select -1.0, as the formats define them.

(cherry picked from commit 645391bcce957054e39f2d3df52177062769283e)

Conflict resolved against the fork's own UNORM top-end select (same rule, kept in upstream's form); the SNORM ends are new. The PR's first commit (c430b1d) is already in the fork as a103937.
What was wrong: buffer and typed-buffer loads of normalized formats
(UNORM, SNORM) converted a code by dividing it on the host. The host's
division is not exact. On the RX 6700 XT, 125 of the 256 8-bit codes
loaded one ulp high, and 16-bit codes such as 257, 261, ..., 32896
loaded one ulp low; WARP was exact. The console's buffer unit gives the
float nearest the exact quotient, and titles rely on it in decisions:
Grand Theft Auto VI's mesh programs read a material index from a vertex
colour's alpha as `v_mul_legacy_f32 255.0` then `v_cvt_u32_f32`. A
quotient one ulp low truncates to the code below (the 16-bit codes above
did), so another material is picked.

What changed: for formats up to 16 bits per channel the quotient
a / (2^n - 1) is worked out from integers: Q = floor(a * 2^32 / d) as
a * K plus a correction (none when n divides 32), rounded to 24 bits up
exactly when its highest dropped bit is set (the quotient is never
exact, so never a tie), and that integer, at most 2^24 and so exact as a
float on any host, scaled by a power of two. The reference is the exact
quotient rounded to the nearest float, which removes the dependence on
how the host rounds a division or converts a large integer (WARP rounds
integers above 2^31 otherwise). Signed formats convert their magnitude
the same way; their two lowest codes stay -1.0. The 11- and 12-bit
widths, whose smallest codes need more bits, are rounded fully in
integers.

Cost: about 18-21 DXIL operations a channel where the division was 2 (a
division by a constant more where n does not divide 32), so a 4-channel
8-bit fetch is about 80 operations instead of about 12. The car body's
mesh program grows from 4,616 to 5,103 instructions (+487 for about 23
normalized channels). In the title it is not measurable: in a run of
the intro cutscene with this commit (cost2, on the previous main
4015a182), on world windows with no pipelines being made, the frame
median was 881 ms and the GPU 349 ms a frame (median 335), against
1,013 and 389 (365) for the same tree without it (s1a). Two runs of one
tree have differed by 132 ms of GPU a frame, so the difference is
within run-to-run noise.

Evidence: tests `normalized_buffer_formats_load_correctly_rounded_quotients`
(every 8- and 10-bit code and a 16-bit sample, unsigned and signed,
bit-exact on hardware and WARP; it failed on the RX 6700 XT before, at
8-bit code 3) and `normalized_codes_truncate_back_to_themselves` (the
title's truncation gives every code back; 16-bit code 257 came back as
256 before). Offline, the integer arithmetic as emitted matches the exact
quotient for every code of every width 2 to 16.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit da82e444ec46a162ca779c82d4cf191a69182971)
The quotient a normalized buffer code loads as is emitted as about twenty
integer operations, and the only checks of it so far run on a GPU. The
two functions that emit it take the module and block now (the method
calls them), so a test can emit them alone; `Module::eval` (test builds)
runs what they emit: 32-bit integer operations and compares, selects,
bitcasts, unsigned conversions to float and float products.

`normalized_quotients_as_emitted_are_the_nearest_floats` runs it for every
code of every width the loads use (1 to 16 bits; 131,054 codes) against
the host's own f32 division, which IEEE 754 rounds correctly: all agree,
and the shifts stay below 32 (the evaluator stops on any other). With the
rounding step taken out, it fails at 1 / 3.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:50 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-26 05:00 UTC · original

RDNA2 data point. This PR (c7f41cf) merged on main 4f62c2a, cargo test --release -p victory-shader --lib on an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1):

  • main: 275 passed, 2 failed.
  • This PR: 279 passed, 2 failed, in 2 runs out of 2. The 4 new tests pass.

The 2 failures are the same as on main: the RDNA2 wave-size tests scalar_counted_in_a_divergent_loop_read_after_it and scalar_counted_in_nested_divergent_loops, fixed by #110 and #204. No regression here. Not run in the game.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 05:00 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/200#issuecomment-7183093869) RDNA2 data point. This PR (`c7f41cf`) merged on `main` `4f62c2a`, `cargo test --release -p victory-shader --lib` on an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1): - `main`: 275 passed, 2 failed. - This PR: 279 passed, 2 failed, in 2 runs out of 2. The 4 new tests pass. The 2 failures are the same as on `main`: the RDNA2 wave-size tests `scalar_counted_in_a_divergent_loop_read_after_it` and `scalar_counted_in_nested_divergent_loops`, fixed by #110 and #204. No regression here. Not run in the game. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:47 UTC · original

Tested on Windows 11 with an RX 7900 XT (RDNA 3, driver 32.0.32029.2010), on the hardware adapter and on WARP.

This branch is based on 000ffdd. I merged head c7f41cf into 4f62c2a (no conflicts) and tested that merge.

Tests:

  • cargo test -p victory-shader --lib: 281 passed, 0 failed, 6 ignored. On 4f62c2a: 277 passed, 0 failed, 6 ignored.
  • The three new GPU tests pass on the RX 7900 XT and on WARP. normalized_quotients_as_emitted_are_the_nearest_floats also passes on Windows.
  • With only the new GPU tests applied to 4f62c2a, two fail on the RX 7900 XT:
    • normalized_buffer_formats_load_correctly_rounded_quotients fails first at 8-bit UNORM code 3, one ulp high.
    • normalized_codes_truncate_back_to_themselves fails at 16-bit code 257, which comes back as 256.
    • normalized_buffer_formats_load_their_ends_exactly already passes on main.
  • WARP gives exact results on main.

Mismatch counts on 4f62c2a, RX 7900 XT. I used a local counting copy of the correctly-rounded test, in a compute shader. It covers every code for 8 and 10 bits. For 16 bits it covers every 17th code plus 257, 261, 265, 269, 32896, 0x7FFF, 0x8000, 0x8001 and 0xFFFE (3865 codes).

  • 8_UNORM: 126 of 256 codes differ from the nearest float, all high. All 256 codes truncate back to themselves after * 255.
  • 8_SNORM: 16 of 256.
  • 2_10_10_10 x channel: UNORM 24 of 1024, all low. SNORM 96 of 1024.
  • 16_UNORM: 37 of 3865, all low. These 37 codes truncate to the code below after * 65535.
  • 16_SNORM: 86 of 3865.
  • WARP: 0 everywhere.

With this PR: 0 mismatches and 0 truncation failures on both adapters, for all of these.

Whole shaders.rpf (26084 programs, translate --validate --host-lanes 32-64), compared with 4f62c2a:

  • VS/PS 14247/14247, CS 2682/2682 and MS 8694/8765 validate, the same as main.
  • Refusals are unchanged (195 and 71).
  • 9693 programs change: 7776 mesh, 1077 compute, 840 vertex.
    • These are exactly the translated programs that have a typed buffer load (tbuffer_load_format_*) of a normalized format.
    • No other program changes.
  • Emitted DXIL grows 1.6% in total.
    • Over the changed programs: mesh +5.4%, vertex +5.3%, compute +1.5%.
    • Per changed program: median +5.3%, max +27%.

About the material-index case in the description:

  • The title's typed buffer loads use three normalized formats: 16_16_SNORM, 8_8_8_8_UNORM and 2_10_10_10_UNORM. None of them is 16-bit UNORM.
  • A scale by 255.0 followed by v_cvt_u32_f32 on a loaded normalized value appears in about 715 programs, all on 8_8_8_8_UNORM. On this card the 8-bit misses on main are all high, so these get the right code back.
  • I found no program that scales a 10-bit or 16-bit normalized channel by its maximum and truncates it. This comes from a simple scan of the disassembly that follows registers in listing order, without control flow. 2_10_10_10_UNORM has 24 codes one ulp low here, but I did not check whether they truncate wrongly.
  • So on this card I would not expect a visible change from that pattern. The PR still makes these loads match the exact quotient on hardware.

Not tested: a world run, frame time, or pipeline creation time with the larger shaders.

**@⁠adrianmjim** on GitHub · 2026-11-26 19:47 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/200#issuecomment-7201966151) Tested on Windows 11 with an RX 7900 XT (RDNA 3, driver 32.0.32029.2010), on the hardware adapter and on WARP. This branch is based on 000ffdd. I merged head c7f41cf into 4f62c2a (no conflicts) and tested that merge. Tests: - `cargo test -p victory-shader --lib`: 281 passed, 0 failed, 6 ignored. On 4f62c2a: 277 passed, 0 failed, 6 ignored. - The three new GPU tests pass on the RX 7900 XT and on WARP. `normalized_quotients_as_emitted_are_the_nearest_floats` also passes on Windows. - With only the new GPU tests applied to 4f62c2a, two fail on the RX 7900 XT: - `normalized_buffer_formats_load_correctly_rounded_quotients` fails first at 8-bit UNORM code 3, one ulp high. - `normalized_codes_truncate_back_to_themselves` fails at 16-bit code 257, which comes back as 256. - `normalized_buffer_formats_load_their_ends_exactly` already passes on main. - WARP gives exact results on main. Mismatch counts on 4f62c2a, RX 7900 XT. I used a local counting copy of the correctly-rounded test, in a compute shader. It covers every code for 8 and 10 bits. For 16 bits it covers every 17th code plus 257, 261, 265, 269, 32896, 0x7FFF, 0x8000, 0x8001 and 0xFFFE (3865 codes). - 8_UNORM: 126 of 256 codes differ from the nearest float, all high. All 256 codes truncate back to themselves after `* 255`. - 8_SNORM: 16 of 256. - 2_10_10_10 x channel: UNORM 24 of 1024, all low. SNORM 96 of 1024. - 16_UNORM: 37 of 3865, all low. These 37 codes truncate to the code below after `* 65535`. - 16_SNORM: 86 of 3865. - WARP: 0 everywhere. With this PR: 0 mismatches and 0 truncation failures on both adapters, for all of these. Whole `shaders.rpf` (26084 programs, `translate --validate --host-lanes 32-64`), compared with 4f62c2a: - VS/PS 14247/14247, CS 2682/2682 and MS 8694/8765 validate, the same as main. - Refusals are unchanged (195 and 71). - 9693 programs change: 7776 mesh, 1077 compute, 840 vertex. - These are exactly the translated programs that have a typed buffer load (`tbuffer_load_format_*`) of a normalized format. - No other program changes. - Emitted DXIL grows 1.6% in total. - Over the changed programs: mesh +5.4%, vertex +5.3%, compute +1.5%. - Per changed program: median +5.3%, max +27%. About the material-index case in the description: - The title's typed buffer loads use three normalized formats: 16_16_SNORM, 8_8_8_8_UNORM and 2_10_10_10_UNORM. None of them is 16-bit UNORM. - A scale by 255.0 followed by `v_cvt_u32_f32` on a loaded normalized value appears in about 715 programs, all on 8_8_8_8_UNORM. On this card the 8-bit misses on main are all high, so these get the right code back. - I found no program that scales a 10-bit or 16-bit normalized channel by its maximum and truncates it. This comes from a simple scan of the disassembly that follows registers in listing order, without control flow. 2_10_10_10_UNORM has 24 codes one ulp low here, but I did not check whether they truncate wrongly. - So on this card I would not expect a visible change from that pattern. The PR still makes these loads match the exact quotient on hardware. Not tested: a world run, frame time, or pipeline creation time with the larger shaders.
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/200/head:github-bot/gh-200'
git switch 'github-bot/gh-200'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-200'
git switch 'github-bot/gh-200'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-200'
git switch 'github-bot/gh-200'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-200'
git switch 'main'
git merge --squash 'github-bot/gh-200'
git switch 'main'
git merge --ff-only 'github-bot/gh-200'
git switch 'main'
git merge 'github-bot/gh-200'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.