Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Merge upstream/main into fix/7537-windows-arm-dx12

Higgs32584c00045d7d950
diff --cc apps/photocraft/src/gpu_startup.rs
index ece3c9886,33be58cd4..9b08d4d29
--- a/apps/photocraft/src/gpu_startup.rs
+++ b/apps/photocraft/src/gpu_startup.rs
@@@ -12,9 -12,10 +12,10 @@@
//! - macOS: Metal → CPU
//!
//! The choice that worked is remembered in `performance.gpuBackend` (Preferences › Performance,
-//! where it can be reset). With `auto`, Intel adapters on Windows use DX12, and when DXGI lists an
-//! Intel adapter the instance leaves Vulkan out, so the first start doesn't load the Intel Vulkan
-//! driver either. `WGPU_BACKEND` still overrides everything, and `--safe-gpu` forces the CPU path
-//! for one launch.
+//! where it can be reset). With `auto`, Intel and Qualcomm (Windows on ARM) adapters on Windows
- //! use DX12. `WGPU_BACKEND` still overrides everything, and `--safe-gpu` forces the CPU path for
- //! one launch.
++//! use DX12, and when DXGI lists an Intel adapter the instance leaves Vulkan out, so the first
++//! start doesn't load the Intel Vulkan driver either. `WGPU_BACKEND` still overrides everything,
++//! and `--safe-gpu` forces the CPU path for one launch.
//!
//! "CPU" composites on the CPU and draws the window with a software adapter where the platform
//! has one (WARP on Windows, llvmpipe over GL elsewhere). On Linux a GL adapter that can't present
@@@ -277,20 -285,59 +302,62 @@@ fn rank(a: &Candidate, backend: GpuBack
(fallback, ty, be)
}
- /// The adapter to use among `adapters` (`None` when there is none).
+ /// The adapter to use among `adapters` (`None` when there is none). Adapters the user asked for
+ /// win over the rest when any of them can present; otherwise the ranking decides (see
+ /// [`wanted_missed`]).
pub fn pick(adapters: &[Candidate], backend: GpuBackend, os: Os) -> Option<usize> {
- adapters.iter().enumerate().filter(|(_, a)| a.surface_ok).min_by_key(|(i, a)| (rank(a, backend, os), *i)).map(|(i, _)| i)
+ let usable = |a: &&Candidate| a.surface_ok;
+ let any_wanted = adapters.iter().filter(usable).any(|a| a.wanted);
+ adapters.iter().enumerate().filter(|(_, a)| a.surface_ok && (a.wanted || !any_wanted)).min_by_key(|(i, a)| (rank(a, backend, os), *i)).map(|(i, _)| i)
+ }
+
+ /// Whether an adapter called `name` is the one the user asked for: `wanted` is a case-insensitive
+ /// piece of its name ("nvidia"), or several words that each appear in it ("arc a370m" finds
+ /// "Intel(R) Arc(tm) A370M Graphics"). An empty `wanted` matches nothing.
+ pub fn adapter_matches(name: &str, wanted: &str) -> bool {
+ photocraft_engine::prefs::gpu_adapter_matches(name, wanted)
+ }
+
+ /// The user asked for an adapter and none of `adapters` that can present is it.
+ pub fn wanted_missed(adapters: &[Candidate], wanted: Option<&str>) -> bool {
+ wanted.is_some() && !adapters.iter().any(|a| a.surface_ok && a.wanted)
+ }
+
+ /// Choosing an adapter is Linux-only for now: Windows has its own Intel/DX12 selection and macOS a
+ /// single Metal path, and neither is tested with a requested adapter.
+ pub const ADAPTER_CHOICE: bool = cfg!(target_os = "linux");
+
+ /// The adapter request that applies to this launch: none off Linux (`linux`), and none in CPU mode,
+ /// which wants a software adapter whatever the preference says.
+ pub fn applicable_adapter(wanted: Option<String>, backend: GpuBackend, linux: bool) -> Option<String> {
+ wanted.filter(|_| linux && backend != GpuBackend::Cpu)
+ }
+
+ /// Whether `PHOTOCRAFT_GPU_ADAPTER` makes this launch its own process: only when startup will act
+ /// on it (a non-empty value, on Linux, outside CPU mode). Otherwise it changes nothing, and the
+ /// launch may still be forwarded to the running window.
+ pub fn adapter_override_is_process_specific(env: Option<&str>, mode: RenderingMode, linux: bool) -> bool {
+ env.is_some_and(|v| !v.trim().is_empty()) && linux && mode != RenderingMode::Cpu
+ }
+
+ /// The adapter to ask for: `PHOTOCRAFT_GPU_ADAPTER` when set (for one launch), else
+ /// `performance.gpuAdapter`; `None` is automatic.
+ pub fn wanted_adapter(env: Option<&str>, pref: &str) -> Option<String> {
+ [env.unwrap_or(""), pref].iter().map(|s| s.trim()).find(|s| !s.is_empty()).map(|s| s.chars().take(256).collect())
}
-/// Whether picking `chosen` applied the Intel-on-Windows DX12 default (a Vulkan adapter of the
-/// same vendor was available, or `vulkan_left_out`: [`windows_auto_backends`] left Vulkan out of
-/// the instance).
-pub fn intel_dx12_applied(adapters: &[Candidate], chosen: usize, os: Os, vulkan_left_out: bool) -> bool {
- let Some(c) = adapters.get(chosen) else { return false };
- os == Os::Windows
- && c.vendor == INTEL
- && c.backend == wgpu::Backend::Dx12
- && (vulkan_left_out || adapters.iter().any(|a| a.vendor == INTEL && a.backend == wgpu::Backend::Vulkan))
+/// The maker's name when picking `chosen` applied the Windows DX12 default of
- /// [`dx12_first_on_windows`] (a Vulkan adapter of the same maker was available).
- pub fn dx12_default_applied(adapters: &[Candidate], chosen: usize, os: Os) -> Option<&'static str> {
++/// [`dx12_first_on_windows`]: a Vulkan adapter of the same maker was available, or, for Intel,
++/// `vulkan_left_out` ([`windows_auto_backends`] left Vulkan out of the instance).
++pub fn dx12_default_applied(adapters: &[Candidate], chosen: usize, os: Os, vulkan_left_out: bool) -> Option<&'static str> {
+ let c = adapters.get(chosen)?;
+ if os != Os::Windows || c.backend != wgpu::Backend::Dx12 {
+ return None;
+ }
+ let maker = dx12_first_on_windows(c.vendor)?;
- adapters.iter().any(|a| a.backend == wgpu::Backend::Vulkan && dx12_first_on_windows(a.vendor) == Some(maker)).then_some(maker)
++ let applied =
++ (vulkan_left_out && c.vendor == INTEL) || adapters.iter().any(|a| a.backend == wgpu::Backend::Vulkan && dx12_first_on_windows(a.vendor) == Some(maker));
++ applied.then_some(maker)
}
/// wgpu compiles indirect-call validation shaders inside `request_device`; Metal's compiler
@@@ -302,9 -349,9 +369,9 @@@ fn skips_indirect_validation(os: Os, in
}
/// Configure eframe's wgpu setup for `plan`: instance backends, the DX12 shader compiler, adapter
-/// selection (Intel on Windows, software adapters for `cpu`) and the marker update once the
-/// adapter is chosen. `note` receives a remark for System Info (e.g. the Intel default).
+/// selection (Intel and Qualcomm on Windows, software adapters for `cpu`) and the marker update
+/// once the adapter is chosen. `note` receives a remark for System Info (e.g. the DX12 default).
- pub fn configure(setup: &mut egui_wgpu::WgpuSetup, plan: &Plan, os: Os, sentinel: SharedSentinel, note: Arc<Mutex<Option<String>>>) {
+ pub fn configure(setup: &mut egui_wgpu::WgpuSetup, plan: &Plan, os: Os, wanted: Option<String>, sentinel: SharedSentinel, note: Arc<Mutex<Option<String>>>) {
if let egui_wgpu::WgpuSetup::CreateNew(create) = setup {
if let Some(b) = backends(plan, os) {
create.instance_descriptor.backends = b;
@@@ -338,11 -396,9 +416,11 @@@
*note.lock().unwrap_or_else(PoisonError::into_inner) =
Some("CPU image rendering; no software graphics adapter is available, so the window uses hardware graphics".into());
}
- if backend == GpuBackend::Auto && intel_dx12_applied(&cands, i, os, vulkan_left_out) {
+ if backend == GpuBackend::Auto
- && let Some(maker) = dx12_default_applied(&cands, i, os)
++ && let Some(maker) = dx12_default_applied(&cands, i, os, vulkan_left_out)
+ {
*note.lock().unwrap_or_else(PoisonError::into_inner) =
- Some("Intel graphics on Windows: using DirectX 12 (the Intel Vulkan driver is known to crash)".into());
+ Some(format!("{maker} graphics on Windows: using DirectX 12 (the {maker} Vulkan driver is known to crash)"));
}
adapters.get(i).cloned().ok_or_else(|| "no graphics adapter found".to_string())
}));
@@@ -634,13 -772,13 +794,13 @@@ mod tests
let intel = [cand(INTEL, T::IntegratedGpu, B::Vulkan), cand(INTEL, T::IntegratedGpu, B::Dx12), cand(INTEL, T::IntegratedGpu, B::Gl)];
let i = pick(&intel, Auto, Os::Windows).unwrap();
assert_eq!(intel[i].backend, B::Dx12);
- assert_eq!(dx12_default_applied(&intel, i, Os::Windows), Some("Intel"));
- assert!(intel_dx12_applied(&intel, i, Os::Windows, false));
++ assert_eq!(dx12_default_applied(&intel, i, Os::Windows, false), Some("Intel"));
// Elsewhere (and for other vendors) Vulkan stays first.
assert_eq!(intel[pick(&intel, Auto, Os::Other).unwrap()].backend, B::Vulkan);
let nv = [cand(0x10de, T::DiscreteGpu, B::Dx12), cand(0x10de, T::DiscreteGpu, B::Vulkan)];
let i = pick(&nv, Auto, Os::Windows).unwrap();
assert_eq!(nv[i].backend, B::Vulkan);
- assert_eq!(dx12_default_applied(&nv, i, Os::Windows), None);
- assert!(!intel_dx12_applied(&nv, i, Os::Windows, false));
++ assert_eq!(dx12_default_applied(&nv, i, Os::Windows, false), None);
// Intel iGPU + discrete GPU: the discrete one wins (high performance), on Vulkan.
let hybrid = [intel[0], intel[1], nv[1]];
assert_eq!(pick(&hybrid, Auto, Os::Windows), Some(2));
@@@ -651,25 -789,45 +811,66 @@@
assert_eq!(pick(&[], Auto, Os::Windows), None);
}
+ /// #7537: on Windows on ARM the Adreno Vulkan driver lost the device mid-session and took the
+ /// window with it; DX12 on the same machine didn't. DXGI and Vulkan report different vendor ids.
+ #[test]
+ fn qualcomm_on_windows_defaults_to_dx12() {
+ use wgpu::{Backend as B, DeviceType as T};
+ let adreno = [cand(QUALCOMM, T::IntegratedGpu, B::Vulkan), cand(QUALCOMM_DXGI, T::IntegratedGpu, B::Dx12)];
+ let i = pick(&adreno, Auto, Os::Windows).unwrap();
+ assert_eq!(adreno[i].backend, B::Dx12);
- assert_eq!(dx12_default_applied(&adreno, i, Os::Windows), Some("Qualcomm"));
++ assert_eq!(dx12_default_applied(&adreno, i, Os::Windows, false), Some("Qualcomm"));
+ // Only Vulkan offered: still Vulkan, and no note.
+ assert_eq!(pick(&adreno[..1], Auto, Os::Windows), Some(0));
- assert_eq!(dx12_default_applied(&adreno, 0, Os::Windows), None);
++ assert_eq!(dx12_default_applied(&adreno, 0, Os::Windows, false), None);
+ // An Intel Vulkan adapter doesn't make a Qualcomm DX12 pick "the default".
+ let mixed = [cand(INTEL, T::IntegratedGpu, B::Vulkan), adreno[1]];
- assert_eq!(dx12_default_applied(&mixed, 1, Os::Windows), None);
++ assert_eq!(dx12_default_applied(&mixed, 1, Os::Windows, false), None);
++ // Vulkan left out of the instance (an Intel adapter was listed) speaks only for Intel.
++ assert_eq!(dx12_default_applied(&adreno[1..], 0, Os::Windows, true), None);
+ // Elsewhere Vulkan stays first.
+ assert_eq!(adreno[pick(&adreno, Auto, Os::Other).unwrap()].backend, B::Vulkan);
+ }
+
+ /// #4: an instance with Vulkan loads the Intel Vulkan driver, which crashed at startup, before
+ /// the selector picks Intel's DX12 adapter. With an Intel adapter in the DX12 list, `auto` on
+ /// Windows creates the instance without Vulkan.
+ #[test]
+ fn auto_on_windows_with_an_intel_adapter_leaves_vulkan_out_of_the_instance() {
+ use wgpu::{Backend as B, DeviceType as T};
+ const NVIDIA: u32 = 0x10de;
+ const AMD: u32 = 0x1002;
+ const WARP: u32 = 0x1414;
+ let auto = plan(Auto, None, None, false, Os::Windows);
+ let dx12_gl = Some(wgpu::Backends::DX12 | wgpu::Backends::GL);
+ assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![INTEL, WARP]), dx12_gl);
+ // Intel iGPU + discrete GPU: the discrete GPU is then used on DX12.
+ assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![NVIDIA, INTEL, WARP]), dx12_gl);
+ // No Intel adapter: egui's default backends, Vulkan included.
+ assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![NVIDIA, WARP]), None);
+ assert_eq!(windows_auto_backends(&auto, Os::Windows, || vec![AMD]), None);
+ assert_eq!(windows_auto_backends(&auto, Os::Windows, Vec::new), None);
+ // `WGPU_BACKEND`, `--safe-gpu`, a chosen backend, a crash fallback and other platforms
+ // don't list the DX12 adapters at all.
+ let not_listed = || -> Vec<u32> { panic!("DX12 adapters listed") };
+ for (p, os) in [
+ (plan(Auto, None, Some("vulkan"), false, Os::Windows), Os::Windows),
+ (plan(Auto, None, None, true, Os::Windows), Os::Windows),
+ (plan(Vulkan, None, None, false, Os::Windows), Os::Windows),
+ (plan(Auto, Some(&crashed("auto", "vulkan")), None, false, Os::Windows), Os::Windows),
+ (plan(Auto, None, None, false, Os::Other), Os::Other),
+ (plan(Auto, None, None, false, Os::Mac), Os::Mac),
+ ] {
+ assert_eq!(windows_auto_backends(&p, os, not_listed), None, "{p:?}");
+ }
+ // Without Vulkan adapters to compare with, System Info still names the Intel default.
+ let listed = [cand(INTEL, T::IntegratedGpu, B::Dx12), cand(WARP, T::Cpu, B::Dx12), cand(INTEL, T::IntegratedGpu, B::Gl)];
+ let i = pick(&listed, Auto, Os::Windows).unwrap();
+ assert_eq!(i, 0);
- assert!(intel_dx12_applied(&listed, i, Os::Windows, true));
- assert!(!intel_dx12_applied(&listed, i, Os::Windows, false));
++ assert_eq!(dx12_default_applied(&listed, i, Os::Windows, true), Some("Intel"));
++ assert_eq!(dx12_default_applied(&listed, i, Os::Windows, false), None);
+ }
+
#[test]
fn cpu_prefers_software_adapters() {
use wgpu::{Backend as B, DeviceType as T};
diff --cc docs/development.md
index 532f97021,4e450daff..5321e6b69
--- a/docs/development.md
+++ b/docs/development.md
@@@ -86,7 -86,9 +86,9 @@@ On Windows and Linux the first PhotoCra
## Graphics startup and device loss
- `--safe-gpu` starts with the CPU renderer for one launch (no GPU canvas; a software adapter for the window where the platform has one: WARP on Windows, llvmpipe over GL on Linux). Before creating the wgpu device the app writes and locks `gpu-starting.json` in the config directory; it clears it once the first frames have rendered. A launch that finds an unlocked marker knows the previous start died inside the graphics driver (#4) and uses the next safer backend (Windows: Vulkan → DX12 → CPU; Linux: Vulkan → GL → CPU; macOS: Metal → CPU), remembering a working hardware backend in `performance.gpuBackend` (Preferences › Performance › GPU Backend, with **Reset GPU Backend**). CPU recovery is temporary until **Keep Using CPU** is chosen in the recovery dialog; dismissing the dialog or quitting without a choice preserves the previous GPU preference (#3483). With `auto`, Intel and Qualcomm (Windows on ARM) adapters on Windows use DX12. Help › System Info shows the adapter, backend, driver and fallback state.
-`--safe-gpu` starts with the CPU renderer for one launch (no GPU canvas; a software adapter for the window where the platform has one: WARP on Windows, llvmpipe over GL on Linux). Before creating the wgpu device the app writes and locks `gpu-starting.json` in the config directory; it clears it once the first frames have rendered. A launch that finds an unlocked marker knows the previous start died inside the graphics driver (#4) and uses the next safer backend (Windows: Vulkan → DX12 → CPU; Linux: Vulkan → GL → CPU; macOS: Metal → CPU), remembering a working hardware backend in `performance.gpuBackend` (Preferences › Performance › GPU Backend, with **Reset GPU Backend**). CPU recovery is temporary until **Keep Using CPU** is chosen in the recovery dialog; dismissing the dialog or quitting without a choice preserves the previous GPU preference (#3483). With `auto`, Intel adapters on Windows use DX12: when DXGI lists an Intel adapter, the app creates its wgpu instance with DX12 and GL only, so the first start doesn't load the Intel Vulkan driver, and a discrete GPU beside the Intel one also runs on DX12. Help › System Info shows the adapter, backend, driver and fallback state.
++`--safe-gpu` starts with the CPU renderer for one launch (no GPU canvas; a software adapter for the window where the platform has one: WARP on Windows, llvmpipe over GL on Linux). Before creating the wgpu device the app writes and locks `gpu-starting.json` in the config directory; it clears it once the first frames have rendered. A launch that finds an unlocked marker knows the previous start died inside the graphics driver (#4) and uses the next safer backend (Windows: Vulkan → DX12 → CPU; Linux: Vulkan → GL → CPU; macOS: Metal → CPU), remembering a working hardware backend in `performance.gpuBackend` (Preferences › Performance › GPU Backend, with **Reset GPU Backend**). CPU recovery is temporary until **Keep Using CPU** is chosen in the recovery dialog; dismissing the dialog or quitting without a choice preserves the previous GPU preference (#3483). With `auto`, Intel and Qualcomm (Windows on ARM) adapters on Windows use DX12; when DXGI lists an Intel adapter, the app creates its wgpu instance with DX12 and GL only, so the first start doesn't load the Intel Vulkan driver, and a discrete GPU beside the Intel one also runs on DX12. Help › System Info shows the adapter, backend, driver and fallback state.
+
+ On Linux, on a machine with several GPUs (say an eGPU, a discrete laptop GPU and an iGPU), `auto` takes the first discrete adapter the driver lists; choose another in Preferences › Performance › Advanced › GPU adapter (`performance.gpuAdapter`, Linux; a case-insensitive piece of the adapter's name, empty is automatic), or for one launch with `PHOTOCRAFT_GPU_ADAPTER=nvidia`. An adapter that is missing or can't present to the window falls back to the automatic choice, with a note in System Info.
On DX12 the shader compiler is FXC (`d3dcompiler_47.dll`, part of Windows), or a `dxcompiler.dll` placed beside `photocraft.exe`, loaded by its full path. wgpu's default looks `dxcompiler.dll` up by name, which reaches the current directory and `PATH` and loaded other programs' incompatible builds (#712).