diff --git a/apps/pdfcraft/src/main.rs b/apps/pdfcraft/src/main.rs index 342995ff89..08a4be0ce0 100644 --- a/apps/pdfcraft/src/main.rs +++ b/apps/pdfcraft/src/main.rs @@ -791,11 +791,11 @@ fn configure_gpu(native: &mut eframe::NativeOptions) { if !displays.is_empty() || compositor.is_some() || cfg!(target_os = "windows") { setup.native_adapter_selector = Some(std::sync::Arc::new(move |adapters, surface| { let usable: Vec<&eframe::wgpu::Adapter> = adapters.iter().filter(|a| surface.is_none_or(|s| a.is_surface_supported(s))).collect(); - let infos: Vec<(u32, u32, eframe::wgpu::DeviceType)> = usable + let infos: Vec<(u32, u32, eframe::wgpu::DeviceType, eframe::wgpu::Backend)> = usable .iter() .map(|a| { let info = a.get_info(); - (info.vendor, info.device, info.device_type) + (info.vendor, info.device, info.device_type, info.backend) }) .collect(); let picked = pick_adapter(&infos, &displays, compositor).and_then(|i| usable.get(i)).map(|a| (*a).clone()); @@ -964,15 +964,22 @@ fn linux_display_gpus(drm: &std::path::Path) -> Vec { /// PCI ids) first, then one that drives a display. Among several of those, the one driving the /// primary display (a hybrid laptop's panel, #8; the primary display on Windows, #378), or else, /// with no primary display known (a Linux desktop with a monitor on each GPU, #445), the discrete -/// one. Otherwise the most frugal kind: integrated, discrete, other, virtual, software. -fn pick_adapter(adapters: &[(u32, u32, eframe::wgpu::DeviceType)], displays: &[DisplayGpu], compositor: Option<(u32, u32)>) -> Option { - use eframe::wgpu::DeviceType; +/// one. Otherwise the most frugal kind: integrated, discrete, other, virtual, software. Of one +/// GPU's adapters, the one wgpu reaches through OpenGL comes last, whatever order wgpu lists them +/// in: on Windows wgpu offers a Direct3D 12 GPU through OpenGL too, and declines OpenGL there +/// (`wgpu_draws_with`). +fn pick_adapter( + adapters: &[(u32, u32, eframe::wgpu::DeviceType, eframe::wgpu::Backend)], + displays: &[DisplayGpu], + compositor: Option<(u32, u32)>, +) -> Option { + use eframe::wgpu::{Backend, DeviceType}; let display = |pci: (u32, u32)| displays.iter().find(|g| g.pci == pci); let multi_gpu_desktop = displays.len() > 1 && !displays.iter().any(|g| g.primary); adapters .iter() .enumerate() - .min_by_key(|(_, (vendor, device, kind))| { + .min_by_key(|(_, (vendor, device, kind, backend))| { let shown = display((*vendor, *device)); let drives_display = shown.is_some(); let drives_primary = shown.is_some_and(|g| g.primary); @@ -986,7 +993,7 @@ fn pick_adapter(adapters: &[(u32, u32, eframe::wgpu::DeviceType)], displays: &[D DeviceType::VirtualGpu => 4, DeviceType::Cpu => 5, }; - (compositor != Some((*vendor, *device)), !drives_display, !drives_primary, rank) + (compositor != Some((*vendor, *device)), !drives_display, !drives_primary, rank, *backend == Backend::Gl) }) .map(|(i, _)| i) } diff --git a/apps/pdfcraft/src/tests.rs b/apps/pdfcraft/src/tests.rs index 5d813603fe..b68ca0c2ca 100644 --- a/apps/pdfcraft/src/tests.rs +++ b/apps/pdfcraft/src/tests.rs @@ -33,6 +33,32 @@ fn on_windows_wgpu_leaves_an_opengl_only_gpu_to_eframes_opengl_renderer() { } } +#[test] +fn pick_adapter_takes_a_gpus_opengl_adapter_last_and_an_opengl_only_gpu_over_software() { + use eframe::wgpu::{Backend, DeviceType}; + // One GPU offered through OpenGL and through Direct3D 12 or Vulkan, in either order: never + // OpenGL, which `wgpu_draws_with` declines on Windows. + for other in [Backend::Dx12, Backend::Vulkan] { + let gl_first = + [(INTEL_IGPU.0, INTEL_IGPU.1, DeviceType::IntegratedGpu, Backend::Gl), (INTEL_IGPU.0, INTEL_IGPU.1, DeviceType::IntegratedGpu, other)]; + assert_eq!(super::pick_adapter(&gl_first, &[panel(INTEL_IGPU)], None), Some(1), "{other:?}"); + assert_eq!(super::pick_adapter(&[gl_first[1], gl_first[0]], &[panel(INTEL_IGPU)], None), Some(0), "{other:?}"); + assert_eq!(super::pick_adapter(&gl_first, &[], None), Some(1), "{other:?} with no display known"); + } + // Issue #4134: an Intel HD 4000 has only OpenGL, and the Microsoft Basic Render Driver (in + // software) has Direct3D 12. The GPU is still picked, by kind, with or without a known + // display, as over Remote Desktop; declining it then starts glow on that GPU. + const HD_4000: (u32, u32) = (0x8086, 0x0166); + let ivy_bridge = [(0x1414, 0x008c, DeviceType::Cpu, Backend::Dx12), (HD_4000.0, HD_4000.1, DeviceType::IntegratedGpu, Backend::Gl)]; + assert_eq!(super::pick_adapter(&ivy_bridge, &[panel(HD_4000)], None), Some(1)); + assert_eq!(super::pick_adapter(&ivy_bridge, &[], None), Some(1)); + assert!(!super::wgpu_draws_with(Backend::Gl, true, false)); + // Only OpenGL, or only software: that adapter. + assert_eq!(super::pick_adapter(&ivy_bridge[1..], &[], None), Some(0)); + assert_eq!(super::pick_adapter(&ivy_bridge[..1], &[], None), Some(0)); + assert_eq!(super::pick_adapter(&[], &[], None), None); +} + #[test] fn opengl_is_the_fallback_unless_a_renderer_was_chosen() { use super::{RendererChoice, native_options, renderer_choice, retry_with_gl}; @@ -336,9 +362,13 @@ const NVIDIA: (u32, u32) = (0x10de, 0x2684); const NVIDIA_3090: (u32, u32) = (0x10de, 0x2204); const AMD_IGPU: (u32, u32) = (0x1002, 0x164e); -fn adapters() -> Vec<(u32, u32, eframe::wgpu::DeviceType)> { - use eframe::wgpu::DeviceType; - vec![(NVIDIA.0, NVIDIA.1, DeviceType::DiscreteGpu), (AMD_IGPU.0, AMD_IGPU.1, DeviceType::IntegratedGpu), (0, 0, DeviceType::Cpu)] +fn adapters() -> Vec<(u32, u32, eframe::wgpu::DeviceType, eframe::wgpu::Backend)> { + use eframe::wgpu::{Backend, DeviceType}; + vec![ + (NVIDIA.0, NVIDIA.1, DeviceType::DiscreteGpu, Backend::Vulkan), + (AMD_IGPU.0, AMD_IGPU.1, DeviceType::IntegratedGpu, Backend::Vulkan), + (0, 0, DeviceType::Cpu, Backend::Vulkan), + ] } const INTEL_IGPU: (u32, u32) = (0x8086, 0xa780); @@ -405,8 +435,12 @@ fn pick_adapter_keeps_the_integrated_gpu_on_hybrid_laptops() { #[test] fn pick_adapter_prefers_the_discrete_gpu_on_desktops_with_a_monitor_on_each() { // Issue #445: one monitor on the Intel iGPU, one on the NVIDIA card, no built-in panel. - use eframe::wgpu::DeviceType; - let desktop = [(INTEL_IGPU.0, INTEL_IGPU.1, DeviceType::IntegratedGpu), (NVIDIA.0, NVIDIA.1, DeviceType::DiscreteGpu), (0, 0, DeviceType::Cpu)]; + use eframe::wgpu::{Backend, DeviceType}; + let desktop = [ + (INTEL_IGPU.0, INTEL_IGPU.1, DeviceType::IntegratedGpu, Backend::Vulkan), + (NVIDIA.0, NVIDIA.1, DeviceType::DiscreteGpu, Backend::Vulkan), + (0, 0, DeviceType::Cpu, Backend::Vulkan), + ]; assert_eq!(super::pick_adapter(&desktop, &[monitor(INTEL_IGPU), monitor(NVIDIA)], None), Some(1)); assert_eq!(super::pick_adapter(&desktop, &[monitor(NVIDIA), monitor(INTEL_IGPU)], None), Some(1)); // A GPU driving no display still loses to one that does.