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
impl GpuTimestampProfiler
Sourcepub fn try_new(ctx: &GpuContext, capacity: u32) -> Option<Self>
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).
Sourcepub fn dummy(period_ns: f32) -> Self
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.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns true if this profiler is actively recording GPU timestamps.
Sourcepub fn period_ns(&self) -> f32
pub fn period_ns(&self) -> f32
Timestamp period in nanoseconds per tick, as reported by
wgpu::Queue::get_timestamp_period.
Sourcepub fn next_slot(&self) -> u32
pub fn next_slot(&self) -> u32
Index of the next free start slot. Always even when the profiler is in a consistent recording state.
Sourcepub fn begin_pass(
&mut self,
encoder: &mut CommandEncoder,
label: &str,
) -> Option<u32>
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.
Sourcepub fn end_pass(&self, encoder: &mut CommandEncoder, start_slot: u32)
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.
Sourcepub fn resolve(&mut self, ctx: &GpuContext) -> GpuResult<Vec<PassTiming>>
pub fn resolve(&mut self, ctx: &GpuContext) -> GpuResult<Vec<PassTiming>>
Resolves all recorded timestamps and decodes them into per-pass
PassTiming entries.
Steps:
- Encodes a
resolve_query_set+copy_buffer_to_buffercommand sequence to move the timestamps from the GPU-side query set into a CPU-mappable staging buffer. - Submits the command buffer.
- Maps the staging buffer for reading, blocking on
wgpu::Device::polluntil the mapping completes. - Converts each raw
u64tick to nanoseconds usingSelf::period_nsand computesduration_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.
Sourcepub fn pass_labels(&self) -> &[String]
pub fn pass_labels(&self) -> &[String]
Returns the recorded pass labels in registration order.
Sourcepub fn reset(&mut self)
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.