Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR #169

Manually merged
github merged 2 commits from gh-pr/169/head into gh-pr/169/base 2026-11-26 03:53:11 +00:00
Owner

@⁠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 UTC

What'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.rs only recognises d16 for names that start with buffer_:

let d16 = name.starts_with("buffer_") && name.contains("_format_d16_");

so tbuffer_load_format_d16_* takes the normal 32-bit path:

memory (16_16_FLOAT):   0x4000_3C00          = the f16 pair (1.0, 2.0)
hardware gives v0:      0x4000_3C00
we give v0:             0x3F80_0000          = channel x as a 32-bit float; channel y is lost

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

  • d16 now applies to tbuffer_ loads as well as buffer_ ones.

  • The typed-format load path (format_load, used by every tbuffer_load) packs its channels when the instruction is d16. A new helper, d16_channels, takes the channels format_load already decoded and packs them two per register, low half first. If the channel count is odd, the last high half is 0.

    • Integer formats (UINT/SINT) keep each channel's low 16 bits.
    • All other formats become f16, using the same conversion the existing buffer_load_format_d16 path uses.
  • A normalized format's ends load as exactly 1.0 and -1.0 (second commit). format_load divides 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) loaded 16_16_UNORM's 65535 through this d16 path as the half 0x3BFF, not 0x3C00. 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, loading 16_16_FLOAT data (1.0, 2.0) gives 0x4000_3C00 in one register, and 16_16_UNORM (0, 0xFFFF) gives the f16 pair (0.0, 1.0).

Tests

The new test tbuffer_d16_loads_pack_two_channels_per_dword in valu_tests.rs uses 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_xy of 16_16_FLOAT
  • d16_xy of 16_16_UINT
  • d16_xy of 16_16_UNORM
  • d16_x of 16_FLOAT

A second test, normalized_buffer_formats_load_their_ends_exactly, loads the largest value of 8_UNORM and 16_UNORM, and the largest and two lowest of 8_SNORM and 16_SNORM, through tbuffer_load_format_x. They must be exactly 1.0 and -1.0.

I ran cargo test -p victory-shader on GitHub's windows-latest with Rust 1.98.1 from rust-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 gives 0x3F800000, channel x as a 32-bit float, instead of the stored 0x40003C00.

  • 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 c430b1d the d16 test gave 0x3bff0000 for 0xffff0000 on an RX 9070 (alexdeliu, below).

  • On RDNA 4 hardware (alexdeliu, RX 9070, 645391b on main): cargo test -p victory-shader passes 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 legacyF32ToF16 with a round-to-nearest-even f16_nearest, since AMD's driver rounds toward zero. The new call in d16_channels should go through it too, whichever lands second. #164 also edits buffer(), a few lines above the d16 line. It refuses the _d16_hi_ forms; this PR packs the plain tbuffer d16 loads. They don't overlap in behaviour, but whichever lands second may need a small rebase.

