Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR #169
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!169
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/169/head"
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?
@Dufftwt on GitHub · 2026-11-24 01:03 UTC · original
Dufftwt:fix/tbuffer-d16-loads-packed→main· +131 −7 · 2 files · 2 commits · merged 2026-11-25 00:16 UTCWhat's wrong
The console GPU has "d16" forms of its typed buffer loads,
tbuffer_load_format_d16_x / _xy / _xyz / _xyzw. Instead of one 32-bit value per channel, they return each channel as 16 bits, packed two per register, low half first. Half-precision vertex data is typically read this way. The decoder (fields.rs) already knows this and sizes the result as half as many registers.The lowering in
lower.rsonly recognises d16 for names that start withbuffer_:so
tbuffer_load_format_d16_*takes the normal 32-bit path:Every shader that loads 16-bit data this way gets garbage in that register, and the other channels are silently dropped, because only half as many registers exist for them.
What the fix does
d16now applies totbuffer_loads as well asbuffer_ones.The typed-format load path (
format_load, used by everytbuffer_load) packs its channels when the instruction is d16. A new helper,d16_channels, takes the channelsformat_loadalready decoded and packs them two per register, low half first. If the channel count is odd, the last high half is 0.UINT/SINT) keep each channel's low 16 bits.buffer_load_format_d16path uses.A normalized format's ends load as exactly 1.0 and -1.0 (second commit).
format_loaddivides a UNORM or SNORM field by its largest value, and a host may divide through a reciprocal and land just below 1.0. alexdeliu's RX 9070 (RDNA 4) loaded16_16_UNORM's 65535 through this d16 path as the half0x3BFF, not0x3C00. The largest value now selects 1.0. An SNORM field's two lowest values select -1.0, which replaces the clamp. This applies to non-d16 loads too.Otherwise, non-d16 loads and all
buffer_loads don't change.How it behaves after the fix
tbuffer_load_format_d16_*fills its registers the way the hardware does. For example, loading16_16_FLOATdata(1.0, 2.0)gives0x4000_3C00in one register, and16_16_UNORM(0, 0xFFFF)gives the f16 pair(0.0, 1.0).Tests
The new test
tbuffer_d16_loads_pack_two_channels_per_dwordinvalu_tests.rsuses the synthetic-shader harness from #37. The shader stores a dword to the buffer and reads it back with the d16 load, in four cases:d16_xyof16_16_FLOATd16_xyof16_16_UINTd16_xyof16_16_UNORMd16_xof16_FLOATA second test,
normalized_buffer_formats_load_their_ends_exactly, loads the largest value of8_UNORMand16_UNORM, and the largest and two lowest of8_SNORMand16_SNORM, throughtbuffer_load_format_x. They must be exactly 1.0 and -1.0.I ran
cargo test -p victory-shaderon GitHub'swindows-latestwith Rust 1.98.1 fromrust-toolchain.toml. There's no GPU there, so the harness runs the shaders on WARP. Results:This PR (
645391b): both new tests pass; the shader crate has 122 passed, 1 ignored.The d16 test alone on
main: it fails. The load gives0x3F800000, channel x as a 32-bit float, instead of the stored0x40003C00.WARP divides exactly, so the second test passes there with or without the second commit. The case it covers was found on RDNA 4 hardware: at
c430b1dthe d16 test gave0x3bff0000for0xffff0000on an RX 9070 (alexdeliu, below).On RDNA 4 hardware (alexdeliu, RX 9070,
645391bonmain):cargo test -p victory-shaderpasses 122, 1 ignored. Both new tests pass on the hardware backend.Not tested: a title's shaders that use these instructions.
Related: #422 replaces every
legacyF32ToF16with a round-to-nearest-evenf16_nearest, since AMD's driver rounds toward zero. The new call ind16_channelsshould go through it too, whichever lands second. #164 also editsbuffer(), a few lines above thed16line. It refuses the_d16_hi_forms; this PR packs the plaintbufferd16 loads. They don't overlap in behaviour, but whichever lands second may need a small rebase.@DaviTostes on GitHub · 2026-11-24 01:19 UTC · original
Tested
a103937on Arch Linux x86_64, rustc 1.98.1: pass.cargo test -p victory-shader: 56 passed, 0 failed (2 test binaries), including the 1 test this PR adds.victory-gpuE0425.No game data used.
@r4tedx on GitHub · 2026-11-24 04:54 UTC · original
A result from a second adapter, in case it helps. This PR's test
tbuffer_d16_loads_pack_two_channels_per_dwordpasses on an NVIDIA RTX 5070 and fails on an AMD Radeon integrated GPU (RDNA2, same machine, selected withVICTORY_ADAPTER=radeon), in a tree that includes this PR onmaind2a4bae:So for a 16-bit UNORM channel of
0xFFFFthe half comes out one step below 1.0 on that driver. The comment already inlower.rsabout norm formats ("Norm formats divide, which the AMD driver does 1 ULP off the hardware's") looks like the same effect: the f32 result of the divide is just under 1.0 there, and the narrowing to f16 then lands on0x3BFF. The packing itself (which the PR is about) is right on both.It does not block the PR, but the test will fail for anyone on AMD as it stands. Either the expected value could allow the neighbouring half for norm formats, or the conversion could avoid the divide for the all-ones input (the exact rule for UNORM is value / (2^n - 1), with all-ones giving exactly 1.0).
From synthetic tests on those two adapters only; not run against the title.
@Damasque on GitHub · 2026-11-24 06:52 UTC · original
Reproduced r4tedx’s result at
a103937on an AMD Radeon RX 6700 XT, driver 32.0.21045.29003. I verified the dispatch device by D3D12 adapter LUID.cargo +1.98.1 test --locked --offline -p victory-shader --lib emit::valu_tests::tbuffer_d16_loads_pack_two_channels_per_dword -- --exact --test-threads=1 --nocapture
16_16_FLOAT and 16_16_UINT passed on both adapters. On the RX 6700 XT, 16_16_UNORM returned 0x3bff0000 for input 0xffff0000, with 0x3c000000 expected. The assertion stopped the run before WARP reached UNORM or either adapter reached 16_FLOAT. A WARP-only rerun passed all four cases and returned 0x3c000000 for UNORM.
@Damasque on GitHub · 2026-11-24 07:00 UTC · original
At
a103937on the RX 6700 XT (driver 32.0.21045.29003), I added a UAV readback of the normalized high channel for 0xFFFF0000 while keeping the d16 conversion in place. With the readback, both Radeon and WARP gave f32 0x3F800000 and packed 0x3C000000. The original Radeon test returned 0x3BFF0000 before and after removing the probe. The extra readback changes the Radeon result, so the intermediate in the failing shader is still unknown.@Damasque on GitHub · 2026-11-24 08:42 UTC · original
@Dufftwt I tested the explicit 0xFFFF → 1.0f32 case in format_load at
a103937. On the RX 6700 XT (driver 32.0.21045.29003), the clean test reproduced 0x3BFF0000. With the fix, all four cases in the original test passed on the LUID-verified Radeon and WARP with the existing expectations. Packed outputs for 0x0000, 0x8000, and 0xFFFE matched before and after on both. I can send the small patch for your branch.@alexdeliu on GitHub · 2026-11-24 17:23 UTC · original
On an AMD RX 9070 (RDNA4) the new
tbuffer_d16_loads_pack_two_channels_per_dwordfails on the hardware backend, merged ontomain@ffe310eb(PR headc430b1de), the same on a retry:16_16_UNORM0xFFFFshould load as f16 1.0 (0x3C00) but comes back as0x3BFF, one ulp below. So the unorm→f16 path is not exact for the top value. CI only runs WARP, so it will not show this.Checked with CI's steps (both clippy runs with
-D warnings,cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87@Dufftwt on GitHub · 2026-11-24 17:26 UTC · original
@alexdeliu thanks for running it on hardware. That's a real bug, and it's in
format_loadrather than the d16 packing. The UNORM decode divides the field by 65535, and the RX 9070 evidently does that through a reciprocal, landing just below 1.0. Converting to half then gives0x3BFF. Exact 1.0 can't convert to0x3BFFunder any rounding, so the f32 was already low. WARP divides exactly, which is why CI never saw it.Fixed in
645391b: a normalized format's largest value now selects exactly 1.0, and an SNORM field's two lowest values select -1.0, as the formats define them. That also covers the non-d16 loads, which returned 0.99999994 on such hardware. A new test,normalized_buffer_formats_load_their_ends_exactly, checks the ends of8_UNORM,16_UNORM,8_SNORMand16_SNORM. It passes on WARP (122 passed onwindows-latest), but WARP passed before too.Could you rerun
cargo test -p victory-shaderon the RX 9070 at645391b? Bothtbuffer_d16_loads_pack_two_channels_per_dwordand the new test should pass on the hardware backend now.@alexdeliu on GitHub · 2026-11-24 17:33 UTC · original
@Dufftwt reran it at
645391b, merged ontomain@ffe310eb, on the RX 9070.cargo test -p victory-shaderpasses 122 tests, 1 ignored. Both tests pass on the hardware backend: