From c344bec50845208fdfdc28b835f462da34fc1eb6 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Wed, 2 Apr 2025 19:28:20 -0400 Subject: [PATCH] Comments --- tests/tests/wgpu-gpu/timestamp_query.rs | 20 +++++++++++++++++-- wgpu-core/src/command/query.rs | 3 ++- wgpu-core/src/device/resource.rs | 10 ++++++++-- wgpu-core/src/timestamp_normalization/mod.rs | 21 ++++++++++++++++---- 4 files changed, 45 insertions(+), 9 deletions(-) diff --git a/tests/tests/wgpu-gpu/timestamp_query.rs b/tests/tests/wgpu-gpu/timestamp_query.rs index 22a325189..ed2ae8d92 100644 --- a/tests/tests/wgpu-gpu/timestamp_query.rs +++ b/tests/tests/wgpu-gpu/timestamp_query.rs @@ -120,13 +120,29 @@ fn timestamp_query(ctx: TestingContext) { let query_data: &[u64] = bytemuck::cast_slice(&query_buffer_view); for i in 0..ITERATIONS { + // The byte and query offset for the current iteration let byte_offset = i as u64 * QUERY_RESOLVE_BUFFER_ALIGNMENT; let query_offset = byte_offset / 8; + // The byte and query offset for the next iteration + let next_byte_offset = (i + 1) as u64 * QUERY_RESOLVE_BUFFER_ALIGNMENT; + let next_query_offset = next_byte_offset / 8; + + // The range of queries that should still be the value they were initialized to. + let untouched_query_start = query_offset + QUERIES_PER_ITERATION as u64; + let untouched_query_end = next_query_offset; + // WebGPU does not define the value of the timestamp queries. They unfortunately // can be `0` in some situations. However, we should expect that some value // has been written, and the odds of it being exactly `init_constant` are vanishingly low. - assert_ne!(query_data[query_offset as usize], init_constant); - assert_ne!(query_data[query_offset as usize + 1], init_constant); + for query in 0..QUERIES_PER_ITERATION { + let query_index = query_offset + query as u64; + assert_ne!(query_data[query_index as usize], init_constant); + } + + // Validate that the queries that were not written to are still the value they were initialized to. + for query in untouched_query_start..untouched_query_end { + assert_eq!(query_data[query as usize], init_constant); + } } } diff --git a/wgpu-core/src/command/query.rs b/wgpu-core/src/command/query.rs index 6303783d4..c83ade669 100644 --- a/wgpu-core/src/command/query.rs +++ b/wgpu-core/src/command/query.rs @@ -481,7 +481,8 @@ impl Global { .get(&snatch_guard) .unwrap(), &dst_buffer, - start_query..end_query, + destination_offset, + query_count, ); } diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index d352c6fe7..bdc4a2775 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -680,6 +680,7 @@ impl Device { self, &*buffer, desc.label.as_deref(), + desc.size, desc.usage, )?, ); @@ -787,8 +788,13 @@ impl Device { .timestamp_normalizer .get() .unwrap() - .create_normalization_bind_group(self, &*hal_buffer, desc.label.as_deref(), desc.usage) - { + .create_normalization_bind_group( + self, + &*hal_buffer, + desc.label.as_deref(), + desc.size, + desc.usage, + ) { Ok(bg) => Snatchable::new(bg), Err(e) => { return ( diff --git a/wgpu-core/src/timestamp_normalization/mod.rs b/wgpu-core/src/timestamp_normalization/mod.rs index 6a44904d5..dd4d46623 100644 --- a/wgpu-core/src/timestamp_normalization/mod.rs +++ b/wgpu-core/src/timestamp_normalization/mod.rs @@ -27,6 +27,8 @@ //! We then multiply the timestamp by the numerator, and shift it right by the //! denominator. This gives us the normalized timestamp. +use core::num::NonZeroU64; + use alloc::{boxed::Box, string::String, string::ToString, sync::Arc}; use hashbrown::HashMap; @@ -118,7 +120,7 @@ impl TimestampNormalizer { ty: wgt::BindingType::Buffer { ty: wgt::BufferBindingType::Storage { read_only: false }, has_dynamic_offset: false, - min_binding_size: None, + min_binding_size: Some(NonZeroU64::new(8).unwrap()), }, count: None, }], @@ -245,6 +247,7 @@ impl TimestampNormalizer { device: &Device, buffer: &dyn hal::DynBuffer, buffer_label: Option<&str>, + buffer_size: u64, buffer_usages: wgt::BufferUsages, ) -> Result { unsafe { @@ -256,6 +259,14 @@ impl TimestampNormalizer { return Ok(TimestampNormalizationBindGroup { raw: None }); } + // If this buffer is large enough that we wouldn't be able to bind the entire thing + // at once to normalize the timestamps, we can't use it. We force the buffer to fail + // to allocate. The lowest max binding size is 128MB, and query sets must be small + // (no more than 4096), so this should never be hit in practice by sane programs. + if buffer_size > device.adapter.limits().max_storage_buffer_binding_size as u64 { + return Err(DeviceError::OutOfMemory); + } + let bg_label_alloc; let label = match buffer_label { Some(label) => { @@ -298,7 +309,8 @@ impl TimestampNormalizer { tracker: &mut BufferTracker, bind_group: &TimestampNormalizationBindGroup, buffer: &Arc, - range: core::ops::Range, + buffer_offset_bytes: u64, + total_timestamps: u32, ) { let Some(ref state) = &self.state else { return; @@ -308,11 +320,12 @@ impl TimestampNormalizer { return; }; + let buffer_offset_timestamps: u32 = (buffer_offset_bytes / 8).try_into().unwrap(); // Unreachable as MAX_QUERIES is way less than u32::MAX + let pending_barrier = tracker.set_single(buffer, wgt::BufferUses::STORAGE_READ_WRITE); let barrier = pending_barrier.map(|pending| pending.into_hal(buffer, snatch_guard)); - let total_timestamps = range.len() as u32; let needed_workgroups = total_timestamps.div_ceil(64); unsafe { @@ -327,7 +340,7 @@ impl TimestampNormalizer { &*state.pipeline_layout, wgt::ShaderStages::COMPUTE, 0, - &[range.start, range.len() as u32], + &[buffer_offset_timestamps, total_timestamps], ); encoder.dispatch([needed_workgroups, 1, 1]); encoder.end_compute_pass();