Profiling support (#743)

This commit is contained in:
Thierry Berger
2024-11-19 16:33:26 +01:00
committed by GitHub
parent ff79f4c674
commit 510237cc29
56 changed files with 185 additions and 635 deletions

View File

@@ -28,6 +28,7 @@ pub struct Box2dWorld {
}
impl Box2dWorld {
#[profiling::function]
pub fn from_rapier(
gravity: Vector2<f32>,
bodies: &RigidBodySet,
@@ -221,6 +222,7 @@ impl Box2dWorld {
}
}
#[profiling::function]
pub fn step(&mut self, counters: &mut Counters, params: &IntegrationParameters) {
counters.step_started();
self.world
@@ -228,6 +230,7 @@ impl Box2dWorld {
counters.step_completed();
}
#[profiling::function]
pub fn sync(&self, bodies: &mut RigidBodySet, colliders: &mut ColliderSet) {
for (handle, body) in bodies.iter_mut() {
if let Some(pb2_handle) = self.rapier2box2d.get(&handle) {

View File

@@ -18,7 +18,7 @@ use rand_pcg::Pcg32;
use std::collections::HashMap;
#[cfg(feature = "dim2")]
pub type BevyMaterial = ColorMaterial;
pub type BevyMaterial = bevy_sprite::ColorMaterial;
#[cfg(feature = "dim3")]
pub type BevyMaterial = StandardMaterial;

View File

@@ -206,6 +206,7 @@ impl Harness {
self.step_with_graphics(None);
}
#[profiling::function]
pub fn step_with_graphics(&mut self, mut graphics: Option<&mut TestbedGraphics>) {
#[cfg(feature = "parallel")]
{

View File

@@ -43,7 +43,7 @@ impl EntityWithGraphics {
}
#[cfg(feature = "dim2")]
let selection_material = ColorMaterial {
let selection_material = bevy_sprite::ColorMaterial {
color: Color::from(Srgba::rgb(1.0, 0.0, 0.0)),
texture: None,
};
@@ -109,7 +109,7 @@ impl EntityWithGraphics {
}
#[cfg(feature = "dim2")]
let material = ColorMaterial {
let material = bevy_sprite::ColorMaterial {
color: bevy_color,
texture: None,
};

View File

@@ -54,6 +54,7 @@ impl PhysicsSnapshot {
})
}
#[profiling::function]
pub fn restore(&self) -> bincode::Result<DeserializedPhysicsSnapshot> {
Ok(DeserializedPhysicsSnapshot {
timestep_id: self.timestep_id,

View File

@@ -147,6 +147,7 @@ impl Drop for PhysxWorld {
}
impl PhysxWorld {
#[profiling::function]
pub fn from_rapier(
gravity: Vector3<f32>,
integration_parameters: &IntegrationParameters,

View File

@@ -249,6 +249,9 @@ impl TestbedApp {
}
pub fn run_with_init(mut self, mut init: impl FnMut(&mut App)) {
#[cfg(feature = "profiling")]
puffin_egui::puffin::set_scopes_on(true);
let mut args = env::args();
let mut benchmark_mode = false;
@@ -885,6 +888,10 @@ impl<'a, 'b, 'c, 'd, 'e, 'f> Testbed<'a, 'b, 'c, 'd, 'e, 'f> {
}
fn handle_common_events(&mut self, events: &ButtonInput<KeyCode>) {
// C can be used to write within profiling filter.
if events.pressed(KeyCode::ControlLeft) || events.pressed(KeyCode::ControlRight) {
return;
}
for key in events.get_just_released() {
match *key {
KeyCode::KeyT => {
@@ -1184,6 +1191,8 @@ fn update_testbed(
),
keys: Res<ButtonInput<KeyCode>>,
) {
profiling::finish_frame!();
let meshes = &mut *meshes;
let materials = &mut *materials;
@@ -1210,7 +1219,9 @@ fn update_testbed(
plugins: &mut plugins,
};
testbed.handle_common_events(&keys);
if !ui_context.ctx_mut().wants_keyboard_input() {
testbed.handle_common_events(&keys);
}
testbed.update_character_controller(&keys);
#[cfg(feature = "dim3")]
{

View File

@@ -21,6 +21,41 @@ pub fn update_ui(
harness: &mut Harness,
debug_render: &mut DebugRenderPipelineResource,
) {
#[cfg(feature = "profiling")]
{
let window = egui::Window::new("Profiling");
let window = window.default_open(false);
#[cfg(feature = "unstable-puffin-pr-235")]
{
use std::sync::Once;
static START: Once = Once::new();
fn set_default_rapier_filter() {
let mut profile_ui = puffin_egui::PROFILE_UI.lock();
profile_ui
.profiler_ui
.flamegraph_options
.scope_name_filter
.set_filter("Harness::step_with_graphics".to_string());
}
START.call_once(|| {
set_default_rapier_filter();
});
window.show(ui_context.ctx_mut(), |ui| {
if ui.button("🔍 Rapier filter").clicked() {
set_default_rapier_filter();
}
puffin_egui::profiler_ui(ui);
});
}
#[cfg(not(feature = "unstable-puffin-pr-235"))]
window.show(ui_context.ctx_mut(), |ui| {
puffin_egui::profiler_ui(ui);
});
}
egui::Window::new("Parameters").show(ui_context.ctx_mut(), |ui| {
if state.backend_names.len() > 1 && !state.example_names.is_empty() {
let mut changed = false;