Merge pull request #2769 from dotCooCoo/fix/gpu-wgpu-real-device-tests

craft-gpu-wgpu: contexts opened on several threads share one wgpu instance
This commit is contained in:
BFlat
2026-11-26 10:12:14 +07:00
committed by GitHub
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.