Skip to content

D3D12.X: encode BGRA8, BGRX8, and A8 typed buffer V# formats and swizzles - #3419

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/typed-buffer-vsharp-bgra-a8
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/typed-buffer-vsharp-bgra-a8

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

What is in it

buffer_view and buffer_words_at in crates/xcl/src/hle/d3d12x/real.rs previously looked up typed buffer formats solely via desc::gfx10_format(format).unwrap_or(22) and encoded every V# with hardcoded identity DST_SEL = [4, 5, 6, 7] (0xFAC):

  • Standard D3D12 typed buffer formats that map to GFX10 formats with non-identity channel swizzles had no entry in gfx10_format, so they silently fell back to GFX10 format 22 (R32_FLOAT, 4-byte stride, identity swizzle):
    • DXGI_FORMAT_B8G8R8A8_UNORM: fell back to R32_FLOAT instead of GFX10 format 56 (8_8_8_8_UNORM, stride 4) with BGRA DST_SEL = [6, 5, 4, 7].
    • DXGI_FORMAT_B8G8R8X8_UNORM: fell back to R32_FLOAT instead of GFX10 format 56 (8_8_8_8_UNORM, stride 4) with BGR1 DST_SEL = [6, 5, 4, 1].
    • DXGI_FORMAT_A8_UNORM: fell back to R32_FLOAT (4-byte element stride and offset instead of 1 byte!) instead of GFX10 format 1 (8_UNORM, stride 1) with DST_SEL = [0, 0, 0, 4].
  • Image-only GFX10 formats > 77 (such as 130 for R8G8B8A8_UNORM_SRGB or 133 for B5G6R5_UNORM) also wrapped modulo 128 in the 7-bit V# FORMAT field (format & 0x7F).

This change adds buffer_words_swizzled and dxgi_buffer_format in crates/xcl/src/gpu/desc.rs, wires both into buffer_view and buffer_words_at in crates/xcl/src/hle/d3d12x/real.rs, and adds unit test typed_buffer_views_encode_bgra_bgrx_and_a8_formats_and_swizzles plus end-to-end GPU test gpu_typed_buffer_vsharp_bgra_bgrx_and_a8_load_expected_channels verifying BUFFER_LOAD_FORMAT_XYZW on both Direct DXIL and HLSL.

Tested

  • Causal reproduction + fix verification (cargo test -p xcl --lib -- typed_buffer_vsharp_bgra typed_buffer_views_encode_bgra a_view_by_address_covers_its_elements gpu::desc::):

    • Before fix: typed_buffer_views_encode_bgra_bgrx_and_a8_formats_and_swizzles failed with (30064775172, 16, 4, 4, 22, [4, 5, 6, 7]) instead of (30064775172, 16, 4, 4, 56, [6, 5, 4, 7]).
    • After fix: 14 passed, 0 failed, including both Direct DXIL and HLSL GPU execution of BUFFER_LOAD_FORMAT_XYZW returning exact expected (R, G, B, A) channels for B8G8R8A8_UNORM, B8G8R8X8_UNORM, and A8_UNORM (at non-zero FirstElement).
  • Full workspace test suite (cargo test --workspace --no-fail-fast -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::, RTX 5070 Ti + DXC 1.9): 41 binaries, 2713 passed, 4 failed, 49 ignored, 9 filtered; 27 SKIPPED (22 no wave64, 3 refused #1426, 2 no heap windows). All 4 failures are pre-existing geometry test failures on main (3b6a91ed, tracked in Tests: fix 4 geometry tests broken on main by split-ES safe-barrier and no-store epilogue merges #3411): gpu::dxil::geometry_tests::gpu_geometry_msg_gs_done_records, gpu::dxil::geometry_tests::msg_gs_done_emits_in_dxil, gpu::dxil::lower_geom::tests::gpu_epilogue_filters_oob_triangles_and_no_store_emits, and gpu::dxil::lower_mem::stage_guard_tests::geometry_export_rejected.

  • Rebased onto 37be0959 with runner::wave32_supported() no-GPU CI guard on gpu_typed_buffer_vsharp_bgra_bgrx_and_a8_load_expected_channels; targeted suite (cargo test -p xcl --lib -- typed_buffer_views_encode_bgra gpu_typed_buffer_vsharp_bgra gpu::desc::tests::): 12 passed, 0 failed.

Regression run

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB
xcl-smoke v1
commit: fb594f1539b4bd4796d9801782af247bbfab0582
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1595 (PresentX 1595)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 32935 MB

Not tested

  • Hardware adapters with wave64-only shaders or non-NVIDIA drivers (22 wave64 tests skipped on RTX 5070 Ti).
  • Full title execution for games other than ea3b5e4d or paths requiring uma_driver::tests:: kernel driver tests.
  • Inverse DST_SEL swizzling on BUFFER_STORE_FORMAT_* writes to swizzled typed buffers (separate translator change, not part of this descriptor encoding PR).

AI use

Used an AI coding assistant to audit typed buffer V# encoding in desc.rs and real.rs, reproduce the missing BGRA8/BGRX8/A8 mapping, implement the fix and synthetic unit/GPU tests, and run local verification.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@Kalmarv Kalmarv closed this Nov 29, 2026
@Kalmarv Kalmarv reopened this Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

I checked this descriptor producer together with the MUBUF store change in #3421. The fixture builds the actual buffer_words_at descriptors for BGRA8, BGRX8 and A8 with nonzero FirstElement, runs three in-bounds indexed stores plus a fourth out-of-view index, and reads all 512 backing bytes before a separate translated load.

This exercises production descriptor words and RTX 5070 shader execution (driver 616.92, DXC 1.8.2502.11). It supplies descriptor words directly to shader registers, so guest descriptor binding and title visuals remain outside the result. The load improvement is independently covered. The store side needs #3421 for these swizzled descriptors to round trip.

One-file integration fixture (applies on #3419)
--- a/crates/xcl/src/hle/d3d12x/real.rs
+++ b/crates/xcl/src/hle/d3d12x/real.rs
@@ -13531,6 +13531,70 @@
         }
     }
 
