mirror of
https://github.com/storytold/photocraft.git
synced 2026-11-17 13:34:29 +00:00
GPU tests: keep one instance in photocraft-gpu's tests; follow WGPU_BACKEND
photocraft-gpu's own tests made and dropped an instance each, so they could still hit the libvulkan teardown crash. They now share one instance, kept for the whole run and made from WGPU_BACKEND (one test used wgpu's defaults, which ignore it). The kept Vulkan instance in gpu_lock ignored WGPU_BACKEND, so setting it to keep a broken Vulkan driver out of the tests didn't. With Vulkan left out, no instance is kept. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EZbgszHyuEuWAzxaamteuU
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
f7dfc48652
commit
abdbc6ee8e
@@ -25,7 +25,7 @@ fn gpu() -> Option<Gpu> {
|
||||
let lock = crate::GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let required = std::env::var_os("WGPU_BACKEND").is_some();
|
||||
// `WGPU_BACKEND` picks the backend; `PHOTOCRAFT_TEST_FALLBACK_ADAPTER=1` the software one (WARP).
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env());
|
||||
let instance = crate::instance();
|
||||
let fallback = std::env::var_os("PHOTOCRAFT_TEST_FALLBACK_ADAPTER").is_some();
|
||||
let options = wgpu::RequestAdapterOptions { force_fallback_adapter: fallback, ..Default::default() };
|
||||
let adapter = match pollster::block_on(instance.request_adapter(&options)) {
|
||||
|
||||
@@ -12,7 +12,7 @@ use photocraft_raster::Surface;
|
||||
use crate::GPU_LOCK;
|
||||
|
||||
fn device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env());
|
||||
let instance = crate::instance();
|
||||
let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
|
||||
@@ -4,6 +4,14 @@
|
||||
/// creates a device takes this lock and holds it until the device is dropped.
|
||||
static GPU_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
/// The wgpu instance every test uses (`WGPU_BACKEND` picks its backends), kept for the whole
|
||||
/// run: the last instance going away tears the Vulkan loader down, and an instance made while
|
||||
/// that happens can crash in libvulkan.
|
||||
pub(crate) fn instance() -> wgpu::Instance {
|
||||
static KEPT: std::sync::OnceLock<wgpu::Instance> = std::sync::OnceLock::new();
|
||||
KEPT.get_or_init(|| wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env())).clone()
|
||||
}
|
||||
|
||||
mod camera_raw;
|
||||
mod device_loss;
|
||||
mod parity;
|
||||
|
||||
@@ -101,7 +101,7 @@ fn timed<T>(what: &str, run: impl FnOnce() -> T) -> T {
|
||||
fn gpu() -> Option<Gpu> {
|
||||
let lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let required = std::env::var_os("WGPU_BACKEND").is_some();
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env());
|
||||
let instance = crate::instance();
|
||||
let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
@@ -145,10 +145,7 @@ fn gpu() -> Option<Gpu> {
|
||||
/// The returned guard holds [`GPU_LOCK`] until the device is dropped.
|
||||
fn any_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue, std::sync::MutexGuard<'static, ()>)> {
|
||||
let lock = GPU_LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let adapter = pollster::block_on(
|
||||
wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()).request_adapter(&wgpu::RequestAdapterOptions::default()),
|
||||
)
|
||||
.ok()?;
|
||||
let adapter = pollster::block_on(crate::instance().request_adapter(&wgpu::RequestAdapterOptions::default())).ok()?;
|
||||
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?;
|
||||
Some((adapter, device, queue, lock))
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use photocraft_gpu::{Compositor, render_to_vec};
|
||||
use crate::GPU_LOCK;
|
||||
|
||||
fn device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
|
||||
let instance = wgpu::Instance::default();
|
||||
let instance = crate::instance();
|
||||
let Ok(adapter) = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) else {
|
||||
eprintln!("skipping: no adapter");
|
||||
return None;
|
||||
|
||||
@@ -38,7 +38,7 @@ fn block_on<F: std::future::Future>(f: F) -> F::Output {
|
||||
/// A real GPU with timestamp queries, or `None` with the reason printed.
|
||||
fn gpu() -> Option<(wgpu::Device, wgpu::Queue, Compositor)> {
|
||||
let options = wgpu::RequestAdapterOptions { power_preference: wgpu::PowerPreference::HighPerformance, ..Default::default() };
|
||||
let Ok(adapter) = block_on(wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env()).request_adapter(&options)) else {
|
||||
let Ok(adapter) = block_on(crate::instance().request_adapter(&options)) else {
|
||||
eprintln!("SKIP gpu timing: no adapter");
|
||||
return None;
|
||||
};
|
||||
|
||||
@@ -20,7 +20,7 @@ fn fills_size(count: usize, size: Size) -> Document {
|
||||
}
|
||||
|
||||
fn constrained_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env());
|
||||
let instance = crate::instance();
|
||||
let adapter = match pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())) {
|
||||
Ok(adapter) => adapter,
|
||||
Err(error) => {
|
||||
|
||||
@@ -6,7 +6,7 @@ use photocraft_doc::{Adjustment, Document, Effect, Layer, LayerContent, LayerMas
|
||||
use photocraft_geom::Rect;
|
||||
|
||||
fn gpu() -> Option<(wgpu::Device, wgpu::Queue, photocraft_gpu::Compositor)> {
|
||||
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle_from_env());
|
||||
let instance = crate::instance();
|
||||
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default())).ok()?;
|
||||
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor::default())).ok()?;
|
||||
// Exact comparison needs 32-bit float targets (see parity.rs); skip on adapters without them.
|
||||
|
||||
@@ -40,12 +40,22 @@ pub fn gpu_lock() -> MutexGuard<'static, ()> {
|
||||
guard
|
||||
}
|
||||
|
||||
/// Creates the Vulkan instance that stays for the rest of the process.
|
||||
/// Creates the Vulkan instance that stays for the rest of the process, unless `WGPU_BACKEND`
|
||||
/// leaves Vulkan out (to keep a broken Vulkan driver out of the tests).
|
||||
#[cfg(target_os = "linux")]
|
||||
fn keep_vulkan() {
|
||||
use wgpu::{Backends, Instance, InstanceDescriptor};
|
||||
static KEPT: std::sync::OnceLock<Instance> = std::sync::OnceLock::new();
|
||||
KEPT.get_or_init(|| Instance::new(InstanceDescriptor { backends: Backends::VULKAN, ..InstanceDescriptor::new_without_display_handle() }));
|
||||
static KEPT: std::sync::OnceLock<Option<Instance>> = std::sync::OnceLock::new();
|
||||
KEPT.get_or_init(|| {
|
||||
kept_backends(Backends::from_env()).map(|backends| Instance::new(InstanceDescriptor { backends, ..InstanceDescriptor::new_without_display_handle() }))
|
||||
});
|
||||
}
|
||||
|
||||
/// The backends of the kept instance for a `WGPU_BACKEND` of `env`: Vulkan, if it allows it.
|
||||
#[cfg(target_os = "linux")]
|
||||
fn kept_backends(env: Option<wgpu::Backends>) -> Option<wgpu::Backends> {
|
||||
let vulkan = env.unwrap_or(wgpu::Backends::VULKAN) & wgpu::Backends::VULKAN;
|
||||
(!vulkan.is_empty()).then_some(vulkan)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -73,6 +83,16 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// `WGPU_BACKEND=gl` keeps Vulkan out of the tests: no Vulkan instance is kept then.
|
||||
#[cfg(target_os = "linux")]
|
||||
#[test]
|
||||
fn the_kept_instance_follows_wgpu_backend() {
|
||||
use wgpu::Backends;
|
||||
assert_eq!(kept_backends(None), Some(Backends::VULKAN));
|
||||
assert_eq!(kept_backends(Some(Backends::GL)), None);
|
||||
assert_eq!(kept_backends(Some(Backends::GL | Backends::VULKAN)), Some(Backends::VULKAN));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_panicked_holder_does_not_block_the_next_test() {
|
||||
let _ = std::thread::spawn(|| {
|
||||
|
||||
Reference in New Issue
Block a user