Skip to content

Commit

Permalink
move surface data out of renderer and into gameloop; shuffle around f…
Browse files Browse the repository at this point in the history
…unction args so framebuffer dependencies are more clear in renderer
  • Loading branch information
Davidster committed Aug 6, 2023
1 parent 3172ec8 commit 33525bb
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 202 deletions.
20 changes: 15 additions & 5 deletions example_game/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use ikari::scene::*;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

const DXC_PATH: &str = "ikari/dxc/";

async fn start() {
let run_result = async {
let application_start_time = ikari::time::Instant::now();
Expand Down Expand Up @@ -51,15 +53,14 @@ async fn start() {

log::debug!("window: {:?}", application_start_time.elapsed());

let base_render_state = {
let (base_renderer, surface_data) = {
let backends = if cfg!(target_os = "windows") {
wgpu::Backends::from(wgpu::Backend::Dx12)
// wgpu::Backends::PRIMARY
} else {
wgpu::Backends::PRIMARY
};

BaseRenderer::new(backends, Some(&window)).await
BaseRenderer::with_window(backends, Some(DXC_PATH.into()), &window).await?
};

log::debug!("base render: {:?}", application_start_time.elapsed());
Expand All @@ -68,11 +69,19 @@ async fn start() {

log::debug!("game scene: {:?}", application_start_time.elapsed());

let mut renderer = Renderer::new(base_render_state).await?;
let (surface_format, surface_size) = {
let surface_config_guard = surface_data.surface_config.lock().unwrap();

(
surface_config_guard.format,
(surface_config_guard.width, surface_config_guard.height),
)
};
let mut renderer = Renderer::new(base_renderer, surface_format, surface_size).await?;

log::debug!("renderer: {:?}", application_start_time.elapsed());

let game_state = init_game_state(game_scene, &mut renderer, &window).await?;
let game_state = init_game_state(game_scene, &mut renderer, &surface_data, &window).await?;

log::debug!("game state: {:?}", application_start_time.elapsed());

Expand All @@ -81,6 +90,7 @@ async fn start() {
event_loop,
game_state,
renderer,
surface_data,
application_start_time,
); // this will block while the game is running
anyhow::Ok(())
Expand Down
53 changes: 29 additions & 24 deletions ikari/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ async fn get_rainbow_texture(renderer_base: &BaseRenderer) -> Result<Texture> {
pub async fn init_game_state(
mut scene: Scene,
renderer: &mut Renderer,
surface_data: &SurfaceData,
window: &winit::window::Window,
) -> Result<GameState> {
let mut physics_state = PhysicsState::new();
Expand Down Expand Up @@ -930,15 +931,7 @@ pub async fn init_game_state(
);

let ui_overlay = {
let surface_format = renderer
.base
.surface_data
.as_ref()
.expect("surface is required to create a ui overlay")
.surface_config
.lock()
.unwrap()
.format;
let surface_format = surface_data.surface_config.lock().unwrap().format;
IkariUiOverlay::new(
window,
&renderer.base.device,
Expand Down Expand Up @@ -1012,6 +1005,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,
event: &winit::event::WindowEvent,
window: &winit::window::Window,
) {
Expand All @@ -1031,11 +1025,23 @@ pub fn process_window_input(
match keycode {
VirtualKeyCode::Z => {
drop(render_data_guard);
increment_render_scale(renderer, false, window, &mut game_state.ui_overlay);
increment_render_scale(
renderer,
surface_data,
false,
window,
&mut game_state.ui_overlay,
);
}
VirtualKeyCode::X => {
drop(render_data_guard);
increment_render_scale(renderer, true, window, &mut game_state.ui_overlay);
increment_render_scale(
renderer,
surface_data,
true,
window,
&mut game_state.ui_overlay,
);
}
VirtualKeyCode::E => {
increment_exposure(&mut render_data_guard, false);
Expand Down Expand Up @@ -1082,14 +1088,15 @@ pub fn process_window_input(

pub fn increment_render_scale(
renderer: &mut Renderer,
surface_data: &SurfaceData,
increase: bool,
window: &winit::window::Window,
ui_overlay: &mut IkariUiOverlay,
) {
let delta = 0.1;
let change = if increase { delta } else { -delta };

if let Some(surface_data) = renderer.base.surface_data.as_ref() {
{
let mut renderer_data_guard = renderer.data.lock().unwrap();
let surface_config_guard = surface_data.surface_config.lock().unwrap();
renderer_data_guard.render_scale =
Expand All @@ -1101,9 +1108,11 @@ pub fn increment_render_scale(
as u32,
(surface_config_guard.height as f32 * renderer_data_guard.render_scale.sqrt()).round()
as u32,
)
);
}
renderer.resize(window.inner_size());

renderer.resize_surface(window.inner_size().into(), surface_data);
renderer.resize(window.inner_size().into());
ui_overlay.resize(window.inner_size(), window.scale_factor());
}

Expand All @@ -1123,7 +1132,11 @@ pub fn increment_bloom_threshold(renderer_data: &mut RendererData, increase: boo
}

#[profiling::function]
pub fn update_game_state(game_state: &mut GameState, renderer: &mut Renderer) {
pub fn update_game_state(
game_state: &mut GameState,
renderer: &mut Renderer,
surface_data: &SurfaceData,
) {
let base_renderer = renderer.base.clone();
let renderer_data = renderer.data.clone();
let renderer_constant_data = renderer.constant_data.clone();
Expand Down Expand Up @@ -1556,15 +1569,7 @@ pub fn update_game_state(game_state: &mut GameState, renderer: &mut Renderer) {
deg_to_rad(90.0),
))
.scale(
(1080.0
/ base_renderer
.surface_data
.as_ref()
.expect("surface is needed to run the game!")
.window_size
.lock()
.unwrap()
.height as f32)
(1080.0 / surface_data.window_size.lock().unwrap().height as f32)
* 0.06
* Vec3::new(1.0, 1.0, 1.0),
)
Expand Down
37 changes: 28 additions & 9 deletions ikari/src/gameloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn run(
event_loop: EventLoop<()>,
mut game_state: GameState,
mut renderer: Renderer,
surface_data: SurfaceData,
application_start_time: Instant,
) {
let mut logged_start_time = false;
Expand Down Expand Up @@ -45,7 +46,7 @@ pub fn run(
let frame_duration = last_frame_start_time.map(|time| time.elapsed());
last_frame_start_time = Some(Instant::now());

update_game_state(&mut game_state, &mut renderer);
update_game_state(&mut game_state, &mut renderer, &surface_data);

{
// sync UI
Expand Down Expand Up @@ -122,9 +123,18 @@ 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);
renderer
.set_culling_frustum_lock(&game_state, ui_state.culling_frustum_lock_mode);
renderer.set_vsync(ui_state.enable_vsync, &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,
ui_state.culling_frustum_lock_mode,
);
}

#[cfg(target_arch = "wasm32")]
Expand All @@ -140,12 +150,13 @@ pub fn run(

game_state.ui_overlay.update(&window, control_flow);

match renderer.render(&mut game_state) {
match renderer.render(&mut game_state, &surface_data) {
Ok(_) => {}
Err(err) => match err.downcast_ref::<wgpu::SurfaceError>() {
// Reconfigure the surface if lost
Some(wgpu::SurfaceError::Lost) => {
renderer.resize(window.inner_size());
renderer.resize_surface(window.inner_size().into(), &surface_data);
renderer.resize(window.inner_size().into());
game_state
.ui_overlay
.resize(window.inner_size(), window.scale_factor());
Expand Down Expand Up @@ -183,13 +194,15 @@ pub fn run(
match &event {
WindowEvent::Resized(size) => {
if size.width > 0 && size.height > 0 {
renderer.resize(*size);
renderer.resize_surface((*size).into(), &surface_data);
renderer.resize((*size).into());
game_state.ui_overlay.resize(*size, window.scale_factor());
}
}
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
if new_inner_size.width > 0 && new_inner_size.height > 0 {
renderer.resize(**new_inner_size);
renderer.resize_surface((**new_inner_size).into(), &surface_data);
renderer.resize((**new_inner_size).into());
game_state
.ui_overlay
.resize(**new_inner_size, window.scale_factor());
Expand All @@ -201,7 +214,13 @@ pub fn run(

game_state.ui_overlay.handle_window_event(&window, &event);

process_window_input(&mut game_state, &mut renderer, &event, &window);
process_window_input(
&mut game_state,
&mut renderer,
&surface_data,
&event,
&window,
);
}
_ => {}
}
Expand Down
Loading

0 comments on commit 33525bb

Please sign in to comment.