Skip to content

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

Merged
Marvisak merged 2 commits into
Marvisak:mainfrom
Dufftwt:fix/tbuffer-d16-loads-packed
Nov 25, 2026
Merged

Marvisak merged 2 commits into
Marvisak:mainfrom
Dufftwt:fix/tbuffer-d16-loads-packed

Conversation

@Dufftwt

@Dufftwt Dufftwt commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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.

@DaviTostes

Copy link
Copy Markdown

Tested a103937 on Arch Linux x86_64, rustc 1.98.1: pass.

No game data used.

AntoninMichelq pushed a commit to AntoninMichelq/VIctory that referenced this pull request Nov 24, 2026
…o 16-bit channels per VGPR) into the integration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
@r4tedx

r4tedx commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

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).
@Dufftwt
Dufftwt force-pushed the fix/tbuffer-d16-loads-packed branch from a103937 to c430b1d Compare November 24, 2026 13:03
Xeltax pushed a commit to Xeltax/VIcky that referenced this pull request Nov 24, 2026
edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
The tester tree (upstream main ffe310e plus its PRs, update 8; see its
INTEGRATION.md) ran 11-25 minutes in the world on an RTX 5070 without a
device removal. Where both sides carry the same PR (the wave64 stack
#223/#233/#320 with #439 and #180, #454, #336, #259, #331 and others), the
tester tree's newer head is taken; our own work and the PRs only we
merged are kept.

Conflicts, file by file:

- descriptor/host.rs: the tester layout (`spare`, the guard slot after the
  dynamic slots, `free_table`, the capped sampler heap). A capped view
  heap keeps `capped_reserve()` dynamic slots, now 2^18 by default (the
  tester measurement, and our 91fed158), still `VICTORY_DYNAMIC_SLOTS` in
  diagnostics builds; uncapped hosts keep our 2^19. Our slot statistics
  (TAKEN/PEAK/SHORT), the bindless plan cache (`CommittedRegions`,
  `timed_valid_title_address`, `timed_find`, `title_bounds`) and the
  revision triple are kept beside the tester's `guard_slot`.
- Buffer states: the tester tree's buffer-state tracker (#334: every list
  moves host copies of title memory between GENERIC_READ and
  UNORDERED_ACCESS and back to common before it closes) and depth twins
  (#469) are taken. #40's enhanced/legacy indirect barriers (the indirect
  prelude, `use_indirect_buffers`, the title buffer handoff mirrored in
  `host_barrier`) cannot share buffers with that tracker and are dropped;
  #40's argument checks (`byte_len`, exact spans, overflow-safe clamp
  shader, aligned counts) stay. `indirect::bound` and #282's
  `rectangles` go through `buffer_state`, and `indirect::execute` (#334)
  replaces `done`. #178's DXC fallback compiles both of our passes.
  Our segments (#387: `close` ends the segment, `close_native` closes a
  native list) carry the tracker: each native list returns its buffers
  to common and its twins to their depth targets, and each prelude also
  holds our texture uploads.
- shadow.rs: our write flow (`queue_write`, one `snapshot` after the
  batch's refreshes, `finish_writes`, the unchanged-page cache) with
  #261's copy-back that also gives the snapshot the title's bytes; its
  tests run through our flow.
- pipeline/make.rs: #275's per-frame budget and per-pipeline wake pair,
  with our compute waits (a dispatch waits for its pipeline, up to 20 s).
- pipeline/library.rs: the tester version (#347, #275's saver thread);
  our concurrent-write test kept. shader_cache.rs: our byte-returning
  cache API with #358's "translated, only not kept".
- draw.rs: #493's root SRV tables; where no table can stand in, our
  drop of a draw whose root view is unset or outside title buffers. Our
  refusal of a table offset past the capped heap stays.
- resource.rs: `HostTexture` has both our `staged` generation and the
  depth twin.
- Shaders: the tester's lower.rs for the wave64 stack, #160's image
  sampling (it covers our #73 and #136 sample forms) and #453/#587; kept
  from our side: #136's preference for an image or buffer use of a
  descriptor read as data and its `s_buffer_` arm, #422's nearest-even
  narrowing (also for #169's d16 loads and #453's interpolation), #452's
  spin-wait limit, #391's LDS limit, #302's spilled root arguments (now in
  `NativeProgram::translation_ir`), #136's PSV0 interpolation beside the
  tester's row harmonisation. split_tests.rs, lane_read_loop_tests.rs and
  docs/wave64-on-wave32.md are the tester versions (the same PRs at newer
  heads).
- Tests: #483's heap guard dwords filled in our GPU tests of #200
  (v_div_scale) and #437 (scalar loads from a raw view), which read
  through the guard slot otherwise; #334's debug-layer test passes an
  offset to our `indirect::bound`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alexdeliu

Copy link
Copy Markdown

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

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

Dufftwt commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@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

Copy link
Copy Markdown

@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

KainanP pushed a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…fied together

Merges Marvisak#156 Marvisak#162 Marvisak#168 Marvisak#169 Marvisak#368 Marvisak#379 Marvisak#456 Marvisak#587 Marvisak#990 Marvisak#1021 Marvisak#1048 Marvisak#1052 Marvisak#1053
Marvisak#1095 Marvisak#1142 Marvisak#1190 Marvisak#1211 Marvisak#1223 Marvisak#1259 Marvisak#1293 on top of the bundle of Marvisak#1402
(which is on Marvisak#1319, on main ffe310e), each head unchanged. Hunks that
conflicted in emit/lower.rs, emit/valu_tests.rs and emit/diff_tests.rs were
merged by keeping both sides (additions next to each other); a PR was kept
only if cargo clippy -p victory-shader --all-targets -- -D warnings still
passed with it. No instruction name is handled in two places.

Marvisak#98 is not in this version: an offline measurement on a retail dump showed
it loses 31 programs that validate on Marvisak#1402.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(381 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit 46bde7f into Marvisak:main Nov 25, 2026
1 check passed
DovisLe added a commit to DovisLe/VIctory that referenced this pull request Nov 25, 2026
Two ignored tests run synthetic shaders on the hardware GPU and WARP and
print every mismatch, grouped by backend, operation and channel, with an
example input:

- sweep_typed_buffer_formats: all 77 GFX10 buffer formats through
  tbuffer_load_format_xyzw (one shader per channel), 72 input sets each,
  against the ISA's exact conversions (UNORM x/(2^n-1) and SNORM
  max(x/(2^(n-1)-1), -1) correctly rounded, scaled, integer, the small
  floats, missing channels 0, 0, 0, 1). SWEEP_D16=1 adds
  tbuffer_load_format_d16_xyzw (needs Marvisak#169); SWEEP_NORM_ONLY=1 keeps the
  20 norm formats.
- sweep_f16_alu: v_add/sub/mul/fma_f16, v_pk_add/mul/fma_f16 (both
  lanes), v_cvt_f16_f32 and v_cvt_f32_f16 against f64 results rounded to
  the nearest-even half.

run_wide is run_cases with a 4-dword UAV, so the wide formats' inputs fit.

Run with: cargo test -p victory-shader --lib sweep -- --ignored --nocapture

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.

6 participants