mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-21 15:00:57 +01:00
[indirect-draw-validation] make sure we don't encounter u64 offsets in indirect draw validation
This commit is contained in:
1 parent
eddbd98028
commit
bf1b57d47f
3 files changed
+52
-14
No files matched your search
@@ -63,7 +63,14 @@ impl Draw {
|
||||
required_features: &wgt::Features,
|
||||
instance_flags: wgt::InstanceFlags,
|
||||
backend: wgt::Backend,
|
||||
limits: &Limits,
|
||||
) -> Result<Self, CreateIndirectValidationPipelineError> {
|
||||
// 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::*;
|
||||
|
||||
@@ -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:?}");
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user