diff --git a/crates/horizon-client/Cargo.toml b/crates/horizon-client/Cargo.toml index ea5859c23..4877c18f2 100644 --- a/crates/horizon-client/Cargo.toml +++ b/crates/horizon-client/Cargo.toml @@ -68,6 +68,11 @@ rustix = { version = "1", default-features = false, features = ["std", "time"] } # on the PATH. wgpu = { version = "30.0.1", default-features = false, features = ["dx12"] } +[target.'cfg(not(target_os = "macos"))'.dependencies] +# OpenGL (WGL on Windows, EGL on Linux), the last fallback, for GPUs with no Vulkan or +# DirectX 12 driver (`graphicsApi`, graphics.rs). +wgpu = { version = "30.0.1", default-features = false, features = ["gles"] } + [target.'cfg(windows)'.build-dependencies] # The icon resource in the .exe (build.rs). winresource = "0.1" diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index b43202074..f2fdfb6b3 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -683,25 +683,31 @@ fn instance_descriptor(backends: wgpu::Backends) -> wgpu::InstanceDescriptor { } /// The backends to open a device on, each tried in turn until one gives a /// device. `api` is the player's choice (`GraphicsApi::with_env` applied); -/// `wgpu_backend`: whether `WGPU_BACKEND` is set, which picks the backends -/// itself. Only Windows has two: Auto tries Vulkan, then DirectX 12 when -/// Vulkan has no surface, adapter (no Vulkan driver) or device. A native crash in -/// the driver can't be caught in the game; the launcher switches the setting -/// to DirectX 12 after one at the renderer's start (#746). -fn backend_order(api: GraphicsApi, windows: bool, wgpu_backend: bool) -> Vec { - if !windows || wgpu_backend { - return vec![wgpu::Backends::default()]; +/// `os` is `std::env::consts::OS`; `wgpu_backend`: whether `WGPU_BACKEND` is set, +/// which picks the backends itself. Auto tries Vulkan, then DirectX 12 on Windows, +/// then OpenGL (Windows and Linux) for GPUs with neither driver, each when the one +/// before has no surface, adapter or device. A native crash in the driver can't be +/// caught in the game; the launcher switches the setting to DirectX 12 after one at +/// the renderer's start (#746). macOS has Metal only. +fn backend_order(api: GraphicsApi, os: &str, wgpu_backend: bool) -> Vec { + use wgpu::Backends; + if os == "macos" || wgpu_backend { + return vec![Backends::default()]; } + let windows = os == "windows"; match api { - GraphicsApi::Auto => vec![wgpu::Backends::VULKAN, wgpu::Backends::DX12], - GraphicsApi::Vulkan => vec![wgpu::Backends::VULKAN], - GraphicsApi::Dx12 => vec![wgpu::Backends::DX12], + GraphicsApi::Auto if windows => vec![Backends::VULKAN, Backends::DX12, Backends::GL], + GraphicsApi::Auto => vec![Backends::VULKAN, Backends::GL], + GraphicsApi::Dx12 if windows => vec![Backends::DX12], + GraphicsApi::Vulkan | GraphicsApi::Dx12 => vec![Backends::VULKAN], + GraphicsApi::OpenGl => vec![Backends::GL], } } fn backends_name(backends: wgpu::Backends) -> &'static str { match backends { wgpu::Backends::VULKAN => "Vulkan", wgpu::Backends::DX12 => "DirectX 12", + wgpu::Backends::GL => "OpenGL", _ => "graphics", } } @@ -780,7 +786,7 @@ async fn open_device( ) -> Result { let order = backend_order( api.with_env(), - cfg!(windows), + std::env::consts::OS, std::env::var_os("WGPU_BACKEND").is_some(), ); let mut failures = Vec::new(); @@ -869,14 +875,14 @@ async fn open_device( /// Vulkan is loaded at run time (the game links no graphics library), so a /// missing driver shows here rather than as a missing-DLL dialog. #[cfg(target_os = "windows")] -const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan or DirectX 12 found: Horizon needs a GPU \ - driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ +const NO_GPU_DRIVER: &str = "No graphics driver with Vulkan, DirectX 12 or OpenGL found: Horizon needs \ + a GPU driver with one of them. Install the latest driver for your NVIDIA, AMD or Intel GPU and \ start the game again"; #[cfg(target_os = "macos")] const NO_GPU_DRIVER: &str = "No Metal graphics device found"; #[cfg(not(any(target_os = "windows", target_os = "macos")))] -const NO_GPU_DRIVER: &str = "No Vulkan graphics driver found: Horizon needs the Vulkan loader \ - (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's)"; +const NO_GPU_DRIVER: &str = "No Vulkan or OpenGL graphics driver found: Horizon needs the Vulkan loader \ + (libvulkan.so.1) and a Vulkan driver for your GPU (Mesa's, or NVIDIA's), or OpenGL through EGL"; /// Logs a step of the renderer's start, for a native crash in the graphics /// driver: it leaves no panic or crash report, so the log's last line is the /// only sign of where the game stopped. The log file is written unbuffered; @@ -4475,25 +4481,27 @@ mod tests { ); } #[test] - fn windows_tries_vulkan_then_directx_12() { + fn auto_tries_vulkan_then_directx_12_then_opengl() { use super::{GraphicsApi, backend_order}; use wgpu::Backends; + let order = |api, os| backend_order(api, os, false); assert_eq!( - backend_order(GraphicsApi::Auto, true, false), - [Backends::VULKAN, Backends::DX12] + order(GraphicsApi::Auto, "windows"), + [Backends::VULKAN, Backends::DX12, Backends::GL] ); + assert_eq!(order(GraphicsApi::Vulkan, "windows"), [Backends::VULKAN]); + assert_eq!(order(GraphicsApi::Dx12, "windows"), [Backends::DX12]); + assert_eq!(order(GraphicsApi::OpenGl, "windows"), [Backends::GL]); assert_eq!( - backend_order(GraphicsApi::Vulkan, true, false), - [Backends::VULKAN] + order(GraphicsApi::Auto, "linux"), + [Backends::VULKAN, Backends::GL] ); - assert_eq!( - backend_order(GraphicsApi::Dx12, true, false), - [Backends::DX12] - ); - // WGPU_BACKEND picks the backends itself; other systems have one. + assert_eq!(order(GraphicsApi::Dx12, "linux"), [Backends::VULKAN]); + assert_eq!(order(GraphicsApi::OpenGl, "linux"), [Backends::GL]); + // WGPU_BACKEND picks the backends itself; macOS has Metal only. for api in GraphicsApi::ALL { - assert_eq!(backend_order(api, true, true), [Backends::default()]); - assert_eq!(backend_order(api, false, false), [Backends::default()]); + assert_eq!(backend_order(api, "windows", true), [Backends::default()]); + assert_eq!(order(api, "macos"), [Backends::default()]); } } /// Every WGSL shader file translates to HLSL as wgpu's DirectX 12 backend diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs index 3cf1402d2..21d38539e 100644 --- a/crates/horizon-client/src/graphics.rs +++ b/crates/horizon-client/src/graphics.rs @@ -98,19 +98,27 @@ impl PresentMode { /// macOS). Applies at the next start. #[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] pub enum GraphicsApi { - /// Vulkan, and DirectX 12 when Vulkan has no adapter or device. + /// Vulkan, then DirectX 12 (Windows), then OpenGL when neither has a surface, adapter or device. #[default] Auto, Vulkan, Dx12, + /// OpenGL, for GPUs with no Vulkan or DirectX 12 driver (Windows and Linux). + OpenGl, } impl GraphicsApi { - pub const ALL: [GraphicsApi; 3] = [GraphicsApi::Auto, GraphicsApi::Vulkan, GraphicsApi::Dx12]; + pub const ALL: [GraphicsApi; 4] = [ + GraphicsApi::Auto, + GraphicsApi::Vulkan, + GraphicsApi::Dx12, + GraphicsApi::OpenGl, + ]; pub fn name(self) -> &'static str { match self { GraphicsApi::Auto => "auto", GraphicsApi::Vulkan => "vulkan", GraphicsApi::Dx12 => "dx12", + GraphicsApi::OpenGl => "opengl", } } pub fn label(self) -> &'static str { @@ -118,6 +126,7 @@ impl GraphicsApi { GraphicsApi::Auto => "Auto", GraphicsApi::Vulkan => "Vulkan", GraphicsApi::Dx12 => "DirectX 12", + GraphicsApi::OpenGl => "OpenGL", } } /// The API a name stands for, ignoring case; `None` for anything else. @@ -126,7 +135,7 @@ impl GraphicsApi { .into_iter() .find(|api| api.name().eq_ignore_ascii_case(s.trim())) } - /// `HORIZON_GRAPHICS_API` (auto, vulkan or dx12) if set, else the setting. + /// `HORIZON_GRAPHICS_API` (auto, vulkan, dx12 or opengl) if set, else the setting. pub fn with_env(self) -> Self { std::env::var("HORIZON_GRAPHICS_API") .ok()