Merge pull request #369 from hecrj/async-wgpu-compositor-creation

Implement async `Compositor::request` in `iced_wgpu`
This commit is contained in:
Héctor Ramón 2020-05-30 00:31:23 +02:00 committed by GitHub
commit 96b2afba31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 39 deletions

View File

@ -36,7 +36,7 @@ mod backend;
mod quad; mod quad;
mod text; mod text;
pub use iced_graphics::{Defaults, Primitive, Viewport}; pub use iced_graphics::{Antialiasing, Defaults, Primitive, Viewport};
pub use wgpu; pub use wgpu;
pub use backend::Backend; pub use backend::Backend;

View File

@ -1,5 +1,5 @@
//! Configure a renderer. //! Configure a renderer.
pub use iced_graphics::Antialiasing; pub use crate::Antialiasing;
/// The settings of a [`Renderer`]. /// The settings of a [`Renderer`].
/// ///

View File

@ -1,4 +1,4 @@
use crate::{Renderer, Settings}; use crate::{Backend, Renderer, Settings};
use iced_graphics::Viewport; use iced_graphics::Viewport;
use iced_native::{futures, mouse}; use iced_native::{futures, mouse};
@ -7,9 +7,55 @@ use raw_window_handle::HasRawWindowHandle;
/// A window graphics backend for iced powered by `wgpu`. /// A window graphics backend for iced powered by `wgpu`.
#[derive(Debug)] #[derive(Debug)]
pub struct Compositor { pub struct Compositor {
settings: Settings,
device: wgpu::Device, device: wgpu::Device,
queue: wgpu::Queue, 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<Self> {
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 { impl iced_graphics::window::Compositor for Compositor {
@ -19,42 +65,12 @@ impl iced_graphics::window::Compositor for Compositor {
type SwapChain = wgpu::SwapChain; type SwapChain = wgpu::SwapChain;
fn new(settings: Self::Settings) -> (Self, Renderer) { fn new(settings: Self::Settings) -> (Self, Renderer) {
let (mut device, queue) = futures::executor::block_on(async { let compositor = futures::executor::block_on(Self::request(settings))
let adapter = wgpu::Adapter::request( .expect("Could not find a suitable graphics adapter");
&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");
adapter let backend = compositor.create_backend();
.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits { max_bind_groups: 2 },
})
.await
});
let renderer = (compositor, Renderer::new(backend))
Renderer::new(crate::Backend::new(&mut device, settings));
(
Self {
device,
queue,
format: settings.format,
},
renderer,
)
} }
fn create_surface<W: HasRawWindowHandle>( fn create_surface<W: HasRawWindowHandle>(
@ -74,7 +90,7 @@ impl iced_graphics::window::Compositor for Compositor {
surface, surface,
&wgpu::SwapChainDescriptor { &wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: self.format, format: self.settings.format,
width, width,
height, height,
present_mode: wgpu::PresentMode::Mailbox, present_mode: wgpu::PresentMode::Mailbox,