From 3407d22183bda181f3d90f9122bb0a518d040c07 Mon Sep 17 00:00:00 2001 From: Tiago Leal <183942127+tiagonleal@users.noreply.github.com> Date: Thu, 26 Nov 2026 11:18:45 +0000 Subject: [PATCH] D3D12.X: Reset and CreateCommandList do not wait for a compute pipeline still being built After #2118 a dispatch waits for its compute pipeline at the submit, not in the bind. SetPipelineState takes a pending compute pipeline lazily (NativePipelineBindMode::Lazy, the stand-in, no wait), and the re-bind after KickoffX uses bind::initial_pipeline, which does not wait either. Reset(allocator, initial_pso) (reset_pc) and CreateCommandList with an initial pipeline (create_list, path without the console list) prepare their pipeline with PreparedNativePipeline::new, which selects with ForBind: that waits for a compute pipeline and may build it on the title's thread. These two now use PreparedNativePipeline::new_lazy, the same retained resolution selected with Lazy, like SetPipelineState. bind::reset and on_create keep the title's own pipeline (the stand-in), and bind::dispatch / execute_indirect defer when it is pending, so the dispatch that follows is still left for the submit. The Reset of the PC path (real::cl_reset) keeps ForBind. Tests a_reset_with_a_compute_pipeline_still_building_does_not_wait, a_kickoff_with_a_compute_pipeline_still_building_does_not_wait and a_command_list_created_with_a_compute_pipeline_still_building_does_not_wait, one per path. Each makes a compute pipeline whose build stays pending and checks the call returns without waiting for it and that a Dispatch right after is put off to the submit; the Reset one also checks the list tracks the stand-in. With the two calls back on PreparedNativePipeline::new the Reset and CreateCommandList tests fail ("Reset waited for the build (10.0014942s)", "CreateCommandList waited for the build (10.001688s)"); the KickoffX one passes on main already and stays as a regression test. --- crates/xcl/src/hle/d3d12x/bind.rs | 19 ++- crates/xcl/src/hle/d3d12x/device_x.rs | 2 +- crates/xcl/src/hle/d3d12x/list_x.rs | 224 +++++++++++++++++++++++++- 3 files changed, 241 insertions(+), 4 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/bind.rs b/crates/xcl/src/hle/d3d12x/bind.rs index b50b835091..c9d8c41a9d 100644 --- a/crates/xcl/src/hle/d3d12x/bind.rs +++ b/crates/xcl/src/hle/d3d12x/bind.rs @@ -1126,12 +1126,27 @@ impl PreparedNativePipeline { Self::from_resolution(original, super::pso_async::begin_pipeline_resolution(original)) } + /// `new` that does not wait for a compute pipeline still being built (the stand-in, as + /// SetPipelineState): Reset and CreateCommandList leave the dispatch that follows for the submit. + pub(super) fn new_lazy(original: usize) -> Self { + let resolution = super::pso_async::begin_pipeline_resolution(original); + Self::from_resolution_mode(original, resolution, super::pso_async::NativePipelineBindMode::Lazy) + } + fn from_resolution( original: usize, resolution: Option, ) -> Self { - use super::pso_async::{NativePipelineBindMode, NativePipelineChoice}; - let choice = resolution.as_ref().map(|r| r.select_for_native_bind(NativePipelineBindMode::ForBind)); + Self::from_resolution_mode(original, resolution, super::pso_async::NativePipelineBindMode::ForBind) + } + + fn from_resolution_mode( + original: usize, + resolution: Option, + mode: super::pso_async::NativePipelineBindMode, + ) -> Self { + use super::pso_async::NativePipelineChoice; + let choice = resolution.as_ref().map(|r| r.select_for_native_bind(mode)); let (raw, generation, poison_on_commit) = match choice { Some(NativePipelineChoice::Exact { raw, generation }) => (raw, Some(generation), false), Some(NativePipelineChoice::InitialFallback { raw, .. }) => (raw, None, false), diff --git a/crates/xcl/src/hle/d3d12x/device_x.rs b/crates/xcl/src/hle/d3d12x/device_x.rs index 260207086e..80bba7af20 100644 --- a/crates/xcl/src/hle/d3d12x/device_x.rs +++ b/crates/xcl/src/hle/d3d12x/device_x.rs @@ -107,7 +107,7 @@ unsafe fn create_list(this: *mut c_void, node: u32, ty: u32, alloc: usize, pso: let hr = if super::console_list::enabled() { real::dev_create_command_list_x(this, node, ty, alloc, pso, riid, ppv) } else { - let prepared = super::bind::PreparedNativePipeline::new(pso); + let prepared = super::bind::PreparedNativePipeline::new_lazy(pso); let resolved_pso = prepared.raw(); let pc_alloc = active_list_allocator(alloc); let hr = real::dev_create_command_list(this, node, ty, pc_alloc, resolved_pso, riid, ppv); diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index b18c6e1c05..df1843d6ae 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -6225,7 +6225,7 @@ unsafe fn reset_pc_inner(this: *mut c_void, alloc: usize, pso: usize) -> i32 { // A failed native Reset leaves the old recording alive. Do not discard its tracked state or // transfer its allocations until the driver accepts the new recording. let ext = *(this as *const usize); - let prepared = super::bind::PreparedNativePipeline::new(pso); + let prepared = super::bind::PreparedNativePipeline::new_lazy(pso); let resolved = prepared.raw(); // (the PC allocator the title's allocator stands for now: `alloc_pool`) let ty = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(this as usize))).and_then(|s| s.list_type).map_or(-1, |t| t.0); @@ -11296,6 +11296,228 @@ mod tests { } } + #[test] + fn a_reset_with_a_compute_pipeline_still_building_does_not_wait() { + use super::super::{bind, pso_async, title_list}; + use std::time::{Duration, Instant}; + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let warp = CreateDXGIFactory1::().and_then(|f| f.EnumWarpAdapter::()); + if warp.and_then(|w| D3D12CreateDevice(&w, D3D_FEATURE_LEVEL_11_0, &mut dev)).is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + } + let dev = dev.unwrap(); + if crate::gpu::recomp::dxc::location().is_err() { + crate::gpu::recomp::runner::skip_test("no DXC"); + return; + } + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap(); + let raw: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &alloc, None).unwrap(); + + // the shared driver pipeline a stand-in binds in its place (`standin::shell`) + let dxil = crate::gpu::recomp::dxc::compile("[RootSignature(\"RootFlags(0)\")] [numthreads(1, 1, 1)] void main() {}", "main", "cs_6_0", &[]).unwrap(); + let rs: ID3D12RootSignature = dev.CreateRootSignature(0, &dxil).unwrap(); + let driver: ID3D12PipelineState = dev + .CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: transmute_copy(&rs), + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: dxil.as_ptr() as _, BytecodeLength: dxil.len() }, + ..Default::default() + }) + .unwrap(); + let standin = 0x7E57_D002usize; + static BUILD: AtomicBool = AtomicBool::new(false); + pso_async::submit( + standin, + true, + Box::new(|| { + let t = Instant::now(); + while !BUILD.load(Ordering::Acquire) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(5)); + } + 0 + }), + ); + pso_async::shared(standin, driver.as_raw() as usize); + let pc0 = raw.into_raw(); + real::adopt(pc0, real::Kind::CommandList); + on_create(pc0, alloc.as_raw() as usize, 0, D3D12_COMMAND_LIST_TYPE_COMPUTE); + let list = ID3D12GraphicsCommandList::from_raw_borrowed(&pc0).unwrap(); + list.Close().unwrap(); + + let t = Instant::now(); + let hr = reset_pc(pc0, alloc.as_raw() as usize, standin); + assert!(t.elapsed() < Duration::from_millis(700), "Reset waited for the build ({:?})", t.elapsed()); + assert!(hr >= 0, "Reset failed ({hr:#x})"); + assert!(pso_async::is_pending(standin), "Reset waited for the build"); + assert_eq!(LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(pc0 as usize))).map(|s| s.bound_pso), Some(standin)); + + // the dispatch that follows is still left for the submit, as after a SetPipelineState + let wrap = title_list::wrap(pc0); + let t = Instant::now(); + bind::dispatch(pc0, 1, 1, 1); + assert!(t.elapsed() < Duration::from_millis(700), "the dispatch waited for the build ({:?})", t.elapsed()); + assert!(pso_async::is_pending(standin)); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 3, "the list before the dispatch, the dispatch, the list after it"); + + BUILD.store(true, Ordering::Release); + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); + assert_eq!(rel_fn(wrap), 0); + let _ = pso_async::released(standin); + } + } + + /// A D3D12 device for the tests of a pipeline still being built (WARP without a GPU); None after + /// `skip_test` when there is no device or no DXC. + unsafe fn device_for_pending_pipeline_tests() -> Option { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + use windows::Win32::Graphics::Dxgi::*; + + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + let warp = CreateDXGIFactory1::().and_then(|f| f.EnumWarpAdapter::()); + if warp.and_then(|w| D3D12CreateDevice(&w, D3D_FEATURE_LEVEL_11_0, &mut dev)).is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return None; + } + } + if crate::gpu::recomp::dxc::location().is_err() { + crate::gpu::recomp::runner::skip_test("no DXC"); + return None; + } + dev + } + + /// The compute stand-in `standin`, its build pending until `release` is set (10 s at most), and + /// the driver pipeline that binds in its place (`standin::shell`; the caller keeps it alive). + unsafe fn pending_compute_standin(dev: &ID3D12Device, standin: usize, release: &'static AtomicBool) -> ID3D12PipelineState { + let dxil = crate::gpu::recomp::dxc::compile("[RootSignature(\"RootFlags(0)\")] [numthreads(1, 1, 1)] void main() {}", "main", "cs_6_0", &[]).unwrap(); + let rs: ID3D12RootSignature = dev.CreateRootSignature(0, &dxil).unwrap(); + let driver: ID3D12PipelineState = dev + .CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC { + pRootSignature: transmute_copy(&rs), + CS: D3D12_SHADER_BYTECODE { pShaderBytecode: dxil.as_ptr() as _, BytecodeLength: dxil.len() }, + ..Default::default() + }) + .unwrap(); + super::super::pso_async::submit( + standin, + true, + Box::new(move || { + let t = Instant::now(); + while !release.load(Ordering::Acquire) && t.elapsed() < Duration::from_secs(10) { + std::thread::sleep(Duration::from_millis(5)); + } + 0 + }), + ); + super::super::pso_async::shared(standin, driver.as_raw() as usize); + driver + } + + #[test] + fn a_kickoff_with_a_compute_pipeline_still_building_does_not_wait() { + use super::super::{bind, console_list, pso_async, title_list}; + + if console_list::enabled() { + return; + } + unsafe { + let Some(dev) = device_for_pending_pipeline_tests() else { return }; + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap(); + let raw: ID3D12GraphicsCommandList = dev.CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_COMPUTE, &alloc, None).unwrap(); + let queue: ID3D12CommandQueue = dev + .CreateCommandQueue(&D3D12_COMMAND_QUEUE_DESC { Type: D3D12_COMMAND_LIST_TYPE_COMPUTE, ..Default::default() }) + .unwrap(); + + static BUILD: AtomicBool = AtomicBool::new(false); + let standin = 0x7E57_D003usize; + let _driver = pending_compute_standin(&dev, standin, &BUILD); + + let q = queue.into_raw(); + real::adopt(q, real::Kind::CommandQueue); + on_queue_create(q as usize, dev.as_raw() as usize, D3D12_COMMAND_LIST_TYPE_COMPUTE); + let pc0 = raw.into_raw(); + real::adopt(pc0, real::Kind::CommandList); + // a list recording with the pending pipeline bound, as after SetPipelineState + on_create(pc0, alloc.as_raw() as usize, standin, D3D12_COMMAND_LIST_TYPE_COMPUTE); + bind::reset(pc0, alloc.as_raw() as usize, standin); + LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&(pc0 as usize))).unwrap().last_queue = Some(q as usize); + + let t = Instant::now(); + kickoff_x(pc0); + assert!(t.elapsed() < Duration::from_millis(700), "KickoffX waited for the build ({:?})", t.elapsed()); + assert!(pso_async::is_pending(standin), "KickoffX waited for the build"); + assert_eq!(LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(pc0 as usize))).map(|s| s.bound_pso), Some(standin)); + + // the dispatch that follows is still left for the submit + let wrap = title_list::wrap(pc0); + let t = Instant::now(); + bind::dispatch(pc0, 1, 1, 1); + assert!(t.elapsed() < Duration::from_millis(700), "the dispatch waited for the build ({:?})", t.elapsed()); + assert!(pso_async::is_pending(standin)); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 3, "the list before the dispatch, the dispatch, the list after it"); + + BUILD.store(true, Ordering::Release); + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); + assert_eq!(rel_fn(wrap), 0); + on_queue_release(q as usize); + let _ = pso_async::released(standin); + } + } + + #[test] + fn a_command_list_created_with_a_compute_pipeline_still_building_does_not_wait() { + use super::super::{bind, console_list, device_x, pso_async, title_list}; + + if console_list::enabled() { + return; + } + unsafe { + let Some(dev) = device_for_pending_pipeline_tests() else { return }; + real::adopt(dev.as_raw(), real::Kind::Device); + let alloc: ID3D12CommandAllocator = dev.CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE).unwrap(); + + static BUILD: AtomicBool = AtomicBool::new(false); + let standin = 0x7E57_D004usize; + let _driver = pending_compute_standin(&dev, standin, &BUILD); + + let mut out: *mut c_void = std::ptr::null_mut(); + let iid = windows_sys::core::GUID::from_u128(0x5b160d0f_ac1b_4185_8ba8_b3ae42a5a455); // ID3D12GraphicsCommandList + let t = Instant::now(); + let hr = device_x::create_command_list_pc(dev.as_raw(), 0, D3D12_COMMAND_LIST_TYPE_COMPUTE.0 as u32, alloc.as_raw() as usize, standin, &iid, &mut out); + assert!(t.elapsed() < Duration::from_millis(700), "CreateCommandList waited for the build ({:?})", t.elapsed()); + assert!(hr >= 0 && !out.is_null(), "CreateCommandList failed ({hr:#x})"); + assert!(pso_async::is_pending(standin), "CreateCommandList waited for the build"); + let wrap = out; + let pc0 = title_list::backing(wrap).expect("wrapped in title_list"); + assert_eq!(LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(pc0 as usize))).map(|s| s.bound_pso), Some(standin)); + + // the dispatch that follows is still left for the submit + let t = Instant::now(); + bind::dispatch(pc0, 1, 1, 1); + assert!(t.elapsed() < Duration::from_millis(700), "the dispatch waited for the build ({:?})", t.elapsed()); + assert!(pso_async::is_pending(standin)); + let mut segs = Vec::new(); + assert!(title_list::push_segments(wrap, &mut segs)); + assert_eq!(segs.len(), 3, "the list before the dispatch, the dispatch, the list after it"); + + BUILD.store(true, Ordering::Release); + let rel_fn: unsafe extern "system" fn(*mut c_void) -> u32 = std::mem::transmute(*(*(wrap as *const *const usize)).add(2)); + assert_eq!(rel_fn(wrap), 0); + let _ = pso_async::released(standin); + } + } + /// Reproduces the exact intra-list fork-join pattern seen on retail compute queues: /// q_compute list O_c: Write64(cell_A, frame) -> Wait64(cell_B >= frame) -> Write64(cell_C, frame) /// q_direct list O_d: Wait64(cell_A >= frame) -> Write64(cell_B, frame)