From 914cbf5bd3b2492e6243dd1ebe88e377f5c78e91 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Tue, 30 Jan 2024 00:51:34 -0500 Subject: [PATCH 1/3] Bump `wgpu` to 0.19 --- Cargo.toml | 8 ++-- examples/cube.rs | 55 +++++++++++++++------------- examples/custom-texture.rs | 75 +++++++++++++++++++------------------- examples/hello-world.rs | 55 +++++++++++++++------------- 4 files changed, 100 insertions(+), 93 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d522ad9..1df48ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,20 +43,20 @@ min = 0 # allow non-first increment [dependencies] bytemuck = "1" -imgui = "0.11" +imgui = "0.12" log = "0.4" smallvec = "1" -wgpu = "0.18" +wgpu = "0.19" [dev-dependencies] bytemuck = { version = "1.13", features = ["derive"] } cgmath = "0.18" env_logger = "0.10" image = { version = "0.24", default-features = false, features = ["png"] } -imgui-winit-support = "0.11" +imgui-winit-support = "0.12" pollster = "0.3" raw-window-handle = "0.5" -winit = "0.27.5" +winit = "0.29" [package.metadata.docs.rs] all-features = true diff --git a/examples/cube.rs b/examples/cube.rs index b20e659..09c9994 100644 --- a/examples/cube.rs +++ b/examples/cube.rs @@ -6,9 +6,10 @@ use std::time::Instant; use wgpu::{include_wgsl, util::DeviceExt, Extent3d}; use winit::{ dpi::LogicalSize, - event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyEvent, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::Window, + keyboard::{Key, NamedKey}, + window::WindowBuilder, }; const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( @@ -332,28 +333,25 @@ fn main() { env_logger::init(); // Set up window and GPU - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: wgpu::Backends::PRIMARY, ..Default::default() }); - let (window, size, surface) = { + let window = { let version = env!("CARGO_PKG_VERSION"); - let window = Window::new(&event_loop).unwrap(); - window.set_inner_size(LogicalSize { - width: 1280.0, - height: 720.0, - }); - window.set_title(&format!("imgui-wgpu {version}")); - let size = window.inner_size(); - - let surface = unsafe { instance.create_surface(&window) }.unwrap(); - - (window, size, surface) + let size = LogicalSize::new(1280.0, 720.0); + WindowBuilder::new() + .with_inner_size(size) + .with_title(&format!("imgui-wgpu {version}")) + .build(&event_loop) + .unwrap() }; + let size = window.inner_size(); + let surface = instance.create_surface(&window).unwrap(); let hidpi_factor = window.scale_factor(); @@ -374,6 +372,7 @@ fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], }; @@ -443,11 +442,11 @@ fn main() { let example_texture_id = renderer.textures.insert(texture); // Event loop - event_loop.run(move |event, _, control_flow| { - *control_flow = if cfg!(feature = "metal-auto-capture") { - ControlFlow::Exit + let _ = event_loop.run(|event, elwt| { + if cfg!(feature = "metal-auto-capture") { + elwt.exit(); } else { - ControlFlow::Poll + elwt.set_control_flow(ControlFlow::Poll); }; match event { Event::WindowEvent { @@ -460,6 +459,7 @@ fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], }; @@ -469,9 +469,9 @@ fn main() { Event::WindowEvent { event: WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), state: ElementState::Pressed, .. }, @@ -483,10 +483,12 @@ fn main() { event: WindowEvent::CloseRequested, .. } => { - *control_flow = ControlFlow::Exit; + elwt.exit(); } - Event::MainEventsCleared => window.request_redraw(), - Event::RedrawEventsCleared => { + Event::WindowEvent { + event: WindowEvent::RedrawRequested, + .. + } => { let now = Instant::now(); imgui.io_mut().update_delta_time(now - last_frame); last_frame = now; @@ -585,7 +587,8 @@ fn main() { queue.submit(Some(encoder.finish())); frame.present(); } - _ => (), + Event::AboutToWait => window.request_redraw(), + _ => {} } platform.handle_event(imgui.io_mut(), &window, &event); diff --git a/examples/custom-texture.rs b/examples/custom-texture.rs index fce8906..c9f8320 100644 --- a/examples/custom-texture.rs +++ b/examples/custom-texture.rs @@ -6,37 +6,35 @@ use std::time::Instant; use wgpu::Extent3d; use winit::{ dpi::LogicalSize, - event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyEvent, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::Window, + keyboard::{Key, NamedKey}, + window::WindowBuilder, }; fn main() { env_logger::init(); // Set up window and GPU - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: wgpu::Backends::PRIMARY, ..Default::default() }); - let (window, size, surface) = { + let window = { let version = env!("CARGO_PKG_VERSION"); - let window = Window::new(&event_loop).unwrap(); - window.set_inner_size(LogicalSize { - width: 1280.0, - height: 720.0, - }); - window.set_title(&format!("imgui-wgpu {version}")); - let size = window.inner_size(); - - let surface = unsafe { instance.create_surface(&window) }.unwrap(); - - (window, size, surface) + let size = LogicalSize::new(1280.0, 720.0); + WindowBuilder::new() + .with_inner_size(size) + .with_title(&format!("imgui-wgpu {version}")) + .build(&event_loop) + .unwrap() }; + let size = window.inner_size(); + let surface = instance.create_surface(&window).unwrap(); let hidpi_factor = window.scale_factor(); @@ -50,8 +48,8 @@ fn main() { let (device, queue) = block_on(adapter.request_device( &wgpu::DeviceDescriptor { label: None, - features: wgpu::Features::empty(), - limits: wgpu::Limits::default(), + required_features: wgpu::Features::empty(), + required_limits: wgpu::Limits::default(), }, None, )) @@ -64,6 +62,7 @@ fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], }; @@ -110,10 +109,10 @@ fn main() { let mut last_frame = Instant::now(); - // Set up Lenna texture - let lenna_bytes = include_bytes!("../resources/checker.png"); - let image = - image::load_from_memory_with_format(lenna_bytes, ImageFormat::Png).expect("invalid image"); + // Set up checker texture + let checker_bytes = include_bytes!("../resources/checker.png"); + let image = image::load_from_memory_with_format(checker_bytes, ImageFormat::Png) + .expect("invalid image"); let image = image.to_rgba8(); let (width, height) = image.dimensions(); let raw_data = image.into_raw(); @@ -124,7 +123,7 @@ fn main() { height, ..Default::default() }, - label: Some("lenna texture"), + label: Some("checker texture"), format: Some(wgpu::TextureFormat::Rgba8Unorm), ..Default::default() }; @@ -132,16 +131,16 @@ fn main() { let texture = Texture::new(&device, &renderer, texture_config); texture.write(&queue, &raw_data, width, height); - let lenna_texture_id = renderer.textures.insert(texture); + let checker_texture_id = renderer.textures.insert(texture); let mut last_cursor = None; // Event loop - event_loop.run(move |event, _, control_flow| { - *control_flow = if cfg!(feature = "metal-auto-capture") { - ControlFlow::Exit + let _ = event_loop.run(|event, elwt| { + if cfg!(feature = "metal-auto-capture") { + elwt.exit(); } else { - ControlFlow::Poll + elwt.set_control_flow(ControlFlow::Poll) }; match event { Event::WindowEvent { @@ -154,6 +153,7 @@ fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], }; @@ -163,9 +163,9 @@ fn main() { Event::WindowEvent { event: WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), state: ElementState::Pressed, .. }, @@ -177,12 +177,12 @@ fn main() { event: WindowEvent::CloseRequested, .. } => { - *control_flow = ControlFlow::Exit; - } - Event::MainEventsCleared => { - window.request_redraw(); + elwt.exit(); } - Event::RedrawEventsCleared => { + Event::WindowEvent { + event: WindowEvent::RedrawRequested, + .. + } => { let now = Instant::now(); imgui.io_mut().update_delta_time(now - last_frame); last_frame = now; @@ -207,7 +207,7 @@ fn main() { .build(|| { ui.text("Hello textures!"); ui.text("Say hello to checker.png"); - Image::new(lenna_texture_id, size).build(ui); + Image::new(checker_texture_id, size).build(ui); }); } @@ -246,7 +246,8 @@ fn main() { queue.submit(Some(encoder.finish())); frame.present(); } - _ => (), + Event::AboutToWait => window.request_redraw(), + _ => {} } platform.handle_event(imgui.io_mut(), &window, &event); diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 4ed984a..76e73d4 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -7,37 +7,35 @@ use pollster::block_on; use std::time::Instant; use winit::{ dpi::LogicalSize, - event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{ElementState, Event, KeyEvent, WindowEvent}, event_loop::{ControlFlow, EventLoop}, - window::Window, + keyboard::{Key, NamedKey}, + window::WindowBuilder, }; fn main() { env_logger::init(); // Set up window and GPU - let event_loop = EventLoop::new(); + let event_loop = EventLoop::new().unwrap(); let instance = wgpu::Instance::new(wgpu::InstanceDescriptor { backends: wgpu::Backends::PRIMARY, ..Default::default() }); - let (window, size, surface) = { + let window = { let version = env!("CARGO_PKG_VERSION"); - let window = Window::new(&event_loop).unwrap(); - window.set_inner_size(LogicalSize { - width: 1280.0, - height: 720.0, - }); - window.set_title(&format!("imgui-wgpu {version}")); - let size = window.inner_size(); - - let surface = unsafe { instance.create_surface(&window) }.unwrap(); - - (window, size, surface) + let size = LogicalSize::new(1280.0, 720.0); + WindowBuilder::new() + .with_inner_size(size) + .with_title(&format!("imgui-wgpu {version}")) + .build(&event_loop) + .unwrap() }; + let size = window.inner_size(); + let surface = instance.create_surface(&window).unwrap(); let hidpi_factor = window.scale_factor(); @@ -58,6 +56,7 @@ fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], }; @@ -109,11 +108,11 @@ fn main() { let mut last_cursor = None; // Event loop - event_loop.run(move |event, _, control_flow| { - *control_flow = if cfg!(feature = "metal-auto-capture") { - ControlFlow::Exit + let _ = event_loop.run(|event, elwt| { + if cfg!(feature = "metal-auto-capture") { + elwt.exit(); } else { - ControlFlow::Poll + elwt.set_control_flow(ControlFlow::Poll); }; match event { Event::WindowEvent { @@ -126,6 +125,7 @@ fn main() { width: size.width, height: size.height, present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, alpha_mode: wgpu::CompositeAlphaMode::Auto, view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], }; @@ -135,9 +135,9 @@ fn main() { Event::WindowEvent { event: WindowEvent::KeyboardInput { - input: - KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + event: + KeyEvent { + logical_key: Key::Named(NamedKey::Escape), state: ElementState::Pressed, .. }, @@ -149,10 +149,12 @@ fn main() { event: WindowEvent::CloseRequested, .. } => { - *control_flow = ControlFlow::Exit; + elwt.exit(); } - Event::MainEventsCleared => window.request_redraw(), - Event::RedrawEventsCleared => { + Event::WindowEvent { + event: WindowEvent::RedrawRequested, + .. + } => { let delta_s = last_frame.elapsed(); let now = Instant::now(); imgui.io_mut().update_delta_time(now - last_frame); @@ -232,7 +234,8 @@ fn main() { frame.present(); } - _ => (), + Event::AboutToWait => window.request_redraw(), + _ => {} } platform.handle_event(imgui.io_mut(), &window, &event); From 68cd1c6fcc246c431cb46284396bb48a350bdf11 Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Mon, 6 May 2024 22:55:15 -0400 Subject: [PATCH 2/3] Refactor event loop Unify match arms for `Event::WindowEvent` variant --- examples/cube.rs | 261 ++++++++++++++++++------------------- examples/custom-texture.rs | 186 ++++++++++++-------------- examples/hello-world.rs | 218 +++++++++++++++---------------- 3 files changed, 314 insertions(+), 351 deletions(-) diff --git a/examples/cube.rs b/examples/cube.rs index 09c9994..ecd2632 100644 --- a/examples/cube.rs +++ b/examples/cube.rs @@ -6,7 +6,7 @@ use std::time::Instant; use wgpu::{include_wgsl, util::DeviceExt, Extent3d}; use winit::{ dpi::LogicalSize, - event::{ElementState, Event, KeyEvent, WindowEvent}, + event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, keyboard::{Key, NamedKey}, window::WindowBuilder, @@ -449,145 +449,136 @@ fn main() { elwt.set_control_flow(ControlFlow::Poll); }; match event { - Event::WindowEvent { - event: WindowEvent::Resized(size), - .. - } => { - let surface_desc = wgpu::SurfaceConfiguration { - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - width: size.width, - height: size.height, - present_mode: wgpu::PresentMode::Fifo, - desired_maximum_frame_latency: 2, - alpha_mode: wgpu::CompositeAlphaMode::Auto, - view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], - }; - - surface.configure(&device, &surface_desc); - } - Event::WindowEvent { - event: - WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - state: ElementState::Pressed, - .. - }, - .. - }, - .. - } - | Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => { - elwt.exit(); - } - Event::WindowEvent { - event: WindowEvent::RedrawRequested, - .. - } => { - let now = Instant::now(); - imgui.io_mut().update_delta_time(now - last_frame); - last_frame = now; - - let frame = match surface.get_current_texture() { - Ok(frame) => frame, - Err(e) => { - eprintln!("dropped frame: {e:?}"); - return; + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { ref event, .. } => { + match event { + WindowEvent::Resized(size) => { + let surface_desc = wgpu::SurfaceConfiguration { + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + format: wgpu::TextureFormat::Bgra8UnormSrgb, + width: size.width, + height: size.height, + present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, + alpha_mode: wgpu::CompositeAlphaMode::Auto, + view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], + }; + + surface.configure(&device, &surface_desc); } - }; - platform - .prepare_frame(imgui.io_mut(), &window) - .expect("Failed to prepare frame"); - let ui = imgui.frame(); - - let view = frame - .texture - .create_view(&wgpu::TextureViewDescriptor::default()); - - // Render example normally at background - example.update(ui.io().delta_time); - example.setup_camera(&queue, ui.io().display_size); - example.render(&view, &device, &queue); - - // Store the new size of Image() or None to indicate that the window is collapsed. - let mut new_example_size: Option<[f32; 2]> = None; - - ui.window("Cube") - .size([512.0, 512.0], Condition::FirstUseEver) - .build(|| { - new_example_size = Some(ui.content_region_avail()); - imgui::Image::new(example_texture_id, new_example_size.unwrap()).build(ui); - }); - - if let Some(size) = new_example_size { - // Resize render target, which is optional - if size != example_size && size[0] >= 1.0 && size[1] >= 1.0 { - example_size = size; - let scale = &ui.io().display_framebuffer_scale; - let texture_config = TextureConfig { - size: Extent3d { - width: (example_size[0] * scale[0]) as u32, - height: (example_size[1] * scale[1]) as u32, - ..Default::default() - }, - usage: wgpu::TextureUsages::RENDER_ATTACHMENT - | wgpu::TextureUsages::TEXTURE_BINDING, - ..Default::default() + WindowEvent::CloseRequested => elwt.exit(), + WindowEvent::KeyboardInput { event, .. } => { + if let Key::Named(NamedKey::Escape) = event.logical_key { + if event.state.is_pressed() { + elwt.exit(); + } + } + } + WindowEvent::RedrawRequested => { + let now = Instant::now(); + imgui.io_mut().update_delta_time(now - last_frame); + last_frame = now; + + let frame = match surface.get_current_texture() { + Ok(frame) => frame, + Err(e) => { + eprintln!("dropped frame: {e:?}"); + return; + } }; - renderer.textures.replace( - example_texture_id, - Texture::new(&device, &renderer, texture_config), - ); + platform + .prepare_frame(imgui.io_mut(), &window) + .expect("Failed to prepare frame"); + let ui = imgui.frame(); + + let view = frame + .texture + .create_view(&wgpu::TextureViewDescriptor::default()); + + // Render example normally at background + example.update(ui.io().delta_time); + example.setup_camera(&queue, ui.io().display_size); + example.render(&view, &device, &queue); + + // Store the new size of Image() or None to indicate that the window is collapsed. + let mut new_example_size: Option<[f32; 2]> = None; + + ui.window("Cube") + .size([512.0, 512.0], Condition::FirstUseEver) + .build(|| { + new_example_size = Some(ui.content_region_avail()); + imgui::Image::new(example_texture_id, new_example_size.unwrap()) + .build(ui); + }); + + if let Some(size) = new_example_size { + // Resize render target, which is optional + if size != example_size && size[0] >= 1.0 && size[1] >= 1.0 { + example_size = size; + let scale = &ui.io().display_framebuffer_scale; + let texture_config = TextureConfig { + size: Extent3d { + width: (example_size[0] * scale[0]) as u32, + height: (example_size[1] * scale[1]) as u32, + ..Default::default() + }, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT + | wgpu::TextureUsages::TEXTURE_BINDING, + ..Default::default() + }; + renderer.textures.replace( + example_texture_id, + Texture::new(&device, &renderer, texture_config), + ); + } + + // Only render example to example_texture if thw window is not collapsed + example.setup_camera(&queue, size); + example.render( + renderer.textures.get(example_texture_id).unwrap().view(), + &device, + &queue, + ); + } + + let mut encoder: wgpu::CommandEncoder = + device.create_command_encoder(&wgpu::CommandEncoderDescriptor { + label: None, + }); + + if last_cursor != Some(ui.mouse_cursor()) { + last_cursor = Some(ui.mouse_cursor()); + platform.prepare_render(ui, &window); + } + + let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Load, // Do not clear + // load: wgpu::LoadOp::Clear(clear_color), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }); + + renderer + .render(imgui.render(), &queue, &device, &mut rpass) + .expect("Rendering failed"); + + drop(rpass); + + queue.submit(Some(encoder.finish())); + frame.present(); } - - // Only render example to example_texture if thw window is not collapsed - example.setup_camera(&queue, size); - example.render( - renderer.textures.get(example_texture_id).unwrap().view(), - &device, - &queue, - ); + _ => {} } - - let mut encoder: wgpu::CommandEncoder = - device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); - - if last_cursor != Some(ui.mouse_cursor()) { - last_cursor = Some(ui.mouse_cursor()); - platform.prepare_render(ui, &window); - } - - let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: &view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Load, // Do not clear - // load: wgpu::LoadOp::Clear(clear_color), - store: wgpu::StoreOp::Store, - }, - })], - depth_stencil_attachment: None, - timestamp_writes: None, - occlusion_query_set: None, - }); - - renderer - .render(imgui.render(), &queue, &device, &mut rpass) - .expect("Rendering failed"); - - drop(rpass); - - queue.submit(Some(encoder.finish())); - frame.present(); } - Event::AboutToWait => window.request_redraw(), _ => {} } diff --git a/examples/custom-texture.rs b/examples/custom-texture.rs index c9f8320..890c3b8 100644 --- a/examples/custom-texture.rs +++ b/examples/custom-texture.rs @@ -6,7 +6,7 @@ use std::time::Instant; use wgpu::Extent3d; use winit::{ dpi::LogicalSize, - event::{ElementState, Event, KeyEvent, WindowEvent}, + event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, keyboard::{Key, NamedKey}, window::WindowBuilder, @@ -143,110 +143,96 @@ fn main() { elwt.set_control_flow(ControlFlow::Poll) }; match event { - Event::WindowEvent { - event: WindowEvent::Resized(size), - .. - } => { - let surface_desc = wgpu::SurfaceConfiguration { - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - width: size.width, - height: size.height, - present_mode: wgpu::PresentMode::Fifo, - desired_maximum_frame_latency: 2, - alpha_mode: wgpu::CompositeAlphaMode::Auto, - view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], - }; - - surface.configure(&device, &surface_desc); - } - Event::WindowEvent { - event: - WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - state: ElementState::Pressed, - .. - }, - .. - }, - .. - } - | Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => { - elwt.exit(); - } - Event::WindowEvent { - event: WindowEvent::RedrawRequested, - .. - } => { - let now = Instant::now(); - imgui.io_mut().update_delta_time(now - last_frame); - last_frame = now; - - let frame = match surface.get_current_texture() { - Ok(frame) => frame, - Err(e) => { - eprintln!("dropped frame: {e:?}"); - return; + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { ref event, .. } => match event { + WindowEvent::Resized(size) => { + let surface_desc = wgpu::SurfaceConfiguration { + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + format: wgpu::TextureFormat::Bgra8UnormSrgb, + width: size.width, + height: size.height, + present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, + alpha_mode: wgpu::CompositeAlphaMode::Auto, + view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], + }; + + surface.configure(&device, &surface_desc); + } + WindowEvent::CloseRequested => elwt.exit(), + WindowEvent::KeyboardInput { event, .. } => { + if let Key::Named(NamedKey::Escape) = event.logical_key { + if event.state.is_pressed() { + elwt.exit(); + } } - }; - platform - .prepare_frame(imgui.io_mut(), &window) - .expect("Failed to prepare frame"); - let ui = imgui.frame(); - - { - let size = [width as f32, height as f32]; - let window = ui.window("Hello world"); - window - .size([400.0, 600.0], Condition::FirstUseEver) - .build(|| { - ui.text("Hello textures!"); - ui.text("Say hello to checker.png"); - Image::new(checker_texture_id, size).build(ui); - }); } + WindowEvent::RedrawRequested => { + let now = Instant::now(); + imgui.io_mut().update_delta_time(now - last_frame); + last_frame = now; + + let frame = match surface.get_current_texture() { + Ok(frame) => frame, + Err(e) => { + eprintln!("dropped frame: {e:?}"); + return; + } + }; + platform + .prepare_frame(imgui.io_mut(), &window) + .expect("Failed to prepare frame"); + let ui = imgui.frame(); + + { + let size = [width as f32, height as f32]; + let window = ui.window("Hello world"); + window + .size([400.0, 600.0], Condition::FirstUseEver) + .build(|| { + ui.text("Hello textures!"); + ui.text("Say hello to checker.png"); + Image::new(checker_texture_id, size).build(ui); + }); + } - let mut encoder: wgpu::CommandEncoder = - device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); + let mut encoder: wgpu::CommandEncoder = device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); - if last_cursor != Some(ui.mouse_cursor()) { - last_cursor = Some(ui.mouse_cursor()); - platform.prepare_render(ui, &window); - } + if last_cursor != Some(ui.mouse_cursor()) { + last_cursor = Some(ui.mouse_cursor()); + platform.prepare_render(ui, &window); + } - let view = frame - .texture - .create_view(&wgpu::TextureViewDescriptor::default()); - let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: &view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(clear_color), - store: wgpu::StoreOp::Store, - }, - })], - depth_stencil_attachment: None, - timestamp_writes: None, - occlusion_query_set: None, - }); - - renderer - .render(imgui.render(), &queue, &device, &mut rpass) - .expect("Rendering failed"); - - drop(rpass); - - queue.submit(Some(encoder.finish())); - frame.present(); - } - Event::AboutToWait => window.request_redraw(), + let view = frame + .texture + .create_view(&wgpu::TextureViewDescriptor::default()); + let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(clear_color), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }); + + renderer + .render(imgui.render(), &queue, &device, &mut rpass) + .expect("Rendering failed"); + + drop(rpass); + + queue.submit(Some(encoder.finish())); + frame.present(); + } + _ => {} + }, _ => {} } diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 76e73d4..a81f3ec 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -7,7 +7,7 @@ use pollster::block_on; use std::time::Instant; use winit::{ dpi::LogicalSize, - event::{ElementState, Event, KeyEvent, WindowEvent}, + event::{Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, keyboard::{Key, NamedKey}, window::WindowBuilder, @@ -115,126 +115,112 @@ fn main() { elwt.set_control_flow(ControlFlow::Poll); }; match event { - Event::WindowEvent { - event: WindowEvent::Resized(size), - .. - } => { - let surface_desc = wgpu::SurfaceConfiguration { - usage: wgpu::TextureUsages::RENDER_ATTACHMENT, - format: wgpu::TextureFormat::Bgra8UnormSrgb, - width: size.width, - height: size.height, - present_mode: wgpu::PresentMode::Fifo, - desired_maximum_frame_latency: 2, - alpha_mode: wgpu::CompositeAlphaMode::Auto, - view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], - }; - - surface.configure(&device, &surface_desc); - } - Event::WindowEvent { - event: - WindowEvent::KeyboardInput { - event: - KeyEvent { - logical_key: Key::Named(NamedKey::Escape), - state: ElementState::Pressed, - .. - }, - .. - }, - .. - } - | Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => { - elwt.exit(); - } - Event::WindowEvent { - event: WindowEvent::RedrawRequested, - .. - } => { - let delta_s = last_frame.elapsed(); - let now = Instant::now(); - imgui.io_mut().update_delta_time(now - last_frame); - last_frame = now; - - let frame = match surface.get_current_texture() { - Ok(frame) => frame, - Err(e) => { - eprintln!("dropped frame: {e:?}"); - return; + Event::AboutToWait => window.request_redraw(), + Event::WindowEvent { ref event, .. } => match event { + WindowEvent::Resized(size) => { + let surface_desc = wgpu::SurfaceConfiguration { + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, + format: wgpu::TextureFormat::Bgra8UnormSrgb, + width: size.width, + height: size.height, + present_mode: wgpu::PresentMode::Fifo, + desired_maximum_frame_latency: 2, + alpha_mode: wgpu::CompositeAlphaMode::Auto, + view_formats: vec![wgpu::TextureFormat::Bgra8Unorm], + }; + + surface.configure(&device, &surface_desc); + } + WindowEvent::CloseRequested => elwt.exit(), + WindowEvent::KeyboardInput { event, .. } => { + if let Key::Named(NamedKey::Escape) = event.logical_key { + if event.state.is_pressed() { + elwt.exit(); + } } - }; - platform - .prepare_frame(imgui.io_mut(), &window) - .expect("Failed to prepare frame"); - let ui = imgui.frame(); - - { - let window = ui.window("Hello world"); - window - .size([300.0, 100.0], Condition::FirstUseEver) - .build(|| { - ui.text("Hello world!"); - ui.text("This...is...imgui-rs on WGPU!"); - ui.separator(); - let mouse_pos = ui.io().mouse_pos; - ui.text(format!( - "Mouse Position: ({:.1},{:.1})", - mouse_pos[0], mouse_pos[1] - )); - }); - - let window = ui.window("Hello too"); - window - .size([400.0, 200.0], Condition::FirstUseEver) - .position([400.0, 200.0], Condition::FirstUseEver) - .build(|| { - ui.text(format!("Frametime: {delta_s:?}")); - }); - - ui.show_demo_window(&mut demo_open); } + WindowEvent::RedrawRequested => { + let delta_s = last_frame.elapsed(); + let now = Instant::now(); + imgui.io_mut().update_delta_time(now - last_frame); + last_frame = now; + + let frame = match surface.get_current_texture() { + Ok(frame) => frame, + Err(e) => { + eprintln!("dropped frame: {e:?}"); + return; + } + }; + platform + .prepare_frame(imgui.io_mut(), &window) + .expect("Failed to prepare frame"); + let ui = imgui.frame(); + + { + let window = ui.window("Hello world"); + window + .size([300.0, 100.0], Condition::FirstUseEver) + .build(|| { + ui.text("Hello world!"); + ui.text("This...is...imgui-rs on WGPU!"); + ui.separator(); + let mouse_pos = ui.io().mouse_pos; + ui.text(format!( + "Mouse Position: ({:.1},{:.1})", + mouse_pos[0], mouse_pos[1] + )); + }); + + let window = ui.window("Hello too"); + window + .size([400.0, 200.0], Condition::FirstUseEver) + .position([400.0, 200.0], Condition::FirstUseEver) + .build(|| { + ui.text(format!("Frametime: {delta_s:?}")); + }); + + ui.show_demo_window(&mut demo_open); + } - let mut encoder: wgpu::CommandEncoder = - device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); + let mut encoder: wgpu::CommandEncoder = device + .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); - if last_cursor != Some(ui.mouse_cursor()) { - last_cursor = Some(ui.mouse_cursor()); - platform.prepare_render(ui, &window); - } + if last_cursor != Some(ui.mouse_cursor()) { + last_cursor = Some(ui.mouse_cursor()); + platform.prepare_render(ui, &window); + } - let view = frame - .texture - .create_view(&wgpu::TextureViewDescriptor::default()); - let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: None, - color_attachments: &[Some(wgpu::RenderPassColorAttachment { - view: &view, - resolve_target: None, - ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(clear_color), - store: wgpu::StoreOp::Store, - }, - })], - depth_stencil_attachment: None, - timestamp_writes: None, - occlusion_query_set: None, - }); - - renderer - .render(imgui.render(), &queue, &device, &mut rpass) - .expect("Rendering failed"); - - drop(rpass); - - queue.submit(Some(encoder.finish())); - - frame.present(); - } - Event::AboutToWait => window.request_redraw(), + let view = frame + .texture + .create_view(&wgpu::TextureViewDescriptor::default()); + let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: None, + color_attachments: &[Some(wgpu::RenderPassColorAttachment { + view: &view, + resolve_target: None, + ops: wgpu::Operations { + load: wgpu::LoadOp::Clear(clear_color), + store: wgpu::StoreOp::Store, + }, + })], + depth_stencil_attachment: None, + timestamp_writes: None, + occlusion_query_set: None, + }); + + renderer + .render(imgui.render(), &queue, &device, &mut rpass) + .expect("Rendering failed"); + + drop(rpass); + + queue.submit(Some(encoder.finish())); + + frame.present(); + } + _ => {} + }, _ => {} } From 98d015b4fd3e7e396e72596589ac1994f2af88ab Mon Sep 17 00:00:00 2001 From: Michael Krasnitski Date: Mon, 6 May 2024 23:06:28 -0400 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3446bd1..48e2672 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,7 +37,7 @@ Per Keep a Changelog there are 6 main categories of changes: ## Unreleased - Internal: Fixed Scissor-Rect to not span across Framebuffersize, by limiting to framebuffer width. @PixelboysTM -- Bump wgpu version to 0.18. @calcoph +- Bump wgpu version to 0.19. @mkrasnitski and @calcoph ## v0.24.0