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.
This commit is contained in:
dotCooCoo
2026-11-20 19:50:51 -08:00
parent ad0cb48e82
commit 98bd29a065
2 changed files with 28 additions and 5 deletions
+16 -5
View File
@@ -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<wgpu::Instance> = 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
+12
View File
@@ -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.