**@⁠Dufftwt** on GitHub · 2026-11-24 01:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/169) `Dufftwt:fix/tbuffer-d16-loads-packed` → `main` · +131 −7 · 2 files · 2 commits · merged 2026-11-25 00:16 UTC ## What'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.rs` only recognises d16 for names that start with `buffer_`: ```rust let d16 = name.starts_with("buffer_") && name.contains("_format_d16_"); ``` so `tbuffer_load_format_d16_*` takes the normal 32-bit path: ``` memory (16_16_FLOAT): 0x4000_3C00 = the f16 pair (1.0, 2.0) hardware gives v0: 0x4000_3C00 we give v0: 0x3F80_0000 = channel x as a 32-bit float; channel y is lost ``` 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 - `d16` now applies to `tbuffer_` loads as well as `buffer_` ones. - The typed-format load path (`format_load`, used by every `tbuffer_load`) packs its channels when the instruction is d16. A new helper, `d16_channels`, takes the channels `format_load` already decoded and packs them two per register, low half first. If the channel count is odd, the last high half is 0. - Integer formats (`UINT`/`SINT`) keep each channel's low 16 bits. - All other formats become f16, using the same conversion the existing `buffer_load_format_d16` path uses. - A normalized format's ends load as exactly 1.0 and -1.0 (second commit). `format_load` divides 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) loaded `16_16_UNORM`'s 65535 through this d16 path as the half `0x3BFF`, not `0x3C00`. 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, loading `16_16_FLOAT` data `(1.0, 2.0)` gives `0x4000_3C00` in one register, and `16_16_UNORM` `(0, 0xFFFF)` gives the f16 pair `(0.0, 1.0)`. ## Tests The new test `tbuffer_d16_loads_pack_two_channels_per_dword` in `valu_tests.rs` uses 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_xy` of `16_16_FLOAT` - `d16_xy` of `16_16_UINT` - `d16_xy` of `16_16_UNORM` - `d16_x` of `16_FLOAT` A second test, `normalized_buffer_formats_load_their_ends_exactly`, loads the largest value of `8_UNORM` and `16_UNORM`, and the largest and two lowest of `8_SNORM` and `16_SNORM`, through `tbuffer_load_format_x`. They must be exactly 1.0 and -1.0. I ran `cargo test -p victory-shader` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-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 gives `0x3F800000`, channel x as a 32-bit float, instead of the stored `0x40003C00`. - 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 `c430b1d` the d16 test gave `0x3bff0000` for `0xffff0000` on an RX 9070 (alexdeliu, below). - **On RDNA 4 hardware (alexdeliu, RX 9070, `645391b` on `main`):** `cargo test -p victory-shader` passes 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 `legacyF32ToF16` with a round-to-nearest-even `f16_nearest`, since AMD's driver rounds toward zero. The new call in `d16_channels` should go through it too, whichever lands second. #164 also edits `buffer()`, a few lines above the `d16` line. It refuses the `_d16_hi_` forms; this PR packs the plain `tbuffer` d16 loads. They don't overlap in behaviour, but whichever lands second may need a small rebase.
The d16 forms of the typed buffer loads return each channel as 16 bits,
two per VGPR, low half first; the decoder already sizes their data that
way. The lowering only treated `buffer_` names as d16, so a
tbuffer_load_format_d16_* went through the 32-bit path: the first VGPR
got channel x as a 32-bit value (the f32 bits of 1.0 instead of the f16
pair (1.0, 2.0)) and the other channels were dropped.

The format path now packs its channels for d16: integer formats keep
their low 16 bits, the other formats become f16 (as the MUBUF d16 path
converts them).
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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:53:08 +00:00
github manually merged commit 645391bcce into gh-pr/169/base 2026-11-26 03:53:11 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 01:19 UTC · original

Tested a103937 on 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.
  • The full workspace still needs #112 for the victory-gpu E0425.

No game data used.

**@⁠DaviTostes** on GitHub · 2026-11-24 01:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7118987935) Tested `a103937` on **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. - The full workspace still needs #112 for the `victory-gpu` E0425. No game data used.
Author
Owner

@⁠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_dword passes on an NVIDIA RTX 5070 and fails on an AMD Radeon integrated GPU (RDNA2, same machine, selected with VICTORY_ADAPTER=radeon), in a tree that includes this PR on main d2a4bae:

hardware: d16 opcode 9 format 23 of 0xffff0000 = 0x3bff0000, want 0x3c000000

So for a 16-bit UNORM channel of 0xFFFF the half comes out one step below 1.0 on that driver. The comment already in lower.rs about 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 on 0x3BFF. 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.

**@⁠r4tedx** on GitHub · 2026-11-24 04:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7122451183) A result from a second adapter, in case it helps. This PR's test `tbuffer_d16_loads_pack_two_channels_per_dword` passes on an NVIDIA RTX 5070 and fails on an AMD Radeon integrated GPU (RDNA2, same machine, selected with `VICTORY_ADAPTER=radeon`), in a tree that includes this PR on `main` `d2a4bae`: ``` hardware: d16 opcode 9 format 23 of 0xffff0000 = 0x3bff0000, want 0x3c000000 ``` So for a 16-bit UNORM channel of `0xFFFF` the half comes out one step below 1.0 on that driver. The comment already in `lower.rs` about 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 on `0x3BFF`. 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.
Author
Owner

