mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
Use profiling crate to support more profiler backends (#5150)
Hey! I am not sure if this is something that's been considered before and decided against (I couldn't find any PR's or issues). This change removes the internal profiling macros in library crates and the `puffin` feature and replaces it with similar functions in the [profiling](https://github.com/aclysma/profiling) crate. This crate provides a layer of abstraction over various profiler instrumentation crates and allows library users to pick their favorite (supported) profiler. An additional benefit for puffin users is that dependencies of egui are included in the instrumentation output too (mainly wgpu which uses the profiling crate), so more details might be available when profiling. A breaking change is that instead of using the `puffin` feature on egui, users that want to profile the crate with puffin instead have to enable the `profile-with-puffin` feature on the profiling crate. Similarly they could instead choose to use `profile-with-tracy` etc. I tried to add a 'tracy' feature to egui_demo_app in order to showcase , however the /scripts/check.sh currently breaks on mutually exclusive features (which this introduces), so I decided against including it for the initial PR. I'm happy to iterate more on this if there is interest in taking this PR though. Screenshot showing the additional info for wgpu now available when using puffin 
This commit is contained in:
@@ -102,7 +102,7 @@ impl<'app> WgpuWinitApp<'app> {
|
||||
native_options: NativeOptions,
|
||||
app_creator: AppCreator<'app>,
|
||||
) -> Self {
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
#[cfg(feature = "__screenshot")]
|
||||
assert!(
|
||||
@@ -181,8 +181,7 @@ impl<'app> WgpuWinitApp<'app> {
|
||||
window: Window,
|
||||
builder: ViewportBuilder,
|
||||
) -> crate::Result<&mut WgpuWinitRunning<'app>> {
|
||||
crate::profile_function!();
|
||||
|
||||
profiling::function_scope!();
|
||||
#[allow(unsafe_code, unused_mut, unused_unsafe)]
|
||||
let mut painter = egui_wgpu::winit::Painter::new(
|
||||
egui_ctx.clone(),
|
||||
@@ -199,7 +198,7 @@ impl<'app> WgpuWinitApp<'app> {
|
||||
let window = Arc::new(window);
|
||||
|
||||
{
|
||||
crate::profile_scope!("set_window");
|
||||
profiling::scope!("set_window");
|
||||
pollster::block_on(painter.set_window(ViewportId::ROOT, Some(window.clone())))?;
|
||||
}
|
||||
|
||||
@@ -268,7 +267,7 @@ impl<'app> WgpuWinitApp<'app> {
|
||||
raw_window_handle: window.window_handle().map(|h| h.as_raw()),
|
||||
};
|
||||
let app = {
|
||||
crate::profile_scope!("user_app_creator");
|
||||
profiling::scope!("user_app_creator");
|
||||
app_creator(&cc).map_err(crate::Error::AppCreation)?
|
||||
};
|
||||
|
||||
@@ -490,7 +489,7 @@ impl<'app> WinitApp for WgpuWinitApp<'app> {
|
||||
|
||||
impl<'app> WgpuWinitRunning<'app> {
|
||||
fn save_and_destroy(&mut self) {
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
let mut shared = self.shared.borrow_mut();
|
||||
if let Some(Viewport { window, .. }) = shared.viewports.get(&ViewportId::ROOT) {
|
||||
@@ -508,7 +507,7 @@ impl<'app> WgpuWinitRunning<'app> {
|
||||
|
||||
/// This is called both for the root viewport, and all deferred viewports
|
||||
fn run_ui_and_paint(&mut self, window_id: WindowId) -> Result<EventResult> {
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
let Some(viewport_id) = self
|
||||
.shared
|
||||
@@ -520,8 +519,7 @@ impl<'app> WgpuWinitRunning<'app> {
|
||||
return Ok(EventResult::Wait);
|
||||
};
|
||||
|
||||
#[cfg(feature = "puffin")]
|
||||
puffin::GlobalProfiler::lock().new_frame();
|
||||
profiling::finish_frame!();
|
||||
|
||||
let Self {
|
||||
app,
|
||||
@@ -533,7 +531,7 @@ impl<'app> WgpuWinitRunning<'app> {
|
||||
frame_timer.start();
|
||||
|
||||
let (viewport_ui_cb, raw_input) = {
|
||||
crate::profile_scope!("Prepare");
|
||||
profiling::scope!("Prepare");
|
||||
let mut shared_lock = shared.borrow_mut();
|
||||
|
||||
let SharedState {
|
||||
@@ -577,7 +575,7 @@ impl<'app> WgpuWinitRunning<'app> {
|
||||
egui_winit::update_viewport_info(info, &integration.egui_ctx, window, false);
|
||||
|
||||
{
|
||||
crate::profile_scope!("set_window");
|
||||
profiling::scope!("set_window");
|
||||
pollster::block_on(painter.set_window(viewport_id, Some(window.clone())))?;
|
||||
}
|
||||
|
||||
@@ -719,7 +717,7 @@ impl<'app> WgpuWinitRunning<'app> {
|
||||
if window.is_minimized() == Some(true) {
|
||||
// On Mac, a minimized Window uses up all CPU:
|
||||
// https://github.com/emilk/egui/issues/325
|
||||
crate::profile_scope!("minimized_sleep");
|
||||
profiling::scope!("minimized_sleep");
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
@@ -846,7 +844,7 @@ impl Viewport {
|
||||
return; // we already have one
|
||||
}
|
||||
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
let viewport_id = self.ids.this;
|
||||
|
||||
@@ -887,7 +885,7 @@ fn create_window(
|
||||
storage: Option<&dyn Storage>,
|
||||
native_options: &mut NativeOptions,
|
||||
) -> Result<(Window, ViewportBuilder), winit::error::OsError> {
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
let window_settings = epi_integration::load_window_settings(storage);
|
||||
let viewport_builder = epi_integration::viewport_builder(
|
||||
@@ -908,7 +906,7 @@ fn render_immediate_viewport(
|
||||
shared: &RefCell<SharedState>,
|
||||
immediate_viewport: ImmediateViewport<'_>,
|
||||
) {
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
let ImmediateViewport {
|
||||
ids,
|
||||
@@ -988,7 +986,7 @@ fn render_immediate_viewport(
|
||||
};
|
||||
|
||||
{
|
||||
crate::profile_scope!("set_window");
|
||||
profiling::scope!("set_window");
|
||||
if let Err(err) = pollster::block_on(painter.set_window(ids.this, Some(window.clone()))) {
|
||||
log::error!(
|
||||
"when rendering viewport_id={:?}, set_window Error {err}",
|
||||
@@ -1096,7 +1094,7 @@ fn initialize_or_update_viewport<'a>(
|
||||
viewport_ui_cb: Option<Arc<dyn Fn(&egui::Context) + Send + Sync>>,
|
||||
painter: &mut egui_wgpu::winit::Painter,
|
||||
) -> &'a mut Viewport {
|
||||
crate::profile_function!();
|
||||
profiling::function_scope!();
|
||||
|
||||
if builder.icon.is_none() {
|
||||
// Inherit icon from parent
|
||||
|
||||
Reference in New Issue
Block a user