Skip to content

Minecraft: Java Edition 26.3 rewritten from scratch in Rust, checked against vanilla with 281 differential harnesses. Get it with the Horizon launcher.

Renderer: a chosen graphics API with no GPU tries the others before software

Ludovic Marie3011a7960062
diff --git a/crates/horizon-client-render/src/gfx.rs b/crates/horizon-client-render/src/gfx.rs
index f03ee772d..441e128c8 100644
--- a/crates/horizon-client-render/src/gfx.rs
+++ b/crates/horizon-client-render/src/gfx.rs
@@ -1523,37 +1523,64 @@ async fn open_device(
api: GraphicsApi,
layers: u32,
) -> Result<Opened> {
- // A software adapter (WARP, llvmpipe) is a last resort: a later backend may
- // have the real GPU (#3978: a Fermi GPU with no Vulkan and no DX12 driver
- // still has OpenGL).
- match open_device_with(window, offscreen, api, layers, false).await {
- Ok(opened) => Ok(opened),
- Err(first) => match open_device_with(window, offscreen, api, layers, true).await {
+ let wgpu_backend = std::env::var_os("WGPU_BACKEND").is_some();
+ let mut first = None;
+ for (order, allow_cpu) in device_search(api.with_env(), std::env::consts::OS, wgpu_backend) {
+ match open_device_with(window, offscreen, &order, layers, allow_cpu).await {
Ok(opened) => {
- log::warn!(
- "Horizon renderer: no GPU adapter could be used ({first}); \
- rendering on the CPU, which is slow"
- );
- Ok(opened)
+ if let Some(first) = &first {
+ log::warn!(
+ "Horizon renderer: the chosen graphics API gave no GPU ({first}); {}",
+ if allow_cpu {
+ "rendering on the CPU, which is slow"
+ } else {
+ "using the GPU through another one"
+ }
+ );
+ }
+ return Ok(opened);
}
- Err(_) => Err(first),
- },
+ Err(e) => {
+ first.get_or_insert(e);
+ }
+ }
+ }
+ Err(first.unwrap_or_else(|| NO_GPU_DRIVER.into()))
+}
+/// The tries `open_device` makes, in turn: the chosen API's backends (`backend_order`) on a
+/// real GPU; when the player chose one API, the other backends `Auto` would try, on a real
+/// GPU; and last the chosen backends with a software adapter (WARP, llvmpipe) allowed.
+/// Software rendering takes most of a weak CPU even on the title screen: an Intel HD 2500 has
+/// DirectX 12 only through WARP and its GPU through OpenGL (#5185), and a Fermi GPU has no
+/// Vulkan or DirectX 12 driver but OpenGL (#3978). DirectX 12 never falls back to Vulkan: the
+/// launcher chooses it after Vulkan crashed in the driver (#746), which the game can't catch.
+fn device_search(
+ api: GraphicsApi,
+ os: &str,
+ wgpu_backend: bool,
+) -> Vec<(Vec<wgpu::Backends>, bool)> {
+ let chosen = backend_order(api, os, wgpu_backend);
+ let others: Vec<_> = backend_order(GraphicsApi::Auto, os, wgpu_backend)
+ .into_iter()
+ .filter(|b| !chosen.contains(b))
+ .filter(|&b| !(api == GraphicsApi::Dx12 && b == wgpu::Backends::VULKAN))
+ .collect();
+ let mut tries = vec![(chosen.clone(), false)];
+ if !others.is_empty() {
+ tries.push((others, false));
}
+ tries.push((chosen, true));
+ tries
}
-/// `open_device`, taking an adapter of the CPU type (software rendering) only
-/// when `allow_cpu`.
+/// `open_device` on `order`'s backends in turn, taking an adapter of the CPU type
+/// (software rendering) only when `allow_cpu`.
async fn open_device_with(
window: &Arc<Window>,
offscreen: bool,
- api: GraphicsApi,
+ order: &[wgpu::Backends],
layers: u32,
allow_cpu: bool,
) -> Result<Opened> {
- let order = backend_order(
- api.with_env(),
- std::env::consts::OS,
- std::env::var_os("WGPU_BACKEND").is_some(),
- );
let mut failures = Vec::new();
for (attempt, backends) in order.iter().copied().enumerate() {
let name = backends_name(backends);
@@ -9312,6 +9339,37 @@ mod tests {
assert_eq!(order(api, "macos"), [Backends::default()]);
}
}
+ #[test]
+ fn a_chosen_api_without_a_gpu_tries_the_others_before_software() {
+ use super::{GraphicsApi, device_search};
+ use wgpu::Backends;
+ let (v, d, g) = (Backends::VULKAN, Backends::DX12, Backends::GL);
+ // #5185: DirectX 12 chosen on an Intel HD 2500 (WARP only): OpenGL's GPU before WARP.
+ assert_eq!(
+ device_search(GraphicsApi::Dx12, "windows", false),
+ [(vec![d], false), (vec![g], false), (vec![d], true)]
+ );
+ assert_eq!(
+ device_search(GraphicsApi::Vulkan, "windows", false),
+ [(vec![v], false), (vec![d, g], false), (vec![v], true)]
+ );
+ assert_eq!(
+ device_search(GraphicsApi::Auto, "windows", false),
+ [(vec![v, d, g], false), (vec![v, d, g], true)]
+ );
+ assert_eq!(
+ device_search(GraphicsApi::OpenGl, "linux", false),
+ [(vec![g], false), (vec![v], false), (vec![g], true)]
+ );
+ // WGPU_BACKEND and macOS: one set of backends, nothing else to try.
+ let all = Backends::default();
+ for (os, env) in [("windows", true), ("macos", false)] {
+ assert_eq!(
+ device_search(GraphicsApi::Vulkan, os, env),
+ [(vec![all], false), (vec![all], true)]
+ );
+ }
+ }
/// The second try at a backend shows the adapters wgpu hides for reporting
/// no Vulkan conformance version (Mesa's hasvk, which drives Ivy Bridge and
/// Haswell, reports 0.0.0.0 and works).