Make egui_wgpu::winit::Painter::set_window async (#2434)

* Make `egui_wgpu::winit::Painter::set_window` async

* Fix changelog link
This commit is contained in:
Emil Ernerfeldt 2022-12-12 15:37:55 +01:00 committed by GitHub
parent 7a658e3ddb
commit 6c4fc50fdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 14 deletions

2
Cargo.lock generated
View file

@ -1321,6 +1321,7 @@ dependencies = [
"image", "image",
"js-sys", "js-sys",
"percent-encoding", "percent-encoding",
"pollster",
"puffin", "puffin",
"raw-window-handle 0.5.0", "raw-window-handle 0.5.0",
"ron", "ron",
@ -1356,7 +1357,6 @@ dependencies = [
"bytemuck", "bytemuck",
"document-features", "document-features",
"egui", "egui",
"pollster",
"puffin", "puffin",
"tracing", "tracing",
"type-map", "type-map",

View file

@ -64,7 +64,7 @@ __screenshot = ["dep:image"]
## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)). ## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)).
## This overrides the `glow` feature. ## This overrides the `glow` feature.
wgpu = ["dep:wgpu", "dep:egui-wgpu"] wgpu = ["dep:wgpu", "dep:egui-wgpu", "dep:pollster"]
[dependencies] [dependencies]
@ -100,6 +100,7 @@ directories-next = { version = "2", optional = true }
egui-wgpu = { version = "0.20.0", path = "../egui-wgpu", optional = true, features = [ egui-wgpu = { version = "0.20.0", path = "../egui-wgpu", optional = true, features = [
"winit", "winit",
] } # if wgpu is used, use it with winit ] } # if wgpu is used, use it with winit
pollster = { version = "0.2", optional = true } # needed for wgpu
# we can expose these to user so that they can select which backends they want to enable to avoid compiling useless deps. # we can expose these to user so that they can select which backends they want to enable to avoid compiling useless deps.
# this can be done at the same time we expose x11/wayland features of winit crate. # this can be done at the same time we expose x11/wayland features of winit crate.

View file

@ -950,7 +950,7 @@ mod wgpu_integration {
self.window = Some(window); self.window = Some(window);
if let Some(running) = &mut self.running { if let Some(running) = &mut self.running {
unsafe { unsafe {
running.painter.set_window(self.window.as_ref())?; pollster::block_on(running.painter.set_window(self.window.as_ref()))?;
} }
} }
Ok(()) Ok(())
@ -962,7 +962,7 @@ mod wgpu_integration {
self.window = None; self.window = None;
if let Some(running) = &mut self.running { if let Some(running) = &mut self.running {
unsafe { unsafe {
running.painter.set_window(None)?; pollster::block_on(running.painter.set_window(None))?;
} }
} }
Ok(()) Ok(())
@ -981,7 +981,7 @@ mod wgpu_integration {
self.native_options.multisampling.max(1) as _, self.native_options.multisampling.max(1) as _,
self.native_options.depth_buffer, self.native_options.depth_buffer,
); );
painter.set_window(Some(&window))?; pollster::block_on(painter.set_window(Some(&window)))?;
painter painter
}; };

View file

@ -4,6 +4,7 @@ All notable changes to the `egui-wgpu` integration will be noted in this file.
## Unreleased ## Unreleased
* Return `Err` instead of panic if we can't find a device ([#2428](https://github.com/emilk/egui/pull/2428)). * Return `Err` instead of panic if we can't find a device ([#2428](https://github.com/emilk/egui/pull/2428)).
* `winit::Painter::set_window` is now `async` ([#2434](https://github.com/emilk/egui/pull/2434)).
## 0.20.0 - 2022-12-08 - web support ## 0.20.0 - 2022-12-08 - web support

View file

@ -32,7 +32,7 @@ all-features = true
puffin = ["dep:puffin"] puffin = ["dep:puffin"]
## Enable [`winit`](https://docs.rs/winit) integration. ## Enable [`winit`](https://docs.rs/winit) integration.
winit = ["dep:pollster", "dep:winit"] winit = ["dep:winit"]
[dependencies] [dependencies]
@ -49,7 +49,6 @@ wgpu = "0.14"
## Enable this when generating docs. ## Enable this when generating docs.
document-features = { version = "0.2", optional = true } document-features = { version = "0.2", optional = true }
pollster = { version = "0.2", optional = true }
winit = { version = "0.27.2", optional = true } winit = { version = "0.27.2", optional = true }
# Native: # Native:

View file

@ -88,17 +88,19 @@ impl Painter {
// //
// After we've initialized our render state once though we expect all future surfaces // After we've initialized our render state once though we expect all future surfaces
// will have the same format and so this render state will remain valid. // will have the same format and so this render state will remain valid.
fn ensure_render_state_for_surface( async fn ensure_render_state_for_surface(
&mut self, &mut self,
surface: &Surface, surface: &Surface,
) -> Result<(), wgpu::RequestDeviceError> { ) -> Result<(), wgpu::RequestDeviceError> {
if self.adapter.is_none() { if self.adapter.is_none() {
self.adapter = self.adapter = self
pollster::block_on(self.instance.request_adapter(&wgpu::RequestAdapterOptions { .instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: self.configuration.power_preference, power_preference: self.configuration.power_preference,
compatible_surface: Some(surface), compatible_surface: Some(surface),
force_fallback_adapter: false, force_fallback_adapter: false,
})); })
.await;
} }
if self.render_state.is_none() { if self.render_state.is_none() {
match &self.adapter { match &self.adapter {
@ -106,7 +108,7 @@ impl Painter {
let swapchain_format = crate::preferred_framebuffer_format( let swapchain_format = crate::preferred_framebuffer_format(
&surface.get_supported_formats(adapter), &surface.get_supported_formats(adapter),
); );
let rs = pollster::block_on(self.init_render_state(adapter, swapchain_format))?; let rs = self.init_render_state(adapter, swapchain_format).await?;
self.render_state = Some(rs); self.render_state = Some(rs);
} }
None => return Err(wgpu::RequestDeviceError {}), None => return Err(wgpu::RequestDeviceError {}),
@ -171,7 +173,7 @@ impl Painter {
/// ///
/// # Errors /// # Errors
/// If the provided wgpu configuration does not match an available device. /// If the provided wgpu configuration does not match an available device.
pub unsafe fn set_window( pub async unsafe fn set_window(
&mut self, &mut self,
window: Option<&winit::window::Window>, window: Option<&winit::window::Window>,
) -> Result<(), wgpu::RequestDeviceError> { ) -> Result<(), wgpu::RequestDeviceError> {
@ -179,7 +181,7 @@ impl Painter {
Some(window) => { Some(window) => {
let surface = self.instance.create_surface(&window); let surface = self.instance.create_surface(&window);
self.ensure_render_state_for_surface(&surface)?; self.ensure_render_state_for_surface(&surface).await?;
let size = window.inner_size(); let size = window.inner_size();
let width = size.width; let width = size.width;