@⁠Damasque on GitHub · 2026-11-24 06:52 UTC · original

Reproduced r4tedx’s result at a103937 on 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 06:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7124379098) Reproduced r4tedx’s result at a103937 on 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.
Author
Owner

@⁠Damasque on GitHub · 2026-11-24 07:00 UTC · original

At a103937 on 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 07:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7124511395) At a103937 on 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.
Author
Owner

@⁠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.

**@⁠Damasque** on GitHub · 2026-11-24 08:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7126467748) @⁠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.
Author
Owner

@⁠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_dword fails on the hardware backend, merged onto main @ ffe310eb (PR head c430b1de), the same on a retry:

hardware: d16 opcode 9 format 23 of 0xffff0000 = 0x3bff0000, want 0x3c000000

16_16_UNORM 0xFFFF should load as f16 1.0 (0x3C00) but comes back as 0x3BFF, 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

**@⁠alexdeliu** on GitHub · 2026-11-24 17:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7139114869) On an AMD RX 9070 (RDNA4) the new `tbuffer_d16_loads_pack_two_channels_per_dword` fails on the hardware backend, merged onto `main` @ `ffe310eb` (PR head `c430b1de`), the same on a retry: ``` hardware: d16 opcode 9 format 23 of 0xffff0000 = 0x3bff0000, want 0x3c000000 ``` `16_16_UNORM` `0xFFFF` should load as f16 1.0 (`0x3C00`) but comes back as `0x3BFF`, 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. <sub>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</sub>
Author
Owner

@⁠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_load rather 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 gives 0x3BFF. Exact 1.0 can't convert to 0x3BFF under 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 of 8_UNORM, 16_UNORM, 8_SNORM and 16_SNORM. It passes on WARP (122 passed on windows-latest), but WARP passed before too.

Could you rerun cargo test -p victory-shader on the RX 9070 at 645391b? Both tbuffer_d16_loads_pack_two_channels_per_dword and the new test should pass on the hardware backend now.

**@⁠Dufftwt** on GitHub · 2026-11-24 17:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7139210982) @⁠alexdeliu thanks for running it on hardware. That's a real bug, and it's in `format_load` rather 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 gives `0x3BFF`. Exact 1.0 can't convert to `0x3BFF` under 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 of `8_UNORM`, `16_UNORM`, `8_SNORM` and `16_SNORM`. It passes on WARP (122 passed on `windows-latest`), but WARP passed before too. Could you rerun `cargo test -p victory-shader` on the RX 9070 at `645391b`? Both `tbuffer_d16_loads_pack_two_channels_per_dword` and the new test should pass on the hardware backend now.
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 17:33 UTC · original

@⁠Dufftwt reran it at 645391b, merged onto main @ ffe310eb, on the RX 9070. cargo test -p victory-shader passes 122 tests, 1 ignored. Both tests pass on the hardware backend:

test emit::valu_tests::tbuffer_d16_loads_pack_two_channels_per_dword ... ok
test emit::valu_tests::normalized_buffer_formats_load_their_ends_exactly ... ok
**@⁠alexdeliu** on GitHub · 2026-11-24 17:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7139363125) @⁠Dufftwt reran it at `645391b`, merged onto `main` @ `ffe310eb`, on the RX 9070. `cargo test -p victory-shader` passes 122 tests, 1 ignored. Both tests pass on the hardware backend: ``` test emit::valu_tests::tbuffer_d16_loads_pack_two_channels_per_dword ... ok test emit::valu_tests::normalized_buffer_formats_load_their_ends_exactly ... ok ```
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!169
No description provided.