I like the way this renders, but the implementation is an absolute disaster...

master
mitchellhansen 4 years ago
parent 4a041cc833
commit ebf5aa0ac4

@ -59,7 +59,7 @@ use crate::imgui_supp::extended_winit_imgui_support;
use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform}; use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
use crate::owned_event::{OwnedEvent, OwnedEventExtension}; use crate::owned_event::{OwnedEvent, OwnedEventExtension};
use crate::physics::state::PhysicsState; use crate::physics::state::PhysicsState;
use crate::render::system::ImguiPerformanceProfiler; use crate::render::system::ImguiPerformanceProfilerLine;
use crate::runtime::state::RuntimeState; use crate::runtime::state::RuntimeState;
mod camera; mod camera;
@ -118,6 +118,8 @@ pub unsafe fn current_ui<'a>() -> Option<&'a imgui::Ui<'a>> {
CURRENT_UI.as_ref() CURRENT_UI.as_ref()
} }
fn main() { fn main() {
let logger = env_logger::builder() let logger = env_logger::builder()
.filter(Some("minimal_viable_game_engine"), LevelFilter::Info) .filter(Some("minimal_viable_game_engine"), LevelFilter::Info)
@ -158,44 +160,7 @@ fn main() {
let mut resources = Resources::default(); 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 // Load up all the resources
{ {
@ -335,15 +300,17 @@ fn main() {
event: winit_24::event::DeviceEvent::Key(keyboard_input), event: winit_24::event::DeviceEvent::Key(keyboard_input),
.. ..
} => { } => {
match keyboard_input.virtual_keycode.unwrap() { if keyboard_input.virtual_keycode.is_some() {
VirtualKeyCode::Escape => { match keyboard_input.virtual_keycode.unwrap() {
if keyboard_input.state == ElementState::Pressed { VirtualKeyCode::Escape => {
*control_flow = ControlFlow::Exit; if keyboard_input.state == ElementState::Pressed {
} else { *control_flow = ControlFlow::Exit;
//d } else {
//d
}
} }
_ => (),
} }
_ => (),
} }
} }
event::Event::WindowEvent { event::Event::WindowEvent {

@ -31,6 +31,8 @@ use crate::imgui_supp::imgui_support::{ImguiContext, ImguiPlatform};
use crate::light::{DirectionalLight, LightRaw}; use crate::light::{DirectionalLight, LightRaw};
use crate::render::state::{RenderState}; use crate::render::state::{RenderState};
use crate::render::{push_debug_group_checked, insert_debug_marker_checked, pop_debug_group_checked, EntityUniforms}; 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] #[system]
@ -55,13 +57,13 @@ pub fn imgui_prepare(
/// Go through each "global" window-data component and render it's data /// Go through each "global" window-data component and render it's data
#[system] #[system]
#[write_component(ImguiWindow<ImguiPerformanceProfiler>)] #[write_component(ImguiWindow<ImguiPerformanceProfilerLine>)]
#[write_component(ImguiPerformanceProfiler)] #[write_component(ImguiPerformanceProfilerLine)]
pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) { pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) {
let ui = unsafe { crate::current_ui().unwrap() }; let ui = unsafe { crate::current_ui().unwrap() };
let mut query = <(&ImguiPerformanceProfiler, &mut ImguiWindow<ImguiPerformanceProfiler>)>::query(); let mut query = <(&ImguiPerformanceProfilerLine, &mut ImguiWindow<ImguiPerformanceProfilerLine>)>::query();
for (state, mut window) in query.iter_mut(world) { for (state, mut window) in query.iter_mut(world) {
let new_window = (window.window)(); let new_window = (window.window)();
new_window.build(&ui, || { (window.func)(ui, state) }); 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 // This would be the shared state for all imgui performance window things
pub struct ImguiPerformanceProfiler { pub struct ImguiPerformanceProfilerLine {
pub top_text: String, pub label: String,
pub list_of_fps: [f32; 400], list_of_fps: [f32; 400],
pub index: usize 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<f32>, Iter<f32>> {
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::<f32>() / 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] #[system]
#[write_component(Camera)] #[write_component(Camera)]
#[write_component(Position)] #[write_component(Position)]
#[write_component(ImguiPerformanceProfiler)] #[write_component(ImguiPerformanceProfilerLine)]
#[write_component(Point3<f32>)] #[write_component(Point3<f32>)]
#[write_component(Mesh)] #[write_component(Mesh)]
#[write_component(DirectionalLight)] #[write_component(DirectionalLight)]
@ -94,21 +135,10 @@ pub fn render_test(
#[resource] imgui_platform: &mut Arc<Mutex<ImguiPlatform>>, #[resource] imgui_platform: &mut Arc<Mutex<ImguiPlatform>>,
) { ) {
let mut query = <(&mut ImguiPerformanceProfiler)>::query(); let mut query = <(&mut ImguiPerformanceProfilerLine)>::query();
for (mut profiler) in query.iter_mut(world) { for (mut profiler) in query.iter_mut(world) {
let delta_time = loop_state.delta_time.as_secs_f32(); let delta_time = loop_state.delta_time.as_secs_f32();
let profiler :&mut ImguiPerformanceProfiler = profiler; // trick clion into giving me the type, ugh profiler.add_sample(delta_time);
profiler.top_text = (profiler.list_of_fps.iter().sum::<f32>() / 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;
}
} }
let mut encoder = renderer let mut encoder = renderer

@ -3,7 +3,8 @@ use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
use cgmath::{Deg, Euler, Point3, Quaternion, Rad}; use cgmath::{Deg, Euler, Point3, Quaternion, Rad};
use imgui::FontSource; use imgui::*;
use imgui::{Condition, FontSource, Ui};
use legion::systems::CommandBuffer; use legion::systems::CommandBuffer;
use legion::world::SubWorld; use legion::world::SubWorld;
use legion::IntoQuery; use legion::IntoQuery;
@ -17,16 +18,76 @@ use rapier3d::na::{Isometry3, Vector, Vector3};
use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline}; use rapier3d::pipeline::{ChannelEventCollector, PhysicsPipeline};
use crate::camera::{Camera, CameraController}; 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::geometry::RawMesh;
use crate::physics::state::PhysicsState; use crate::physics::state::PhysicsState;
use crate::render::state::RenderState; use crate::render::state::RenderState;
use crate::render::system::ImguiPerformanceProfilerLine;
use crate::runtime::state::RuntimeState; use crate::runtime::state::RuntimeState;
pub fn quad_color(color: [f32; 4]) -> [[f32; 4]; 4] {
[color, color, color, color]
}
#[system] #[system]
#[write_component(Mesh)] #[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")); 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] #[system]

Loading…
Cancel
Save