+    #[test]
+    fn gpu_typed_buffer_swizzled_views_and_indexed_stores() {
+        use crate::gpu::dxil::kernel::{translate as dxil_translate, KernelOptions};
+        use crate::gpu::rdna2::encode;
+        use crate::gpu::recomp::{dxc, runner::{self, KernelState}, Options};
+        use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_A8_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_B8G8R8X8_UNORM};
+
+        if runner::adapter().is_err() || dxc::location().is_err() {
+            return runner::skip_test("no GPU or DXC");
+        }
+        let formats = [(DXGI_FORMAT_B8G8R8A8_UNORM, 0x100, 2), (DXGI_FORMAT_B8G8R8X8_UNORM, 0x140, 2), (DXGI_FORMAT_A8_UNORM, 0x180, 3)];
+        let colors = [[255u8, 128, 0, 64], [0, 51, 102, 153], [204, 17, 238, 85], [1, 2, 3, 4]];
+        let mut state = KernelState::default();
+        let mut expected = vec![0x5a; 512];
+        for lane in 0..4 {
+            let regs = &mut state.vgpr[lane];
+            regs[0] = lane as u32;
+            for (reg, byte) in regs[4..8].iter_mut().zip(colors[lane]) {
+                *reg = (f32::from(byte) / 255.0).to_bits();
+            }
+            if lane < 3 {
+                let [r, g, b, a] = colors[lane];
+                expected[0x108 + lane * 4..0x10c + lane * 4].copy_from_slice(&[b, g, r, a]);
+                expected[0x148 + lane * 4..0x14c + lane * 4].copy_from_slice(&[b, g, r, 0]);
+                expected[0x183 + lane] = a;
+            }
+        }
+        for (i, (format, base, first)) in formats.into_iter().enumerate() {
+            let (words, _, _) = buffer_words_at(base, format, first, 3, 0, false);
+            state.sgpr[12 + 4 * i..16 + 4 * i].copy_from_slice(&words);
+        }
+
+        let program = |store| {
+            let mut code = encode("V_CMPX_GT_U32", "ENC_VOPC", &[("SRC0", 132), ("VSRC1", 0)], None).unwrap();
+            for i in 0..3 {
+                let (op, vdata) = if store { ("BUFFER_STORE_FORMAT_XYZW", 4) } else { ("BUFFER_LOAD_FORMAT_XYZW", 10 + 4 * i) };
+                code.extend(encode(op, "ENC_MUBUF", &[("VDATA", vdata), ("VADDR", 0), ("SRSRC", 3 + i), ("SOFFSET", 128), ("IDXEN", 1)], None).unwrap());
+            }
+            code.extend(encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap());
+            let direct = dxil_translate(&code, KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }).unwrap();
+            let translated = crate::gpu::recomp::translate(&code, &Options { wave64: false, ..Default::default() }).unwrap();
+            assert!(translated.complete(), "{:?}", translated.unsupported);
+            [direct, dxc::compile(&translated.hlsl, "main", "cs_6_6", &[]).unwrap()]
+        };
+        let stores = program(true);
+        let loads = program(false);
+        for ((backend, store), load) in ["Direct DXIL", "HLSL"].into_iter().zip(stores).zip(loads) {
+            let written = runner::run(&store, &state, &[0x5a5a_5a5a; 128]).unwrap();
+            let bytes: Vec<_> = written.mem.iter().flat_map(|word| word.to_le_bytes()).collect();
+            for (i, (&got, &want)) in bytes.iter().zip(&expected).enumerate() {
+                assert_eq!(got, want, "{backend}: byte {i:#x}");
+            }
+            assert_eq!(bytes.len(), expected.len(), "{backend}: readback length");
+            let read = runner::run(&load, &state, &written.mem).unwrap();
+            for (lane, &[r, g, b, a]) in colors[..3].iter().enumerate() {
+                for (i, byte) in [r, g, b, a, r, g, b, 255, 0, 0, 0, a].into_iter().enumerate() {
+                    let expected = (f32::from(byte) / 255.0).to_bits();
+                    let actual = read.vgpr[lane][10 + i];
+                    assert!(actual.abs_diff(expected) <= 2, "{backend}: lane {lane}, channel {i}: {actual:#x} vs {expected:#x}");
+                }
+            }
+        }
+    }
+
     /// A constant buffer view in a GPU view carries its address: shaders read the view window
     /// there (its tiles show the CPU view). It was left empty: the address is no CPU memory.
     #[test]

