From 7304313dbdf8ae8e57163d88140e7073408e3869 Mon Sep 17 00:00:00 2001 From: David Huculak Date: Sun, 6 Aug 2023 00:52:31 -0400 Subject: [PATCH] remove mutex from surface data type --- example_game/src/main.rs | 16 +++++------ ikari/src/game.rs | 34 +++++++++++++---------- ikari/src/gameloop.rs | 24 ++++++++-------- ikari/src/renderer.rs | 59 ++++++++++++++++++---------------------- 4 files changed, 65 insertions(+), 68 deletions(-) diff --git a/example_game/src/main.rs b/example_game/src/main.rs index 17b08450..3eeb365e 100644 --- a/example_game/src/main.rs +++ b/example_game/src/main.rs @@ -69,15 +69,15 @@ async fn start() { log::debug!("game scene: {:?}", application_start_time.elapsed()); - let (surface_format, surface_size) = { - let surface_config_guard = surface_data.surface_config.lock().unwrap(); - + let mut renderer = Renderer::new( + base_renderer, + surface_data.surface_config.format, ( - surface_config_guard.format, - (surface_config_guard.width, surface_config_guard.height), - ) - }; - let mut renderer = Renderer::new(base_renderer, surface_format, surface_size).await?; + surface_data.surface_config.width, + surface_data.surface_config.height, + ), + ) + .await?; log::debug!("renderer: {:?}", application_start_time.elapsed()); diff --git a/ikari/src/game.rs b/ikari/src/game.rs index ae61168b..71358edb 100644 --- a/ikari/src/game.rs +++ b/ikari/src/game.rs @@ -932,7 +932,7 @@ pub async fn init_game_state( ); let ui_overlay = { - let surface_format = surface_data.surface_config.lock().unwrap().format; + let surface_format = surface_data.surface_config.format; IkariUiOverlay::new( window, &renderer.base.device, @@ -1006,7 +1006,7 @@ pub fn process_device_input(game_state: &mut GameState, event: &winit::event::De pub fn process_window_input( game_state: &mut GameState, renderer: &mut Renderer, - surface_data: &SurfaceData, + surface_data: &mut SurfaceData, event: &winit::event::WindowEvent, window: &winit::window::Window, ) { @@ -1089,7 +1089,7 @@ pub fn process_window_input( pub fn increment_render_scale( renderer: &mut Renderer, - surface_data: &SurfaceData, + surface_data: &mut SurfaceData, increase: bool, window: &winit::window::Window, ui_overlay: &mut IkariUiOverlay, @@ -1097,25 +1097,31 @@ pub fn increment_render_scale( let delta = 0.1; let change = if increase { delta } else { -delta }; - let framebuffer_size = { + { let mut renderer_data_guard = renderer.data.lock().unwrap(); - let surface_config_guard = surface_data.surface_config.lock().unwrap(); renderer_data_guard.render_scale = (renderer_data_guard.render_scale + change).clamp(0.1, 4.0); log::info!( "Render scale: {:?} ({:?}x{:?})", renderer_data_guard.render_scale, - (surface_config_guard.width as f32 * renderer_data_guard.render_scale.sqrt()).round() - as u32, - (surface_config_guard.height as f32 * renderer_data_guard.render_scale.sqrt()).round() - as u32, + (surface_data.surface_config.width as f32 * renderer_data_guard.render_scale.sqrt()) + .round() as u32, + (surface_data.surface_config.height as f32 * renderer_data_guard.render_scale.sqrt()) + .round() as u32, ); + } - (surface_config_guard.width, surface_config_guard.height) - }; - - resize_window(renderer, ui_overlay, surface_data, window, framebuffer_size); + resize_window( + renderer, + ui_overlay, + surface_data, + window, + ( + surface_data.surface_config.width, + surface_data.surface_config.height, + ), + ); } pub fn increment_exposure(renderer_data: &mut RendererData, increase: bool) { @@ -1571,7 +1577,7 @@ pub fn update_game_state( deg_to_rad(90.0), )) .scale( - (1080.0 / surface_data.surface_config.lock().unwrap().height as f32) + (1080.0 / surface_data.surface_config.height as f32) * 0.06 * Vec3::new(1.0, 1.0, 1.0), ) diff --git a/ikari/src/gameloop.rs b/ikari/src/gameloop.rs index d36ff0d7..9d9ba7ce 100644 --- a/ikari/src/gameloop.rs +++ b/ikari/src/gameloop.rs @@ -17,7 +17,7 @@ use winit::{ pub fn resize_window( renderer: &mut Renderer, ui_overlay: &mut IkariUiOverlay, - surface_data: &SurfaceData, + surface_data: &mut SurfaceData, window: &winit::window::Window, new_size: (u32, u32), ) { @@ -31,7 +31,7 @@ pub fn run( event_loop: EventLoop<()>, mut game_state: GameState, mut renderer: Renderer, - surface_data: SurfaceData, + mut surface_data: SurfaceData, application_start_time: Instant, ) { let mut logged_start_time = false; @@ -136,16 +136,14 @@ pub fn run( renderer_data_guard.draw_culling_frustum = ui_state.draw_culling_frustum; renderer_data_guard.draw_point_light_culling_frusta = ui_state.draw_point_light_culling_frusta; - renderer.set_vsync(ui_state.enable_vsync, &surface_data); + renderer.set_vsync(ui_state.enable_vsync, &mut surface_data); - let framebuffer_size = { - let surface_config_guard = surface_data.surface_config.lock().unwrap(); - - (surface_config_guard.width, surface_config_guard.height) - }; renderer.set_culling_frustum_lock( &game_state, - framebuffer_size, + ( + surface_data.surface_config.width, + surface_data.surface_config.height, + ), ui_state.culling_frustum_lock_mode, ); } @@ -171,7 +169,7 @@ pub fn run( resize_window( &mut renderer, &mut game_state.ui_overlay, - &surface_data, + &mut surface_data, &window, window.inner_size().into(), ); @@ -212,7 +210,7 @@ pub fn run( resize_window( &mut renderer, &mut game_state.ui_overlay, - &surface_data, + &mut surface_data, &window, (*size).into(), ); @@ -223,7 +221,7 @@ pub fn run( resize_window( &mut renderer, &mut game_state.ui_overlay, - &surface_data, + &mut surface_data, &window, (**new_inner_size).into(), ); @@ -238,7 +236,7 @@ pub fn run( process_window_input( &mut game_state, &mut renderer, - &surface_data, + &mut surface_data, &event, &window, ); diff --git a/ikari/src/renderer.rs b/ikari/src/renderer.rs index e1aaac35..8db29d22 100644 --- a/ikari/src/renderer.rs +++ b/ikari/src/renderer.rs @@ -336,8 +336,7 @@ pub struct BaseRenderer { pub struct SurfaceData { pub surface: wgpu::Surface, - // TODO: does this need to be a mutex still? - pub surface_config: Mutex, + pub surface_config: wgpu::SurfaceConfiguration, } impl BaseRenderer { @@ -370,7 +369,7 @@ impl BaseRenderer { let surface_data = SurfaceData { surface, - surface_config: Mutex::new(surface_config), + surface_config, }; Ok((base, surface_data)) @@ -2788,38 +2787,33 @@ impl Renderer { index_buffer } - pub fn set_vsync(&self, vsync: bool, surface_data: &SurfaceData) { - let surface_config = { - let mut surface_config_guard = surface_data.surface_config.lock().unwrap(); - let new_present_mode = if vsync { - wgpu::PresentMode::AutoVsync - } else { - wgpu::PresentMode::AutoNoVsync - }; - if surface_config_guard.present_mode == new_present_mode { - return; - } - surface_config_guard.present_mode = new_present_mode; - surface_config_guard.clone() + pub fn set_vsync(&self, vsync: bool, surface_data: &mut SurfaceData) { + let new_present_mode = if vsync { + wgpu::PresentMode::AutoVsync + } else { + wgpu::PresentMode::AutoNoVsync }; + if surface_data.surface_config.present_mode == new_present_mode { + return; + } + + surface_data.surface_config.present_mode = new_present_mode; + surface_data .surface - .configure(&self.base.device, &surface_config); + .configure(&self.base.device, &surface_data.surface_config); } - pub fn resize_surface(&mut self, new_size: (u32, u32), surface_data: &SurfaceData) { + pub fn resize_surface(&mut self, new_size: (u32, u32), surface_data: &mut SurfaceData) { let (new_width, new_height) = new_size; - let surface_config = { - let mut surface_config_guard = surface_data.surface_config.lock().unwrap(); - surface_config_guard.width = new_width; - surface_config_guard.height = new_height; - surface_config_guard.clone() - }; + + surface_data.surface_config.width = new_width; + surface_data.surface_config.height = new_height; surface_data .surface - .configure(&self.base.device, &surface_config); + .configure(&self.base.device, &surface_data.surface_config); } pub fn resize(&mut self, new_framebuffer_size: (u32, u32)) { @@ -3253,15 +3247,14 @@ impl Renderer { game_state: &mut GameState, surface_data: &SurfaceData, ) -> anyhow::Result<()> { - let (surface_format, surface_size) = { - let surface_config_guard = surface_data.surface_config.lock().unwrap(); - + self.update_internal( + game_state, + surface_data.surface_config.format, ( - surface_config_guard.format, - (surface_config_guard.width, surface_config_guard.height), - ) - }; - self.update_internal(game_state, surface_format, surface_size); + surface_data.surface_config.width, + surface_data.surface_config.height, + ), + ); self.render_internal(game_state, surface_data.surface.get_current_texture()?) }