Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions crates/xcl/src/gpu/desc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,10 +695,31 @@ pub fn cbv_words(address: u64, size: u32) -> [u32; 4] {
}

/// A V# for a buffer view: `stride` 0 = raw (num_records in bytes), else structured / typed
/// (num_records in elements); `format` = GFX10 format for typed loads (raw: 32_FLOAT).
/// (num_records in elements); `format` = GFX10 format for typed loads (raw: 32_FLOAT), with
/// identity channel swizzle `[4, 5, 6, 7]`.
pub fn buffer_words(address: u64, stride: u32, num_records: u32, format: u32) -> [u32; 4] {
buffer_words_swizzled(address, stride, num_records, format, [4, 5, 6, 7])
}

/// Like `buffer_words`, encoding an explicit GFX10 `DST_SEL` (`[x, y, z, w]`, 0=0, 1=1, 4..7=x..w)
/// into word 3 `[11:0]`.
pub fn buffer_words_swizzled(address: u64, stride: u32, num_records: u32, format: u32, dst_sel: [u32; 4]) -> [u32; 4] {
let oob = if stride == 0 { 3 } else { 0 };
[address as u32, (address >> 32) as u32 & 0xFFFF | (stride & 0x3FFF) << 16, num_records, 0xFAC | (format & 0x7F) << 12 | 1 << 24 | oob << 28]
let sel = (dst_sel[0] & 7) | ((dst_sel[1] & 7) << 3) | ((dst_sel[2] & 7) << 6) | ((dst_sel[3] & 7) << 9);
[address as u32, (address >> 32) as u32 & 0xFFFF | (stride & 0x3FFF) << 16, num_records, sel | (format & 0x7F) << 12 | 1 << 24 | oob << 28]
}

/// GFX10 7-bit buffer format (`1..=77`) and `DST_SEL` swizzle for a D3D12 typed buffer format.
/// Handles channel-swizzled DXGI buffer formats (`B8G8R8A8_UNORM`, `B8G8R8X8_UNORM`, `A8_UNORM`)
/// that have no separate GFX10 format code, and rejects `> 77` image-only formats that would
/// otherwise truncate in the 7-bit V# `FORMAT` field.
pub fn dxgi_buffer_format(f: DXGI_FORMAT) -> Option<(u32, [u32; 4])> {
match f {
DXGI_FORMAT_B8G8R8A8_UNORM => Some((56, [6, 5, 4, 7])),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new format-56 + BGRA/BGR1 and format-1 + 000X V#s are also used for typed buffer UAVs (dev_create_uav_x -> buffer_view at real.rs:3763, dev_create_uav_at_x -> buffer_words_at at real.rs:3468). Neither translator applies DST_SEL on stores: lower_mem.rs fmt_pack packs v[c] into component c, and recomp/emit.rs does the same. A BUFFER_STORE_FORMAT_XYZW of float4(r,g,b,a) to a B8G8R8A8_UNORM RWBuffer writes bytes [r,g,b,a] instead of D3D's [b,g,r,a], and a later load through the same view returns (b,g,r,a), so R and B swap on a round trip. Before, the R32_FLOAT view was at least self-consistent. For A8_UNORM, component x lands in the alpha byte instead of .a. No test covers a store or UAV.

DXGI_FORMAT_B8G8R8X8_UNORM => Some((56, [6, 5, 4, 1])),
DXGI_FORMAT_A8_UNORM => Some((1, [0, 0, 0, 4])),
_ => gfx10_format(f).filter(|&g| g <= 77).map(|g| (g, [4, 5, 6, 7])),
}
}

/// An RDNA2 BVH resource descriptor (SRSRC, 4 dwords) for `image_bvh(64)_intersect_ray`:
Expand Down
93 changes: 89 additions & 4 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ fn buffer_view(res: usize, format: windows::Win32::Graphics::Dxgi::Common::DXGI_
note_buffer_view();
return [0; 4];
};
let fmt = crate::gpu::desc::gfx10_format(format).unwrap_or(22);
let (fmt, dst_sel) = crate::gpu::desc::dxgi_buffer_format(format).unwrap_or((22, [4, 5, 6, 7]));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formats that dxgi_buffer_format now rejects (B5G6R5_UNORM=133, B5G5R5A1_UNORM=134, B4G4R4A4_UNORM=136, R8G8B8A8_UNORM_SRGB=130) fall back to (22, identity) with stride format_bytes(22) = 4, so 2-byte formats still get the wrong element size. A B5G6R5 SRV with FirstElement=10, NumElements=100 gets base = at + 40 (should be +20) and stride 4 (should be 2), so every element reads the wrong address and records = min(n, room/4) cuts the view short. buffer_words_at (~3480) does the same and also over-requests tiles from view_want. The PR describes these as rejected, but a broken view is still emitted. Skipping the view, or using a size-correct fallback, would match the description.

