Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ workspace = true
[features]
wgpu = ["iced_wgpu/default"]
wgpu-bare = ["iced_wgpu"]
tiny-skia = ["iced_tiny_skia"]
tiny-skia = ["iced_tiny_skia", "iced_wgpu?/software-fallback"]
image = ["iced_tiny_skia?/image", "iced_wgpu?/image"]
svg = ["iced_tiny_skia?/svg", "iced_wgpu?/svg"]
geometry = ["iced_graphics/geometry", "iced_tiny_skia?/geometry", "iced_wgpu?/geometry"]
Expand Down
1 change: 1 addition & 0 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ svg = ["iced_graphics/svg", "resvg/text"]
web-colors = ["iced_graphics/web-colors"]
webgl = ["wgpu/webgl"]
strict-assertions = []
software-fallback = []

[dependencies]
iced_debug.workspace = true
Expand Down
17 changes: 17 additions & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ use crate::graphics::mesh;
use crate::graphics::text::{Editor, Paragraph};
use crate::graphics::{Shell, Viewport};

fn adapter_is_software(information: &wgpu::AdapterInfo) -> bool {
matches!(information.device_type, wgpu::DeviceType::Cpu)
|| known_software_adapter(&information.name)
}

fn known_software_adapter(name: &str) -> bool {
let name = name.to_ascii_lowercase();

["llvmpipe", "lavapipe", "softpipe", "swiftshader"]
.iter()
.any(|software| name.contains(software))
}

/// A [`wgpu`] graphics renderer for [`iced`].
///
/// [`wgpu`]: https://github.com/gfx-rs/wgpu-rs
Expand Down Expand Up @@ -911,6 +924,10 @@ impl renderer::Headless for Renderer {
.await
.ok()?;

if cfg!(feature = "software-fallback") && adapter_is_software(&adapter.get_info()) {
return None;
}

let (device, queue) = adapter
.request_device(&wgpu::DeviceDescriptor {
label: Some("iced_wgpu [headless]"),
Expand Down
78 changes: 77 additions & 1 deletion wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum Error {
/// No adapter was found for the options requested.
#[error("no adapter was found for the options requested: {0:?}")]
NoAdapterFound(String),
/// The selected adapter renders in software.
#[error("the selected adapter renders in software: {0:#?}")]
SoftwareAdapter(wgpu::AdapterInfo),
/// No device request succeeded.
#[error("no device request succeeded: {0:?}")]
RequestDeviceFailed(Vec<(wgpu::Limits, wgpu::RequestDeviceError)>),
Expand All @@ -43,6 +46,14 @@ impl From<Error> for backend::Error {
}
}

fn reject_software_adapter(information: &wgpu::AdapterInfo) -> Result<(), Error> {
if cfg!(feature = "software-fallback") && crate::adapter_is_software(information) {
Err(Error::SoftwareAdapter(information.clone()))
} else {
Ok(())
}
}

impl Compositor {
/// Requests a new [`Compositor`] with the given [`Settings`].
///
Expand Down Expand Up @@ -94,7 +105,11 @@ impl Compositor {
.await
.map_err(|_error| Error::NoAdapterFound(format!("{adapter_options:?}")))?;

log::info!("Selected: {:#?}", adapter.get_info());
let adapter_info = adapter.get_info();

log::info!("Selected: {adapter_info:#?}");

reject_software_adapter(&adapter_info)?;

let (format, alpha_mode) = compatible_surface
.as_ref()
Expand Down Expand Up @@ -442,3 +457,64 @@ pub fn present_mode_from_env() -> Option<wgpu::PresentMode> {
_ => None,
}
}

#[cfg(test)]
mod tests {
use super::reject_software_adapter;

#[cfg(feature = "software-fallback")]
use super::Error;

fn adapter_info(name: &str, device_type: wgpu::DeviceType) -> wgpu::AdapterInfo {
wgpu::AdapterInfo {
name: name.to_owned(),
vendor: 0,
device: 0,
device_type,
device_pci_bus_id: String::new(),
driver: String::new(),
driver_info: String::new(),
backend: wgpu::Backend::Gl,
subgroup_min_size: 4,
subgroup_max_size: 128,
transient_saves_memory: false,
}
}

#[cfg(feature = "software-fallback")]
#[test]
fn llvmpipe_adapter_is_rejected_before_requesting_device() {
let information = adapter_info("llvmpipe (LLVM 17.0.2, 256 bits)", wgpu::DeviceType::Cpu);

assert!(matches!(
reject_software_adapter(&information),
Err(Error::SoftwareAdapter(adapter)) if adapter.name == information.name
));
}

#[cfg(feature = "software-fallback")]
#[test]
fn llvmpipe_name_is_rejected_even_if_device_type_is_unknown() {
let information = adapter_info("llvmpipe (LLVM 17.0.2, 256 bits)", wgpu::DeviceType::Other);

assert!(matches!(
reject_software_adapter(&information),
Err(Error::SoftwareAdapter(adapter)) if adapter.name == information.name
));
}

#[cfg(not(feature = "software-fallback"))]
#[test]
fn llvmpipe_adapter_is_accepted_without_software_fallback() {
let information = adapter_info("llvmpipe (LLVM 17.0.2, 256 bits)", wgpu::DeviceType::Cpu);

assert!(reject_software_adapter(&information).is_ok());
}

#[test]
fn hardware_adapter_is_accepted() {
let information = adapter_info("NVIDIA GeForce RTX 4090", wgpu::DeviceType::DiscreteGpu);

assert!(reject_software_adapter(&information).is_ok());
}
}