From 0167617bc1321173b1db29d30694f090b43effd8 Mon Sep 17 00:00:00 2001 From: Okko Hakola Date: Thu, 11 Jan 2024 16:40:25 +0200 Subject: [PATCH] Update winit & other libs --- Cargo.toml | 22 ++--- examples/egui_gui.rs | 28 +++--- examples/fluid_sim/app.rs | 19 ++-- examples/fluid_sim/timer.rs | 2 + examples/multiple_windows/main.rs | 33 +++---- src/glass.rs | 146 +++++++++++++++++------------- 6 files changed, 138 insertions(+), 112 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5c5b535..6fc6f3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,28 +15,28 @@ trace = ["wgpu/trace"] [dependencies] indexmap = "2.1" -pollster = "0.3.0" +pollster = "0.3" image = "0.24" -bytemuck = { version = "1.13.1", features = ["derive"] } +bytemuck = { version = "1.14", features = ["derive"] } wgpu = { version = "0.18", default_features = true, features = ["naga"] } naga = "0.14" -winit = "0.28" -glam = "0.24.0" +winit = { version = "0.29", features = ["rwh_05"] } +glam = "0.25" path-clean = "1.0.1" notify = "6.1" flume = "0.11" log = "0.4" # Optional Egui -egui = { version = "0.24", optional = true } -egui-wgpu = { version = "0.24", optional = true } -egui-winit = { version = "0.24", optional = true } -egui_extras = { version = "0.24", optional = true } -egui_demo_lib = { version = "0.24", optional = true } +egui = { version = "0.25", optional = true } +egui-wgpu = { version = "0.25", optional = true } +egui-winit = { version = "0.25", optional = true } +egui_extras = { version = "0.25", optional = true } +egui_demo_lib = { version = "0.25", optional = true } [dev-dependencies] -egui_demo_lib = { version = "0.24" } -winit_input_helper = "0.14" +egui_demo_lib = { version = "0.25" } +winit_input_helper = "0.15" rapier2d = { version = "0.17.2", features = ["default", "debug-render"] } [profile.dev] diff --git a/examples/egui_gui.rs b/examples/egui_gui.rs index 87ce4f0..559326a 100644 --- a/examples/egui_gui.rs +++ b/examples/egui_gui.rs @@ -58,8 +58,10 @@ fn initialize_gui_app( context: &mut GlassContext, event_loop: &EventLoopWindowTarget<()>, ) { + let ctx = egui::Context::default(); let pixels_per_point = context.primary_render_window().window().scale_factor() as f32; let egui_winit = egui_winit::State::new( + ctx.clone(), ViewportId::ROOT, event_loop, Some(pixels_per_point), @@ -72,7 +74,7 @@ fn initialize_gui_app( 1, ); app.gui = Some(GuiState { - egui_ctx: egui::Context::default(), + egui_ctx: ctx, egui_winit, renderer, repaint: false, @@ -80,20 +82,24 @@ fn initialize_gui_app( }); } -fn update_egui_with_winit_event(app: &mut GuiApp, _context: &mut GlassContext, event: &Event<()>) { +fn update_egui_with_winit_event(app: &mut GuiApp, context: &mut GlassContext, event: &Event<()>) { match event { Event::WindowEvent { - event, .. + window_id, + event, + .. } => { let gui = app.gui(); - let EventResponse { - consumed, - repaint, - } = gui.egui_winit.on_window_event(&gui.egui_ctx, event); - gui.repaint = repaint; - // Skip input if event was consumed by egui - if consumed { - return; + if let Some(window) = context.render_window(*window_id) { + let EventResponse { + consumed, + repaint, + } = gui.egui_winit.on_window_event(window.window(), event); + gui.repaint = repaint; + // Skip input if event was consumed by egui + if consumed { + return; + } } } _ => {} diff --git a/examples/fluid_sim/app.rs b/examples/fluid_sim/app.rs index bd4fe99..d7bf9ac 100644 --- a/examples/fluid_sim/app.rs +++ b/examples/fluid_sim/app.rs @@ -7,12 +7,13 @@ use glass::{ wgpu::{BlendState, ColorTargetState, ColorWrites, Extent3d, TextureFormat, TextureUsages}, window::GlassWindow, winit::{ - event::{Event, VirtualKeyCode}, + event::Event, event_loop::{EventLoop, EventLoopWindowTarget}, }, GlassApp, GlassContext, RenderData, }; use wgpu::StoreOp; +use winit::keyboard::KeyCode; use winit_input_helper::WinitInputHelper; use crate::{ @@ -77,7 +78,7 @@ impl GlassApp for FluidSimApp { &mut self, _context: &mut GlassContext, _event_loop: &EventLoopWindowTarget<()>, - event: &Event<'_, ()>, + event: &Event<()>, ) { self.input.update(event); } @@ -87,7 +88,7 @@ impl GlassApp for FluidSimApp { .primary_render_window() .window() .set_title(&format!("FPS: {:.3}", self.timer.avg_fps())); - let scroll_diff = self.input.scroll_diff(); + let (_, scroll_diff) = self.input.scroll_diff(); if scroll_diff > 0.0 { self.camera.set_scale(self.camera.scale() / 1.05); } else if scroll_diff < 0.0 { @@ -97,22 +98,22 @@ impl GlassApp for FluidSimApp { self.resize(context); } // Read inputs state - if self.input.key_pressed(VirtualKeyCode::Space) { + if self.input.key_pressed(KeyCode::Space) { self.fluid_scene.toggle_pause(); } - if self.input.key_pressed(VirtualKeyCode::R) { + if self.input.key_pressed(KeyCode::KeyR) { self.fluid_scene.reset(); } - if self.input.key_pressed(VirtualKeyCode::G) { + if self.input.key_pressed(KeyCode::KeyG) { self.fluid_scene.toggle_grid(); } - if self.input.key_pressed(VirtualKeyCode::P) { + if self.input.key_pressed(KeyCode::KeyP) { self.fluid_scene.toggle_particles(); } - if self.input.key_pressed(VirtualKeyCode::F) { + if self.input.key_pressed(KeyCode::KeyF) { self.fluid_scene.toggle_gravity(); } - if let Some((x, y)) = self.input.mouse() { + if let Some((x, y)) = self.input.cursor() { let screen_size = context.primary_render_window().surface_size(); let scale_factor = context.primary_render_window().window().scale_factor() as f32; let pos = cursor_to_world( diff --git a/examples/fluid_sim/timer.rs b/examples/fluid_sim/timer.rs index 088c9e8..8f7492d 100644 --- a/examples/fluid_sim/timer.rs +++ b/examples/fluid_sim/timer.rs @@ -33,6 +33,7 @@ impl Timer { self.time.elapsed().as_nanos() as f64 * 1.0e-6 } + #[allow(unused)] pub fn time_since_last_update_sec(&self) -> f64 { self.end() / 1000.0 } @@ -60,6 +61,7 @@ impl Timer { self.delta } + #[allow(unused)] pub fn dt_sec(&self) -> f64 { self.delta / 1000.0 } diff --git a/examples/multiple_windows/main.rs b/examples/multiple_windows/main.rs index 20a2340..372dfdd 100644 --- a/examples/multiple_windows/main.rs +++ b/examples/multiple_windows/main.rs @@ -3,8 +3,9 @@ use glass::{ }; use wgpu::{Color, StoreOp}; use winit::{ - event::{DeviceEvent, ElementState, Event, VirtualKeyCode}, + event::{DeviceEvent, ElementState, Event}, event_loop::{EventLoop, EventLoopWindowTarget}, + keyboard::{KeyCode, PhysicalKey}, window::WindowId, }; @@ -55,21 +56,21 @@ impl GlassApp for MultiWindowApp { .. } = event { - if let Some(key) = input.virtual_keycode { - if key == VirtualKeyCode::Space && input.state == ElementState::Pressed { - // Create window - self.window_ids.push( - context - .create_window(event_loop, WindowConfig { - width: WIDTH, - height: HEIGHT, - exit_on_esc: true, - ..WindowConfig::default() - }) - .unwrap(), - ); - println!("Window ids: {:#?}", self.window_ids); - } + if input.physical_key == PhysicalKey::Code(KeyCode::Space) + && input.state == ElementState::Pressed + { + // Create window + self.window_ids.push( + context + .create_window(event_loop, WindowConfig { + width: WIDTH, + height: HEIGHT, + exit_on_esc: true, + ..WindowConfig::default() + }) + .unwrap(), + ); + println!("Window ids: {:#?}", self.window_ids); } } } diff --git a/src/glass.rs b/src/glass.rs index 9f378ae..d789c7d 100644 --- a/src/glass.rs +++ b/src/glass.rs @@ -7,9 +7,10 @@ use wgpu::{ SurfaceConfiguration, }; use winit::{ - error::OsError, - event::{ElementState, Event, VirtualKeyCode, WindowEvent}, - event_loop::{EventLoop, EventLoopWindowTarget}, + error::{EventLoopError, OsError}, + event::{ElementState, Event, WindowEvent}, + event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, + keyboard::{Key, NamedKey}, window::{Fullscreen, Window, WindowId}, }; @@ -39,17 +40,22 @@ impl Glass { } pub fn run(mut self) -> Result<(), GlassError> { - let event_loop = EventLoop::new(); + let event_loop = match EventLoop::new() { + Ok(e) => e, + Err(e) => return Err(GlassError::EventLoopError(e)), + }; let mut context = GlassContext::new(&event_loop, self.config.clone())?; self.app.start(&event_loop, &mut context); let mut remove_windows = vec![]; let mut request_window_close = false; - event_loop.run(move |event, event_loop, control_flow| { - control_flow.set_poll(); + match event_loop.run(move |event, event_loop| { + event_loop.set_control_flow(ControlFlow::Poll); // Run input fn - self.app.input(&mut context, event_loop, &event); + if !event_loop.exiting() { + self.app.input(&mut context, event_loop, &event); + } match event { Event::WindowEvent { window_id, @@ -68,28 +74,27 @@ impl Glass { } } WindowEvent::ScaleFactorChanged { - new_inner_size, .. + .. } => { + let size = window.window().inner_size(); window.configure_surface_with_size( context.device_context.device(), - *new_inner_size, + size, ); } WindowEvent::KeyboardInput { - input, + event, is_synthetic, .. } => { - if let Some(key) = input.virtual_keycode { - if !is_synthetic - && window.exit_on_esc() - && window.is_focused() - && key == VirtualKeyCode::Escape - && input.state == ElementState::Pressed - { - request_window_close = true; - remove_windows.push(window_id); - } + if event.logical_key == Key::Named(NamedKey::Escape) + && !is_synthetic + && window.exit_on_esc() + && window.is_focused() + && event.state == ElementState::Pressed + { + request_window_close = true; + remove_windows.push(window_id); } } WindowEvent::Focused(has_focus) => { @@ -103,63 +108,72 @@ impl Glass { } } } - Event::MainEventsCleared => { - self.app.update(&mut context); - // Close window(s) - if request_window_close || context.exit { - for window in remove_windows.iter() { - context.windows.remove(window); - } - remove_windows.clear(); - request_window_close = false; - // Exit - if context.windows.is_empty() || context.exit { - control_flow.set_exit(); - // Run end - self.app.end(&mut context); + Event::AboutToWait => { + if !event_loop.exiting() { + self.app.update(&mut context); + // Close window(s) + if request_window_close || context.exit { + for window in remove_windows.iter() { + context.windows.remove(window); + } + remove_windows.clear(); + request_window_close = false; + // Exit + if context.windows.is_empty() || context.exit { + event_loop.exit(); + } } - } - // Render - for (_, window) in context.windows.iter() { - match window.surface().get_current_texture() { - Ok(frame) => { - let mut encoder = context - .device_context - .device() - .create_command_encoder(&wgpu::CommandEncoderDescriptor { - label: Some("Render Commands"), + // Render + for (_, window) in context.windows.iter() { + match window.surface().get_current_texture() { + Ok(frame) => { + let mut encoder = context + .device_context + .device() + .create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: Some("Render Commands"), + }); + + // Run render + self.app.render(&context, RenderData { + encoder: &mut encoder, + window, + frame: &frame, }); - // Run render - self.app.render(&context, RenderData { - encoder: &mut encoder, - window, - frame: &frame, - }); - - context - .device_context - .queue() - .submit(Some(encoder.finish())); + context + .device_context + .queue() + .submit(Some(encoder.finish())); - frame.present(); + frame.present(); - self.app.after_render(&context); - } - Err(error) => { - if error == wgpu::SurfaceError::OutOfMemory { - panic!("Swapchain error: {error}. Rendering cannot continue.") + self.app.after_render(&context); + } + Err(error) => { + if error == wgpu::SurfaceError::OutOfMemory { + panic!( + "Swapchain error: {error}. Rendering cannot continue." + ) + } } } + window.window().request_redraw(); } - window.window().request_redraw(); + // End of frame + self.app.end_of_frame(&mut context); } - // End of frame - self.app.end_of_frame(&mut context); + } + Event::LoopExiting => { + // Run end + self.app.end(&mut context); } _ => {} } - }); + }) { + Err(e) => Err(GlassError::EventLoopError(e)), + Ok(a) => Ok(a), + } } } @@ -210,6 +224,7 @@ pub enum GlassError { AdapterError, DeviceError(RequestDeviceError), ImageError(ImageError), + EventLoopError(EventLoopError), } impl std::fmt::Display for GlassError { @@ -220,6 +235,7 @@ impl std::fmt::Display for GlassError { GlassError::AdapterError => "AdapterError".to_owned(), GlassError::DeviceError(e) => format!("DeviceError: {}", e), GlassError::ImageError(e) => format!("ImageError: {}", e), + GlassError::EventLoopError(e) => format!("EventLoopError: {}", e), }; write!(f, "{}", s) }