From bf1b57d47f2143104ee8edd9379a06fe195ebe86 Mon Sep 17 00:00:00 2001 From: Teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Thu, 28 May 2026 19:15:15 +0200 Subject: [PATCH] [indirect-draw-validation] make sure we don't encounter u64 offsets in indirect draw validation --- wgpu-core/src/indirect_validation/draw.rs | 24 +++++++++++----- wgpu-core/src/indirect_validation/mod.rs | 8 +++++- wgpu-core/src/instance.rs | 34 +++++++++++++++++++---- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/wgpu-core/src/indirect_validation/draw.rs b/wgpu-core/src/indirect_validation/draw.rs index e6d3a6513..df22818df 100644 --- a/wgpu-core/src/indirect_validation/draw.rs +++ b/wgpu-core/src/indirect_validation/draw.rs @@ -63,7 +63,14 @@ impl Draw { required_features: &wgt::Features, instance_flags: wgt::InstanceFlags, backend: wgt::Backend, + limits: &Limits, ) -> Result { + // Indirect draw validation doesn't support buffer sizes higher than u32 + // since its offsets in the shader and dynamic offsets are u32. + // + // See also: `u64_offset_to_u32_offset`. + assert!(limits.max_buffer_size <= u32::MAX as u64); + let module = create_validation_module(device, instance_flags)?; let metadata_bind_group_layout = create_bind_group_layout( @@ -437,7 +444,7 @@ impl Draw { pipeline_layout, 1, src_bind_group, - &[batch.src_dynamic_offset as u32], + &[u64_offset_to_u32_offset(batch.src_dynamic_offset)], ); } @@ -901,11 +908,7 @@ impl MetadataEntry { ) -> Self { const U32_MAX_AS_U64: u64 = u32::MAX as u64; - // NOTE: buffer sizes should never exceed `u32::MAX`. - assert!(src_offset <= U32_MAX_AS_U64); - assert!(dst_offset <= U32_MAX_AS_U64); - - let src_offset = src_offset as u32; + let src_offset = u64_offset_to_u32_offset(src_offset); let src_offset = src_offset / 4; // translate byte offset to offset in u32's // `src_offset` needs at most 30 bits, @@ -923,7 +926,7 @@ impl MetadataEntry { let instance_limit_bit_32 = (instance_limit >> 32) as u32; // extract bit 32 let instance_limit = instance_limit as u32; // truncate the limit to a u32 - let dst_offset = dst_offset as u32; + let dst_offset = u64_offset_to_u32_offset(dst_offset); let dst_offset = dst_offset / 4; // translate byte offset to offset in u32's // `dst_offset` needs at most 30 bits, @@ -1042,6 +1045,13 @@ impl DrawBatcher { } } +/// Indirect draw validation doesn't support u64 offsets. +/// +/// This fn should never panic due to the assert in [`Draw::new`]. +fn u64_offset_to_u32_offset(offset: u64) -> u32 { + offset.try_into().unwrap() +} + #[cfg(test)] mod tests { use super::*; diff --git a/wgpu-core/src/indirect_validation/mod.rs b/wgpu-core/src/indirect_validation/mod.rs index 00b3bbc28..0dd73b3bf 100644 --- a/wgpu-core/src/indirect_validation/mod.rs +++ b/wgpu-core/src/indirect_validation/mod.rs @@ -43,7 +43,13 @@ impl IndirectValidation { return Err(DeviceError::Lost); } }; - let draw = match Draw::new(device, required_features, instance_flags, backend) { + let draw = match Draw::new( + device, + required_features, + instance_flags, + backend, + required_limits, + ) { Ok(draw) => draw, Err(e) => { log::error!("indirect-draw-validation error: {e:?}"); diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 57cd7aab4..1ee81054c 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -437,6 +437,10 @@ impl Instance { adapters.extend( hal_adapters .into_iter() + .map(|mut raw| { + self.adjust_limits_for_indirect_validation(&mut raw.capabilities.limits); + raw + }) .filter_map(|raw| { if apply_limit_buckets { limits::apply_limit_buckets(raw) @@ -522,14 +526,15 @@ impl Instance { } } + let backend_adapters = backend_adapters.into_iter().map(|mut raw| { + self.adjust_limits_for_indirect_validation(&mut raw.capabilities.limits); + raw + }); + if desc.apply_limit_buckets { - adapters.extend( - backend_adapters - .into_iter() - .filter_map(limits::apply_limit_buckets), - ); + adapters.extend(backend_adapters.filter_map(limits::apply_limit_buckets)); } else { - adapters.append(&mut backend_adapters); + adapters.extend(backend_adapters); } } @@ -597,6 +602,23 @@ impl Instance { } } + /// This is similar to wgpu-hal's `adjust_raw_limits` but tailored to + /// wgpu-core's constraints. + fn adjust_limits_for_indirect_validation(&self, limits: &mut wgt::Limits) { + // Indirect draw validation can't support u64 offsets, + // lower max buffer and binding size to fit in an u32. + if self + .flags + .contains(wgt::InstanceFlags::VALIDATION_INDIRECT_CALL) + { + limits.max_buffer_size = limits.max_buffer_size.min(u32::MAX as u64); + limits.max_uniform_buffer_binding_size = + limits.max_uniform_buffer_binding_size.min(u32::MAX as u64); + limits.max_storage_buffer_binding_size = + limits.max_storage_buffer_binding_size.min(u32::MAX as u64); + } + } + fn active_backends(&self) -> Backends { self.instance_per_backend .iter()