diff --git a/crates/gpu/src/path/mod.rs b/crates/gpu/src/path/mod.rs index 6215400cc..41d19e3cf 100644 --- a/crates/gpu/src/path/mod.rs +++ b/crates/gpu/src/path/mod.rs @@ -217,6 +217,7 @@ impl Executor { lost.store(false, Ordering::Release); log::warn!("gpu-path device lost ({reason:?}); executor unavailable: {message}"); }); + let compiling = one_compile_at_a_time(); let scopes = scopes(&device); let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor { label: Some("gpu-path"), source: wgpu::ShaderSource::Wgsl(SHADER.into()) }); @@ -251,11 +252,21 @@ impl Executor { cache: None, }); checked(scopes)?; + drop(compiling); warm_up(&device, &queue, &pipeline, &placeholder_bind(&device, &layout, 7)?, &healthy)?; Ok(Self { context: Arc::new(Context { healthy, device, queue, pipeline, layout, denoise: std::sync::OnceLock::new() }) }) } } +/// Native drivers compile the path pipeline for minutes and gigabytes on a cold shader cache. +/// Executors opened side by side (parallel tests, each on its own device) compiled it together +/// until memory ran out (#6706). One compile at a time per process: the first fills the driver's +/// cache, and the waiting ones then get it from there. +fn one_compile_at_a_time() -> std::sync::MutexGuard<'static, ()> { + static COMPILE: std::sync::Mutex<()> = std::sync::Mutex::new(()); + COMPILE.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} + // Small zeroed bindings: binding 0 is a uniform whose zero work count makes every // invocation return at once; the others are storage buffers. fn placeholder_bind(device: &wgpu::Device, layout: &wgpu::BindGroupLayout, count: u32) -> Result { diff --git a/crates/gpu/src/path/tests.rs b/crates/gpu/src/path/tests.rs index 77d6fe18e..ea150c073 100644 --- a/crates/gpu/src/path/tests.rs +++ b/crates/gpu/src/path/tests.rs @@ -593,6 +593,25 @@ fn path_devices_are_vulkan_or_metal_only() { } } +/// #6706: executors opened side by side never compile the path pipeline at the same time. +#[test] +fn path_pipelines_compile_one_at_a_time() { + use std::sync::atomic::AtomicUsize; + let inside = AtomicUsize::new(0); + let most = AtomicUsize::new(0); + std::thread::scope(|scope| { + for _ in 0..8 { + scope.spawn(|| { + let _compiling = one_compile_at_a_time(); + most.fetch_max(inside.fetch_add(1, Ordering::SeqCst) + 1, Ordering::SeqCst); + std::thread::sleep(Duration::from_millis(5)); + inside.fetch_sub(1, Ordering::SeqCst); + }); + } + }); + assert_eq!(most.load(Ordering::SeqCst), 1); +} + #[test] fn viewport_path_backends_keep_windows_on_its_selected_backend() { for backend in [wgpu::Backend::Dx12, wgpu::Backend::Gl, wgpu::Backend::Vulkan] {