1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -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

![image](https://github.com/user-attachments/assets/49fc0e7e-8f88-40cb-a69e-74ca2e3f90f3)
This commit is contained in:
Ted de Munnik
2024-12-16 09:15:54 +01:00
committed by GitHub
parent 9aae14cdf4
commit 3af907919b
46 changed files with 272 additions and 482 deletions

View File

@@ -110,33 +110,3 @@ pub fn check_for_gl_error_impl(gl: &glow::Context, file: &str, line: u32, contex
}
}
}
// ---------------------------------------------------------------------------
mod profiling_scopes {
#![allow(unused_macros)]
#![allow(unused_imports)]
/// Profiling macro for feature "puffin"
macro_rules! profile_function {
($($arg: tt)*) => {
#[cfg(feature = "puffin")]
#[cfg(not(target_arch = "wasm32"))] // Disabled on web because of the coarse 1ms clock resolution there.
puffin::profile_function!($($arg)*);
};
}
pub(crate) use profile_function;
/// Profiling macro for feature "puffin"
macro_rules! profile_scope {
($($arg: tt)*) => {
#[cfg(feature = "puffin")]
#[cfg(not(target_arch = "wasm32"))] // Disabled on web because of the coarse 1ms clock resolution there.
puffin::profile_scope!($($arg)*);
};
}
pub(crate) use profile_scope;
}
#[allow(unused_imports)]
pub(crate) use profiling_scopes::{profile_function, profile_scope};

View File

@@ -144,7 +144,7 @@ impl Painter {
shader_version: Option<ShaderVersion>,
dithering: bool,
) -> Result<Self, PainterError> {
crate::profile_function!();
profiling::function_scope!();
crate::check_for_gl_error_even_in_release!(&gl, "before Painter::new");
// some useful debug info. all three of them are present in gl 1.1.
@@ -366,7 +366,7 @@ impl Painter {
clipped_primitives: &[egui::ClippedPrimitive],
textures_delta: &egui::TexturesDelta,
) {
crate::profile_function!();
profiling::function_scope!();
for (id, image_delta) in &textures_delta.set {
self.set_texture(*id, image_delta);
@@ -405,7 +405,7 @@ impl Painter {
pixels_per_point: f32,
clipped_primitives: &[egui::ClippedPrimitive],
) {
crate::profile_function!();
profiling::function_scope!();
self.assert_not_destroyed();
unsafe { self.prepare_painting(screen_size_px, pixels_per_point) };
@@ -423,7 +423,7 @@ impl Painter {
}
Primitive::Callback(callback) => {
if callback.rect.is_positive() {
crate::profile_scope!("callback");
profiling::scope!("callback");
let info = egui::PaintCallbackInfo {
viewport: callback.rect,
@@ -508,7 +508,7 @@ impl Painter {
// ------------------------------------------------------------------------
pub fn set_texture(&mut self, tex_id: egui::TextureId, delta: &egui::epaint::ImageDelta) {
crate::profile_function!();
profiling::function_scope!();
self.assert_not_destroyed();
@@ -540,7 +540,7 @@ impl Painter {
);
let data: Vec<u8> = {
crate::profile_scope!("font -> sRGBA");
profiling::scope!("font -> sRGBA");
image
.srgba_pixels(None)
.flat_map(|a| a.to_array())
@@ -559,7 +559,7 @@ impl Painter {
options: egui::TextureOptions,
data: &[u8],
) {
crate::profile_function!();
profiling::function_scope!();
assert_eq!(data.len(), w * h * 4);
assert!(
w <= self.max_texture_side && h <= self.max_texture_side,
@@ -610,7 +610,7 @@ impl Painter {
let level = 0;
if let Some([x, y]) = pos {
crate::profile_scope!("gl.tex_sub_image_2d");
profiling::scope!("gl.tex_sub_image_2d");
self.gl.tex_sub_image_2d(
glow::TEXTURE_2D,
level,
@@ -625,7 +625,7 @@ impl Painter {
check_for_gl_error!(&self.gl, "tex_sub_image_2d");
} else {
let border = 0;
crate::profile_scope!("gl.tex_image_2d");
profiling::scope!("gl.tex_image_2d");
self.gl.tex_image_2d(
glow::TEXTURE_2D,
level,
@@ -675,7 +675,7 @@ impl Painter {
}
pub fn read_screen_rgba(&self, [w, h]: [u32; 2]) -> egui::ColorImage {
crate::profile_function!();
profiling::function_scope!();
let mut pixels = vec![0_u8; (w * h * 4) as usize];
unsafe {
@@ -700,8 +700,7 @@ impl Painter {
}
pub fn read_screen_rgb(&self, [w, h]: [u32; 2]) -> Vec<u8> {
crate::profile_function!();
profiling::function_scope!();
let mut pixels = vec![0_u8; (w * h * 3) as usize];
unsafe {
self.gl.read_pixels(
@@ -748,7 +747,7 @@ impl Painter {
}
pub fn clear(gl: &glow::Context, screen_size_in_pixels: [u32; 2], clear_color: [f32; 4]) {
crate::profile_function!();
profiling::function_scope!();
unsafe {
gl.disable(glow::SCISSOR_TEST);