irox_egui_extras/
frame_history.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: MIT
// Modifications Copyright 2023 IROX Contributors

//!
//! Implementations around the rendering history of individual frames
//!

use egui::util::History;

///
/// Tracks information about the render history of previous frames
///
pub struct FrameHistory {
    frame_times: History<f32>,
}

impl Default for FrameHistory {
    fn default() -> Self {
        let max_age: f32 = 1.0;
        let max_len = (max_age * 300.0).round() as usize;
        Self {
            frame_times: History::new(0..max_len, max_age),
        }
    }
}

impl FrameHistory {
    // Called first
    pub fn on_new_frame(&mut self, now: f64, previous_frame_time: Option<f32>) {
        let previous_frame_time = previous_frame_time.unwrap_or_default();
        if let Some(latest) = self.frame_times.latest_mut() {
            *latest = previous_frame_time; // rewrite history now that we know
        }
        self.frame_times.add(now, previous_frame_time); // projected
    }

    #[must_use]
    pub fn mean_frame_time(&self) -> f32 {
        self.frame_times.average().unwrap_or_default()
    }

    #[must_use]
    pub fn mean_time_interval(&self) -> f32 {
        self.frame_times.mean_time_interval().unwrap_or_default()
    }

    #[must_use]
    pub fn fps(&self) -> f32 {
        1.0 / self.mean_time_interval()
    }

    #[must_use]
    pub fn max_fps(&self) -> f32 {
        1.0 / self.mean_frame_time()
    }

    pub fn ui(&mut self, ui: &mut egui::Ui) {
        let mean_frame_time = 1e3 * self.mean_frame_time();
        let mean_frame_interval = 1e3 * self.mean_time_interval();
        let avg_fps = self.fps();
        let utilization =
            (1.0 - (mean_frame_interval - mean_frame_time).max(0.0) / mean_frame_interval) * 100.0;

        ui.label("Frame Stats:");
        ui.label(format!("Count {}", ui.ctx().cumulative_pass_nr()))
            .on_hover_text("Total number of frames rendered");
        ui.label(format!("Duration {mean_frame_time:.2} ms / frame"))
            .on_hover_text(
                "Single-thread CPU time for a single frame, excluding some GPU transfer times",
            );
        ui.label(format!("Avg FPS {avg_fps:.1}")).on_hover_text("Average frames per second. With VSync, will max out at the monitor's refresh rate, usually about 60 FPS");
        ui.label(format!("Util: {utilization:.2}%"))
            .on_hover_text("Single-thread CPU Render thread utilization, lower is better");
    }

    pub fn graph(&mut self, ui: &mut egui::Ui) -> egui::Response {
        use egui::{
            emath, epaint, pos2, vec2, Align2, Pos2, Rect, Sense, Shape, Stroke, TextStyle,
        };

        ui.label("CPU usage history");

        let history = &self.frame_times;

        let height = ui.spacing().slider_width;
        let size = vec2(ui.available_size_before_wrap().x, height);
        let (rect, response) = ui.allocate_at_least(size, Sense::hover());
        let style = ui.style().noninteractive();

        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());
        shapes.push(Shape::Rect(epaint::RectShape {
            rect,
            rounding: style.rounding,
            fill: ui.visuals().extreme_bg_color,
            stroke: ui.style().noninteractive().bg_stroke,
            fill_texture_id: Default::default(),
            uv: Rect::ZERO,
            blur_width: 0.0f32,
        }));

        let rect = rect.shrink(4.0);
        let color = ui.visuals().text_color();
        let line_stroke = Stroke::new(1.0, color);

        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(ui.fonts(|f| {
                Shape::text(
                    f,
                    pos2(rect.left(), y),
                    Align2::LEFT_BOTTOM,
                    text,
                    TextStyle::Monospace.resolve(ui.style()),
                    color,
                )
            }));
        }

        let circle_color = color;
        let radius = 2.0;
        let right_side_time = ui.input(|i| i.time); // Time at right side of screen

        for (time, cpu_usage) in history.iter() {
            let age = (right_side_time - time) as f32;
            let pos = to_screen.transform_pos_clamped(Pos2::new(age, cpu_usage));

            shapes.push(Shape::line_segment(
                [pos2(pos.x, rect.bottom()), pos],
                line_stroke,
            ));

            if cpu_usage < graph_top_cpu_usage {
                shapes.push(Shape::circle_filled(pos, radius, circle_color));
            }
        }

        ui.painter().extend(shapes);

        response
    }
}