Skip to content

Commit

Permalink
remove mutex from surface data type
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidster committed Aug 6, 2023
1 parent a32bd6e commit 7304313
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 68 deletions.
16 changes: 8 additions & 8 deletions example_game/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
34 changes: 20 additions & 14 deletions ikari/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -1089,33 +1089,39 @@ 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,
) {
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) {
Expand Down Expand Up @@ -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),
)
Expand Down
24 changes: 11 additions & 13 deletions ikari/src/gameloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
) {
Expand All @@ -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;
Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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(),
);
Expand Down Expand Up @@ -212,7 +210,7 @@ pub fn run(
resize_window(
&mut renderer,
&mut game_state.ui_overlay,
&surface_data,
&mut surface_data,
&window,
(*size).into(),
);
Expand All @@ -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(),
);
Expand All @@ -238,7 +236,7 @@ pub fn run(
process_window_input(
&mut game_state,
&mut renderer,
&surface_data,
&mut surface_data,
&event,
&window,
);
Expand Down
59 changes: 26 additions & 33 deletions ikari/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<wgpu::SurfaceConfiguration>,
pub surface_config: wgpu::SurfaceConfiguration,
}

impl BaseRenderer {
Expand Down Expand Up @@ -370,7 +369,7 @@ impl BaseRenderer {

let surface_data = SurfaceData {
surface,
surface_config: Mutex::new(surface_config),
surface_config,
};

Ok((base, surface_data))
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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()?)
}

Expand Down

0 comments on commit 7304313

Please sign in to comment.