Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(profiler): Re-add puffin profiler (F10) #908

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading