diff --git a/src/main.rs b/src/main.rs index ae7d610..b6a63cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,7 @@ use crate::imgui_supp::extended_winit_imgui_support; use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform}; use crate::owned_event::{OwnedEvent, OwnedEventExtension}; use crate::physics::state::PhysicsState; -use crate::render::system::ImguiPerformanceProfiler; +use crate::render::system::ImguiPerformanceProfilerLine; use crate::runtime::state::RuntimeState; mod camera; @@ -118,6 +118,8 @@ pub unsafe fn current_ui<'a>() -> Option<&'a imgui::Ui<'a>> { CURRENT_UI.as_ref() } + + fn main() { let logger = env_logger::builder() .filter(Some("minimal_viable_game_engine"), LevelFilter::Info) @@ -158,44 +160,7 @@ fn main() { let mut resources = Resources::default(); - let entity: Entity = world.push(( - ImguiWindow { - // a window that does everything for the performance profiler - window: || { - imgui::Window::new(im_str!("Performance Profiler")) - .size([400.0, 500.0], Condition::FirstUseEver) - .position([50.0, 50.0], Condition::FirstUseEver) - }, - func: |ui: &Ui, a: &ImguiPerformanceProfiler| { - ui.plot_lines(im_str!("blah"), &a.list_of_fps) - .graph_size([200.0, 200.0]) - .scale_min(0.0) - .scale_min(0.01).build(); - ui.text(im_str!("blah blah blah {:?}", a.top_text)); - - let draw_list = ui.get_window_draw_list(); - let o = ui.cursor_screen_pos(); - let ws = ui.content_region_avail(); - a.list_of_fps.iter().fold((0, 0.0f32), |accum, &b| { - let b = b / 10.0; - let x1 = accum.0 as f32 * 1.0 + o[0]; - let x2 = (accum.0 as f32 + 1.0) * 1.0 + o[0]; - let p1 = [x1, accum.1 + o[1]]; - let p2 = [x2, accum.1 + b + o[1]]; - draw_list - .add_line(p1, p2, [128.0,64.0,0.0]) - .thickness(1.0) - .build(); - (accum.0 + 1, b) - }); - }, - }, - ImguiPerformanceProfiler { - top_text: "FPS".to_string(), - list_of_fps: [0.0; 400], - index: 0, - }, - )); + // Load up all the resources { @@ -335,15 +300,17 @@ fn main() { event: winit_24::event::DeviceEvent::Key(keyboard_input), .. } => { - match keyboard_input.virtual_keycode.unwrap() { - VirtualKeyCode::Escape => { - if keyboard_input.state == ElementState::Pressed { - *control_flow = ControlFlow::Exit; - } else { - //d + if keyboard_input.virtual_keycode.is_some() { + match keyboard_input.virtual_keycode.unwrap() { + VirtualKeyCode::Escape => { + if keyboard_input.state == ElementState::Pressed { + *control_flow = ControlFlow::Exit; + } else { + //d + } } + _ => (), } - _ => (), } } event::Event::WindowEvent { diff --git a/src/render/system.rs b/src/render/system.rs index 578962a..d2ea7d5 100644 --- a/src/render/system.rs +++ b/src/render/system.rs @@ -31,6 +31,8 @@ use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform}; use crate::light::{DirectionalLight, LightRaw}; use crate::render::state::{RenderState}; use crate::render::{push_debug_group_checked, insert_debug_marker_checked, pop_debug_group_checked, EntityUniforms}; +use std::iter::Chain; +use std::slice::Iter; #[system] @@ -55,13 +57,13 @@ pub fn imgui_prepare( /// Go through each "global" window-data component and render it's data #[system] -#[write_component(ImguiWindow)] -#[write_component(ImguiPerformanceProfiler)] +#[write_component(ImguiWindow)] +#[write_component(ImguiPerformanceProfilerLine)] pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) { let ui = unsafe { crate::current_ui().unwrap() }; - let mut query = <(&ImguiPerformanceProfiler, &mut ImguiWindow)>::query(); + let mut query = <(&ImguiPerformanceProfilerLine, &mut ImguiWindow)>::query(); for (state, mut window) in query.iter_mut(world) { let new_window = (window.window)(); new_window.build(&ui, || { (window.func)(ui, state) }); @@ -71,17 +73,56 @@ pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState // This would be the shared state for all imgui performance window things -pub struct ImguiPerformanceProfiler { - pub top_text: String, - pub list_of_fps: [f32; 400], - pub index: usize +pub struct ImguiPerformanceProfilerLine { + pub label: String, + list_of_fps: [f32; 400], + index: usize, + scale_min: f32, + pub scale_max: f32, } +impl ImguiPerformanceProfilerLine { + fn add_sample(&mut self, sample: f32) { + + self.list_of_fps[self.index] = sample; + + if self.index >= 399 { + self.scale_max = self.list_of_fps.iter().cloned().fold(0./0., f32::max); + self.index = 0; + } else { + self.index += 1; + } + + if self.scale_max < sample { + self.scale_max = sample; + } + } + + pub fn iter_data_from_head(&self) -> Chain, Iter> { + let (left, right) = self.list_of_fps.split_at(self.index); + right.iter().chain(left.iter()) + } + + pub fn current_average_label(&self) -> (f32, String) { + let (left, right) = self.list_of_fps.split_at(self.index); + ((left.iter().rev().chain(right.iter().rev()).take(50).sum::() / 50.0), "FPS".to_string()) + } + + pub fn new(label: String) -> ImguiPerformanceProfilerLine { + ImguiPerformanceProfilerLine { + label, + list_of_fps: [0.0; 400], + index: 0, + scale_min: 0.0, + scale_max: 0.0 + } + } +} #[system] #[write_component(Camera)] #[write_component(Position)] -#[write_component(ImguiPerformanceProfiler)] +#[write_component(ImguiPerformanceProfilerLine)] #[write_component(Point3)] #[write_component(Mesh)] #[write_component(DirectionalLight)] @@ -94,21 +135,10 @@ pub fn render_test( #[resource] imgui_platform: &mut Arc>, ) { - let mut query = <(&mut ImguiPerformanceProfiler)>::query(); + let mut query = <(&mut ImguiPerformanceProfilerLine)>::query(); for (mut profiler) in query.iter_mut(world) { let delta_time = loop_state.delta_time.as_secs_f32(); - let profiler :&mut ImguiPerformanceProfiler = profiler; // trick clion into giving me the type, ugh - - - profiler.top_text = (profiler.list_of_fps.iter().sum::() / profiler.list_of_fps.len() as f32).to_string(); - - profiler.list_of_fps[profiler.index] = 1.0 / delta_time; - - if profiler.index >= 399 { - profiler.index = 0; - } else { - profiler.index += 1; - } + profiler.add_sample(delta_time); } let mut encoder = renderer diff --git a/src/runtime/system.rs b/src/runtime/system.rs index 202962f..04b941e 100644 --- a/src/runtime/system.rs +++ b/src/runtime/system.rs @@ -3,7 +3,8 @@ use std::path::PathBuf; use std::time::Instant; use cgmath::{Deg, Euler, Point3, Quaternion, Rad}; -use imgui::FontSource; +use imgui::*; +use imgui::{Condition, FontSource, Ui}; use legion::systems::CommandBuffer; use legion::world::SubWorld; use legion::IntoQuery; @@ -17,16 +18,76 @@ use rapier3d::na::{Isometry3, Vector, Vector3}; use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline}; use crate::camera::{Camera, CameraController}; -use crate::components::{Collider, LoopState, Mesh, Physics, Position}; +use crate::components::{Collider, ImguiWindow, LoopState, Mesh, Physics, Position}; use crate::geometry::RawMesh; use crate::physics::state::PhysicsState; use crate::render::state::RenderState; +use crate::render::system::ImguiPerformanceProfilerLine; use crate::runtime::state::RuntimeState; +pub fn quad_color(color: [f32; 4]) -> [[f32; 4]; 4] { + [color, color, color, color] +} + #[system] #[write_component(Mesh)] -pub fn runtime_load(world: &mut SubWorld, #[resource] runtime_state: &mut RuntimeState) { +pub fn runtime_load( + cmd: &mut CommandBuffer, + world: &mut SubWorld, + #[resource] runtime_state: &mut RuntimeState, +) { runtime_state.preload_meshes(PathBuf::from("./resources")); + + let entity: Entity = cmd.push(( + ImguiWindow { + // a window that does everything for the performance profiler + window: || { + imgui::Window::new(im_str!("Performance Profiler")) + .size([400.0, 500.0], Condition::FirstUseEver) + .position([50.0, 50.0], Condition::FirstUseEver) + }, + func: |ui: &Ui, a: &ImguiPerformanceProfilerLine| { + ui.text(im_str!("Performance Graph")); + + let draw_list = ui.get_window_draw_list(); + let top_left = ui.cursor_screen_pos(); + let region_size = ui.content_region_avail(); + + // Fill rect + let qcolor = quad_color([0.5, 0.5, 1.0, 0.1]); + draw_list.add_rect_filled_multicolor( + top_left, + [top_left[0] + (region_size[0] - region_size[0]/5.0), top_left[1] + region_size[1]], + qcolor[0], + qcolor[1], + qcolor[2], + qcolor[3], + ); + + let x_scale = (region_size[0] - region_size[0]/5.0) / 400.0; + let y_scale = region_size[1] / a.scale_max; + a.iter_data_from_head() + .fold((0, 0.0f32), |accum, &fps_val| { + let x1 = accum.0 as f32 * x_scale + top_left[0]; + let y1 = top_left[1] + region_size[1] - accum.1 * y_scale; + let x2 = (accum.0 as f32 + 1.0) * x_scale + top_left[0]; + let y2 = top_left[1] + region_size[1] - fps_val * y_scale; + let p1 = [x1, y1]; + let p2 = [x2, y2]; + draw_list + .add_line(p1, p2, [1.0, 1.0, 0.0]) + .thickness(1.0) + .build(); + (accum.0 + 1, fps_val) + }); + + let text_x = (region_size[0] - region_size[0]/5.0 + 75.0); + let text_y = top_left[1] + region_size[1] - a.current_average_label().0 * y_scale; + draw_list.add_text([text_x, text_y], [1.0,1.0,0.0,1.0], format!("{} {:.0}",a.current_average_label().1, 1.0/a.current_average_label().0)); + }, + }, + ImguiPerformanceProfilerLine::new("RenderFFS".to_string()), + )); } #[system]