Repository: bevyengine/bevy
File: crates/bevy_dev_tools/src/frame_time_graph/mod.rs (Rust)

Code before:

/// Plugin that sets up everything to render the frame time graph material
pub struct FrameTimeGraphPlugin;

impl Plugin for FrameTimeGraphPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        load_internal_asset!(
            app,
            FRAME_TIME_GRAPH_SHADER_HANDLE,
            "frame_time_graph.wgsl",
            Shader::from_wgsl
        );

        // TODO: Use plugin dependencies, see https://github.com/bevyengine/bevy/issues/69
        if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
            panic!("Requires FrameTimeDiagnosticsPlugin");
            // app.add_plugins(FrameTimeDiagnosticsPlugin);
        }

        app.add_plugins(UiMaterialPlugin::<FrametimeGraphMaterial>::default())
            .add_systems(Update, update_frame_time_values);
    }
}

/// The config values sent to the frame time graph shader
#[derive(Debug, Clone, Copy, ShaderType)]
pub struct FrameTimeGraphConfigUniform {
    // minimum expected delta time
    dt_min: f32,
    // maximum expected delta time
    dt_max: f32,
    dt_min_log2: f32,
    dt_max_log2: f32,
    // controls whether or not the bars width are proportional to their delta time
    proportional_width: u32,
}

impl FrameTimeGraphConfigUniform {
    /// `proportional_width`: controls whether or not the bars width are proportional to their delta time
    pub fn new(target_fps: f32, min_fps: f32, proportional_width: bool) -> Self {
        // we want an upper limit that is above the target otherwise the bars will disappear
        let dt_min = 1. / (target_fps * 1.2);
        let dt_max = 1. / min_fps;
        Self {
            dt_min,
            dt_max,
            dt_min_log2: log2(dt_min),
            dt_max_log2: log2(dt_max),
            proportional_width: u32::from(proportional_width),
        }
    }

Review comment (suggestion):
I don't think there are any remaining problems, but I'm a bit unclear as to whether `FrameTimeGraphPlugin` is meant to be able to run independently of `FpsOverlayPlugin` or not. I guess it doesn't matter for this PR.

Code after:

const FRAME_TIME_GRAPH_SHADER_HANDLE: Handle<Shader> =
    uuid_handle!("4e38163a-5782-47a5-af52-d9161472ab59");

/// Plugin that sets up everything to render the frame time graph material
pub struct FrameTimeGraphPlugin;

impl Plugin for FrameTimeGraphPlugin {
    fn build(&self, app: &mut bevy_app::App) {
        load_internal_asset!(
            app,
            FRAME_TIME_GRAPH_SHADER_HANDLE,
            "frame_time_graph.wgsl",
            Shader::from_wgsl
        );

        // TODO: Use plugin dependencies, see https://github.com/bevyengine/bevy/issues/69
        if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
            panic!("Requires FrameTimeDiagnosticsPlugin");
            // app.add_plugins(FrameTimeDiagnosticsPlugin);
        }

        app.add_plugins(UiMaterialPlugin::<FrametimeGraphMaterial>::default())
            .add_systems(
                Update,
                update_frame_time_values.in_set(FpsOverlaySystems::UpdateText),
            );
    }
}

/// The config values sent to the frame time graph shader
#[derive(Debug, Clone, Copy, ShaderType)]
pub struct FrameTimeGraphConfigUniform {
    // minimum expected delta time
    dt_min: f32,
    // maximum expected delta time
    dt_max: f32,
    dt_min_log2: f32,
    dt_max_log2: f32,
    // controls whether or not the bars width are proportional to their delta time
    proportional_width: u32,
}

impl FrameTimeGraphConfigUniform {
    /// `proportional_width`: controls whether or not the bars width are proportional to their delta time
    pub fn new(target_fps: f32, min_fps: f32, proportional_width: bool) -> Self {
        // we want an upper limit that is above the target otherwise the bars will disappear
        let dt_min = 1. / (target_fps * 1.2);
        let dt_max = 1. / min_fps;
        Self {
            dt_min,