From 14b0948b9c341c08d66d470e38f411ebdb66ca77 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Mon, 22 Feb 2021 21:24:41 -0800 Subject: [PATCH] actually, the interface for writing isn't too bad. It just sucks when you want to write data from a system --- src/main.rs | 5 ++-- src/render/system.rs | 71 +++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9038f71..c41f163 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,7 +140,8 @@ fn main() { let mut render_schedule = Schedule::builder() .add_system(render::system::render_imgui_system()) - .add_system(render::system::render_test_system(ImguiPerformanceProfilerLine::new("Render system".to_string()))) + .add_system(render::system::render_test_system()) + .add_system(render::system::render_performance_flag_system()) .build(); let mut update_schedule = Schedule::builder() @@ -153,7 +154,7 @@ fn main() { let mut event_schedule = Schedule::builder() .add_system(owned_event::event_dispatch_system()) .build(); - + let event_loop = EventLoop::::with_user_event(); let mut builder = winit_24::window::WindowBuilder::new(); builder = builder.with_title("MVGE"); diff --git a/src/render/system.rs b/src/render/system.rs index 7be2d7d..ce3aeb0 100644 --- a/src/render/system.rs +++ b/src/render/system.rs @@ -1,4 +1,6 @@ use std::cell::RefCell; +use std::iter::Chain; +use std::slice::Iter; use std::sync::{Arc, Mutex}; use std::thread::current; use std::time::Duration; @@ -18,22 +20,25 @@ use legion::world::SubWorld; use legion::*; use rapier3d::parry::motion::RigidMotionComposition; use wgpu::util::DeviceExt; -use wgpu::{BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, Device, FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, TextureView, VertexState, CommandEncoder}; +use wgpu::{ + BackendBit, BindGroup, BindGroupLayout, Buffer, BufferBindingType, CommandEncoder, Device, + FragmentState, Instance, Queue, Surface, SwapChain, SwapChainDescriptor, SwapChainFrame, + TextureView, VertexState, +}; use winit_24::dpi::PhysicalSize; use winit_24::platform::unix::x11::ffi::Time; use winit_24::window::Window; use crate::camera::{Camera, CameraController}; -use crate::components::{Mesh, Position, RangeCopy, LoopState, ImguiWindow}; +use crate::components::{ImguiWindow, LoopState, Mesh, Position, RangeCopy}; use crate::current_ui; use crate::geometry::{load_obj, Vertex}; 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; - +use crate::render::state::RenderState; +use crate::render::{ + insert_debug_marker_checked, pop_debug_group_checked, push_debug_group_checked, EntityUniforms, +}; #[system] #[write_component(Camera)] @@ -55,9 +60,7 @@ pub fn imgui_prepare( unsafe { crate::CURRENT_UI = Some(std::mem::transmute(imgui_context.frame())) } } - fn run_imgui_render_step(world: &mut SubWorld, ui: &Ui) { - let mut component_query = <(&G)>::query(); let mut window_query = <(&ImguiWindow)>::query(); @@ -73,7 +76,9 @@ fn run_imgui_render_step(world: &mut SubWorld, for (component_state) in component_query.iter(world) { v.push(component_state) } - window_data.unwrap().build(&ui, || { (window_func.unwrap())(ui, v) }); + window_data + .unwrap() + .build(&ui, || (window_func.unwrap())(ui, v)); } } /// Go through each "global" window-data component and render it's data @@ -83,7 +88,6 @@ fn run_imgui_render_step(world: &mut SubWorld, #[write_component(ImguiWindow)] #[write_component(ImguiGenericOutputLine)] pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) { - let ui = unsafe { crate::current_ui().unwrap() }; // Pull out the window associated with this type, and render each of the components in the sytem @@ -91,7 +95,6 @@ pub fn render_imgui(world: &mut SubWorld, #[resource] loop_state: &mut LoopState run_imgui_render_step::(world, &ui); } - // This would be the shared state for all imgui generic output things pub struct ImguiGenericOutputLine { pub label: String, @@ -99,9 +102,7 @@ pub struct ImguiGenericOutputLine { impl ImguiGenericOutputLine { pub fn new(label: String) -> ImguiGenericOutputLine { - ImguiGenericOutputLine { - label - } + ImguiGenericOutputLine { label } } } @@ -116,11 +117,10 @@ pub struct ImguiPerformanceProfilerLine { 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.scale_max = self.list_of_fps.iter().cloned().fold(0. / 0., f32::max); self.index = 0; } else { self.index += 1; @@ -138,7 +138,16 @@ impl ImguiPerformanceProfilerLine { 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()) + ( + (left + .iter() + .rev() + .chain(right.iter().rev()) + .take(50) + .sum::() + / 50.0), + "FPS".to_string(), + ) } pub fn new(label: String) -> ImguiPerformanceProfilerLine { @@ -147,11 +156,22 @@ impl ImguiPerformanceProfilerLine { list_of_fps: [0.0; 400], index: 0, scale_min: 0.0, - scale_max: 0.0 + scale_max: 0.0, } } } +#[system] +#[write_component(ImguiPerformanceProfilerLine)] +pub fn render_performance_flag(world: &mut SubWorld, #[resource] loop_state: &mut LoopState) { + let delta_time = loop_state.delta_time.as_secs_f32(); + + let mut query = <(&mut ImguiPerformanceProfilerLine)>::query(); + for (mut profiler) in query.iter_mut(world) { + profiler.add_sample(delta_time); + } +} + #[system] #[write_component(Camera)] #[write_component(Position)] @@ -161,17 +181,12 @@ impl ImguiPerformanceProfilerLine { #[write_component(DirectionalLight)] pub fn render_test( world: &mut SubWorld, - #[state] performance_profiler_line: &mut ImguiPerformanceProfilerLine, #[resource] loop_state: &mut LoopState, #[resource] renderer: &mut RenderState, #[resource] winit_window: &mut Window, #[resource] imgui_context: &mut Arc>, #[resource] imgui_platform: &mut Arc>, ) { - - let delta_time = loop_state.delta_time.as_secs_f32(); - performance_profiler_line.add_sample(delta_time); - let mut encoder = renderer .device .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None }); @@ -237,8 +252,10 @@ pub fn render_test( let mut query = <(&mut DirectionalLight, &mut Position)>::query(); for (i, (light, pos)) in query.iter_mut(world).enumerate() { - insert_debug_marker_checked(&format!("shadow pass {} (light at position {:?})", i, pos), &mut encoder); - + insert_debug_marker_checked( + &format!("shadow pass {} (light at position {:?})", i, pos), + &mut encoder, + ); // The light uniform buffer already has the projection, // let's just copy it over to the shadow uniform buffer. @@ -252,7 +269,6 @@ pub fn render_test( insert_debug_marker_checked("render entities", &mut encoder); - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("render pass"), color_attachments: &[], @@ -328,7 +344,6 @@ pub fn render_test( let ui = unsafe { crate::current_ui().unwrap() }; - // ui.show_demo_window(&mut true); imgui_platform.prepare_render(&ui, &winit_window);