Skip to content

Shaders: normalized buffer codes load as the float nearest their quotient (port of upstream da82e44, restores 645391b) - #218

Open
murillojndem wants to merge 1 commit into
DexNom:mainfrom
murillojndem:fix/normalized-formats-exact-quotients
Open

murillojndem wants to merge 1 commit into
DexNom:mainfrom
murillojndem:fix/normalized-formats-exact-quotients

Conversation

@murillojndem

Copy link
Copy Markdown

tbuffer_load_format_* loads of normalized formats (UNORM, SNORM) converted a code by dividing it on the host, code / (2^n - 1), and the host's division is not exact. On an RX 7900 XT (driver 32.0.32015.7029), main with the tests below:

  • loads 8-bit code 3 as 0x3c40c0c2, one ulp above the nearest float, 0x3c40c0c1;
  • gives 16-bit code 257 back as 256 after a multiply by 65535 and a truncation (v_mul_legacy_f32, then v_cvt_u32_f32).

A diagnostic run of the same codes on main (not in this PR) counted the loads the card gets wrong. WARP got every code right.

Channel Codes run Above the nearest float Below it
8-bit UNORM all 256 126 0
8-bit SNORM all 256 8 8
10-bit UNORM all 1024 0 24
10-bit SNORM all 1024 48 48
11-bit UNORM all 2048 0 9
16-bit UNORM 279 sampled 0 4 (257, 261, 265, 269)
16-bit SNORM 279 sampled 3 6
2-bit UNORM and SNORM all 4 each 0 0

On the same card, #214's d16 test read 16_16_UNORM's 65535 as the half 0x3bff instead of 0x3c00, as Marvisak/VIctory@645391b reports for an RX 9070.

Upstream's da82e44 describes the console's buffer unit as giving the float nearest the exact quotient, with the ends exactly 1.0 and -1.0; that was not checked on a console here. It also names a use in the title: mesh programs read a material index from a vertex colour's alpha as v_mul_legacy_f32 by 255.0, then v_cvt_u32_f32, so a quotient one ulp low picks the material below. On this card main already gives every 8-bit code back that way, since its 8-bit misses are all above the nearest float; the 16-bit truncation fails for 4 of the 279 sampled codes (257, 261, 265, 269). The title itself was not checked here.

Change, in format_load:

  • UNORM and SNORM fields of up to 16 bits convert through norm_quotient. It computes Q = floor(a * 2^32 / d) in integers, as a * K plus a correction when the width does not divide 32, and rounds it to 24 bits (the quotient is never exact, so there is never a tie). The rounded integer, at most 2^24, converts exactly on any host and is then scaled by a power of two.
  • The 11- and 12-bit widths take exact_norm, which rounds fully in integers. The fast path rounds the 11-bit codes 1, 3, 5 and 7 wrong; no buffer format has a 12-bit channel.
  • Selects give the largest code exactly 1.0 and a signed field's two lowest codes -1.0. norm_quotient is exact only below the top code.
  • A signed field converts its magnitude and then takes the sign.

This ports Marvisak/VIctory@da82e44 (by Marvis), which is not in this repository's history, together with Marvisak/VIctory@645391b (by Dufftwt), which is: 570a941 ("Publish DexNom Runtime Radeon sources with upstream credits and build instructions") put the older code back over it, with no note on why. If that was deliberate for the Radeon runtime, this PR is not wanted. The lowering is upstream's, and so are the tests, with three changes:

  • a local MTBUF helper in place of upstream's tbuffer_load_of_v1, which this tree does not have (if Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR (port of upstream c430b1d) #214 merges first, the helper can use its mtbuf);
  • a 16-bit stride of 251 instead of 257, since 257k / 65535 = k / 255 makes a stride of 257 repeat the 8-bit quotients;
  • three more formats in the quotient test: 10_11_11_UNORM, whose 11-bit channel is the width exact_norm takes, and 10_10_10_2_UNORM and _SNORM, whose x channel has 2 bits.