AI disclosure: OpenAI Codex (GPT-6.2 Sol) implemented and ran the integration fixture and wrote this report.

@Kalmarv
Kalmarv force-pushed the samplers/typed-buffer-vsharp-bgra-a8 branch from cc302ce to fb594f1 Compare November 29, 2026 15:40
@Aniseto

Aniseto commented Nov 29, 2026

Copy link
Copy Markdown

Ran the typed-buffer format tests at fb594f15 on an NVIDIA Blackwell desktop part.

System

  • GPU: NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, mesh shaders), driver 32.0.16.2140 (621.40)
  • CPU: Intel Core i5-12400F, 64 GB RAM
  • OS: Windows 11 Pro build 26200 x64
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09), MSVC 14.44.35207, DXC 1.9.2609.5
  • Build: debug

Results

  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib typed_buffer_views_encode_bgra_bgrx_and_a8_formats_and_swizzles -- --test-threads=1: 1 passed, 0 failed, 0 ignored.
  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib gpu_typed_buffer_vsharp_bgra_bgrx_and_a8_load_expected_channels -- --test-threads=1: 1 passed, 0 failed, 0 ignored. The GPU draw ran here, with no SKIPPED line.
  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib gpu::desc::tests:: -- --test-threads=1: 10 passed, 0 failed, 0 ignored.

What this adds
The BGRA8/BGRX8/A8 V# encoding unit test, the GPU load test comparing Direct DXIL and HLSL, and the descriptor group all pass. The author also tested on an RTX 5070 Ti; this adds an independent run on driver 32.0.16.2140 at head fb594f15. Synthetic GPU tests only, no title run.

