Shaders: normalized buffer codes load as the float nearest their quotient (port of upstream da82e44) #200
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Belobug--VIctory-Belobug-AMD!200
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-200"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@lcg007xd-c on GitHub · 2026-11-25 21:27 UTC · original
lcg007xd-c:port/norm-quotient→main· +385 −20 · 3 files · 3 commits · openOn 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_loadalready 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.0thenv_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
da82e44(Marvis), cherry-picked with-x. For formats up to 16 bits a channel, the quotienta / (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.&mut self, and the method calls them. A test-onlyModule::evalruns the instructions they emit: 32-bit integer ops and compares, selects, bitcasts,uitofpandfmul. The testnormalized_quotients_as_emitted_are_the_nearest_floatsruns 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 ownf32division, 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 into4f62c2a: 197 passed, 0 failed (main: 196 + the new test).cargo clippy -p victory-shader --all-targetsfor Linux and forx86_64-pc-windows-msvc: the same findings as main, none new.cargo check --tests --target x86_64-pc-windows-msvcbuilds the GPU tests.Not tested
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.🤖 Generated with Claude Code
@MamaLeVrai on GitHub · 2026-11-26 05:00 UTC · original
RDNA2 data point. This PR (
c7f41cf) merged onmain4f62c2a,cargo test --release -p victory-shader --libon an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1):main: 275 passed, 2 failed.The 2 failures are the same as on
main: the RDNA2 wave-size testsscalar_counted_in_a_divergent_loop_read_after_itandscalar_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.
@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 headc7f41cfinto4f62c2a(no conflicts) and tested that merge.Tests:
cargo test -p victory-shader --lib: 281 passed, 0 failed, 6 ignored. On4f62c2a: 277 passed, 0 failed, 6 ignored.normalized_quotients_as_emitted_are_the_nearest_floatsalso passes on Windows.4f62c2a, two fail on the RX 7900 XT:normalized_buffer_formats_load_correctly_rounded_quotientsfails first at 8-bit UNORM code 3, one ulp high.normalized_codes_truncate_back_to_themselvesfails at 16-bit code 257, which comes back as 256.normalized_buffer_formats_load_their_ends_exactlyalready passes 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).* 255.* 65535.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 with4f62c2a:tbuffer_load_format_*) of a normalized format.About the material-index case in the description:
v_cvt_u32_f32on 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.Not tested: a world run, frame time, or pipeline creation time with the larger shaders.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.