The commit merges without conflicts with each of my open PRs (#204 to #206, #208, #209, #211, #213 to #217).

Tests, in emit::valu_tests:

  • normalized_buffer_formats_load_their_ends_exactly (645391b): the ends of 8_UNORM, 16_UNORM, 8_SNORM and 16_SNORM.
  • normalized_buffer_formats_load_correctly_rounded_quotients: every code of the 2-, 8-, 10- and 11-bit channels and 279 sampled 16-bit codes, unsigned and signed, against Rust's correctly rounded f32 division.
  • normalized_codes_truncate_back_to_themselves: the title's multiply and truncation give every 8-bit code and the sampled 16-bit codes back.

Results on 4ad8692:

  • RX 7900 XT, main with the new tests: the quotient test fails at 8-bit code 3 and the truncation test at 16-bit code 257, as above. The ends test passes there: in those programs the card's division lands on 1.0 and -1.0.
  • RX 7900 XT, with the change: the three pass. The whole suite gives 240 passed, 2 failed, 4 ignored; the two failures are the rect tests, which fail PSV0 validation with the SDK 10.0.22621 dxil.dll on main too.
  • RX 7900 XT, with Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR (port of upstream c430b1d) #214's d16 packing applied as well: Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR (port of upstream c430b1d) #214's test passes with the end cases it had at 8da103b (16_16_UNORM 0xffff, 16_16_SNORM 0x7fff and 0x8001) added back. At 8da103b alone, 0xffff gave 0x3bff.
  • Mutations, on WARP: sending the 11-bit channel through the fast path fails the quotient test at 11-bit code 1 (0x3a001000 for 0x3a001002), and dropping the UNORM top select fails it at 10-bit code 1023 (0.0 for 1.0).
  • WARP: the three pass before and after; WARP divides exactly, so it does not show the bug. The suite goes from 234 passed, 5 failed, 4 ignored to 237, 5, 4, with the same five failures: the two rect tests, and three valu_tests that fail pipeline creation on WARP only.
  • A Python emulation of the emitted operations matches the exactly rounded quotient for every code strictly between 0 and the top, at every width 2 to 16 (131,038 codes). It models this code and is checked against an exact reference; it is not a test in the tree.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings is clean with and without --all-features, cargo xtask policy passes, and rustfmt reports nothing new.

Limits:

Claude (Anthropic) ported the upstream changes and tests, ran the hardware and WARP checks, the diagnostic count and the offline emulation.

Generated with Claude Code

…ient

tbuffer_load_format loads of UNORM and SNORM formats divided the code
on the host, and the host's division is not exact. On an RX 7900 XT,
126 of the 256 8-bit UNORM codes loaded above the nearest float, and
16-bit code 257, multiplied by 65535 and truncated, came back as 256;
with DexNom#214's d16 packing, 16_16_UNORM's 65535 loaded as the half 0x3bff.
Upstream describes the console's buffer unit as giving the float
nearest the exact quotient, with the ends exactly 1.0 and -1.0.

Fields of up to 16 bits now convert through norm_quotient: Q =
floor(a * 2^32 / d) in integers, rounded to 24 bits (never a tie),
converted exactly and scaled by a power of two; 11 and 12 bits take
exact_norm, rounded fully in integers. Selects give the largest code
1.0 and a signed field's two lowest codes -1.0.

Tests: normalized_buffer_formats_load_their_ends_exactly,
normalized_buffer_formats_load_correctly_rounded_quotients and
normalized_codes_truncate_back_to_themselves. The last two fail on main
on the RX 7900 XT; all three pass with this change, on the card and on
WARP. Unlike upstream's, the 16-bit sample steps by 251 (257k / 65535
is k / 255), and the quotient test adds 10_11_11_UNORM and
10_10_10_2_UNORM and _SNORM.

Ports Marvisak/VIctory da82e44, and 645391b, which 570a941 removed.

Co-authored-by: Marvis <marvis@noreply.invalid>
Co-authored-by: Dufftwt <19514584+Dufftwt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant