2021-01-02 00:01:01 +00:00
|
|
|
use egui::util::History;
|
|
|
|
|
|
|
|
pub struct FrameHistory {
|
|
|
|
frame_times: History<f32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FrameHistory {
|
|
|
|
fn default() -> Self {
|
2021-09-20 19:36:56 +00:00
|
|
|
let max_age: f32 = 1.0;
|
|
|
|
let max_len = (max_age * 300.0).round() as usize;
|
2021-01-02 00:01:01 +00:00
|
|
|
Self {
|
2021-09-20 19:36:56 +00:00
|
|
|
frame_times: History::new(0..max_len, max_age),
|
2021-01-02 00:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FrameHistory {
|
|
|
|
// Called first
|
2021-03-09 17:57:45 +00:00
|
|
|
pub fn on_new_frame(&mut self, now: f64, previous_frame_time: Option<f32>) {
|
|
|
|
let previous_frame_time = previous_frame_time.unwrap_or_default();
|
2021-01-02 00:01:01 +00:00
|
|
|
if let Some(latest) = self.frame_times.latest_mut() {
|
2021-03-09 17:57:45 +00:00
|
|
|
*latest = previous_frame_time; // rewrite history now that we know
|
2021-01-02 00:01:01 +00:00
|
|
|
}
|
2021-03-09 17:57:45 +00:00
|
|
|
self.frame_times.add(now, previous_frame_time); // projected
|
2021-01-02 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mean_frame_time(&self) -> f32 {
|
|
|
|
self.frame_times.average().unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn fps(&self) -> f32 {
|
|
|
|
1.0 / self.frame_times.mean_time_interval().unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
|
|
|
ui.label(format!(
|
|
|
|
"Total frames painted: {}",
|
|
|
|
self.frame_times.total_count()
|
|
|
|
))
|
|
|
|
.on_hover_text("Includes this frame.");
|
|
|
|
|
|
|
|
ui.label(format!(
|
|
|
|
"Mean CPU usage: {:.2} ms / frame",
|
|
|
|
1e3 * self.mean_frame_time()
|
|
|
|
))
|
|
|
|
.on_hover_text(
|
2021-01-17 13:48:59 +00:00
|
|
|
"Includes egui layout and tessellation time.\n\
|
2021-01-02 00:01:01 +00:00
|
|
|
Does not include GPU usage, nor overhead for sending data to GPU.",
|
|
|
|
);
|
|
|
|
egui::warn_if_debug_build(ui);
|
|
|
|
|
|
|
|
egui::CollapsingHeader::new("📊 CPU usage history")
|
|
|
|
.default_open(false)
|
|
|
|
.show(ui, |ui| {
|
|
|
|
self.graph(ui);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn graph(&mut self, ui: &mut egui::Ui) -> egui::Response {
|
|
|
|
use egui::*;
|
|
|
|
|
2021-01-17 13:48:59 +00:00
|
|
|
ui.label("egui CPU usage history");
|
2021-01-02 00:01:01 +00:00
|
|
|
|
|
|
|
let history = &self.frame_times;
|
|
|
|
|
|
|
|
// TODO: we should not use `slider_width` as default graph width.
|
2021-02-01 15:55:40 +00:00
|
|
|
let height = ui.spacing().slider_width;
|
2021-08-28 14:02:16 +00:00
|
|
|
let size = vec2(ui.available_size_before_wrap().x, height);
|
2021-01-06 10:03:29 +00:00
|
|
|
let (rect, response) = ui.allocate_at_least(size, Sense::hover());
|
2021-01-02 00:01:01 +00:00
|
|
|
let style = ui.style().noninteractive();
|
|
|
|
|
2021-02-14 09:33:44 +00:00
|
|
|
let graph_top_cpu_usage = 0.010;
|
|
|
|
let graph_rect = Rect::from_x_y_ranges(history.max_age()..=0.0, graph_top_cpu_usage..=0.0);
|
|
|
|
let to_screen = emath::RectTransform::from_to(graph_rect, rect);
|
|
|
|
|
|
|
|
let mut shapes = Vec::with_capacity(3 + 2 * history.len());
|
2021-09-20 19:36:56 +00:00
|
|
|
shapes.push(Shape::Rect(epaint::RectShape {
|
2021-01-02 00:01:01 +00:00
|
|
|
rect,
|
|
|
|
corner_radius: style.corner_radius,
|
2021-02-03 00:08:23 +00:00
|
|
|
fill: ui.visuals().extreme_bg_color,
|
2021-01-02 00:01:01 +00:00
|
|
|
stroke: ui.style().noninteractive().bg_stroke,
|
2021-09-20 19:36:56 +00:00
|
|
|
}));
|
2021-01-02 00:01:01 +00:00
|
|
|
|
|
|
|
let rect = rect.shrink(4.0);
|
2021-02-03 00:08:23 +00:00
|
|
|
let color = ui.visuals().text_color();
|
|
|
|
let line_stroke = Stroke::new(1.0, color);
|
2021-01-02 00:01:01 +00:00
|
|
|
|
2021-03-06 09:48:39 +00:00
|
|
|
if let Some(pointer_pos) = response.hover_pos() {
|
|
|
|
let y = pointer_pos.y;
|
|
|
|
shapes.push(Shape::line_segment(
|
|
|
|
[pos2(rect.left(), y), pos2(rect.right(), y)],
|
|
|
|
line_stroke,
|
|
|
|
));
|
|
|
|
let cpu_usage = to_screen.inverse().transform_pos(pointer_pos).y;
|
|
|
|
let text = format!("{:.1} ms", 1e3 * cpu_usage);
|
|
|
|
shapes.push(Shape::text(
|
|
|
|
ui.fonts(),
|
|
|
|
pos2(rect.left(), y),
|
|
|
|
egui::Align2::LEFT_BOTTOM,
|
|
|
|
text,
|
|
|
|
TextStyle::Monospace,
|
|
|
|
color,
|
|
|
|
));
|
2021-01-02 00:01:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 00:08:23 +00:00
|
|
|
let circle_color = color;
|
2021-01-02 00:01:01 +00:00
|
|
|
let radius = 2.0;
|
|
|
|
let right_side_time = ui.input().time; // Time at right side of screen
|
|
|
|
|
|
|
|
for (time, cpu_usage) in history.iter() {
|
|
|
|
let age = (right_side_time - time) as f32;
|
2021-02-14 09:33:44 +00:00
|
|
|
let pos = to_screen.transform_pos_clamped(Pos2::new(age, cpu_usage));
|
2021-01-02 00:01:01 +00:00
|
|
|
|
2021-01-10 10:43:01 +00:00
|
|
|
shapes.push(Shape::line_segment(
|
2021-02-14 09:33:44 +00:00
|
|
|
[pos2(pos.x, rect.bottom()), pos],
|
2021-01-02 00:01:01 +00:00
|
|
|
line_stroke,
|
|
|
|
));
|
|
|
|
|
|
|
|
if cpu_usage < graph_top_cpu_usage {
|
2021-02-14 09:33:44 +00:00
|
|
|
shapes.push(Shape::circle_filled(pos, radius, circle_color));
|
2021-01-02 00:01:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 10:43:01 +00:00
|
|
|
ui.painter().extend(shapes);
|
2021-01-02 00:01:01 +00:00
|
|
|
|
|
|
|
response
|
|
|
|
}
|
|
|
|
}
|