From 7289b6091b61b0aa448a756cfe32211c78a4cce0 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Tue, 9 Jan 2024 07:19:15 -0800 Subject: WIP raw-window-handle 0.6 --- wgpu/src/window/compositor.rs | 73 ++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 29 deletions(-) (limited to 'wgpu') diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 090e0e9f..e2dc4901 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -6,13 +6,13 @@ use crate::graphics::compositor; use crate::graphics::{Error, Viewport}; use crate::{Backend, Primitive, Renderer, Settings}; -use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; use std::marker::PhantomData; /// A window graphics backend for iced powered by `wgpu`. #[allow(missing_debug_implementations)] -pub struct Compositor { +pub struct Compositor { settings: Settings, instance: wgpu::Instance, adapter: wgpu::Adapter, @@ -20,15 +20,18 @@ pub struct Compositor { queue: wgpu::Queue, format: wgpu::TextureFormat, theme: PhantomData, + w: PhantomData, } -impl Compositor { +impl + Compositor +{ /// Requests a new [`Compositor`] with the given [`Settings`]. /// /// Returns `None` if no compatible graphics adapter could be found. - pub async fn request( + pub async fn request( settings: Settings, - compatible_window: Option<&W>, + compatible_window: Option, ) -> Option { let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: settings.internal_backend, @@ -41,6 +44,7 @@ impl Compositor { if log::max_level() >= log::LevelFilter::Info { let available_adapters: Vec<_> = instance .enumerate_adapters(settings.internal_backend) + .iter() .map(|adapter| adapter.get_info()) .collect(); log::info!("Available adapters: {available_adapters:#?}"); @@ -48,7 +52,7 @@ impl Compositor { #[allow(unsafe_code)] let compatible_surface = compatible_window - .and_then(|window| unsafe { instance.create_surface(window).ok() }); + .and_then(|window| instance.create_surface(window).ok()); let adapter = instance .request_adapter(&wgpu::RequestAdapterOptions { @@ -100,14 +104,14 @@ impl Compositor { let (device, queue) = loop { - let limits = limits.next()?; + let required_limits = limits.next()?; let device = adapter.request_device( &wgpu::DeviceDescriptor { label: Some( "iced_wgpu::window::compositor device descriptor", ), - features: wgpu::Features::empty(), - limits, + required_features: wgpu::Features::empty(), + required_limits, }, None, ).await.ok(); @@ -125,6 +129,7 @@ impl Compositor { queue, format, theme: PhantomData, + w: PhantomData, }) } @@ -136,10 +141,13 @@ impl Compositor { /// Creates a [`Compositor`] and its [`Backend`] for the given [`Settings`] and /// window. -pub fn new( +pub fn new< + Theme, + W: HasWindowHandle + HasDisplayHandle + wgpu::WasmNotSendSync, +>( settings: Settings, - compatible_window: Option<&W>, -) -> Result, Error> { + compatible_window: Option, +) -> Result, Error> { let compositor = futures::executor::block_on(Compositor::request( settings, compatible_window, @@ -150,10 +158,10 @@ pub fn new( } /// Presents the given primitives with the given [`Compositor`] and [`Backend`]. -pub fn present>( - compositor: &mut Compositor, +pub fn present>( + compositor: &mut Compositor, backend: &mut Backend, - surface: &mut wgpu::Surface, + surface: &mut wgpu::Surface<'static>, primitives: &[Primitive], viewport: &Viewport, background_color: Color, @@ -204,14 +212,19 @@ pub fn present>( } } -impl graphics::Compositor for Compositor { +impl< + W: HasDisplayHandle + HasWindowHandle + wgpu::WasmNotSendSync + 'static, + Theme, + > graphics::Compositor for Compositor +{ type Settings = Settings; type Renderer = Renderer; - type Surface = wgpu::Surface; + // XXX generic instead of 'static + type Surface = wgpu::Surface<'static>; - fn new( + fn new( settings: Self::Settings, - compatible_window: Option<&W>, + compatible_window: Option, ) -> Result { new(settings, compatible_window) } @@ -224,14 +237,15 @@ impl graphics::Compositor for Compositor { ) } - fn create_surface( + fn create_surface( &mut self, - window: &W, + window: W, width: u32, height: u32, - ) -> wgpu::Surface { - #[allow(unsafe_code)] - let mut surface = unsafe { self.instance.create_surface(window) } + ) -> wgpu::Surface<'static> { + let mut surface = self + .instance + .create_surface(window) .expect("Create surface"); self.configure_surface(&mut surface, width, height); @@ -241,7 +255,7 @@ impl graphics::Compositor for Compositor { fn configure_surface( &mut self, - surface: &mut Self::Surface, + surface: &mut wgpu::Surface<'static>, width: u32, height: u32, ) { @@ -255,6 +269,7 @@ impl graphics::Compositor for Compositor { height, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![], + desired_maximum_frame_latency: 2, }, ); } @@ -271,7 +286,7 @@ impl graphics::Compositor for Compositor { fn present>( &mut self, renderer: &mut Self::Renderer, - surface: &mut Self::Surface, + surface: &mut wgpu::Surface<'static>, viewport: &Viewport, background_color: Color, overlay: &[T], @@ -292,7 +307,7 @@ impl graphics::Compositor for Compositor { fn screenshot>( &mut self, renderer: &mut Self::Renderer, - _surface: &mut Self::Surface, + _surface: &mut wgpu::Surface<'static>, viewport: &Viewport, background_color: Color, overlay: &[T], @@ -313,8 +328,8 @@ impl graphics::Compositor for Compositor { /// Renders the current surface to an offscreen buffer. /// /// Returns RGBA bytes of the texture data. -pub fn screenshot>( - compositor: &Compositor, +pub fn screenshot>( + compositor: &Compositor, backend: &mut Backend, primitives: &[Primitive], viewport: &Viewport, -- cgit From 8bf238697226e827dc983f9d89afbd0e252c5254 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jan 2024 09:55:27 +0100 Subject: Remove `Compositor` window generic And update `glyphon` and `window_clipboard` --- wgpu/Cargo.toml | 1 - wgpu/src/window/compositor.rs | 40 +++++++++++++--------------------------- 2 files changed, 13 insertions(+), 28 deletions(-) (limited to 'wgpu') diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index a460c127..1d3b57a7 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -32,7 +32,6 @@ glyphon.workspace = true guillotiere.workspace = true log.workspace = true once_cell.workspace = true -raw-window-handle.workspace = true wgpu.workspace = true lyon.workspace = true diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index e2dc4901..0c063d0b 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -6,13 +6,11 @@ use crate::graphics::compositor; use crate::graphics::{Error, Viewport}; use crate::{Backend, Primitive, Renderer, Settings}; -use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; - use std::marker::PhantomData; /// A window graphics backend for iced powered by `wgpu`. #[allow(missing_debug_implementations)] -pub struct Compositor { +pub struct Compositor { settings: Settings, instance: wgpu::Instance, adapter: wgpu::Adapter, @@ -20,16 +18,13 @@ pub struct Compositor { queue: wgpu::Queue, format: wgpu::TextureFormat, theme: PhantomData, - w: PhantomData, } -impl - Compositor -{ +impl Compositor { /// Requests a new [`Compositor`] with the given [`Settings`]. /// /// Returns `None` if no compatible graphics adapter could be found. - pub async fn request( + pub async fn request( settings: Settings, compatible_window: Option, ) -> Option { @@ -45,7 +40,7 @@ impl let available_adapters: Vec<_> = instance .enumerate_adapters(settings.internal_backend) .iter() - .map(|adapter| adapter.get_info()) + .map(wgpu::Adapter::get_info) .collect(); log::info!("Available adapters: {available_adapters:#?}"); } @@ -129,7 +124,6 @@ impl queue, format, theme: PhantomData, - w: PhantomData, }) } @@ -141,13 +135,10 @@ impl /// Creates a [`Compositor`] and its [`Backend`] for the given [`Settings`] and /// window. -pub fn new< - Theme, - W: HasWindowHandle + HasDisplayHandle + wgpu::WasmNotSendSync, ->( +pub fn new( settings: Settings, compatible_window: Option, -) -> Result, Error> { +) -> Result, Error> { let compositor = futures::executor::block_on(Compositor::request( settings, compatible_window, @@ -158,8 +149,8 @@ pub fn new< } /// Presents the given primitives with the given [`Compositor`] and [`Backend`]. -pub fn present>( - compositor: &mut Compositor, +pub fn present>( + compositor: &mut Compositor, backend: &mut Backend, surface: &mut wgpu::Surface<'static>, primitives: &[Primitive], @@ -212,17 +203,12 @@ pub fn present>( } } -impl< - W: HasDisplayHandle + HasWindowHandle + wgpu::WasmNotSendSync + 'static, - Theme, - > graphics::Compositor for Compositor -{ +impl graphics::Compositor for Compositor { type Settings = Settings; type Renderer = Renderer; - // XXX generic instead of 'static type Surface = wgpu::Surface<'static>; - fn new( + fn new( settings: Self::Settings, compatible_window: Option, ) -> Result { @@ -237,7 +223,7 @@ impl< ) } - fn create_surface( + fn create_surface( &mut self, window: W, width: u32, @@ -328,8 +314,8 @@ impl< /// Renders the current surface to an offscreen buffer. /// /// Returns RGBA bytes of the texture data. -pub fn screenshot>( - compositor: &Compositor, +pub fn screenshot>( + compositor: &Compositor, backend: &mut Backend, primitives: &[Primitive], viewport: &Viewport, -- cgit From 5fc49edc55a0e64c4c46ca55eddafe9d4e8232e1 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jan 2024 10:06:30 +0100 Subject: Make `compatible_window` mandatory in `Compositor` --- wgpu/src/window/compositor.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'wgpu') diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 0c063d0b..105d83a8 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -137,11 +137,11 @@ impl Compositor { /// window. pub fn new( settings: Settings, - compatible_window: Option, + compatible_window: W, ) -> Result, Error> { let compositor = futures::executor::block_on(Compositor::request( settings, - compatible_window, + Some(compatible_window), )) .ok_or(Error::GraphicsAdapterNotFound)?; @@ -210,7 +210,7 @@ impl graphics::Compositor for Compositor { fn new( settings: Self::Settings, - compatible_window: Option, + compatible_window: W, ) -> Result { new(settings, compatible_window) } -- cgit From c929e6f5dd30044df4e7400ab633eaf0a53ce3dd Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 18 Jan 2024 10:56:02 +0100 Subject: Use `Self::Surface` in `Compositor` implementors --- wgpu/src/window/compositor.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'wgpu') diff --git a/wgpu/src/window/compositor.rs b/wgpu/src/window/compositor.rs index 105d83a8..31cf3819 100644 --- a/wgpu/src/window/compositor.rs +++ b/wgpu/src/window/compositor.rs @@ -228,7 +228,7 @@ impl graphics::Compositor for Compositor { window: W, width: u32, height: u32, - ) -> wgpu::Surface<'static> { + ) -> Self::Surface { let mut surface = self .instance .create_surface(window) @@ -241,7 +241,7 @@ impl graphics::Compositor for Compositor { fn configure_surface( &mut self, - surface: &mut wgpu::Surface<'static>, + surface: &mut Self::Surface, width: u32, height: u32, ) { @@ -272,7 +272,7 @@ impl graphics::Compositor for Compositor { fn present>( &mut self, renderer: &mut Self::Renderer, - surface: &mut wgpu::Surface<'static>, + surface: &mut Self::Surface, viewport: &Viewport, background_color: Color, overlay: &[T], @@ -293,7 +293,7 @@ impl graphics::Compositor for Compositor { fn screenshot>( &mut self, renderer: &mut Self::Renderer, - _surface: &mut wgpu::Surface<'static>, + _surface: &mut Self::Surface, viewport: &Viewport, background_color: Color, overlay: &[T], -- cgit