From 98bd29a0650bf7737c70db6aeaac2c6d4b193878 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 19:50:51 -0800 Subject: [PATCH] craft-gpu-wgpu: share one wgpu instance across contexts and probes WgpuContext::new and probe_caps each made their own wgpu instance, so every context loaded and unloaded every installed Vulkan driver. On Ubuntu with mesa-vulkan-drivers installed, contexts opened from several threads at once crashed with SIGSEGV while a lavapipe device was in use: cargo test of craft-gpu-wgpu with adapter-probe,native-vulkan crashed in 3 of 10 runs. native::instance() now makes the instance once and returns a clone of it on every later call. WGPU_BACKEND is read on the first call. --- crates/gpu-wgpu/src/lib.rs | 21 ++++++++++++++++----- crates/gpu-wgpu/src/tests.rs | 12 ++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/gpu-wgpu/src/lib.rs b/crates/gpu-wgpu/src/lib.rs index 1b26b30383..235e6e2406 100644 --- a/crates/gpu-wgpu/src/lib.rs +++ b/crates/gpu-wgpu/src/lib.rs @@ -90,12 +90,23 @@ mod native { /// `new_without_display_handle` because this context never has a window (no surface is ever /// requested); FilmCraft's own off-screen export device uses the same constructor for the /// same reason (`filmcraft-gpu`'s `export_renderer.rs`, see `docs/gpu-backends.md`). + /// + /// The process holds one instance, made on the first call and returned by every later one, + /// so `WGPU_BACKEND` is read once. Every context and every probe requests its adapter from + /// it. An instance per context loads and unloads every installed Vulkan driver each time, and + /// on Linux with Ubuntu's `mesa-vulkan-drivers` installed, contexts opened that way from + /// several threads at once crash with SIGSEGV while a lavapipe device is in use. pub(crate) fn instance() -> wgpu::Instance { - let mut desc = wgpu::InstanceDescriptor::new_without_display_handle(); - if let Some(backends) = wgpu::Backends::from_env() { - desc.backends = backends; - } - wgpu::Instance::new(desc) + static SHARED: std::sync::OnceLock = std::sync::OnceLock::new(); + SHARED + .get_or_init(|| { + let mut desc = wgpu::InstanceDescriptor::new_without_display_handle(); + if let Some(backends) = wgpu::Backends::from_env() { + desc.backends = backends; + } + wgpu::Instance::new(desc) + }) + .clone() } /// Request a real device. `None` on the defined no-adapter path (no usable adapter): the diff --git a/crates/gpu-wgpu/src/tests.rs b/crates/gpu-wgpu/src/tests.rs index 6f0b74f88e..e4bd2a05a3 100644 --- a/crates/gpu-wgpu/src/tests.rs +++ b/crates/gpu-wgpu/src/tests.rs @@ -335,6 +335,18 @@ fn device_status_surfaces_loss_even_with_nothing_pending() { assert!(matches!(ctx.device_status(), Err(GpuFault::DeviceLost(_))), "device.destroy() must surface through device_status() alone"); } +/// Every context and probe requests its adapter from one process-wide instance, so opening +/// contexts on several threads does not load and unload the Vulkan drivers once per context. No +/// adapter is needed: an instance exists even on a host with no usable backend. +#[cfg(all(not(target_arch = "wasm32"), feature = "adapter-probe"))] +#[test] +fn every_context_and_probe_shares_one_wgpu_instance() { + let first = crate::native::instance(); + let again = std::thread::spawn(crate::native::instance).join().expect("instance() on another thread"); + assert_eq!(first, again, "a second call, from another thread, must return the same instance"); + assert_eq!(first, crate::native::instance(), "a later call must return the same instance"); +} + /// Same two guarantees on a real device: empty buffers round-trip (the readback resolves without /// mapping, since wgpu refuses an empty map range) and `read_buffer` re-queues completions that /// belong to another outstanding request.