summaryrefslogtreecommitdiff
path: root/engine
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-10-13 21:57:35 +0200
committerHampusM <hampus@hampusmat.com>2024-10-13 21:57:35 +0200
commit5e232a891c45d84d89f814eee84faeb12df0f052 (patch)
tree31db2efa2febc663cfcfe024edee6a4682a0cca3 /engine
parent04ea6ebc3f0f0f5277b402fab1557da617e273ea (diff)
feat(engine): add extension logging frame time
Diffstat (limited to 'engine')
-rw-r--r--engine/src/lib.rs1
-rw-r--r--engine/src/performance.rs59
2 files changed, 60 insertions, 0 deletions
diff --git a/engine/src/lib.rs b/engine/src/lib.rs
index c87f7f7..36e9285 100644
--- a/engine/src/lib.rs
+++ b/engine/src/lib.rs
@@ -34,6 +34,7 @@ pub mod lighting;
pub mod material;
pub mod math;
pub mod mesh;
+pub mod performance;
pub mod projection;
pub mod renderer;
pub mod shader;
diff --git a/engine/src/performance.rs b/engine/src/performance.rs
new file mode 100644
index 0000000..ffc5c27
--- /dev/null
+++ b/engine/src/performance.rs
@@ -0,0 +1,59 @@
+use std::time::Instant;
+
+use ecs::component::local::Local;
+use ecs::system::{Into, System};
+use ecs::Component;
+
+use crate::event::PostPresent as PostPresentEvent;
+
+#[derive(Debug, Default)]
+#[non_exhaustive]
+pub struct Extension {}
+
+impl ecs::extension::Extension for Extension
+{
+ fn collect(self, mut collector: ecs::extension::Collector<'_>)
+ {
+ collector.add_system(
+ PostPresentEvent,
+ log_perf.into_system().initialize((State::default(),)),
+ );
+ }
+}
+
+#[cfg(feature = "debug")]
+macro_rules! log_perf {
+ ($($tt: tt)*) => {
+ tracing::info!($($tt)*);
+ };
+}
+
+#[cfg(not(feature = "debug"))]
+macro_rules! log_perf {
+ ($($tt: tt)*) => {
+ println!($($tt)*);
+ };
+}
+
+fn log_perf(mut state: Local<State>)
+{
+ let Some(last_time) = state.last_time else {
+ state.last_time = Some(Instant::now());
+ return;
+ };
+
+ let time_now = Instant::now();
+
+ state.last_time = Some(time_now);
+
+ log_perf!(
+ "Frame time: {}us",
+ time_now.duration_since(last_time).as_micros()
+ );
+}
+
+#[derive(Debug, Default, Component)]
+struct State
+{
+ last_time: Option<Instant>,
+}