From 78f3d61ed45b157262f2a1061caace8d17bb9ac4 Mon Sep 17 00:00:00 2001 From: RonLogic <307541314+RonLogic@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:20:09 +0200 Subject: [PATCH] Test translated compute shader output with D3D12 WARP --- Cargo.lock | 164 ++++++++++++++++ crates/tombox-xshader/Cargo.toml | 11 ++ crates/tombox-xshader/tests/support/d3d12.rs | 196 +++++++++++++++++++ crates/tombox-xshader/tests/synthetic.rs | 46 +++++ 4 files changed, 417 insertions(+) create mode 100644 crates/tombox-xshader/tests/support/d3d12.rs diff --git a/Cargo.lock b/Cargo.lock index d7f4212..b099b39 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,35 @@ dependencies = [ "adler2", ] +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "tombox-archive" version = "0.1.0" @@ -100,8 +129,143 @@ name = "tombox-xshader" version = "0.1.0" dependencies = [ "miniz_oxide", + "windows", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-strings", + "windows-targets", +] + +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets", ] +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + [[package]] name = "xcrdapi" version = "0.1.0" diff --git a/crates/tombox-xshader/Cargo.toml b/crates/tombox-xshader/Cargo.toml index 520b246..3346ecb 100644 --- a/crates/tombox-xshader/Cargo.toml +++ b/crates/tombox-xshader/Cargo.toml @@ -20,6 +20,17 @@ path = "src/bin/xscan.rs" [dependencies] miniz_oxide = "0.8" +[target.'cfg(windows)'.dev-dependencies] +windows = { version = "0.58", features = [ + "Win32_Foundation", + "Win32_Graphics_Direct3D", + "Win32_Graphics_Direct3D12", + "Win32_Graphics_Dxgi", + "Win32_Graphics_Dxgi_Common", + "Win32_Security", + "Win32_System_Threading", +] } + [[bin]] name = "xpso" path = "src/bin/xpso.rs" diff --git a/crates/tombox-xshader/tests/support/d3d12.rs b/crates/tombox-xshader/tests/support/d3d12.rs new file mode 100644 index 0000000..7cfee86 --- /dev/null +++ b/crates/tombox-xshader/tests/support/d3d12.rs @@ -0,0 +1,196 @@ +//! Execute one compute dispatch against an initialized buffer using software D3D12. + +use std::mem::{size_of_val, ManuallyDrop}; +use std::os::windows::io::{FromRawHandle, OwnedHandle}; +use std::{ptr, slice}; +use windows::core::Interface; +use windows::Win32::Foundation::WAIT_OBJECT_0; +use windows::Win32::Graphics::Direct3D::{ID3DBlob, D3D_FEATURE_LEVEL_11_0}; +use windows::Win32::Graphics::Direct3D12::*; +use windows::Win32::Graphics::Dxgi::{ + CreateDXGIFactory2, IDXGIAdapter, IDXGIFactory4, DXGI_CREATE_FACTORY_FLAGS, +}; +use windows::Win32::System::Threading::{CreateEventW, WaitForSingleObject}; + +pub(super) fn dispatch_buffer( + dxil: &[u8], + input: [u32; 4], +) -> Result<[u32; 4], Box> { + // SAFETY: COM objects stay alive through fence completion. The shader and the + // root signature agree on a structured UAV at u0 with one u32 per element. + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory2(DXGI_CREATE_FACTORY_FLAGS(0))?; + let adapter: IDXGIAdapter = factory.EnumWarpAdapter()?; + let mut device = None; + D3D12CreateDevice(&adapter, D3D_FEATURE_LEVEL_11_0, &mut device)?; + let device: ID3D12Device = device.expect("D3D12 device"); + + let parameter = D3D12_ROOT_PARAMETER { + ParameterType: D3D12_ROOT_PARAMETER_TYPE_UAV, + Anonymous: D3D12_ROOT_PARAMETER_0 { + Descriptor: D3D12_ROOT_DESCRIPTOR::default(), + }, + ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL, + }; + let desc = D3D12_ROOT_SIGNATURE_DESC { + NumParameters: 1, + pParameters: ¶meter, + ..Default::default() + }; + let mut blob: Option = None; + D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &mut blob, None)?; + let blob = blob.expect("serialized root signature"); + let root_bytes = + slice::from_raw_parts(blob.GetBufferPointer().cast::(), blob.GetBufferSize()); + let root: ID3D12RootSignature = device.CreateRootSignature(0, root_bytes)?; + let mut desc = D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: ManuallyDrop::new(Some(root.clone())), + CS: D3D12_SHADER_BYTECODE { + pShaderBytecode: dxil.as_ptr().cast(), + BytecodeLength: dxil.len(), + }, + ..Default::default() + }; + let pipeline = device.CreateComputePipelineState::(&desc); + ManuallyDrop::drop(&mut desc.pRootSignature); + let pipeline = pipeline?; + + let kind = D3D12_COMMAND_LIST_TYPE_COMPUTE; + let queue: ID3D12CommandQueue = device.CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { + Type: kind, + ..Default::default() + })?; + let allocator: ID3D12CommandAllocator = device.CreateCommandAllocator(kind)?; + let list: ID3D12GraphicsCommandList = + device.CreateCommandList(0, kind, &allocator, &pipeline)?; + let bytes = size_of_val(&input); + let none = D3D12_RANGE { Begin: 0, End: 0 }; + let all = D3D12_RANGE { + Begin: 0, + End: bytes, + }; + + let upload = buffer( + &device, + bytes, + D3D12_HEAP_TYPE_UPLOAD, + D3D12_RESOURCE_STATE_GENERIC_READ, + D3D12_RESOURCE_FLAG_NONE, + )?; + let mut mapped = ptr::null_mut(); + upload.Map(0, Some(&none), Some(&mut mapped))?; + ptr::copy_nonoverlapping(input.as_ptr(), mapped.cast::(), input.len()); + upload.Unmap(0, Some(&all)); + let data = buffer( + &device, + bytes, + D3D12_HEAP_TYPE_DEFAULT, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + )?; + let readback = buffer( + &device, + bytes, + D3D12_HEAP_TYPE_READBACK, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_FLAG_NONE, + )?; + + list.CopyBufferRegion(&data, 0, &upload, 0, bytes as u64); + transition( + &list, + &data, + D3D12_RESOURCE_STATE_COPY_DEST, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + ); + list.SetComputeRootSignature(&root); + list.SetComputeRootUnorderedAccessView(0, data.GetGPUVirtualAddress()); + list.Dispatch(1, 1, 1); + transition( + &list, + &data, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS, + D3D12_RESOURCE_STATE_COPY_SOURCE, + ); + list.CopyBufferRegion(&readback, 0, &data, 0, bytes as u64); + list.Close()?; + + let fence: ID3D12Fence = device.CreateFence(0, D3D12_FENCE_FLAG_NONE)?; + let event = CreateEventW(None, false, false, None)?; + let _event_handle = OwnedHandle::from_raw_handle(event.0); + let command_list = list.cast::()?; + queue.ExecuteCommandLists(&[Some(command_list)]); + queue.Signal(&fence, 1)?; + fence.SetEventOnCompletion(1, event)?; + let status = WaitForSingleObject(event, 10_000); + if status != WAIT_OBJECT_0 { + return Err(format!("D3D12 fence wait returned {:#x}", status.0).into()); + } + device.GetDeviceRemovedReason()?; + + let mut output = [0u32; 4]; + readback.Map(0, Some(&all), Some(&mut mapped))?; + ptr::copy_nonoverlapping(mapped.cast::(), output.as_mut_ptr(), output.len()); + readback.Unmap(0, Some(&none)); + Ok(output) + } +} + +unsafe fn buffer( + device: &ID3D12Device, + bytes: usize, + heap_type: D3D12_HEAP_TYPE, + state: D3D12_RESOURCE_STATES, + flags: D3D12_RESOURCE_FLAGS, +) -> windows::core::Result { + let heap = D3D12_HEAP_PROPERTIES { + Type: heap_type, + CreationNodeMask: 1, + VisibleNodeMask: 1, + ..Default::default() + }; + let mut desc = D3D12_RESOURCE_DESC { + Dimension: D3D12_RESOURCE_DIMENSION_BUFFER, + Width: bytes as u64, + Height: 1, + DepthOrArraySize: 1, + MipLevels: 1, + Layout: D3D12_TEXTURE_LAYOUT_ROW_MAJOR, + Flags: flags, + ..Default::default() + }; + desc.SampleDesc.Count = 1; + let mut resource = None; + device.CreateCommittedResource( + &heap, + D3D12_HEAP_FLAG_NONE, + &desc, + state, + None, + &mut resource, + )?; + Ok(resource.expect("committed buffer")) +} + +unsafe fn transition( + list: &ID3D12GraphicsCommandList, + resource: &ID3D12Resource, + before: D3D12_RESOURCE_STATES, + after: D3D12_RESOURCE_STATES, +) { + let mut barrier = D3D12_RESOURCE_BARRIER { + Type: D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, + Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE, + Anonymous: D3D12_RESOURCE_BARRIER_0 { + Transition: ManuallyDrop::new(D3D12_RESOURCE_TRANSITION_BARRIER { + pResource: ManuallyDrop::new(Some(resource.clone())), + Subresource: D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, + StateBefore: before, + StateAfter: after, + }), + }, + }; + list.ResourceBarrier(slice::from_ref(&barrier)); + let mut transition = ManuallyDrop::take(&mut barrier.Anonymous.Transition); + ManuallyDrop::drop(&mut transition.pResource); +} diff --git a/crates/tombox-xshader/tests/synthetic.rs b/crates/tombox-xshader/tests/synthetic.rs index 80cd8cf..5b1cd47 100644 --- a/crates/tombox-xshader/tests/synthetic.rs +++ b/crates/tombox-xshader/tests/synthetic.rs @@ -8,6 +8,10 @@ use tombox_xshader::hlsl::translate; use tombox_xshader::meta::{RootParam, RootParamKind, RootRange, RootSig, SigElement}; use tombox_xshader::{ShaderExtract, Stage}; +#[cfg(windows)] +#[path = "support/d3d12.rs"] +mod d3d12; + const S_ENDPGM: u32 = 0xbf81_0000; const S_NOP: u32 = 0xbf80_0000; /// `v_cvt_pkrtz_f16_f32 v0, v2, v2` / `v1, v2, v2`: the default colour-export packing. @@ -171,3 +175,45 @@ fn texel_type_follows_the_use_of_the_loaded_value() { assert!(t.hlsl.contains("Texture2D tex0 : register(t0);"), "{}", t.hlsl); assert!(t.hlsl.contains("float4 t0 = tex0.Load("), "{}", t.hlsl); } + +fn buffer_add_shader() -> ShaderExtract { + let words = [ + 0xf408_0002, 0xfa00_0000, // s_load_dwordx4 s[0:3], s[4:5], 0 + 0xbf8c_0000, // s_waitcnt all + 0xe030_2000, 0x8000_0300, // buffer_load_dword v3, v0, s[0:3], 0 idxen + 0xbf8c_0000, // s_waitcnt all + 0x4a06_0687, // v_add_nc_u32 v3, 7, v3 + 0xe070_2000, 0x8000_0300, // buffer_store_dword v3, v0, s[0:3], 0 idxen + S_ENDPGM, + ]; + let root = RootSig { + version: 2, + flags: 0, + params: vec![table(1, 1, 0)], + num_static_samplers: 0, + }; + let mut ex = shader(Stage::Compute, &words, vec![], vec![], Some(root)); + ex.isa_preamble = [0, 4, 1, 1]; + ex +} + +#[test] +fn buffer_add_translates() { + let translated = translate(&buffer_add_shader()).expect("translate buffer add"); + assert!(translated.hlsl.contains("RWStructuredBuffer ubuf0 : register(u0);")); + assert!(translated.hlsl.contains("[numthreads(4, 1, 1)]")); +} + +/// Run explicitly with a Windows SDK and a shader model 6-capable D3D12 WARP runtime. +#[cfg(windows)] +#[test] +#[ignore = "requires DXC and the Windows D3D12 WARP runtime"] +fn translated_compute_buffer_adds_seven() { + let translated = translate(&buffer_add_shader()).expect("translate buffer add"); + let profile = tombox_xshader::dxc::profile_for(&translated.profile); + let dxil = tombox_xshader::dxc::compile(&translated.hlsl, &profile).expect("compile with DXC"); + + let input = [3, 29, u32::MAX - 2, 950]; + let output = d3d12::dispatch_buffer(&dxil, input).expect("dispatch on D3D12 WARP"); + assert_eq!(output, [10, 36, 4, 957]); +}