From 0e71923f0732dbc79f87764ea321aa8bdbf38e30 Mon Sep 17 00:00:00 2001 From: Manuel Galindo Date: Mon, 23 Nov 2026 20:36:55 -0500 Subject: [PATCH] Tests: format_bytes agrees with the driver, gfx10_format inverts dxgi_format format_bytes had no test, and typed buffer views take their element stride from it. The new integration test compares it, for every GFX10 format 0-77 that dxgi_format maps, with the element size the device's GetCopyableFootprints gives for that DXGI format, and checks that gfx10_format gives back every format dxgi_format maps. On an RX 9060 XT all 42 formats agree; changing 7..=19 => 2 to 1 makes the test fail and name each 16-bit format. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/tests/format_bytes_driver.rs | 64 +++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 crates/xcl/tests/format_bytes_driver.rs diff --git a/crates/xcl/tests/format_bytes_driver.rs b/crates/xcl/tests/format_bytes_driver.rs new file mode 100644 index 0000000000..3b5e35f8f7 --- /dev/null +++ b/crates/xcl/tests/format_bytes_driver.rs @@ -0,0 +1,64 @@ +//! `gpu::desc::format_bytes` against the PC driver: for every GFX10 buffer / image format of its +//! range (0–77) that `gpu::desc::dxgi_format` maps, the element size it gives is the one the +//! device's `GetCopyableFootprints` gives for a texture in that DXGI format. Typed buffer views +//! take their element stride from it (`real::buffer_words_at`). No game data; any D3D12 device +//! answers, WARP included. + +use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC; + +use xcl::gpu::desc; + +/// Bytes per element of one row of a 64-texel-wide 2D texture in `format`, as the device lays it out. +unsafe fn driver_element_bytes(dev: &ID3D12Device, format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT) -> u64 { + let rd = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D, + Width: 64, + Height: 64, + DepthOrArraySize: 1, + MipLevels: 1, + Format: format, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + ..Default::default() + }; + let mut row = 0u64; + dev.GetCopyableFootprints(&rd, 0, 1, 0, None, None, Some(&mut row), None); + row / 64 +} + +#[test] +fn format_bytes_matches_the_drivers_element_size() { + let Ok(dev) = xcl::gpu::agility::create_device(None, D3D_FEATURE_LEVEL_11_0) else { + xcl::gpu::recomp::runner::skip_test("no GPU"); + return; + }; + let mut checked = 0; + let mut wrong = Vec::new(); + for f in 0..=77u32 { + let Some((d, _)) = desc::dxgi_format(f) else { continue }; + let driver = unsafe { driver_element_bytes(&dev, d) }; + if desc::format_bytes(f) as u64 != driver { + wrong.push(format!("format {f} ({d:?}): format_bytes {}, driver {driver}", desc::format_bytes(f))); + } + checked += 1; + } + assert!(checked >= 40, "only {checked} formats of 0–77 have a DXGI format"); + assert!(wrong.is_empty(), "{}", wrong.join("; ")); +} + +/// `gfx10_format` is the inverse of `dxgi_format` for every format it maps, 0–255: no two GFX10 +/// formats share a DXGI one, so a view's DXGI format names its GFX10 format back. +#[test] +fn gfx10_format_inverts_dxgi_format() { + let mut wrong = Vec::new(); + for f in 0..=255u32 { + if let Some((d, _)) = desc::dxgi_format(f) { + let back = desc::gfx10_format(d); + if back != Some(f) { + wrong.push(format!("format {f} ({d:?}) comes back as {back:?}")); + } + } + } + assert!(wrong.is_empty(), "{}", wrong.join("; ")); +}