Repository navigation
D3D12.X: encode BGRA8, BGRX8, and A8 typed buffer V# formats and swizzles #3419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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])); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formats that |
||
| let (stride, elem) = if raw { | ||
| (0, 4) | ||
| } else if stride > 0 { | ||
|
|
@@ -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 | ||
|
|
@@ -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) => { | ||
|
|
@@ -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 { | ||
|
|
@@ -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] | ||
|
|
||
There was a problem hiding this comment.
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_viewat real.rs:3763,dev_create_uav_at_x->buffer_words_atat real.rs:3468). Neither translator applies DST_SEL on stores:lower_mem.rsfmt_packpacks 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.