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

Merged
Dufftwt merged 2 commits from upstream-pr/169 into main 2026-11-25 00:16:08 +00:00
Dufftwt commented 2026-11-24 01:03:35 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T01:03:35Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6572209059 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T01:03:35Z. --- ## 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.
DaviTostes commented 2026-11-24 06:54:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T01:19:32Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118987935 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7118987935). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T01:19:32Z. --- 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.
r4tedx commented 2026-11-24 06:55:54 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-24T04:54:48Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7122451183 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7122451183). Originally posted by [r4tedx](https://github.com/r4tedx) on 2026-11-24T04:54:48Z. --- 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.
Damasque commented 2026-11-24 06:56:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Damasque on 2026-11-24T06:52:28Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7124379098 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7124379098). Originally posted by [Damasque](https://github.com/Damasque) on 2026-11-24T06:52:28Z. --- 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 commented 2026-11-24 07:01:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Damasque on 2026-11-24T07:00:15Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7124511395 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7124511395). Originally posted by [Damasque](https://github.com/Damasque) on 2026-11-24T07:00:15Z. --- 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 commented 2026-11-24 09:06:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Damasque on 2026-11-24T08:42:42Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7126467748 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7126467748). Originally posted by [Damasque](https://github.com/Damasque) on 2026-11-24T08:42:42Z. --- @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 commented 2026-11-24 18:05:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T17:23:11Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139114869 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7139114869). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T17:23:11Z. --- 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>
Dufftwt commented 2026-11-24 18:05:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dufftwt on 2026-11-24T17:26:59Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7139210982 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7139210982). Originally posted by [Dufftwt](https://github.com/Dufftwt) on 2026-11-24T17:26:59Z. --- @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.
alexdeliu commented 2026-11-24 18:05:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by alexdeliu on 2026-11-24T17:33:05Z.


@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
<!-- greyfinch-sync:Marvisak/VIctory:comment:7139363125 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/169#issuecomment-7139363125). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T17:33:05Z. --- @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 ```
upstream-sync closed this pull request 2026-11-25 01:02:53 +00:00
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!169
No description provided.