let (stride, elem) = if raw {
(0, 4)
} else if stride > 0 {
Expand All @@ -1979,7 +1979,7 @@ fn buffer_view(res: usize, format: windows::Win32::Graphics::Dxgi::Common::DXGI_
let room = (bytes as u64).saturating_sub(first * elem as u64);
let records = if stride == 0 { (n as u64 * 4).min(room) } else { (n as u64).min(room / stride as u64) };
super::view_want::want(super::view_want::Path::BufferView, base, if stride == 0 { records } else { records * stride as u64 });
crate::gpu::desc::buffer_words(base, stride, records as u32, fmt)
crate::gpu::desc::buffer_words_swizzled(base, stride, records as u32, fmt, dst_sel)
}

/// Console buffer → a placed resource in the unified memory heap, so its console address is
Expand Down Expand Up @@ -3477,7 +3477,7 @@ unsafe extern "system" fn dev_create_uav_at_x(this: *mut c_void, rdesc: *const u
/// V# of `n` elements from element `first` of the buffer at `va`, and the bytes it covers
/// (address, length): what a view in a GPU view asks the tiles of (`view_want`).
fn buffer_words_at(va: u64, format: windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT, first: u64, n: u32, stride: u32, raw: bool) -> ([u32; 4], u64, u64) {
let fmt = crate::gpu::desc::gfx10_format(format).unwrap_or(22);
let (fmt, dst_sel) = crate::gpu::desc::dxgi_buffer_format(format).unwrap_or((22, [4, 5, 6, 7]));
let (stride, elem) = match (raw, stride) {
(true, _) => (0, 4),
(false, 0) => {
Expand All @@ -3489,7 +3489,7 @@ fn buffer_words_at(va: u64, format: windows::Win32::Graphics::Dxgi::Common::DXGI
let records = if stride == 0 { n.saturating_mul(4) } else { n };
let at = va + first * elem as u64;
let len = if stride == 0 { records as u64 } else { records as u64 * stride as u64 };
(crate::gpu::desc::buffer_words(at, stride, records, fmt), at, len)
(crate::gpu::desc::buffer_words_swizzled(at, stride, records, fmt, dst_sel), at, len)
}

fn resolve_uav_array_size(array_size: u32, total_slices: u32, first_slice: u32) -> u32 {
Expand Down Expand Up @@ -13455,6 +13455,91 @@ mod gpu_view_tests {
assert_eq!((at, len), (va, 0), "an empty view asks for nothing");
}

#[test]
fn typed_buffer_views_encode_bgra_bgrx_and_a8_formats_and_swizzles() {
use windows::Win32::Graphics::Dxgi::Common::{
DXGI_FORMAT_A8_UNORM, DXGI_FORMAT_B5G6R5_UNORM, DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_B8G8R8X8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
};
let va = 0x7_0000_1000u64;
// B8G8R8A8_UNORM: 8_8_8_8_UNORM (56), stride 4, BGRA swizzle [6, 5, 4, 7]
let (w, at, len) = buffer_words_at(va, DXGI_FORMAT_B8G8R8A8_UNORM, 1, 4, 0, false);
let b = crate::gpu::desc::buffer(&w);
assert_eq!((at, len, b.stride, b.num_records, b.format, b.dst_sel), (va + 4, 16, 4, 4, 56, [6, 5, 4, 7]));
// B8G8R8X8_UNORM: 8_8_8_8_UNORM (56), stride 4, BGR1 swizzle [6, 5, 4, 1]
let (w, at, len) = buffer_words_at(va, DXGI_FORMAT_B8G8R8X8_UNORM, 2, 3, 0, false);
let b = crate::gpu::desc::buffer(&w);
assert_eq!((at, len, b.stride, b.num_records, b.format, b.dst_sel), (va + 8, 12, 4, 3, 56, [6, 5, 4, 1]));
// A8_UNORM: 8_UNORM (1), stride 1 (NOT 4), 000X swizzle [0, 0, 0, 4]
let (w, at, len) = buffer_words_at(va, DXGI_FORMAT_A8_UNORM, 3, 5, 0, false);
let b = crate::gpu::desc::buffer(&w);
assert_eq!((at, len, b.stride, b.num_records, b.format, b.dst_sel), (va + 3, 5, 1, 5, 1, [0, 0, 0, 4]));
// Image-only formats (> 77) must not wrap in the 7-bit V# FORMAT field.
assert_eq!(crate::gpu::desc::dxgi_buffer_format(DXGI_FORMAT_R8G8B8A8_UNORM_SRGB), None);
assert_eq!(crate::gpu::desc::dxgi_buffer_format(DXGI_FORMAT_B5G6R5_UNORM), None);
}

#[test]
fn gpu_typed_buffer_vsharp_bgra_bgrx_and_a8_load_expected_channels() {
use crate::gpu::dxil::kernel::{translate as dxil_translate, KernelOptions};
use crate::gpu::rdna2;
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};
match (runner::wave32_supported(), dxc::location()) {
(Ok(true), Ok(_)) => {}
(w, d) => {
return runner::skip_test(if d.is_err() {
"no DXC"
} else if matches!(w, Ok(false)) {
"no wave32"
} else {
"no GPU"
});
}
}
let mut code = Vec::new();
for &(vdata, srsrc) in &[(10u64, 3u64), (14, 4), (18, 5)] {
code.extend(rdna2::encode("BUFFER_LOAD_FORMAT_XYZW", "ENC_MUBUF", &[("VDATA", vdata), ("VADDR", 0), ("SRSRC", srsrc), ("SOFFSET", 128), ("IDXEN", 1)], None).unwrap());
}
code.extend(rdna2::encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap());
let mut mem = vec![0u32; 128];
// Element 0 (skip) + element 1 at 0x104: B=0x00, G=0x80, R=0xFF, A=0x40
mem[0x104 / 4] = u32::from_le_bytes([0x00, 0x80, 0xFF, 0x40]);
// A8 bytes at 0x110: [0x11, 0x22, 0x80, 0xFF] (first=2 -> byte 0x112 = 0x80)
mem[0x110 / 4] = u32::from_le_bytes([0x11, 0x22, 0x80, 0xFF]);
let (v_bgra, _, _) = buffer_words_at(0x100, DXGI_FORMAT_B8G8R8A8_UNORM, 1, 2, 0, false);
let (v_bgrx, _, _) = buffer_words_at(0x100, DXGI_FORMAT_B8G8R8X8_UNORM, 1, 2, 0, false);
let (v_a8, _, _) = buffer_words_at(0x110, DXGI_FORMAT_A8_UNORM, 2, 2, 0, false);
let mut st = KernelState::default();
st.sgpr[12..16].copy_from_slice(&v_bgra);
st.sgpr[16..20].copy_from_slice(&v_bgrx);
st.sgpr[20..24].copy_from_slice(&v_a8);
let dxil_direct = dxil_translate(&code, KernelOptions { wave64: false, split: false, mem_windows: 1, sampler_base: 0 }).expect("direct dxil");
let hlsl = crate::gpu::recomp::translate(&code, &Options { wave64: false, ..Default::default() }).expect("hlsl");
assert!(hlsl.complete(), "hlsl incomplete: {:?}", hlsl.unsupported);
let dxil_hlsl = dxc::compile(&hlsl.hlsl, "main", "cs_6_6", &[]).expect("dxc");
let near = |got: u32, want: f32| (got as i64 - want.to_bits() as i64).abs() <= 2;
let one = 1.0f32.to_bits();
for (label, blob) in [("direct-dxil", &dxil_direct), ("hlsl", &dxil_hlsl)] {
let res = runner::run(blob, &st, &mem).unwrap_or_else(|e| panic!("{label}: {e}"));
let v = &res.vgpr[0];
assert!(
v[10] == one && near(v[11], 128.0 / 255.0) && v[12] == 0 && near(v[13], 64.0 / 255.0),
"{label} BGRA8: {:?}",
[f32::from_bits(v[10]), f32::from_bits(v[11]), f32::from_bits(v[12]), f32::from_bits(v[13])]
);
assert!(
v[14] == one && near(v[15], 128.0 / 255.0) && v[16] == 0 && v[17] == one,
"{label} BGRX8: {:?}",
[f32::from_bits(v[14]), f32::from_bits(v[15]), f32::from_bits(v[16]), f32::from_bits(v[17])]
);
assert!(
v[18] == 0 && v[19] == 0 && v[20] == 0 && near(v[21], 128.0 / 255.0),
"{label} A8: {:?}",
[f32::from_bits(v[18]), f32::from_bits(v[19]), f32::from_bits(v[20]), f32::from_bits(v[21])]
);
}
}

/// 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]
Expand Down
Loading