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

First pass at using puffin to display the query results #81

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ license = "MIT OR Apache-2.0"
clippy.doc_markdown = "warn"

[features]
default=["puffin"]
Gonkalbell marked this conversation as resolved.
Show resolved Hide resolved
tracy = ["dep:tracy-client", "profiling/profile-with-tracy"]
puffin = ["dep:puffin", "profiling/profile-with-puffin"]

[lib]

Expand All @@ -23,10 +25,11 @@ thiserror = "1"
wgpu = "22.1.0"

tracy-client = { version = "0.17", optional = true }

puffin = { version = "0.19.1", optional = true }

[dev-dependencies]
futures-lite = "2"
profiling = { version = "1" }
profiling = "1"
puffin_http = "0.16.1"
tracy-client = "0.17.0"
winit = "0.30"
71 changes: 53 additions & 18 deletions examples/demo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{borrow::Cow, sync::Arc};
use std::{
borrow::Cow,
sync::{Arc, LazyLock, Mutex},
};
use wgpu_profiler::{GpuProfiler, GpuProfilerSettings, GpuTimerQueryResult};
use winit::{
application::ApplicationHandler,
Expand All @@ -7,6 +10,13 @@ use winit::{
keyboard::{KeyCode, PhysicalKey},
};

#[cfg(feature = "puffin")]
use puffin::GlobalProfiler;

Gonkalbell marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "puffin")]
static PUFFIN_GPU_PROFILER: LazyLock<Mutex<GlobalProfiler>> =
LazyLock::new(|| Mutex::new(GlobalProfiler::default()));

fn scopes_to_console_recursive(results: &[GpuTimerQueryResult], indentation: u32) {
for scope in results {
if indentation > 0 {
Expand Down Expand Up @@ -139,22 +149,26 @@ impl GfxState {

// Create a new profiler instance.
#[cfg(feature = "tracy")]
let profiler = GpuProfiler::new_with_tracy_client(
GpuProfilerSettings::default(),
adapter.get_info().backend,
&device,
&queue,
)
.unwrap_or_else(|err| match err {
wgpu_profiler::CreationError::TracyClientNotRunning
| wgpu_profiler::CreationError::TracyGpuContextCreationError(_) => {
println!("Failed to connect to Tracy. Continuing without Tracy integration.");
GpuProfiler::new(GpuProfilerSettings::default()).expect("Failed to create profiler")
}
_ => {
panic!("Failed to create profiler: {}", err);
}
});
let profiler = {
tracy_client::Client::start();
GpuProfiler::new_with_tracy_client(
GpuProfilerSettings::default(),
adapter.get_info().backend,
&device,
&queue,
)
.unwrap_or_else(|err| match err {
wgpu_profiler::CreationError::TracyClientNotRunning
| wgpu_profiler::CreationError::TracyGpuContextCreationError(_) => {
println!("Failed to connect to Tracy. Continuing without Tracy integration.");
GpuProfiler::new(GpuProfilerSettings::default())
.expect("Failed to create profiler")
}
_ => {
panic!("Failed to create profiler: {}", err);
}
})
};
#[cfg(not(feature = "tracy"))]
let profiler =
GpuProfiler::new(GpuProfilerSettings::default()).expect("Failed to create profiler");
Expand Down Expand Up @@ -254,6 +268,15 @@ impl ApplicationHandler<()> for State {
self.latest_profiler_results =
profiler.process_finished_frame(queue.get_timestamp_period());
console_output(&self.latest_profiler_results, device.features());
#[cfg(feature = "puffin")]
{
let mut gpu_profiler = PUFFIN_GPU_PROFILER.lock().unwrap();
wgpu_profiler::puffin::output_frame_to_puffin(
&mut gpu_profiler,
self.latest_profiler_results.as_deref().unwrap_or_default(),
);
gpu_profiler.new_frame();
}
}
Gonkalbell marked this conversation as resolved.
Show resolved Hide resolved

WindowEvent::KeyboardInput {
Expand Down Expand Up @@ -386,9 +409,21 @@ fn draw(
}

fn main() {
tracy_client::Client::start();
//env_logger::init_from_env(env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "warn"));
let event_loop = EventLoop::new().unwrap();
Wumpf marked this conversation as resolved.
Show resolved Hide resolved
event_loop.set_control_flow(winit::event_loop::ControlFlow::Poll);

#[cfg(feature = "puffin")]
let (_cpu_server, _gpu_server) = {
puffin::set_scopes_on(true);
let cpu_server =
puffin_http::Server::new(&format!("0.0.0.0:{}", puffin_http::DEFAULT_PORT)).unwrap();
let gpu_server = puffin_http::Server::new_custom(
&format!("0.0.0.0:{}", puffin_http::DEFAULT_PORT + 1),
|sink| PUFFIN_GPU_PROFILER.lock().unwrap().add_sink(sink),
|id| _ = PUFFIN_GPU_PROFILER.lock().unwrap().remove_sink(id),
);
(cpu_server, gpu_server)
};
let _ = event_loop.run_app(&mut State::default());
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ mod profiler_settings;
mod scope;
#[cfg(feature = "tracy")]
mod tracy;
#[cfg(feature = "puffin")]
pub mod puffin;

pub use errors::{CreationError, EndFrameError, SettingsError};
pub use profiler::GpuProfiler;
Expand Down
45 changes: 45 additions & 0 deletions src/puffin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use puffin::{GlobalProfiler, NanoSecond, ScopeDetails, StreamInfo, ThreadInfo};

use crate::GpuTimerQueryResult;

/// Visualize the query results in a `puffin::GlobalProfiler`.
pub fn output_frame_to_puffin(profiler: &mut GlobalProfiler, query_result: &[GpuTimerQueryResult]) {
let mut stream_info = StreamInfo::default();
recusive_profile_gpu_frame(profiler, &mut stream_info, query_result, 0);
Gonkalbell marked this conversation as resolved.
Show resolved Hide resolved

profiler.report_user_scopes(
ThreadInfo {
start_time_ns: None,
name: "GPU".to_string(),
},
&stream_info.as_stream_into_ref(),
);
}

fn recusive_profile_gpu_frame(
profiler: &mut GlobalProfiler,
stream_info: &mut StreamInfo,
query_result: &[GpuTimerQueryResult],
depth: usize,
) {
let details: Vec<_> = query_result
.iter()
.map(|query| ScopeDetails::from_scope_name(query.label.clone()))
.collect();
let ids = profiler.register_user_scopes(&details);
for (query, id) in query_result.iter().zip(ids) {
if let Some(time) = &query.time {
let start = (time.start * 1e9) as NanoSecond;
let end = (time.end * 1e9) as NanoSecond;

stream_info.depth = stream_info.depth.max(depth);
stream_info.num_scopes += 1;
stream_info.range_ns.0 = stream_info.range_ns.0.min(start);
stream_info.range_ns.1 = stream_info.range_ns.0.max(end);

let (offset, _) = stream_info.stream.begin_scope(|| start, id, "");
recusive_profile_gpu_frame(profiler, stream_info, &query.nested_queries, depth + 1);
stream_info.stream.end_scope(offset, end as NanoSecond);
}
}
}
Loading