From 9d033446417b3a92b4997e353645d410067c2071 Mon Sep 17 00:00:00 2001 From: Eero Lehtinen Date: Sun, 31 Mar 2024 01:10:54 +0200 Subject: [PATCH] Fix crash on Linux Nvidia 550 driver (#12542) # Objective Fix crashing on Linux with latest stable Nvidia 550 driver when resizing. The crash happens at startup with some setups. Fixes #12199 I think this would be nice to get into 0.13.1 ## Solution Ignore `wgpu::SurfaceError::Outdated` always on this platform+driver. It looks like Nvidia considered the previous behaviour of not returning this error a bug: "Fixed a bug where vkAcquireNextImageKHR() was not returning VK_ERROR_OUT_OF_DATE_KHR when it should with WSI X11 swapchains" (https://www.nvidia.com/Download/driverResults.aspx/218826/en-us/) What I gather from this is that the surface was outdated on previous drivers too, but they just didn't report it as an error. So behaviour shouldn't change. In the issue conversation we experimented with calling `continue` when this error happens, but I found that it results in some small issues like bevy_egui scale not updating with the window sometimes. Just doing nothing seems to work better. ## Changelog - Fixed crashing on Linux with Nvidia 550 driver when resizing the window ## Migration Guide --------- Co-authored-by: James Liu --- crates/bevy_render/src/view/window/mod.rs | 35 ++++++++++++++++++++--- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/view/window/mod.rs b/crates/bevy_render/src/view/window/mod.rs index 6bca35efdea2d..75175a6a5e5f9 100644 --- a/crates/bevy_render/src/view/window/mod.rs +++ b/crates/bevy_render/src/view/window/mod.rs @@ -8,6 +8,8 @@ use crate::{ }; use bevy_app::{App, Plugin}; use bevy_ecs::{entity::EntityHashMap, prelude::*}; +#[cfg(target_os = "linux")] +use bevy_utils::warn_once; use bevy_utils::{default, tracing::debug, HashSet}; use bevy_window::{ CompositeAlphaMode, PresentMode, PrimaryWindow, RawHandleWrapper, Window, WindowClosed, @@ -216,6 +218,9 @@ impl WindowSurfaces { } } +#[cfg(target_os = "linux")] +const NVIDIA_VENDOR_ID: u32 = 0x10DE; + /// (re)configures window surfaces, and obtains a swapchain texture for rendering. /// /// NOTE: `get_current_texture` in `prepare_windows` can take a long time if the GPU workload is @@ -304,19 +309,41 @@ pub fn prepare_windows( }) }; + #[cfg(target_os = "linux")] + let is_nvidia = || { + render_instance + .enumerate_adapters(wgpu::Backends::VULKAN) + .iter() + .any(|adapter| adapter.get_info().vendor & 0xFFFF == NVIDIA_VENDOR_ID) + }; + let not_already_configured = window_surfaces.configured_windows.insert(window.entity); let surface = &surface_data.surface; if not_already_configured || window.size_changed || window.present_mode_changed { - let frame = surface - .get_current_texture() - .expect("Error configuring surface"); - window.set_swapchain_texture(frame); + match surface.get_current_texture() { + Ok(frame) => window.set_swapchain_texture(frame), + #[cfg(target_os = "linux")] + Err(wgpu::SurfaceError::Outdated) if is_nvidia() => { + warn_once!( + "Couldn't get swap chain texture. This often happens with \ + the NVIDIA drivers on Linux. It can be safely ignored." + ); + } + Err(err) => panic!("Error configuring surface: {err}"), + }; } else { match surface.get_current_texture() { Ok(frame) => { window.set_swapchain_texture(frame); } + #[cfg(target_os = "linux")] + Err(wgpu::SurfaceError::Outdated) if is_nvidia() => { + warn_once!( + "Couldn't get swap chain texture. This often happens with \ + the Nvidia 550 driver. It can be safely ignored." + ); + } Err(wgpu::SurfaceError::Outdated) => { render_device.configure_surface(surface, &surface_data.configuration); let frame = surface