Merge pull request #369 from hecrj/async-wgpu-compositor-creation
Implement async `Compositor::request` in `iced_wgpu`
This commit is contained in:
commit
96b2afba31
@ -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;
|
||||
|
@ -1,5 +1,5 @@
|
||||
//! Configure a renderer.
|
||||
pub use iced_graphics::Antialiasing;
|
||||
pub use crate::Antialiasing;
|
||||
|
||||
/// The settings of a [`Renderer`].
|
||||
///
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{Renderer, Settings};
|
||||
use crate::{Backend, Renderer, Settings};
|
||||
|
||||
use iced_graphics::Viewport;
|
||||
use iced_native::{futures, mouse};
|
||||
@ -7,19 +7,19 @@ 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 iced_graphics::window::Compositor for Compositor {
|
||||
type Settings = Settings;
|
||||
type Renderer = Renderer;
|
||||
type Surface = wgpu::Surface;
|
||||
type SwapChain = wgpu::SwapChain;
|
||||
|
||||
fn new(settings: Self::Settings) -> (Self, Renderer) {
|
||||
let (mut device, queue) = futures::executor::block_on(async {
|
||||
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() {
|
||||
@ -31,30 +31,46 @@ impl iced_graphics::window::Compositor for Compositor {
|
||||
},
|
||||
wgpu::BackendBit::PRIMARY,
|
||||
)
|
||||
.await
|
||||
.expect("Request adapter");
|
||||
.await?;
|
||||
|
||||
adapter
|
||||
let (device, queue) = adapter
|
||||
.request_device(&wgpu::DeviceDescriptor {
|
||||
extensions: wgpu::Extensions {
|
||||
anisotropic_filtering: false,
|
||||
},
|
||||
limits: wgpu::Limits { max_bind_groups: 2 },
|
||||
})
|
||||
.await
|
||||
});
|
||||
.await;
|
||||
|
||||
let renderer =
|
||||
Renderer::new(crate::Backend::new(&mut device, settings));
|
||||
|
||||
(
|
||||
Self {
|
||||
Some(Compositor {
|
||||
settings,
|
||||
device,
|
||||
queue,
|
||||
format: settings.format,
|
||||
},
|
||||
renderer,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
type Settings = Settings;
|
||||
type Renderer = Renderer;
|
||||
type Surface = wgpu::Surface;
|
||||
type SwapChain = wgpu::SwapChain;
|
||||
|
||||
fn new(settings: Self::Settings) -> (Self, Renderer) {
|
||||
let compositor = futures::executor::block_on(Self::request(settings))
|
||||
.expect("Could not find a suitable graphics adapter");
|
||||
|
||||
let backend = compositor.create_backend();
|
||||
|
||||
(compositor, Renderer::new(backend))
|
||||
}
|
||||
|
||||
fn create_surface<W: HasRawWindowHandle>(
|
||||
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user