From 0a872d729373430f163359b40c298134cee301f5 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 9 Oct 2024 10:02:48 -0600 Subject: [PATCH] Make iced_wgpu build on redox --- wgpu/Cargo.toml | 3 +- wgpu/src/window.rs | 10 +-- wgpu/src/window/compositor.rs | 114 +++++++++++++++------------------- 3 files changed, 56 insertions(+), 71 deletions(-) diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 375c962a40..cd69d8aa54 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -45,7 +45,8 @@ lyon.optional = true resvg.workspace = true resvg.optional = true -[target.'cfg(all(unix, not(target_os = "macos")))'.dependencies] +[target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))'.dependencies] + rustix = { version = "0.38" } raw-window-handle.workspace = true sctk.workspace = true diff --git a/wgpu/src/window.rs b/wgpu/src/window.rs index 92f1687372..011f60e5c5 100644 --- a/wgpu/src/window.rs +++ b/wgpu/src/window.rs @@ -1,19 +1,19 @@ //! Display rendering results on windows. pub mod compositor; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] mod wayland; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] mod x11; pub use compositor::Compositor; pub use wgpu::Surface; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] use rustix::fs::{major, minor}; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] use std::{fs::File, io::Read, path::PathBuf}; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] fn ids_from_dev(dev: u64) -> Option<(u16, u16)> { let path = PathBuf::from(format!( "/sys/dev/char/{}:{}/device", diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index eb0fe3bdaa..8c2eaf0ff3 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -7,9 +7,9 @@ use crate::graphics::{self, Viewport}; use crate::settings::{self, Settings}; use crate::{Engine, Renderer}; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] use super::wayland::get_wayland_device_ids; -#[cfg(all(unix, not(target_os = "macos")))] +#[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] use super::x11::get_x11_device_ids; use std::future::Future; @@ -60,7 +60,7 @@ impl Compositor { settings: Settings, compatible_window: Option, ) -> Result { - #[cfg(all(unix, not(target_os = "macos")))] + #[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] let ids = compatible_window.as_ref().and_then(|window| { get_wayland_device_ids(window) .or_else(|| get_x11_device_ids(window)) @@ -71,7 +71,7 @@ impl Compositor { // 2. and nobody set an adapter name, // 3. and the user didn't request the high power pref // => don't load the nvidia icd, as it might power on the gpu in hybrid setups causing severe delays - #[cfg(all(unix, not(target_os = "macos")))] + #[cfg(all(unix, not(target_os = "macos"), not(target_os = "redox")))] if !matches!(ids, Some((0x10de, _))) && std::env::var_os("WGPU_ADAPTER_NAME").is_none() && std::env::var("WGPU_POWER_PREF").as_deref() != Ok("high") @@ -115,68 +115,52 @@ impl Compositor { compatible_surface: compatible_surface.as_ref(), force_fallback_adapter: false, }; + let mut adapter = None; + #[cfg_attr(not(unix), allow(dead_code))] + if std::env::var_os("WGPU_ADAPTER_NAME").is_none() { + #[cfg(all( + unix, + not(target_os = "macos"), + not(target_os = "redox") + ))] + if let Some((vendor_id, device_id)) = ids { + adapter = available_adapters + .into_iter() + .filter(|adapter| { + let info = adapter.get_info(); + info.device == device_id as u32 + && info.vendor == vendor_id as u32 + }) + .find(|adapter| { + if let Some(surface) = compatible_surface.as_ref() { + adapter.is_surface_supported(surface) + } else { + true + } + }); + } + } else if let Ok(name) = std::env::var("WGPU_ADAPTER_NAME") { + adapter = available_adapters + .into_iter() + .filter(|adapter| { + let info = adapter.get_info(); + info.name == name + }) + .find(|adapter| { + if let Some(surface) = compatible_surface.as_ref() { + adapter.is_surface_supported(surface) + } else { + true + } + }); + } - let adapter = instance - .request_adapter(&adapter_options) - .await - .ok_or(Error::NoAdapterFound(format!("{:?}", adapter_options)))?; - // start pop - // let mut adapter = None; - // #[cfg_attr(not(unix), allow(dead_code))] - // if std::env::var_os("WGPU_ADAPTER_NAME").is_none() { - // #[cfg(all(unix, not(target_os = "macos")))] - // if let Some((vendor_id, device_id)) = ids { - // adapter = available_adapters - // .into_iter() - // .filter(|adapter| { - // let info = adapter.get_info(); - // info.device == device_id as u32 - // && info.vendor == vendor_id as u32 - // }) - // .find(|adapter| { - // if let Some(surface) = compatible_surface.as_ref() { - // adapter.is_surface_supported(surface) - // } else { - // true - // } - // }); - // } - // } else if let Ok(name) = std::env::var("WGPU_ADAPTER_NAME") { - // adapter = available_adapters - // .into_iter() - // .filter(|adapter| { - // let info = adapter.get_info(); - // info.name == name - // }) - // .find(|adapter| { - // if let Some(surface) = compatible_surface.as_ref() { - // adapter.is_surface_supported(surface) - // } else { - // true - // } - // }); - // } - - // let adapter = - // match adapter { - // Some(adapter) => adapter, - // None => instance - // .request_adapter(&wgpu::RequestAdapterOptions { - // power_preference: - // wgpu::util::power_preference_from_env().unwrap_or( - // if settings.antialiasing.is_none() { - // wgpu::PowerPreference::LowPower - // } else { - // wgpu::PowerPreference::HighPerformance - // }, - // ), - // compatible_surface: compatible_surface.as_ref(), - // force_fallback_adapter: false, - // }) - // .await?, - // }; - // end pop - // TODO(POP): Merge conflict ensued with above stuff, is your code still needed? + let adapter = match adapter { + Some(adapter) => adapter, + None => instance.request_adapter(&adapter_options).await.ok_or( + Error::NoAdapterFound(format!("{:?}", adapter_options)), + )?, + }; log::info!("Selected: {:#?}", adapter.get_info()); let (format, alpha_mode) = compatible_surface