Skip to content

Commit 1fb348f

Browse files
feat(profiler): Re-add puffin profiler (F10) (#908)
Re add puffin profiler. Haven't added any new scopes / fleshed anything out, but got it handy at least for now.
1 parent 3ffeaf0 commit 1fb348f

File tree

5 files changed

+110
-4
lines changed

5 files changed

+110
-4
lines changed

Cargo.lock

+51-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ bones_bevy_renderer = { git = "https://github.com/fishfolk/bones" }
2020
bevy_tasks = "0.11"
2121
turborand = { version = "0.10.0", features = ["atomic"] }
2222
tracing = "0.1.37"
23-
puffin = "0.16.0"
23+
puffin = "0.17.0"
24+
puffin_egui = "0.23.0"
2425
petgraph = "0.6.4"
2526
rapier2d = { version = "0.17.2", features = ["debug-render", "enhanced-determinism"] }
2627
indexmap = "2.0.0"

src/main.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub mod core;
1717
pub mod debug;
1818
pub mod fullscreen;
1919
pub mod input;
20+
pub mod profiler;
2021
pub mod sessions;
2122
pub mod settings;
2223
pub mod ui;
@@ -126,6 +127,7 @@ fn main() {
126127
.install_plugin(input::game_plugin)
127128
.install_plugin(core::game_plugin)
128129
.install_plugin(debug::game_plugin)
130+
.install_plugin(profiler::game_plugin)
129131
// We initialize the asset server and register asset types
130132
.init_shared_resource::<AssetServer>()
131133
.register_default_assets();

src/profiler.rs

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! Game plugin for performance profiling tools.
2+
3+
use crate::prelude::*;
4+
5+
/// Installs profiler ui plugins
6+
pub fn game_plugin(game: &mut Game) {
7+
game.systems.add_before_system(mark_new_frame);
8+
game.sessions
9+
.create(SessionNames::PROFILER)
10+
.install_plugin(session_plugin);
11+
}
12+
13+
/// Install the profiler UI to profiler session.
14+
fn session_plugin(session: &mut Session) {
15+
session
16+
.stages
17+
.add_system_to_stage(CoreStage::First, profiler);
18+
}
19+
20+
#[derive(HasSchema, Clone, Debug, Default)]
21+
struct ProfilerState {
22+
pub show_window: bool,
23+
}
24+
25+
fn profiler(
26+
ctx: ResMut<EguiCtx>,
27+
localization: Localization<GameMeta>,
28+
mut state: ResMutInit<ProfilerState>,
29+
keyboard_inputs: Res<KeyboardInputs>,
30+
) {
31+
puffin::set_scopes_on(true);
32+
33+
let toggle_window = keyboard_inputs
34+
.key_events
35+
.iter()
36+
.any(|x| x.key_code.option() == Some(KeyCode::F10) && !x.button_state.pressed());
37+
38+
let show_window = &mut state.show_window;
39+
if toggle_window {
40+
*show_window = !*show_window;
41+
}
42+
43+
egui::Window::new(localization.get("profiler"))
44+
.id(egui::Id::new("profiler"))
45+
.open(show_window)
46+
.show(&ctx, |ui| {
47+
puffin_egui::profiler_ui(ui);
48+
});
49+
}
50+
51+
/// Notify profilers we are at frame boundary
52+
pub fn mark_new_frame(_game: &mut Game) {
53+
puffin::GlobalProfiler::lock().new_frame();
54+
}

src/sessions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ impl SessionNames {
77
pub const GAME: &'static str = "game";
88
pub const MAIN_MENU: &'static str = "main_menu";
99
pub const PAUSE_MENU: &'static str = "pause_menu";
10+
pub const PROFILER: &'static str = "profiler";
1011
}
1112

1213
pub trait SessionExt {

0 commit comments

Comments
 (0)