Skip to main content

GpuTimestampProfiler

Struct GpuTimestampProfiler 

Source
pub struct GpuTimestampProfiler { /* private fields */ }
Expand description

Opt-in GPU timestamp profiler backed by a single wgpu::QuerySet.

Each pass consumes two adjacent timestamp slots (start, end). After recording, call Self::resolve to copy the query results into a staging buffer, map it, and decode the raw u64 timestamps into PassTiming structs.

See the module-level documentation for usage.

Implementations§

Source§

impl GpuTimestampProfiler

Source

pub fn try_new(ctx: &GpuContext, capacity: u32) -> Option<Self>

Attempt to construct a profiler with capacity timestamp slots.

capacity is rounded up to the nearest even number (and clamped to a minimum of 2) because each pass uses one start slot and one end slot.

Returns None if the adapter does not support both wgpu::Features::TIMESTAMP_QUERY and wgpu::Features::TIMESTAMP_QUERY_INSIDE_ENCODERS. Callers must enable these features in the crate::context::GpuContextConfig used to create the context (see the module-level docs).

Source

pub fn dummy(period_ns: f32) -> Self

Test-only constructor that allocates no GPU resources.

Useful for unit tests that need to exercise the PassTiming types or the disabled-path branches without touching wgpu. All recording methods become no-ops and Self::resolve returns an empty vector.

Source

pub fn is_enabled(&self) -> bool

Returns true if this profiler is actively recording GPU timestamps.

Source

pub fn period_ns(&self) -> f32

Timestamp period in nanoseconds per tick, as reported by wgpu::Queue::get_timestamp_period.

Source

pub fn capacity(&self) -> u32

Total number of timestamp slots available in the query set.

Source

pub fn next_slot(&self) -> u32

Index of the next free start slot. Always even when the profiler is in a consistent recording state.

Source

pub fn begin_pass( &mut self, encoder: &mut CommandEncoder, label: &str, ) -> Option<u32>

Records the start timestamp for a pass labelled label.

Returns the slot index that was used (which is also the index of the start–end pair). Pass this value to Self::end_pass to record the matching end timestamp.

Returns None when:

  • the profiler is disabled (constructed via Self::dummy), or
  • the query set capacity has been exhausted.
Source

pub fn end_pass(&self, encoder: &mut CommandEncoder, start_slot: u32)

Records the end timestamp for a pass at start_slot + 1.

Silently no-ops when the profiler is disabled or has no query set. The caller is responsible for passing the slot index returned by the matching Self::begin_pass call.

Source

pub fn resolve(&mut self, ctx: &GpuContext) -> GpuResult<Vec<PassTiming>>

Resolves all recorded timestamps and decodes them into per-pass PassTiming entries.

Steps:

  1. Encodes a resolve_query_set + copy_buffer_to_buffer command sequence to move the timestamps from the GPU-side query set into a CPU-mappable staging buffer.
  2. Submits the command buffer.
  3. Maps the staging buffer for reading, blocking on wgpu::Device::poll until the mapping completes.
  4. Converts each raw u64 tick to nanoseconds using Self::period_ns and computes duration_us.

Returns an empty Vec if the profiler is disabled or no passes were recorded.

§Errors

Returns GpuError::ExecutionFailed when the device poll, the buffer mapping callback, or the channel that delivers the mapping result fails.

Source

pub fn pass_labels(&self) -> &[String]

Returns the recorded pass labels in registration order.

Source

pub fn reset(&mut self)

Clears all recorded labels and resets the next-slot pointer back to zero, allowing the same query-set capacity to be re-used for another round of measurements.

Note that the underlying wgpu::QuerySet storage is not re-allocated; previously written timestamps remain in the resolve buffer until overwritten by subsequent write_timestamp calls.

Trait Implementations§

Source§

impl Debug for GpuTimestampProfiler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more