Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

ui-egui offscreen tests pick their GPU by SOLIDCRAFT_GPU (#9052)

kaltenbrunnermarkus46a2d6b53cff
diff --git a/crates/gpu/src/select.rs b/crates/gpu/src/select.rs
index 9d40665150..388f189c3d 100644
--- a/crates/gpu/src/select.rs
+++ b/crates/gpu/src/select.rs
@@ -232,7 +232,15 @@ pub fn pick_path(policy: Policy, keep: impl Fn(&wgpu::AdapterInfo) -> bool) -> O
/// The adapter `policy` picks, with its report; `None` when no adapter qualifies.
#[cfg(not(target_arch = "wasm32"))]
pub fn pick(policy: Policy) -> Option<(wgpu::Adapter, AdapterReport)> {
- adapters(&instance())
+ pick_among(adapters(&instance()), policy)
+}
+
+/// The adapter `policy` picks among `found`, as [`pick`] picks among the native backends' (best
+/// first, the first whose tier the policy admits), with its report; `None` when none qualifies.
+/// For a caller with an instance of its own, such as the UI's offscreen tests.
+pub fn pick_among(mut found: Vec<wgpu::Adapter>, policy: Policy) -> Option<(wgpu::Adapter, AdapterReport)> {
+ found.sort_by_key(|a| std::cmp::Reverse(rank(a.get_info().device_type)));
+ found
.into_iter()
.map(|a| {
let r = report(&a.get_info());
diff --git a/crates/ui-egui/src/gpu.rs b/crates/ui-egui/src/gpu.rs
index 192f6d29b5..0d2ee73032 100644
--- a/crates/ui-egui/src/gpu.rs
+++ b/crates/ui-egui/src/gpu.rs
@@ -225,18 +225,44 @@ pub(crate) fn offscreen_backends() -> wgpu::Backends {
})
}
-/// A device of its own for an offscreen render or check (`None` without a GPU adapter), on
-/// [`offscreen_backends`]. One test at a time starts its instance, adapter and device: starting an
-/// instance scans the Vulkan drivers, and tests doing that side by side crashed the test process
-/// inside the Vulkan loader (`loader_icd_scan`). Rendering on the devices still runs side by side.
+/// Held while a test starts an instance, adapter or device: starting an instance scans the Vulkan
+/// drivers, and tests doing that side by side crashed the test process inside the Vulkan loader
+/// (`loader_icd_scan`). Rendering on the devices still runs side by side.
#[cfg(all(test, not(target_arch = "wasm32")))]
-pub(crate) fn offscreen_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
+fn offscreen_starting() -> std::sync::MutexGuard<'static, ()> {
static STARTING: std::sync::Mutex<()> = std::sync::Mutex::new(());
- let _one_at_a_time = STARTING.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
+ STARTING.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
+}
+
+/// The adapter an offscreen test opens under `policy` (`SOLIDCRAFT_GPU`, #9040), on
+/// [`offscreen_backends`]; call it holding [`offscreen_starting`]. Under `auto` (the variable
+/// unset) wgpu's default adapter, as before; under `hardware` or `software` the one
+/// `solidcraft_gpu::select` picks for headless pictures of that tier, so `software` keeps the
+/// tests off the hardware GPU; under `cpu`, as for pictures, none. `None` when none qualifies.
+#[cfg(all(test, not(target_arch = "wasm32")))]
+fn offscreen_adapter(policy: solidcraft_gpu::select::Policy) -> Option<wgpu::Adapter> {
+ use solidcraft_gpu::select::Policy;
let mut desc = solidcraft_gpu::instance_descriptor_from_env();
desc.backends = offscreen_backends();
let instance = wgpu::Instance::new(desc);
- let adapter = pollster::block_on(instance.request_adapter(&Default::default())).ok()?;
+ match policy {
+ Policy::Auto => pollster::block_on(instance.request_adapter(&Default::default())).ok(),
+ Policy::Hardware | Policy::Software => {
+ let found = pollster::block_on(instance.enumerate_adapters(offscreen_backends()));
+ solidcraft_gpu::select::pick_among(found, policy).map(|(adapter, _)| adapter)
+ }
+ Policy::Cpu => None,
+ }
+}
+
+/// A device of its own for an offscreen render or check (`None` without a GPU adapter the
+/// `SOLIDCRAFT_GPU` policy admits, or when the variable names no policy), on [`offscreen_backends`]. One test at a time starts its
+/// instance, adapter and device ([`offscreen_starting`]).
+#[cfg(all(test, not(target_arch = "wasm32")))]
+pub(crate) fn offscreen_device() -> Option<(wgpu::Adapter, wgpu::Device, wgpu::Queue)> {
+ let policy = solidcraft_gpu::select::Policy::from_env().map_err(|e| eprintln!("no offscreen device: {e}")).ok()?;
+ let _one_at_a_time = offscreen_starting();
+ let adapter = offscreen_adapter(policy)?;
let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
required_features: solidcraft_gpu::depth_features(&adapter),
required_limits: wgpu::Limits::downlevel_defaults().using_resolution(adapter.limits()),
@@ -648,6 +674,33 @@ mod tests {
assert!(chosen.contains(used), "the offscreen device runs on {used:?}, not on {chosen:?}");
}
}
+ /// `SOLIDCRAFT_GPU` picks the offscreen tests' adapter as it picks headless pictures' (#9040):
+ /// `software` never a hardware GPU (it did, and the tests crashed one), `hardware` never a
+ /// software rasterizer, `cpu` none. A tier without an adapter here is skipped. Only adapters
+ /// are picked: no device is opened.
+ #[cfg(not(target_arch = "wasm32"))]
+ #[test]
+ fn offscreen_tests_pick_their_adapter_by_the_gpu_policy() {
+ use solidcraft_gpu::select::Policy;
+ let _one_at_a_time = super::offscreen_starting();
+ assert!(super::offscreen_adapter(Policy::Cpu).is_none(), "cpu opens no adapter");
+ for (policy, software) in [(Policy::Software, true), (Policy::Hardware, false)] {
+ match super::offscreen_adapter(policy) {
+ Some(adapter) => {
+ let info = adapter.get_info();
+ assert_eq!(
+ info.device_type == egui_wgpu::wgpu::DeviceType::Cpu,
+ software,
+ "{policy:?} picked {} ({:?})",
+ info.name,
+ info.device_type
+ );
+ }
+ None => eprintln!("skipping {policy:?}: no adapter of that tier"),
+ }
+ }
+ }
+
/// A device the driver loses (#4172): its lost callback marks it, the painter's probe sees it
/// before the next frame's uploads, those uploads no longer reach the device (they panicked
/// inside wgpu when they were the first to meet the loss), and pictures (thumbnails of saves)