From 0691ec31953c5b9e2493b8da96c6876eb583f867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 29 May 2020 23:08:34 +0200 Subject: [PATCH 1/2] Implement async `Compositor::request` in `iced_wgpu` --- wgpu/src/window/compositor.rs | 90 +++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 37 deletions(-) diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 600bc81c..8345679a 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -1,4 +1,4 @@ -use crate::{Renderer, Settings}; +use crate::{Backend, Renderer, Settings}; use iced_graphics::Viewport; use iced_native::{futures, mouse}; @@ -7,9 +7,55 @@ use raw_window_handle::HasRawWindowHandle; /// A window graphics backend for iced powered by `wgpu`. #[derive(Debug)] pub struct Compositor { + settings: Settings, device: wgpu::Device, queue: wgpu::Queue, - format: wgpu::TextureFormat, +} + +impl Compositor { + /// Requests a new [`Compositor`] with the given [`Settings`]. + /// + /// Returns `None` if no compatible graphics adapter could be found. + /// + /// [`Compositor`]: struct.Compositor.html + /// [`Settings`]: struct.Settings.html + pub async fn request(settings: Settings) -> Option { + let adapter = wgpu::Adapter::request( + &wgpu::RequestAdapterOptions { + power_preference: if settings.antialiasing.is_none() { + wgpu::PowerPreference::Default + } else { + wgpu::PowerPreference::HighPerformance + }, + compatible_surface: None, + }, + wgpu::BackendBit::PRIMARY, + ) + .await?; + + let (device, queue) = adapter + .request_device(&wgpu::DeviceDescriptor { + extensions: wgpu::Extensions { + anisotropic_filtering: false, + }, + limits: wgpu::Limits { max_bind_groups: 2 }, + }) + .await; + + Some(Compositor { + settings, + device, + queue, + }) + } + + /// Creates a new rendering [`Backend`] for this [`Compositor`]. + /// + /// [`Compositor`]: struct.Compositor.html + /// [`Backend`]: struct.Backend.html + pub fn create_backend(&self) -> Backend { + Backend::new(&self.device, self.settings) + } } impl iced_graphics::window::Compositor for Compositor { @@ -19,42 +65,12 @@ impl iced_graphics::window::Compositor for Compositor { type SwapChain = wgpu::SwapChain; fn new(settings: Self::Settings) -> (Self, Renderer) { - let (mut device, queue) = futures::executor::block_on(async { - let adapter = wgpu::Adapter::request( - &wgpu::RequestAdapterOptions { - power_preference: if settings.antialiasing.is_none() { - wgpu::PowerPreference::Default - } else { - wgpu::PowerPreference::HighPerformance - }, - compatible_surface: None, - }, - wgpu::BackendBit::PRIMARY, - ) - .await - .expect("Request adapter"); + let compositor = futures::executor::block_on(Self::request(settings)) + .expect("Could not find a suitable graphics adapter"); - adapter - .request_device(&wgpu::DeviceDescriptor { - extensions: wgpu::Extensions { - anisotropic_filtering: false, - }, - limits: wgpu::Limits { max_bind_groups: 2 }, - }) - .await - }); + let backend = compositor.create_backend(); - let renderer = - Renderer::new(crate::Backend::new(&mut device, settings)); - - ( - Self { - device, - queue, - format: settings.format, - }, - renderer, - ) + (compositor, Renderer::new(backend)) } fn create_surface( @@ -74,7 +90,7 @@ impl iced_graphics::window::Compositor for Compositor { surface, &wgpu::SwapChainDescriptor { usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, - format: self.format, + format: self.settings.format, width, height, present_mode: wgpu::PresentMode::Mailbox, From 3ef49cd51c99b1927abc6f2a363b1fb9373b6dc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Fri, 29 May 2020 23:09:15 +0200 Subject: [PATCH 2/2] Re-export `Antialiasing` in `iced_wgpu` --- wgpu/src/lib.rs | 2 +- wgpu/src/settings.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 0c351eea..e67221c7 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -36,7 +36,7 @@ mod backend; mod quad; mod text; -pub use iced_graphics::{Defaults, Primitive, Viewport}; +pub use iced_graphics::{Antialiasing, Defaults, Primitive, Viewport}; pub use wgpu; pub use backend::Backend; diff --git a/wgpu/src/settings.rs b/wgpu/src/settings.rs index 8378d734..4655e64f 100644 --- a/wgpu/src/settings.rs +++ b/wgpu/src/settings.rs @@ -1,5 +1,5 @@ //! Configure a renderer. -pub use iced_graphics::Antialiasing; +pub use crate::Antialiasing; /// The settings of a [`Renderer`]. ///