Renderer: OpenGL as the last fallback, for GPUs with no Vulkan or DirectX 12 driver

wgpu's GL backend (WGL on Windows, EGL on Linux). Auto tries Vulkan, then DirectX 12 on Windows,
then OpenGL; graphicsApi:opengl (or HORIZON_GRAPHICS_API=opengl) picks it. A Radeon HD 7400M
(TeraScale 2: DirectX 11, OpenGL 4.x) could not start Horizon at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 21:11:47 +01:00
parent b7b4a26164
commit 03a5cbdcd8
3 changed files with 53 additions and 31 deletions
+5
View File
@@ -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"
+36 -28
View File
@@ -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<wgpu::Backends> {
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<wgpu::Backends> {
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<Opened> {
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
+12 -3
View File
@@ -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()