From 93bf728475e82dadc04225d7ced89171306a9801 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Fri, 27 Mar 2020 15:03:47 -0700 Subject: [PATCH] new Diagnostics system --- examples/instancing.rs | 3 +- examples/instancing_old.rs | 1 - examples/spawner.rs | 3 +- examples/ui_bench.rs | 3 +- src/app/app_builder.rs | 17 ++++- src/core/time.rs | 7 +- src/diagnostic/diagnostics.rs | 94 ++++++++++++++++++++++++++ src/diagnostic/mod.rs | 124 ++++++++++++++++++++++++++++++++++ src/diagnostics/mod.rs | 32 --------- src/lib.rs | 2 +- 10 files changed, 245 insertions(+), 41 deletions(-) create mode 100644 src/diagnostic/diagnostics.rs create mode 100644 src/diagnostic/mod.rs delete mode 100644 src/diagnostics/mod.rs diff --git a/examples/instancing.rs b/examples/instancing.rs index c5472643f3..ead3faf417 100644 --- a/examples/instancing.rs +++ b/examples/instancing.rs @@ -5,7 +5,8 @@ fn main() { App::build() .add_defaults() .add_system(build_move_system()) - .add_system(bevy::diagnostics::build_fps_printer_system()) + .add_default_diagnostics() + .print_diagnostics(std::time::Duration::from_secs_f64(1.0)) .setup_world(setup) .run(); } diff --git a/examples/instancing_old.rs b/examples/instancing_old.rs index 1792cf9441..890c819221 100644 --- a/examples/instancing_old.rs +++ b/examples/instancing_old.rs @@ -24,7 +24,6 @@ fn main() { .add_system(build_wander_system()) .add_system(build_navigate_system()) .add_system(build_move_system()) - .add_system(bevy::diagnostics::build_fps_printer_system()) .run(); } diff --git a/examples/spawner.rs b/examples/spawner.rs index f52e99a7da..e5750d375f 100644 --- a/examples/spawner.rs +++ b/examples/spawner.rs @@ -5,7 +5,8 @@ fn main() { App::build() .add_defaults() .add_system(build_move_system()) - .add_system(bevy::diagnostics::build_fps_printer_system()) + .add_default_diagnostics() + .print_diagnostics(std::time::Duration::from_secs_f64(1.0)) .setup_world(setup) .run(); } diff --git a/examples/ui_bench.rs b/examples/ui_bench.rs index 3e135620de..825bcf8ad1 100644 --- a/examples/ui_bench.rs +++ b/examples/ui_bench.rs @@ -5,7 +5,8 @@ fn main() { .add_defaults() .setup_world(setup) .add_system(build_move_system()) - .add_system(bevy::diagnostics::build_fps_printer_system()) + .add_default_diagnostics() + .print_diagnostics(std::time::Duration::from_secs_f64(1.0)) .run(); } diff --git a/src/app/app_builder.rs b/src/app/app_builder.rs index d642d75e11..793987ddb3 100644 --- a/src/app/app_builder.rs +++ b/src/app/app_builder.rs @@ -9,7 +9,7 @@ use crate::{ draw_target::draw_targets::*, mesh::Mesh, pass::passes::*, pipeline::pipelines::*, render_resource::resource_providers::*, renderer::Renderer, texture::Texture, *, }, - ui, + ui, diagnostic::{Diagnostics, diagnostics}, }; use bevy_transform::{prelude::LocalToWorld, transform_system_bundle}; @@ -20,7 +20,7 @@ use render_resource::{ EntityRenderResourceAssignments, RenderResourceAssignments, }; use shader::Shader; -use std::collections::HashMap; +use std::{time::Duration, collections::HashMap}; pub struct AppBuilder { pub world: Option, @@ -160,6 +160,7 @@ impl AppBuilder { pub fn add_default_resources(&mut self) -> &mut Self { let resources = self.resources.as_mut().unwrap(); resources.insert(Time::new()); + resources.insert(Diagnostics::default()); resources.insert(AssetStorage::::new()); resources.insert(AssetStorage::::new()); resources.insert(AssetStorage::::new()); @@ -173,6 +174,18 @@ impl AppBuilder { self } + pub fn add_default_diagnostics(&mut self) -> &mut Self { + let frame_time_diagnostic_system = { + let resources = self.resources.as_mut().unwrap(); + diagnostics::frame_time_diagnostic_system(resources, 10) + }; + self.add_system(frame_time_diagnostic_system) + } + + pub fn print_diagnostics(&mut self, wait: Duration) -> &mut Self { + self.add_system(diagnostics::print_diagnostics_system(wait)) + } + pub fn batch_types2(&mut self) -> &mut Self where T1: 'static, diff --git a/src/core/time.rs b/src/core/time.rs index 2070f58a55..ff7711bf5f 100644 --- a/src/core/time.rs +++ b/src/core/time.rs @@ -3,6 +3,7 @@ use std::time::{Duration, Instant}; pub struct Time { pub delta: Duration, pub instant: Instant, + pub delta_seconds_f64: f64, pub delta_seconds: f32, } @@ -11,6 +12,7 @@ impl Time { Time { delta: Duration::from_secs(0), instant: Instant::now(), + delta_seconds_f64: 0.0, delta_seconds: 0.0, } } @@ -21,7 +23,8 @@ impl Time { pub fn stop(&mut self) { self.delta = Instant::now() - self.instant; - self.delta_seconds = - self.delta.as_secs() as f32 + (self.delta.subsec_nanos() as f32 / 1.0e9); + self.delta_seconds_f64 = + self.delta.as_secs() as f64 + (self.delta.subsec_nanos() as f64 / 1.0e9); + self.delta_seconds = self.delta_seconds_f64 as f32; } } diff --git a/src/diagnostic/diagnostics.rs b/src/diagnostic/diagnostics.rs new file mode 100644 index 0000000000..20a684748a --- /dev/null +++ b/src/diagnostic/diagnostics.rs @@ -0,0 +1,94 @@ +use super::{Diagnostic, DiagnosticId, Diagnostics}; +use crate::{ + core::Time, + prelude::{Resources, SystemBuilder}, +}; +use legion::prelude::Schedulable; +use std::time::Duration; +use uuid::Uuid; + +pub const FPS: DiagnosticId = DiagnosticId(Uuid::from_bytes([ + 157, 191, 0, 72, 223, 223, 70, 128, 137, 117, 54, 177, 132, 13, 170, 124, +])); + +pub const FRAME_TIME: DiagnosticId = DiagnosticId(Uuid::from_bytes([ + 216, 184, 55, 12, 28, 116, 69, 201, 187, 137, 176, 77, 83, 89, 251, 241, +])); + +pub fn frame_time_diagnostic_system( + resources: &Resources, + max_history_length: usize, +) -> Box { + let mut diagnostics = resources.get_mut::().unwrap(); + diagnostics.add(Diagnostic::new( + FRAME_TIME, + "frame_time", + max_history_length, + )); + diagnostics.add(Diagnostic::new(FPS, "fps", max_history_length)); + SystemBuilder::new("FrameTimeDiagnostic") + .read_resource::