Repository navigation
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
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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),mainwith the tests below:0x3c40c0c2, one ulp above the nearest float,0x3c40c0c1;v_mul_legacy_f32, thenv_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.On the same card, #214's d16 test read 16_16_UNORM's 65535 as the half
0x3bffinstead of0x3c00, 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_f32by 255.0, thenv_cvt_u32_f32, so a quotient one ulp low picks the material below. On this cardmainalready 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:norm_quotient. It computesQ = floor(a * 2^32 / d)in integers, asa * Kplus 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.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.norm_quotientis exact only below the top code.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:
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 itsmtbuf);exact_normtakes, 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:mainwith 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.recttests, which fail PSV0 validation with the SDK 10.0.22621dxil.dllonmaintoo.0xffff, 16_16_SNORM0x7fffand0x8001) added back. At 8da103b alone,0xffffgave0x3bff.0x3a001000for0x3a001002), and dropping the UNORM top select fails it at 10-bit code 1023 (0.0 for 1.0).recttests, and threevalu_teststhat fail pipeline creation on WARP only.cargo clippy -p victory-shader --all-targets --locked -- -D warningsis clean with and without--all-features,cargo xtask policypasses, and rustfmt reports nothing new.Limits:
tbuffer_load_format_*of a normalized format pays it. Upstream reports the difference in a run of the title as within run-to-run noise; it was not measured here.legacyF32ToF16, so below the ends its rounding is the host's; Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR (port of upstream c430b1d) #214's test covers a few codes only.tbuffer_load_format_*was not measured here.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