Not tested
A retail title, a paired smoke run, the full workspace suite here, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - ran the commands above on this machine and wrote this report; no game data was used.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: d0832e6
head: fb594f1
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2892 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (desc.rs, real.rs). It changes the default path: these are the only two places that build typed buffer V#s for SRVs and UAVs, and on main B8G8R8A8_UNORM, B8G8R8X8_UNORM and A8_UNORM fell back to format 22 (R32_FLOAT) with the identity swizzle (A8 with a 4-byte stride). Findings: (1) typed stores ignore DST_SEL on main (lower_mem.rs fmt_pack, and the HLSL format store in recomp/emit.rs). So with this PR alone, a store to a BGRA8 UAV writes red and blue swapped and a load through the same view swaps them back, and an A8 store writes .x instead of .w. Not a regression for stores (main wrote float bits), but loads and stores now disagree until #3421 (same author, applies the inverse DST_SEL on stores) lands. Better to stack this on #3421 or say so in the description. (2) AI use names no tool or model ("an AI coding assistant"). (3) Formats above 77 now fall back to R32_FLOAT with a 4-byte stride instead of wrapping, still wrong for B5G6R5 (stride 2) and similar. Fine: the DST_SEL packing ([4,5,6,7] gives back 0xFAC), format numbers 56 (8_8_8_8_UNORM) and 1 (8_UNORM), the swizzles BGRA [6,5,4,7], BGRX [6,5,4,1] (alpha 1) and A8 [0,0,0,4] against DXGI, both loaders already applying DST_SEL, and both tests failing on main's mapping (the GPU one uses skip_test).
performance: 60 s smoke of a retail title, main d0832e6 vs this merge (4a1f1620): 1516 -> 1672 frames shown (110 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 17755 MB, 0 device removals, 0 crashes.
verdict: safe to merge (best together with or after #3421, so typed UAV loads and stores agree)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 1, 2026
@brainhay5

Copy link
Copy Markdown

merge-check claim
head: fb594f1

@brainhay5

Copy link
Copy Markdown

merge-check claim withdrawn
Our automated merge-check loop claimed this PR and then stopped before posting a check — two loops were running at once and both are now shut down. No check from us is coming, so this claim is void and the head is free for someone else to take.

AI use: an agent posted this withdrawal.

@kole07139

Copy link
Copy Markdown

merge-check claim
head: fb594f1

@kole07139

Copy link
Copy Markdown

merge-check
base: 1c8b551
head: fb594f1
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2911 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 driver cache needs Developer Mode; the same set as main), and the PR's GPU test ran here (wave32) and passed; failing: none
review: read the whole diff (desc.rs, real.rs), and the paths it feeds, in both translators. Findings, most serious first:

  • (1) Typed stores still ignore DST_SEL (fmt_pack(uint4(...)) in recomp/emit.rs, the FormatStore path in dxil/lower_mem.rs), while loads apply it.
  • (2) The change's full extent: a local test compared main's V# with this PR's for all 192 DXGI values. 22 change:
    • the three targeted formats;
    • sRGB, BC and R9G9B9E5 formats, which D3D12 does not allow for buffer views;
    • B5G6R5 / B5G5R5A1 / B4G4R4A4, wrong before and after (4-byte stride).
    • In none of them did main's wrapped code (& 0x7F) decode to the requested format, so no format that worked before changes.
  • (3) AI use names neither the tool nor the model; CONTRIBUTING asks for both.
  • Not covered here: AMD and native wave64 (the PR is labelled needs:amd-test), the split-wave64 path, and VS/PS loads through these views (the GPU tests are compute only). The title does not reach these formats (see performance).
  • Confirmed:
    • buffer_view and buffer_words_at are the only producers of typed-buffer V#s (besides cbv_words and tests).
    • The element size follows format_bytes(fmt), so A8's FirstElement offset is now 1 byte.
    • The DST_SEL packing keeps 0xFAC for the identity, and the BGRA / BGRX / A8 swizzles match DXGI's byte order.
    • Both loaders apply DST_SEL.
      performance: only descriptor words change, no per-draw work. Retail title, main 1c8b551 vs this merge (b2fe43b6), 0 device removals and 0 crashes in every run:
  • 60 s: 1538 -> 1572 frames shown (102 %), peak commit 16288 -> 16417 MB.
  • 150 s, in the order base / merge / merge / base: 4270, 4302, 4323, 4301 frames (merge +0.6 % on average), peak commit 17302 / 17724 / 16929 / 16997 MB.
  • A local counting probe (not part of the PR) found the title making typed buffer views in 10 DXGI formats during 150 s, none of them among the 22 this PR changes. On this title the runs check for regressions only, not the fix.
    verdict: safe to merge (best in the same batch as Recompiler: honor V# DST_SEL inverse swizzle on MUBUF buffer_store_format #3421, so typed loads and stores through these views agree)
    hardware / tool and model: NVIDIA GeForce RTX 3070 8 GB (driver 32.0.16.2140), Intel Core i9-9900K, Windows 11 Pro N 26200 / Claude Code with Claude Opus 5.5 (ran the merge, build, suite, smoke and the fixture, and wrote this check)

AI-written; posted without a human check of each point.

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants