Skip to content

Commit

Permalink
feat(profiler): Re-add puffin profiler (F10) (#908)
Browse files Browse the repository at this point in the history
Re add puffin profiler. Haven't added any new scopes / fleshed anything
out, but got it handy at least for now.
  • Loading branch information
MaxCWhitehead authored Feb 2, 2024
1 parent 3ffeaf0 commit 1fb348f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
54 changes: 51 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ bones_bevy_renderer = { git = "https://github.com/fishfolk/bones" }
bevy_tasks = "0.11"
turborand = { version = "0.10.0", features = ["atomic"] }
tracing = "0.1.37"
puffin = "0.16.0"
puffin = "0.17.0"
puffin_egui = "0.23.0"
petgraph = "0.6.4"
rapier2d = { version = "0.17.2", features = ["debug-render", "enhanced-determinism"] }
indexmap = "2.0.0"
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod core;
pub mod debug;
pub mod fullscreen;
pub mod input;
pub mod profiler;
pub mod sessions;
pub mod settings;
pub mod ui;
Expand Down Expand Up @@ -126,6 +127,7 @@ fn main() {
.install_plugin(input::game_plugin)
.install_plugin(core::game_plugin)
.install_plugin(debug::game_plugin)
.install_plugin(profiler::game_plugin)
// We initialize the asset server and register asset types
.init_shared_resource::<AssetServer>()
.register_default_assets();
Expand Down
54 changes: 54 additions & 0 deletions src/profiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! Game plugin for performance profiling tools.
use crate::prelude::*;

/// Installs profiler ui plugins
pub fn game_plugin(game: &mut Game) {
game.systems.add_before_system(mark_new_frame);
game.sessions
.create(SessionNames::PROFILER)
.install_plugin(session_plugin);
}

/// Install the profiler UI to profiler session.
fn session_plugin(session: &mut Session) {
session
.stages
.add_system_to_stage(CoreStage::First, profiler);
}

#[derive(HasSchema, Clone, Debug, Default)]
struct ProfilerState {
pub show_window: bool,
}

fn profiler(
ctx: ResMut<EguiCtx>,
localization: Localization<GameMeta>,
mut state: ResMutInit<ProfilerState>,
keyboard_inputs: Res<KeyboardInputs>,
) {
puffin::set_scopes_on(true);

let toggle_window = keyboard_inputs
.key_events
.iter()
.any(|x| x.key_code.option() == Some(KeyCode::F10) && !x.button_state.pressed());

let show_window = &mut state.show_window;
if toggle_window {
*show_window = !*show_window;
}

egui::Window::new(localization.get("profiler"))
.id(egui::Id::new("profiler"))
.open(show_window)
.show(&ctx, |ui| {
puffin_egui::profiler_ui(ui);
});
}

/// Notify profilers we are at frame boundary
pub fn mark_new_frame(_game: &mut Game) {
puffin::GlobalProfiler::lock().new_frame();
}
1 change: 1 addition & 0 deletions src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ impl SessionNames {
pub const GAME: &'static str = "game";
pub const MAIN_MENU: &'static str = "main_menu";
pub const PAUSE_MENU: &'static str = "pause_menu";
pub const PROFILER: &'static str = "profiler";
}

pub trait SessionExt {
Expand Down

0 comments on commit 1fb348f

Please sign in to comment.