MCPcopy Index your code
hub / github.com/Wumpf/wgpu-profiler

github.com/Wumpf/wgpu-profiler @v.14.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v.14.1 ↗ · + Follow
38 symbols 59 edges 7 files 11 documented · 29%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

wgpu-profiler

Crates.io

Simple profiler scopes for wgpu using timer queries

Features

  • Easy to use profiler scopes
  • Allows nesting!
  • Can be disabled by runtime flag
  • Additionally generates debug markers
  • Internally creates pools of timer queries automatically
  • Does not need to know in advance how many queries/profiling scopes are needed
  • Caches up profiler-frames until results are available
    • No stalling of the device at any time!
  • Many profiler instances can live side by side
  • chrome trace flamegraph json export
  • Tracy integration (behind tracy feature flag)

TODO: * Better error messages * Disable via feature flag

How to use

Create a new profiler object:

use wgpu_profiler::{wgpu_profiler, GpuProfiler};
// ...
let mut profiler = GpuProfiler::new(4, queue.get_timestamp_period(), device.features()); // buffer up to 4 frames

Using scopes is easiest with the macro:

wgpu_profiler!("name of your scope", &mut profiler, &mut encoder, &device, {
  // wgpu commands go here
});

Note that GpuProfiler reads the device features - if your wgpu device doesn't have wgpu::Features::TIMESTAMP_QUERY enabled, it will automatically not attempt to emit any timer queries. Similarly, if wgpu::Features::WRITE_TIMESTAMP_INSIDE_PASSES is not present, no queries will be issued from inside passes.

Wgpu-profiler needs to insert buffer copy commands, so when you're done with an encoder and won't do any more profiling scopes on it, you need to resolve the queries:

profiler.resolve_queries(&mut encoder);

And finally, to end a profiling frame, call end_frame. This does a few checks and will let you know if something is off!

profiler.end_frame().unwrap();

Retrieving the oldest available frame and writing it out to a chrome trace file.

if let Some(profiling_data) = profiler.process_finished_frame() {
    // You usually want to write to disk only under some condition, e.g. press of a key or button
    wgpu_profiler::chrometrace::write_chrometrace(std::path::Path::new("mytrace.json"), &profiling_data);
}

To get a look of it in action, check out the example project!

License

Licensed under either of

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Changelog

  • 0.14.0
  • Tracy integration, by @cwfitzgerald in #35
  • 0.13.0
  • Upgrade to wgpu 0.17, by @waywardmonkeys in #31
  • 0.12.1
  • Fix wgpu validation error due to mapping of query resolve buffer, by @Davidster #28
  • 0.12.0
  • Upgrade to wgpu 0.16, by @davidster in #26
  • 0.11.0
  • Upgrade to wgpu 0.15
  • 0.10.0
  • Upgrade to wgpu 0.14 and switch to rust 2021 edition, by @Imberflur in #23
  • 0.9.1
  • Better docs #21
  • Fix crash on dropped frame #20, reported by @JCapucho in #19
  • Fix enable_pass_timer/enable_encoder_timer checking wrong features

Extension points exported contracts — how you extend this code

ProfilerCommandRecorder (Interface)
(no doc)
src/lib.rs

Core symbols most depended-on inside this repo

begin_scope
called by 3
src/lib.rs
write_results_recursive
called by 2
src/chrometrace.rs
resolve_queries
called by 2
src/lib.rs
end_frame
called by 2
src/lib.rs
reset_and_cache_unused_query_pools
called by 2
src/lib.rs
read_buffer_slice
called by 2
src/lib.rs
end_scope
called by 2
src/scope.rs
write_chrometrace
called by 1
src/chrometrace.rs

Shape

Method 19
Class 9
Function 9
Interface 1

Languages

Rust100%

Modules by API surface

src/lib.rs18 symbols
src/scope.rs11 symbols
examples/demo.rs4 symbols
tests/dropped_frame_handling.rs2 symbols
src/chrometrace.rs2 symbols
src/tracy.rs1 symbols

For agents

$ claude mcp add wgpu-profiler \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact