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

Connect to tracy if the tracy feature is enabled #612

Merged
merged 6 commits into from
Oct 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
127 changes: 125 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions masonry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ all-features = true
# rustdoc-scrape-examples tracking issue https://github.com/rust-lang/rust/issues/88791
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]

[features]
default = []
# Enables tracing using tracy if the default Masonry tracing is used.
# https://github.com/wolfpld/tracy can be connected to when this feature is enabled.
tracy = [
"tracing-tracy/enable",
"dep:tracing-tracy",
"dep:wgpu-profiler",
"wgpu-profiler/tracy",
"vello/wgpu-profiler",
]

[lints]
workspace = true

Expand All @@ -42,6 +54,8 @@ time = { workspace = true, features = ["macros", "formatting"] }
cursor-icon = "1.1.0"
dpi.workspace = true
nv-flip.workspace = true
tracing-tracy = { version = "0.11.3", optional = true }
wgpu-profiler = { optional = true, version = "0.17.0", default-features = false }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web-time.workspace = true
Expand Down
9 changes: 9 additions & 0 deletions masonry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

</div>

[tracing_tracy]: https://crates.io/crates/tracing-tracy

<!-- cargo-rdme start -->

Masonry gives you a platform to create windows (using [winit] as a backend) each with a tree of widgets. It also gives you tools to inspect that widget tree at runtime, write unit tests on it, and generally have an easier time debugging and maintaining your app.
Expand Down Expand Up @@ -90,6 +92,13 @@ fn main() {
}
```

### Create feature flags

The following feature flags are available:

- `tracy`: Enables creating output for the [Tracy](https://github.com/wolfpld/tracy) profiler using [`tracing-tracy`][tracing_tracy].
This can be used by installing Tracy and connecting to a Masonry with this feature enabled.

[winit]: https://crates.io/crates/winit
[Druid]: https://crates.io/crates/druid
[Xilem]: https://crates.io/crates/xilem
Expand Down
30 changes: 29 additions & 1 deletion masonry/src/event_loop_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ pub struct MasonryState<'a> {
// TODO: Winit doesn't seem to let us create these proxies from within the loop
// The reasons for this are unclear
proxy: EventLoopProxy,
#[cfg(feature = "tracy")]
frame: Option<tracing_tracy::client::Frame>,

// Per-Window state
// In future, this will support multiple windows
Expand Down Expand Up @@ -236,6 +238,8 @@ impl MasonryState<'_> {
},
),
renderer: None,
#[cfg(feature = "tracy")]
frame: None,
pointer_state: PointerState::empty(),
proxy: event_loop.create_proxy(),

Expand Down Expand Up @@ -392,12 +396,32 @@ impl MasonryState<'_> {
{
let _render_span = tracing::info_span!("Rendering using Vello").entered();
self.renderer
.get_or_insert_with(|| Renderer::new(device, renderer_options).unwrap())
.get_or_insert_with(|| {
// Should be `expect`, when we up our MSRV.
#[cfg_attr(not(feature = "tracy"), allow(unused_mut))]
let mut renderer = Renderer::new(device, renderer_options).unwrap();
#[cfg(feature = "tracy")]
{
let new_profiler = wgpu_profiler::GpuProfiler::new_with_tracy_client(
wgpu_profiler::GpuProfilerSettings::default(),
// We don't have access to the adapter until we get https://github.com/linebender/vello/pull/634
// Luckily, this `backend` is only used for visual display in the profiling, so we can just guess here
wgpu::Backend::Vulkan,
device,
queue,
)
.unwrap_or(renderer.profiler);
renderer.profiler = new_profiler;
}
renderer
})
.render_to_surface(device, queue, scene_ref, &surface_texture, &render_params)
.expect("failed to render to surface");
}
surface_texture.present();
device.poll(wgpu::Maintain::Wait);
#[cfg(feature = "tracy")]
drop(self.frame.take());
}

// --- MARK: WINDOW_EVENT ---
Expand All @@ -420,6 +444,10 @@ impl MasonryState<'_> {
);
return;
};
#[cfg(feature = "tracy")]
if self.frame.is_none() {
self.frame = Some(tracing_tracy::client::non_continuous_frame!("Masonry"));
}
accesskit_adapter.process_event(window, &event);

match event {
Expand Down
7 changes: 7 additions & 0 deletions masonry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@
//! }
//! ```
//!
//! ## Create feature flags
//!
//! The following feature flags are available:
//!
//! - `tracy`: Enables creating output for the [Tracy](https://github.com/wolfpld/tracy) profiler using [`tracing-tracy`][tracing_tracy].
//! This can be used by installing Tracy and connecting to a Masonry with this feature enabled.
//!
//! [winit]: https://crates.io/crates/winit
//! [Druid]: https://crates.io/crates/druid
//! [Xilem]: https://crates.io/crates/xilem
Expand Down
4 changes: 4 additions & 0 deletions masonry/src/tracing_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ fn try_init_layered_tracing(default_level: LevelFilter) -> Result<(), SetGlobalD
#[cfg(target_os = "android")]
let registry = registry.with(android_trace_layer);

// After the above line because of https://github.com/linebender/android_trace/pull/17
#[cfg(feature = "tracy")]
let registry = registry.with(tracing_tracy::TracyLayer::default());

tracing::dispatcher::set_global_default(registry.into())?;

if let Some(err) = env_var_error {
Expand Down