From 034693eceaea267b51b148b82b83bef21a1a341a Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Thu, 9 Jul 2026 13:26:11 -0700 Subject: [PATCH] refactor: Parameterize type of `hal::BufferBinding::size`; resolve sizes in core For storage and uniform buffer bindings, the size is a `wgt::BufferSize` (`NonZeroU64`). For vertex and index buffer bindings, the size is a `wgt::BufferAddress` (regular `u64`). Temporarily breaks some zero-size vertex/index binding cases that worked before (end of buffer); will be resolved in an immediate follow-up commit that fully implements zero-size vertex/index binding support. (cherry picked from commit ea0afc62162cb16cc4e27e53ac7e8249386b026a) --- wgpu-core/src/binding_model.rs | 12 +++ wgpu-core/src/command/bundle.rs | 25 ++++-- wgpu-core/src/command/render.rs | 11 +-- wgpu-core/src/command/render_command.rs | 4 +- wgpu-core/src/device/ray_tracing.rs | 3 +- wgpu-core/src/device/resource.rs | 87 ++++++++++++------- wgpu-core/src/indirect_validation/dispatch.rs | 4 +- wgpu-core/src/indirect_validation/draw.rs | 2 +- wgpu-core/src/indirect_validation/mod.rs | 4 + wgpu-core/src/resource.rs | 51 +++++++---- wgpu-core/src/scratch.rs | 2 +- wgpu-core/src/timestamp_normalization/mod.rs | 12 ++- wgpu-hal/examples/halmark/main.rs | 10 +-- wgpu-hal/examples/ray-traced-triangle/main.rs | 10 +-- wgpu-hal/src/dx12/command.rs | 10 +-- wgpu-hal/src/dx12/device.rs | 28 +++--- wgpu-hal/src/dx12/mod.rs | 21 ++--- wgpu-hal/src/dynamic/command.rs | 8 +- wgpu-hal/src/dynamic/device.rs | 7 +- wgpu-hal/src/dynamic/mod.rs | 4 +- wgpu-hal/src/gles/command.rs | 4 +- wgpu-hal/src/gles/device.rs | 55 ++++++------ wgpu-hal/src/gles/mod.rs | 1 - wgpu-hal/src/lib.rs | 48 +++++----- wgpu-hal/src/metal/command.rs | 21 +++-- wgpu-hal/src/metal/device.rs | 24 ++--- wgpu-hal/src/metal/mod.rs | 10 --- wgpu-hal/src/noop/buffer.rs | 14 ++- wgpu-hal/src/noop/command.rs | 4 +- wgpu-hal/src/noop/mod.rs | 5 +- wgpu-hal/src/vulkan/command.rs | 4 +- wgpu-hal/src/vulkan/device.rs | 21 ++--- 32 files changed, 295 insertions(+), 231 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index e50b9d1b7..1bde23f2c 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -118,6 +118,17 @@ pub enum BindingError { offset: wgt::BufferAddress, buffer_size: u64, }, + /// The supplied binding offset was equal to the buffer size. + /// + /// A binding offset equal to the buffer size is legal for vertex and index buffer + /// bindings, but not for storage/uniform buffer bindings, thus this error should only + /// be raised for the latter. + #[error("Buffer {buffer}: Binding offset {offset} must be strictly less than buffer size {buffer_size}")] + BindingOffsetEqualsSize { + buffer: ResourceErrorIdent, + offset: wgt::BufferAddress, + buffer_size: u64, + }, #[error("Unbinding vertex buffer at slot {slot} expects offset to be 0. However an offset of {offset} was provided.")] UnbindingVertexBufferOffsetNotZero { slot: u32, offset: u64 }, #[error("Unbinding vertex buffer at slot {slot} expects size to be 0. However a size of {size} was provided.")] @@ -130,6 +141,7 @@ impl WebGpuError for BindingError { Self::DestroyedResource(e) => e.webgpu_error_type(), Self::BindingRangeTooLarge { .. } | Self::BindingOffsetTooLarge { .. } + | Self::BindingOffsetEqualsSize { .. } | BindingError::UnbindingVertexBufferOffsetNotZero { .. } | BindingError::UnbindingVertexBufferSizeNotZero { .. } => ErrorType::Validation, } diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index a87a31aee..73d239dec 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -704,7 +704,10 @@ fn set_index_buffer( } .into()); } - let end = offset + buffer.resolve_binding_size(offset, size)?; + let end = offset + + buffer + .resolve_binding_size::(offset, size)? + .get(); state .buffer_memory_init_actions @@ -750,7 +753,7 @@ fn set_vertex_buffer( return Err(RenderCommandError::UnalignedVertexBuffer { slot, offset }.into()); } let binding_size = buffer.resolve_binding_size(offset, size)?; - let buffer_range = offset..(offset + binding_size); + let buffer_range = offset..(offset + binding_size.get()); state .buffer_memory_init_actions @@ -1136,7 +1139,13 @@ impl RenderBundle { let buffer = buffer.try_raw(snatch_guard)?; // SAFETY: The binding size was checked against the buffer size // in `set_index_buffer` and again in `IndexState::flush`. - let bb = hal::BufferBinding::new_unchecked(buffer, *offset, *size); + let bb = hal::BufferBinding::new_unchecked( + buffer, + *offset, + size.expect( + "Index buffer binding size should already be resolved when executing bundle", + ), + ); unsafe { raw.set_index_buffer(bb, *index_format) }; } Cmd::SetVertexBuffer { @@ -1148,7 +1157,13 @@ impl RenderBundle { let buffer = buffer.as_ref().unwrap().try_raw(snatch_guard)?; // SAFETY: The binding size was checked against the buffer size // in `set_vertex_buffer` and again in `VertexState::flush`. - let bb = hal::BufferBinding::new_unchecked(buffer, *offset, *size); + let bb = hal::BufferBinding::new_unchecked( + buffer, + *offset, + size.expect( + "Vertex buffer binding size should already be resolved when executing bundle", + ), + ); unsafe { raw.set_vertex_buffer(*slot, bb) }; } Cmd::SetImmediate { @@ -1445,7 +1460,7 @@ impl State { slot, buffer: Some(buffer.clone()), offset, - size, + size: Some(size), }); }); } diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 69d55c555..dd9334686 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -626,7 +626,7 @@ impl VertexState { /// Call `f` for each dirty slot with `(slot_index, buffer, offset, size)` and mark them clean. pub(crate) fn flush(&mut self, mut f: F) where - F: FnMut(u32, &Arc, BufferAddress, Option), + F: FnMut(u32, &Arc, BufferAddress, BufferSize), { for (i, slot) in self.slots.iter_mut().enumerate() { let Some(slot) = slot.as_mut() else { continue }; @@ -639,7 +639,8 @@ impl VertexState { i as u32, &slot.buffer, slot.range.start, - BufferSize::new(size), + BufferSize::new(size) + .expect("TODO(#3170): empty vertex buffer bindings temporarily unsupported"), ); } } @@ -2689,10 +2690,10 @@ fn set_index_buffer( } .into()); } - let (binding, resolved_size) = buffer + let binding = buffer .binding(offset, size, state.pass.base.snatch_guard) .map_err(RenderCommandError::from)?; - let end = offset + resolved_size; + let end = offset + binding.size.get(); state.index.update_buffer(offset..end, index_format); state.pass.base.buffer_memory_init_actions.extend( @@ -2750,7 +2751,7 @@ fn set_vertex_buffer( let binding_size = buffer .resolve_binding_size(offset, size) .map_err(RenderCommandError::from)?; - let buffer_range = offset..(offset + binding_size); + let buffer_range = offset..(offset + binding_size.get()); state .pass diff --git a/wgpu-core/src/command/render_command.rs b/wgpu-core/src/command/render_command.rs index 962b3bd55..2661450fd 100644 --- a/wgpu-core/src/command/render_command.rs +++ b/wgpu-core/src/command/render_command.rs @@ -23,13 +23,13 @@ pub enum RenderCommand { buffer: R::Buffer, index_format: wgt::IndexFormat, offset: BufferAddress, - size: Option, + size: Option, // optional in encoder; must be `Some` in a finished render bundle }, SetVertexBuffer { slot: u32, buffer: Option, offset: BufferAddress, - size: Option, + size: Option, // optional in encoder; must be `Some` in a finished render bundle }, SetBlendConstant(Color), SetStencilReference(u32), diff --git a/wgpu-core/src/device/ray_tracing.rs b/wgpu-core/src/device/ray_tracing.rs index da7c0da66..38afaae7e 100644 --- a/wgpu-core/src/device/ray_tracing.rs +++ b/wgpu-core/src/device/ray_tracing.rs @@ -183,6 +183,7 @@ impl Device { memory_flags: hal::MemoryFlags::PREFER_COHERENT, }) .map_err(DeviceError::from_hal)? + .0 })) } else { None @@ -270,7 +271,7 @@ impl Device { .raw_tlas_instance_size .checked_mul(desc.max_instances.max(1)) .expect("max_tlas_instance_count should not allow excessive buffer size"); - let instance_buffer = unsafe { + let (instance_buffer, _) = unsafe { self.raw().create_buffer(&hal::BufferDescriptor { label: hal_label(Some("(wgpu-core) instances_buffer"), self.instance_flags), size: u64::from(instance_buffer_size), diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index b65a4f447..839202d95 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -8,10 +8,9 @@ use alloc::{ use core::{ fmt, mem::{self, ManuallyDrop}, - num::NonZeroU32, + num::{NonZeroU32, NonZeroU64}, sync::atomic::{AtomicBool, Ordering}, }; -use hal::ShouldBeNonZeroExt; use arrayvec::ArrayVec; use bitflags::Flags; @@ -172,6 +171,9 @@ pub struct ExternalTextureParams { pub _padding: [u8; 4], } +const _: () = + assert!(size_of::() == wgpu_hal::EXTERNAL_TEXTURE_PARAMS_SIZE as usize); + impl ExternalTextureParams { pub fn from_desc(desc: &wgt::ExternalTextureDescriptor) -> Self { let gamut_conversion_matrix = [ @@ -468,7 +470,8 @@ impl Device { memory_flags: hal::MemoryFlags::empty(), }) } - .map_err(DeviceError::from_hal)?; + .map_err(DeviceError::from_hal)? + .0; let empty_bgl = unsafe { raw_device.create_bind_group_layout(&hal::BindGroupLayoutDescriptor { @@ -485,12 +488,13 @@ impl Device { Some("(wgpu internal) default external texture params buffer"), instance_flags, ), - size: size_of::() as _, + size: align_to(size_of::() as _, 256), usage: wgt::BufferUses::COPY_DST | wgt::BufferUses::UNIFORM, memory_flags: hal::MemoryFlags::empty(), }) } - .map_err(DeviceError::from_hal)?; + .map_err(DeviceError::from_hal)? + .0; // Cloned as we need them below anyway. let alignments = adapter.raw.capabilities.alignments.clone(); @@ -1079,34 +1083,45 @@ impl Device { // The great thing about buffer sizes is there are so many to choose from! // - The application may request an arbitrary byte-aligned size. - // - We add an extra byte if it's a vertex buffer so that we can simulate binding an - // empty range at the end of the buffer, which Vulkan does not natively allow. - // - The overall buffer size must be a non-zero multiple of 4. - // Because initialization operates at multiples of 4 bytes, and initialization - // tracking will never determine that it is necessary to initialize a region outside - // the application-visible range, we eagerly zero-initialize anything beyond that. - let actual_size = if desc.size == 0 { + // - We add an extra byte if it's a vertex or index buffer so that we can simulate + // binding an empty range at the end of the buffer, which Vulkan does not natively + // allow. + // - The overall buffer size must generally be a non-zero multiple of + // COPY_BUFFER_ALIGNMENT (4), but in special cases an additional platform-dependent + // requirement may apply (e.g. 256 for D3D12 constant buffers). + // + // Because initialization operates at multiples of COPY_BUFFER_ALIGNMENT, and + // initialization tracking will never determine that it is necessary to initialize a + // region outside the application-visible range, round the tracked initialization range + // down to a multiple of COPY_BUFFER_ALIGNMENT (tail_start), and eagerly zero-initialize + // from there to the buffer's true end offset. + + let padded_size = if desc.size == 0 { wgt::COPY_BUFFER_ALIGNMENT - } else if desc.usage.contains(wgt::BufferUsages::VERTEX) { + } else if desc + .usage + .intersects(wgt::BufferUsages::VERTEX | wgt::BufferUsages::INDEX) + { desc.size + 1 } else { desc.size }; - let actual_size = align_to(actual_size, wgt::COPY_BUFFER_ALIGNMENT); let tail_start = desc.size & !(wgt::COPY_BUFFER_ALIGNMENT - 1); - debug_assert!(actual_size - 4 <= tail_start); let hal_desc = hal::BufferDescriptor { label: desc.label.to_hal(self.instance_flags), - size: actual_size, + size: padded_size, usage, memory_flags: hal::MemoryFlags::empty(), }; - let buffer = unsafe { self.raw().create_buffer(&hal_desc) } + let (buffer, final_size) = unsafe { self.raw().create_buffer(&hal_desc) } .map_err(|e| self.handle_hal_error_with_nonfatal_oom(e))?; + debug_assert!(final_size >= padded_size); + let timestamp_normalization_bind_group = Snatchable::new(unsafe { - // SAFETY: The size passed here must not overflow the buffer. + // SAFETY: The size passed here must be 4B aligned, >= the application size, and + // <= the buffer size. self.timestamp_normalizer .get() .unwrap() @@ -1145,7 +1160,7 @@ impl Device { let mapping = map_buffer( buffer, tail_start, - actual_size - tail_start, + final_size - tail_start, HostMap::Write, snatch_guard, )?; @@ -1154,11 +1169,13 @@ impl Device { .expect("newly-created buffer cannot be destroyed"); unsafe { // SAFETY: The buffer tail is valid and aligned. - mapping.ptr.cast::().as_ptr().write(0); + mapping + .ptr + .write_bytes(0, (final_size - tail_start) as usize); if !mapping.is_coherent { #[allow(clippy::single_range_in_vec_init)] self.raw() - .flush_mapped_ranges(raw, &[tail_start..actual_size]); + .flush_mapped_ranges(raw, &[tail_start..final_size]); } self.raw().unmap_buffer(raw); } @@ -1166,7 +1183,7 @@ impl Device { }; let buffer_use = if !desc.mapped_at_creation { - if tail_start == actual_size { + if tail_start == final_size { // No tail init necessary } else if desc.usage.contains(wgt::BufferUsages::MAP_WRITE) { // Tail init by mapping @@ -1183,7 +1200,7 @@ impl Device { pending_writes.clear_buffer( self, &buffer, - tail_start..actual_size, + tail_start..final_size, &snatch_guard, )?; return Ok(buffer); @@ -1196,7 +1213,7 @@ impl Device { // application-visible portion of the buffer. Don't do both in the same // mapping, because the application shouldn't see the tail. let snatch_guard = self.snatchable_lock.read(); - if tail_start != actual_size { + if tail_start != final_size { init_buffer_tail(&buffer, &snatch_guard)?; } let map_size = buffer.size; @@ -1223,12 +1240,12 @@ impl Device { // which may cause problems for its shaders if they read out-of-bounds, but // won't cause problems in wgpu. let mut staging_buffer = - StagingBuffer::new(self, wgt::BufferSize::new(actual_size).unwrap())?; + StagingBuffer::new(self, wgt::BufferSize::new(final_size).unwrap())?; // Zero initialize memory and then mark the buffer as initialized // (it's guaranteed that this is the case by the time the buffer is usable) staging_buffer.write_zeros(); - buffer.initialization_status.write().drain(0..actual_size); + buffer.initialization_status.write().drain(0..final_size); *buffer.map_state.lock() = resource::BufferMapState::Init { staging_buffer }; wgt::BufferUses::COPY_DST @@ -1343,7 +1360,8 @@ impl Device { /// # Safety /// /// - `hal_buffer` must have been created on this device. - /// - `hal_buffer` must have been created respecting `desc` (in particular, the size). + /// - `hal_buffer` must have been created respecting `desc` (in particular, the size, + /// which must always be a multiple of 4, and may be subject to other requirements). /// - `hal_buffer` must be initialized. /// - `hal_buffer` must not have zero size. pub(crate) unsafe fn create_buffer_from_hal( @@ -1352,6 +1370,8 @@ impl Device { desc: &resource::BufferDescriptor, ) -> (Fallible, Option) { let timestamp_normalization_bind_group = unsafe { + // SAFETY: The size passed here must be 4B aligned, >= the application size, and + // <= the buffer size. match self .timestamp_normalizer .get() @@ -1411,6 +1431,10 @@ impl Device { (Fallible::Valid(buffer), None) } + /// If needed, creates the bind groups for indirect validation shaders to read + /// from `raw_buffer`. + /// + /// `buffer_size` is the user-requested size of the buffer. fn create_indirect_validation_bind_groups( &self, raw_buffer: &dyn hal::DynBuffer, @@ -2991,7 +3015,7 @@ impl Device { late_buffer_binding_sizes: &mut FastHashMap, used: &mut BindGroupStates, snatch_guard: &'a SnatchGuard<'a>, - ) -> Result, CreateBindGroupError> { + ) -> Result, CreateBindGroupError> { use crate::binding_model::CreateBindGroupError as Error; let (binding_ty, dynamic, min_size) = match decl.ty { @@ -3052,7 +3076,8 @@ impl Device { // Requested zero size Some(None) => return Err(CreateBindGroupError::BindingZeroSize(buffer.error_ident())), }; - let (bb, bind_size) = buffer.binding(bb.offset, req_size, snatch_guard)?; + let bb = buffer.binding(bb.offset, req_size, snatch_guard)?; + let bind_size = bb.size.get(); if matches!(binding_ty, wgt::BufferBindingType::Storage { .. }) && bind_size % u64::from(wgt::STORAGE_BINDING_SIZE_ALIGNMENT) != 0 @@ -3308,7 +3333,7 @@ impl Device { used.buffers .insert_single(external_texture.params.clone(), wgt::BufferUses::UNIFORM); - let params = external_texture.params.binding(0, None, snatch_guard)?.0; + let params = external_texture.params.binding(0, None, snatch_guard)?; Ok(hal::ExternalTextureBinding { planes, params }) } @@ -3360,7 +3385,7 @@ impl Device { let params = hal::BufferBinding::new_unchecked( self.default_external_texture_params_buffer.as_ref(), 0, - None, + NonZeroU64::new(size_of::() as u64).unwrap(), ); Ok(hal::ExternalTextureBinding { planes, params }) diff --git a/wgpu-core/src/indirect_validation/dispatch.rs b/wgpu-core/src/indirect_validation/dispatch.rs index 06002e3d5..fb83e3583 100644 --- a/wgpu-core/src/indirect_validation/dispatch.rs +++ b/wgpu-core/src/indirect_validation/dispatch.rs @@ -233,7 +233,7 @@ impl Dispatch { usage: wgt::BufferUses::INDIRECT | wgt::BufferUses::STORAGE_READ_WRITE, memory_flags: hal::MemoryFlags::empty(), }; - let dst_buffer = + let (dst_buffer, _) = unsafe { device.create_buffer(&dst_buffer_desc) }.map_err(DeviceError::from_hal)?; let dst_bind_group_desc = hal::BindGroupDescriptor { @@ -251,7 +251,7 @@ impl Dispatch { buffers: &[hal::BufferBinding::new_unchecked( dst_buffer.as_ref(), 0, - Some(DST_BUFFER_SIZE), + DST_BUFFER_SIZE, )], samplers: &[], textures: &[], diff --git a/wgpu-core/src/indirect_validation/draw.rs b/wgpu-core/src/indirect_validation/draw.rs index df22818df..cadb12f74 100644 --- a/wgpu-core/src/indirect_validation/draw.rs +++ b/wgpu-core/src/indirect_validation/draw.rs @@ -747,7 +747,7 @@ fn create_buffer_and_bind_group( usage, memory_flags: hal::MemoryFlags::empty(), }; - let buffer = unsafe { device.create_buffer(&buffer_desc) }?; + let (buffer, _) = unsafe { device.create_buffer(&buffer_desc) }?; let bind_group_desc = hal::BindGroupDescriptor { label: bind_group_label, layout: bind_group_layout, diff --git a/wgpu-core/src/indirect_validation/mod.rs b/wgpu-core/src/indirect_validation/mod.rs index 0dd73b3bf..ad8009939 100644 --- a/wgpu-core/src/indirect_validation/mod.rs +++ b/wgpu-core/src/indirect_validation/mod.rs @@ -74,6 +74,10 @@ pub(crate) struct BindGroups { } impl BindGroups { + /// Creates the bind groups for indirect validation shaders to read from `buffer`. + /// + /// `buffer_size` is the user-requested size of the buffer. + /// /// `Ok(None)` will only be returned if `buffer_size` is `0`. pub(crate) fn new( indirect_validation: &IndirectValidation, diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 670724341..5fe368262 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -527,32 +527,44 @@ impl Buffer { /// zero-size bindings are permitted by WebGPU, they are not permitted by /// some backends. See [`Buffer::binding`] and /// [#3170](https://github.com/gfx-rs/wgpu/issues/3170). - pub fn resolve_binding_size( + pub fn resolve_binding_size< + S: Copy + Into + TryFrom, + >( &self, offset: wgt::BufferAddress, - binding_size: Option, - ) -> Result { + binding_size: Option, + ) -> Result { let buffer_size = self.size; match binding_size { - Some(binding_size) => match offset.checked_add(binding_size.get()) { - Some(end) if end <= buffer_size => Ok(binding_size.get()), + Some(binding_size) => match offset.checked_add(binding_size.into()) { + Some(end) if end <= buffer_size => Ok(binding_size), _ => Err(BindingError::BindingRangeTooLarge { buffer: self.error_ident(), offset, - binding_size: binding_size.get(), + binding_size: binding_size.into(), buffer_size, }), }, - None => { - buffer_size - .checked_sub(offset) - .ok_or_else(|| BindingError::BindingOffsetTooLarge { - buffer: self.error_ident(), - offset, - buffer_size, - }) - } + None => buffer_size + .checked_sub(offset) + .and_then(|remaining| S::try_from(remaining).ok()) + .ok_or_else(|| { + if offset <= buffer_size { + debug_assert_eq!(offset, buffer_size); + BindingError::BindingOffsetEqualsSize { + buffer: self.error_ident(), + offset, + buffer_size, + } + } else { + BindingError::BindingOffsetTooLarge { + buffer: self.error_ident(), + offset, + buffer_size, + } + } + }), } } @@ -581,13 +593,14 @@ impl Buffer { offset: wgt::BufferAddress, binding_size: Option, snatch_guard: &'a SnatchGuard, - ) -> Result<(hal::BufferBinding<'a, dyn hal::DynBuffer>, u64), BindingError> { + ) -> Result, BindingError> { let buf_raw = self.try_raw(snatch_guard)?; let resolved_size = self.resolve_binding_size(offset, binding_size)?; // SAFETY: The offset and size passed to hal::BufferBinding::new_unchecked must // define a binding contained within the buffer. - Ok(( - hal::BufferBinding::new_unchecked(buf_raw, offset, binding_size), + Ok(hal::BufferBinding::new_unchecked( + buf_raw, + offset, resolved_size, )) } @@ -1206,7 +1219,7 @@ impl StagingBuffer { memory_flags: hal::MemoryFlags::TRANSIENT, }; - let raw = unsafe { device.raw().create_buffer(&stage_desc) } + let (raw, _) = unsafe { device.raw().create_buffer(&stage_desc) } .map_err(|e| device.handle_hal_error(e))?; let mapping = unsafe { device.raw().map_buffer(raw.as_ref(), 0..size.get()) } .map_err(|e| device.handle_hal_error(e))?; diff --git a/wgpu-core/src/scratch.rs b/wgpu-core/src/scratch.rs index 797f9db14..074ab4f10 100644 --- a/wgpu-core/src/scratch.rs +++ b/wgpu-core/src/scratch.rs @@ -14,7 +14,7 @@ pub struct ScratchBuffer { impl ScratchBuffer { pub(crate) fn new(device: &Arc, size: wgt::BufferSize) -> Result { - let raw = unsafe { + let (raw, _) = unsafe { device .raw() .create_buffer(&hal::BufferDescriptor { diff --git a/wgpu-core/src/timestamp_normalization/mod.rs b/wgpu-core/src/timestamp_normalization/mod.rs index 34be348b5..e7e6ece80 100644 --- a/wgpu-core/src/timestamp_normalization/mod.rs +++ b/wgpu-core/src/timestamp_normalization/mod.rs @@ -253,8 +253,16 @@ impl TimestampNormalizer { /// Create a bind group for normalizing timestamps in `buffer`. /// - /// This function is unsafe because it does not know that `buffer_size` is - /// the true size of the buffer. + /// `buffer_size` must be 4B-aligned, so that it is valid as a storage binding, + /// must be the same or more than the application-visible buffer size, and must + /// not overrun the buffer. This function is unsafe because it does not know and + /// cannot check whether `buffer_size` meets these requirements. + /// + /// The buffer size passed here is used to construct a binding that must be + /// able to access the full application-visible range of the buffer. It is + /// not used to check the destination range of query resolution requests. That + /// uses the application-requested buffer size, obtained from the `wgpu_core` + /// `Buffer`. pub unsafe fn create_normalization_bind_group( &self, device: &Device, diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 9983de95e..62f3dd963 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -299,7 +299,7 @@ impl Example { usage: wgpu_types::BufferUses::MAP_WRITE | wgpu_types::BufferUses::COPY_SRC, memory_flags: hal::MemoryFlags::TRANSIENT | hal::MemoryFlags::PREFER_COHERENT, }; - let staging_buffer = unsafe { device.create_buffer(&staging_buffer_desc).unwrap() }; + let (staging_buffer, _) = unsafe { device.create_buffer(&staging_buffer_desc).unwrap() }; unsafe { let mapping = device .map_buffer(&staging_buffer, 0..staging_buffer_desc.size) @@ -418,7 +418,7 @@ impl Example { memory_flags: hal::MemoryFlags::PREFER_COHERENT, }; let global_buffer = unsafe { - let buffer = device.create_buffer(&global_buffer_desc).unwrap(); + let (buffer, _) = device.create_buffer(&global_buffer_desc).unwrap(); let mapping = device .map_buffer(&buffer, 0..global_buffer_desc.size) .unwrap(); @@ -443,7 +443,7 @@ impl Example { usage: wgpu_types::BufferUses::MAP_WRITE | wgpu_types::BufferUses::UNIFORM, memory_flags: hal::MemoryFlags::PREFER_COHERENT, }; - let local_buffer = unsafe { device.create_buffer(&local_buffer_desc).unwrap() }; + let (local_buffer, _) = unsafe { device.create_buffer(&local_buffer_desc).unwrap() }; let view_desc = hal::TextureViewDescriptor { label: None, @@ -459,7 +459,7 @@ impl Example { let global_buffer_binding = hal::BufferBinding::new_unchecked( &global_buffer, 0, - NonZeroU64::new(global_buffer_desc.size), + NonZeroU64::new(global_buffer_desc.size).unwrap(), ); let texture_binding = hal::TextureBinding { view: &texture_view, @@ -499,7 +499,7 @@ impl Example { let local_buffer_binding = hal::BufferBinding::new_unchecked( &local_buffer, 0, - wgpu_types::BufferSize::new(size_of::() as _), + wgpu_types::BufferSize::new(size_of::() as _).unwrap(), ); let local_group_desc = hal::BindGroupDescriptor { label: Some("local"), diff --git a/wgpu-hal/examples/ray-traced-triangle/main.rs b/wgpu-hal/examples/ray-traced-triangle/main.rs index b9cf617bf..4b7ca355b 100644 --- a/wgpu-hal/examples/ray-traced-triangle/main.rs +++ b/wgpu-hal/examples/ray-traced-triangle/main.rs @@ -427,7 +427,7 @@ impl Example { let indices_size_in_bytes = indices.len() * 4; let vertices_buffer = unsafe { - let vertices_buffer = device + let (vertices_buffer, _) = device .create_buffer(&hal::BufferDescriptor { label: Some("vertices buffer"), size: vertices_size_in_bytes as u64, @@ -453,7 +453,7 @@ impl Example { let indices_buffer = if index_buffer { unsafe { - let indices_buffer = device + let (indices_buffer, _) = device .create_buffer(&hal::BufferDescriptor { label: Some("indices buffer"), size: indices_size_in_bytes as u64, @@ -563,7 +563,7 @@ impl Example { let instances_buffer_size = instances.len() * size_of::(); let instances_buffer = unsafe { - let instances_buffer = device + let (instances_buffer, _) = device .create_buffer(&hal::BufferDescriptor { label: Some("instances_buffer"), size: instances_buffer_size as u64, @@ -629,7 +629,7 @@ impl Example { let uniforms_size = size_of::(); let uniform_buffer = unsafe { - let uniform_buffer = device + let (uniform_buffer, _) = device .create_buffer(&hal::BufferDescriptor { label: Some("uniform buffer"), size: uniforms_size as u64, @@ -719,7 +719,7 @@ impl Example { unsafe { device.create_bind_group(&group_desc).unwrap() } }; - let scratch_buffer = unsafe { + let (scratch_buffer, _) = unsafe { device .create_buffer(&hal::BufferDescriptor { label: Some("scratch buffer"), diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index ae45f8406..4ccc4221c 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -396,7 +396,7 @@ impl super::CommandEncoder { )); super::Buffer { resource, - size: size.get(), + allocated_size: size.get(), allocation, } }; @@ -1317,12 +1317,12 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_index_buffer<'a>( &mut self, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferAddress>, format: wgt::IndexFormat, ) { let ibv = Direct3D12::D3D12_INDEX_BUFFER_VIEW { BufferLocation: binding.resolve_address(), - SizeInBytes: binding.resolve_size().try_into().unwrap(), + SizeInBytes: binding.size.try_into().unwrap(), Format: auxil::dxgi::conv::map_index_format(format), }; @@ -1331,11 +1331,11 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferAddress>, ) { let vb = &mut self.pass.vertex_buffers[index as usize]; vb.BufferLocation = binding.resolve_address(); - vb.SizeInBytes = binding.resolve_size().try_into().unwrap(); + vb.SizeInBytes = binding.size.try_into().unwrap(); self.pass.dirty_vertex_buffers |= 1 << index; } diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 879367874..05a50dc74 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -487,7 +487,7 @@ impl super::Device { ) -> super::Buffer { super::Buffer { resource, - size, + allocated_size: size, allocation: suballocation::Allocation::none( suballocation::AllocationType::Buffer, size, @@ -502,7 +502,7 @@ impl crate::Device for super::Device { unsafe fn create_buffer( &self, desc: &crate::BufferDescriptor, - ) -> Result { + ) -> Result<(super::Buffer, wgt::BufferAddress), crate::DeviceError> { let mut desc = desc.clone(); if desc.usage.contains(wgt::BufferUses::UNIFORM) { @@ -516,11 +516,14 @@ impl crate::Device for super::Device { self.counters.buffers.add(1); - Ok(super::Buffer { - resource, - size: desc.size, - allocation, - }) + Ok(( + super::Buffer { + resource, + allocated_size: desc.size, + allocation, + }, + desc.size, + )) } unsafe fn destroy_buffer(&self, buffer: super::Buffer) { @@ -1543,7 +1546,7 @@ impl crate::Device for super::Device { let end = start + entry.count as usize; for data in &desc.buffers[start..end] { let gpu_address = data.resolve_address(); - let mut size = data.resolve_size().try_into().unwrap(); + let mut size = data.size.get().try_into().unwrap(); if has_dynamic_offset { match ty { @@ -1555,8 +1558,13 @@ impl crate::Device for super::Device { )); continue; } + // TODO(https://github.com/gfx-rs/wgpu/issues/9865): There is not currently + // any mechanism to check that shader accesses are within the valid range + // of a dynamic offset binding, and while using the actual size of the buffer + // here would be slightly closer to being correct, it isn't sufficient + // (because the upper bound moves with the dynamic offset). wgt::BufferBindingType::Storage { .. } => { - size = (data.buffer.size - data.offset) as u32; + size = (data.buffer.allocated_size - data.offset) as u32; dynamic_buffers.push(super::DynamicBuffer::Storage); } } @@ -1687,7 +1695,7 @@ impl crate::Device for super::Device { cpu_views.as_mut().unwrap().stage.push(plane_handle.raw); } let gpu_address = external_texture.params.resolve_address(); - let size = external_texture.params.resolve_size() as u32; + let size = crate::EXTERNAL_TEXTURE_PARAMS_SIZE as u32; let inner = cpu_views.as_mut().unwrap(); let cpu_index = inner.stage.len() as u32; let params_handle = desc.layout.cpu_heap_views.as_ref().unwrap().at(cpu_index); diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index d9fe4dbd6..2b5ffe860 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -1015,10 +1015,14 @@ unsafe impl Sync for CommandBuffer {} #[derive(Debug)] pub struct Buffer { resource: Direct3D12::ID3D12Resource, - // While the allocation also has _a_ size, it may not - // be the same as the original size of the buffer, - // as the allocation size varies for assorted reasons. - size: wgt::BufferAddress, + /// The allocated size of the buffer. + /// + /// This may not be the same as the size the application requested. + // + // TODO(https://github.com/gfx-rs/wgpu/issues/9865): Consider removing this. + // The usage associated with the TODO is currently the only usage. Computing + // things from the allocated buffer size is usually incorrect. + allocated_size: wgt::BufferAddress, allocation: suballocation::Allocation, } @@ -1033,14 +1037,7 @@ unsafe impl Sync for Buffer {} impl crate::DynBuffer for Buffer {} -impl crate::BufferBinding<'_, Buffer> { - fn resolve_size(&self) -> wgt::BufferAddress { - match self.size { - Some(size) => size.get(), - None => self.buffer.size - self.offset, - } - } - +impl crate::BufferBinding<'_, Buffer, S> { // TODO: Return GPU handle directly? fn resolve_address(&self) -> wgt::BufferAddress { (unsafe { self.buffer.resource.GetGPUVirtualAddress() }) + self.offset diff --git a/wgpu-hal/src/dynamic/command.rs b/wgpu-hal/src/dynamic/command.rs index 6b4e4fdb0..83762c8ae 100644 --- a/wgpu-hal/src/dynamic/command.rs +++ b/wgpu-hal/src/dynamic/command.rs @@ -100,14 +100,14 @@ pub trait DynCommandEncoder: DynResource + core::fmt::Debug { unsafe fn set_index_buffer<'a>( &mut self, - binding: BufferBinding<'a, dyn DynBuffer>, + binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferSize>, format: wgt::IndexFormat, ); unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: BufferBinding<'a, dyn DynBuffer>, + binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferSize>, ); unsafe fn set_viewport(&mut self, rect: &Rect, depth_range: Range); unsafe fn set_scissor_rect(&mut self, rect: &Rect); @@ -630,7 +630,7 @@ impl DynCommandEncoder for C { unsafe fn set_index_buffer<'a>( &mut self, - binding: BufferBinding<'a, dyn DynBuffer>, + binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferSize>, format: wgt::IndexFormat, ) { let binding = binding.expect_downcast(); @@ -640,7 +640,7 @@ impl DynCommandEncoder for C { unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: BufferBinding<'a, dyn DynBuffer>, + binding: BufferBinding<'a, dyn DynBuffer, wgt::BufferSize>, ) { let binding = binding.expect_downcast(); unsafe { self.set_vertex_buffer(index, binding) }; diff --git a/wgpu-hal/src/dynamic/device.rs b/wgpu-hal/src/dynamic/device.rs index b5ff59045..211934856 100644 --- a/wgpu-hal/src/dynamic/device.rs +++ b/wgpu-hal/src/dynamic/device.rs @@ -21,7 +21,7 @@ pub trait DynDevice: DynResource { unsafe fn create_buffer( &self, desc: &BufferDescriptor, - ) -> Result, DeviceError>; + ) -> Result<(Box, wgt::BufferAddress), DeviceError>; unsafe fn destroy_buffer(&self, buffer: Box); unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer); @@ -171,8 +171,9 @@ impl DynDevice for D { unsafe fn create_buffer( &self, desc: &BufferDescriptor, - ) -> Result, DeviceError> { - unsafe { D::create_buffer(self, desc) }.map(|b| -> Box { Box::new(b) }) + ) -> Result<(Box, wgt::BufferAddress), DeviceError> { + unsafe { D::create_buffer(self, desc) } + .map(|(b, size)| -> (Box, _) { (Box::new(b), size) }) } unsafe fn destroy_buffer(&self, buffer: Box) { diff --git a/wgpu-hal/src/dynamic/mod.rs b/wgpu-hal/src/dynamic/mod.rs index f45051363..d7f6574de 100644 --- a/wgpu-hal/src/dynamic/mod.rs +++ b/wgpu-hal/src/dynamic/mod.rs @@ -115,8 +115,8 @@ pub trait DynSurfaceTexture: pub trait DynTexture: DynResource + fmt::Debug {} pub trait DynTextureView: DynResource + fmt::Debug {} -impl<'a> BufferBinding<'a, dyn DynBuffer> { - pub fn expect_downcast(self) -> BufferBinding<'a, B> { +impl<'a, S> BufferBinding<'a, dyn DynBuffer, S> { + pub fn expect_downcast(self) -> BufferBinding<'a, B, S> { BufferBinding { buffer: self.buffer.expect_downcast_ref(), offset: self.offset, diff --git a/wgpu-hal/src/gles/command.rs b/wgpu-hal/src/gles/command.rs index 409481b68..63a8b5616 100644 --- a/wgpu-hal/src/gles/command.rs +++ b/wgpu-hal/src/gles/command.rs @@ -1023,7 +1023,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_index_buffer<'a>( &mut self, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferSize>, format: wgt::IndexFormat, ) { self.state.index_offset = binding.offset; @@ -1035,7 +1035,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferSize>, ) { self.state.dirty_vbuf_mask |= 1 << index; let (_, ref mut vb) = self.state.vertex_buffers[index as usize]; diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index 6f3f31920..87ab64b87 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -549,7 +549,7 @@ impl crate::Device for super::Device { unsafe fn create_buffer( &self, desc: &crate::BufferDescriptor, - ) -> Result { + ) -> Result<(super::Buffer, wgt::BufferAddress), crate::DeviceError> { let target = if desc.usage.contains(wgt::BufferUses::INDEX) { glow::ELEMENT_ARRAY_BUFFER } else { @@ -566,17 +566,19 @@ impl crate::Device for super::Device { .contains(PrivateCapabilities::BUFFER_ALLOCATION); if emulate_map && desc.usage.intersects(wgt::BufferUses::MAP_WRITE) { - return Ok(super::Buffer { - raw: None, - target, - size: desc.size, - map_flags: 0, - map_state: Arc::new(MaybeMutex::new(super::BufferMapState { - mapped: false, - data: Some(vec![0; desc.size as usize]), - offset_of_current_mapping: 0, - })), - }); + return Ok(( + super::Buffer { + raw: None, + target, + map_flags: 0, + map_state: Arc::new(MaybeMutex::new(super::BufferMapState { + mapped: false, + data: Some(vec![0; desc.size as usize]), + offset_of_current_mapping: 0, + })), + }, + desc.size, + )); } let gl = &self.shared.context.lock(); @@ -669,17 +671,19 @@ impl crate::Device for super::Device { self.counters.buffers.add(1); - Ok(super::Buffer { - raw, - target, - size: desc.size, - map_flags, - map_state: Arc::new(MaybeMutex::new(super::BufferMapState { - mapped: false, - data, - offset_of_current_mapping: 0, - })), - }) + Ok(( + super::Buffer { + raw, + target, + map_flags, + map_state: Arc::new(MaybeMutex::new(super::BufferMapState { + mapped: false, + data, + offset_of_current_mapping: 0, + })), + }, + desc.size, + )) } unsafe fn destroy_buffer(&self, buffer: super::Buffer) { @@ -1335,10 +1339,7 @@ impl crate::Device for super::Device { super::RawBinding::Buffer { raw: bb.buffer.raw.unwrap(), offset: bb.offset as i32, - size: match bb.size { - Some(s) => s.get() as i32, - None => (bb.buffer.size - bb.offset) as i32, - }, + size: bb.size.get() as i32, } } wgt::BindingType::Sampler { .. } => { diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 87c8be619..945b66fd2 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -341,7 +341,6 @@ impl Drop for Queue { pub struct Buffer { raw: Option, target: BindTarget, - size: wgt::BufferAddress, /// Flags to use within calls to [`Device::map_buffer`](crate::Device::map_buffer). map_flags: u32, /// Buffer mapping state. diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 1d5e2c0f9..46ba778a7 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -327,6 +327,7 @@ pub const MAX_MIP_LEVELS: u32 = 16; /// Size of a single occlusion/timestamp query, when copied into a buffer, in bytes. /// cbindgen:ignore pub const QUERY_SIZE: wgt::BufferAddress = 8; +pub const EXTERNAL_TEXTURE_PARAMS_SIZE: wgt::BufferAddress = 208; pub type Label<'a> = Option<&'a str>; pub type MemoryRange = Range; @@ -886,7 +887,7 @@ pub trait Device: WasmNotSendSync { unsafe fn create_buffer( &self, desc: &BufferDescriptor, - ) -> Result<::Buffer, DeviceError>; + ) -> Result<(::Buffer, wgt::BufferAddress), DeviceError>; /// Free `buffer` and any GPU resources it owns. /// @@ -1612,13 +1613,13 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug { unsafe fn set_index_buffer<'a>( &mut self, - binding: BufferBinding<'a, ::Buffer>, + binding: BufferBinding<'a, ::Buffer, wgt::BufferSize>, format: wgt::IndexFormat, ); unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: BufferBinding<'a, ::Buffer>, + binding: BufferBinding<'a, ::Buffer, wgt::BufferSize>, ); unsafe fn set_viewport(&mut self, rect: &Rect, depth_range: Range); unsafe fn set_scissor_rect(&mut self, rect: &Rect); @@ -2066,6 +2067,15 @@ pub struct BufferMapping { #[derive(Clone, Debug)] pub struct BufferDescriptor<'a> { pub label: Label<'a>, + + /// The requested size of the buffer. + /// + /// `wgpu-hal` may allocate more bytes than requested, if required by the + /// platform. The actual allocation size is returned by `create_buffer`. + /// Where platforms offer bounds checking, it will operate based on the + /// allocated size, not the requested size, so other means may be necessary + /// to prevent access beyond the original requested size. The content of + /// newly-created buffers is undefined. pub size: wgt::BufferAddress, pub usage: wgt::BufferUses, pub memory_flags: MemoryFlags, @@ -2214,7 +2224,7 @@ pub struct PipelineLayoutDescriptor<'a, B: DynBindGroupLayout + ?Sized> { /// [bbr]: https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glBindBufferRange.xhtml /// [woob]: https://gpuweb.github.io/gpuweb/wgsl/#out-of-bounds-access-sec #[derive(Debug)] -pub struct BufferBinding<'a, B: DynBuffer + ?Sized> { +pub struct BufferBinding<'a, B: DynBuffer + ?Sized, S> { /// The buffer being bound. /// /// This is not fully `pub` to prevent direct construction of @@ -2228,15 +2238,11 @@ pub struct BufferBinding<'a, B: DynBuffer + ?Sized> { pub offset: wgt::BufferAddress, /// The size of the region bound, in bytes. - /// - /// If `None`, the region extends from `offset` to the end of the - /// buffer. Given the restrictions on `offset`, this means that - /// the size is always greater than zero. - pub size: Option, + pub size: S, } // We must implement this manually because `B` is not necessarily `Clone`. -impl Clone for BufferBinding<'_, B> { +impl Clone for BufferBinding<'_, B, S> { fn clone(&self) -> Self { BufferBinding { buffer: self.buffer, @@ -2274,7 +2280,7 @@ impl ShouldBeNonZeroExt for Option { } } -impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> { +impl<'a, B: DynBuffer + ?Sized, S> BufferBinding<'a, B, S> { /// Construct a `BufferBinding` with the given contents. /// /// When possible, use the `binding` method on a wgpu-core `Buffer` instead @@ -2285,21 +2291,13 @@ impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> { /// not having direct access to the size of a `DynBuffer`. /// /// SAFETY: The caller is responsible for ensuring that a binding of `size` - /// bytes starting at `offset` is contained within the buffer. - /// - /// The `S` type parameter is a temporary convenience to allow callers to - /// pass a zero size. When the zero-size binding issue is resolved, the - /// argument should just match the type of the member. - /// TODO(): remove the parameter - pub fn new_unchecked>>( - buffer: &'a B, - offset: wgt::BufferAddress, - size: S, - ) -> Self { + /// bytes starting at `offset` is contained within the buffer. `size` + /// may be zero only for vertex/index buffer bindings. + pub fn new_unchecked(buffer: &'a B, offset: wgt::BufferAddress, size: S) -> Self { Self { buffer, offset, - size: size.into(), + size, } } } @@ -2322,7 +2320,7 @@ impl<'a, T: DynTextureView + ?Sized> Clone for TextureBinding<'a, T> { #[derive(Debug)] pub struct ExternalTextureBinding<'a, B: DynBuffer + ?Sized, T: DynTextureView + ?Sized> { pub planes: [TextureBinding<'a, T>; 3], - pub params: BufferBinding<'a, B>, + pub params: BufferBinding<'a, B, wgt::BufferSize>, } impl<'a, B: DynBuffer + ?Sized, T: DynTextureView + ?Sized> Clone @@ -2364,7 +2362,7 @@ pub struct BindGroupDescriptor< > { pub label: Label<'a>, pub layout: &'a Bgl, - pub buffers: &'a [BufferBinding<'a, B>], + pub buffers: &'a [BufferBinding<'a, B, wgt::BufferSize>], pub samplers: &'a [&'a S], pub textures: &'a [TextureBinding<'a, T>], pub entries: &'a [BindGroupEntry], diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index 41f4ebc13..f2e33ed5c 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -1364,7 +1364,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_index_buffer<'a>( &mut self, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferSize>, format: wgt::IndexFormat, ) { let (stride, raw_type) = conv::map_index_format(format); @@ -1379,7 +1379,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferSize>, ) { let buffer_index = VERTEX_BUFFER_SLOT_START + index; let encoder = self.state.render.as_ref().unwrap(); @@ -1391,15 +1391,14 @@ impl crate::CommandEncoder for super::CommandEncoder { ) }; - let buffer_size = binding.resolve_size(); - if buffer_size > 0 { - self.state.vertex_buffer_size_map.insert( - buffer_index, - core::num::NonZeroU64::new(buffer_size).unwrap(), - ); - } else { - self.state.vertex_buffer_size_map.remove(&buffer_index); - } + // TODO(#3170): temporary + //if let Some(size) = NonZeroU64::new(binding.size) { + self.state + .vertex_buffer_size_map + .insert(buffer_index, binding.size); + //} else { + // self.state.vertex_buffer_size_map.remove(&buffer_index); + //} if let Some((index, sizes)) = self .state diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 70bf2a7ee..3a31a2aac 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -415,11 +415,8 @@ impl super::Device { } } - pub unsafe fn buffer_from_raw( - raw: Retained>, - size: wgt::BufferAddress, - ) -> super::Buffer { - super::Buffer { raw, size } + pub unsafe fn buffer_from_raw(raw: Retained>) -> super::Buffer { + super::Buffer { raw } } pub fn raw_device(&self) -> &Retained> { @@ -430,7 +427,10 @@ impl super::Device { impl crate::Device for super::Device { type A = super::Api; - unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult { + unsafe fn create_buffer( + &self, + desc: &crate::BufferDescriptor, + ) -> DeviceResult<(super::Buffer, wgt::BufferAddress)> { let map_read = desc.usage.contains(wgt::BufferUses::MAP_READ); let map_write = desc.usage.contains(wgt::BufferUses::MAP_WRITE); @@ -455,10 +455,7 @@ impl crate::Device for super::Device { raw.setLabel(Some(&NSString::from_str(label))); } self.counters.buffers.add(1); - Ok(super::Buffer { - raw, - size: desc.size, - }) + Ok((super::Buffer { raw }, desc.size)) }) } unsafe fn destroy_buffer(&self, _buffer: super::Buffer) { @@ -1078,14 +1075,9 @@ impl crate::Device for super::Device { let end = start + 1; bg.buffers .extend(desc.buffers[start..end].iter().map(|source| { - // Given the restrictions on `BufferBinding::offset`, - // this should never be `None`. - let remaining_size = wgt::BufferSize::new( - source.buffer.size - source.offset, - ); let binding_size = match ty { wgt::BufferBindingType::Storage { .. } => { - source.size.or(remaining_size) + Some(source.size) } _ => None, }; diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 08270e189..b845a929d 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -639,7 +639,6 @@ impl crate::Queue for Queue { #[derive(Debug)] pub struct Buffer { raw: Retained>, - size: wgt::BufferAddress, } unsafe impl Send for Buffer {} @@ -653,15 +652,6 @@ impl Buffer { } } -impl crate::BufferBinding<'_, Buffer> { - fn resolve_size(&self) -> wgt::BufferAddress { - match self.size { - Some(size) => size.get(), - None => self.buffer.size - self.offset, - } - } -} - #[derive(Debug)] pub struct Texture { raw: Retained>, diff --git a/wgpu-hal/src/noop/buffer.rs b/wgpu-hal/src/noop/buffer.rs index 9b8ab17a1..54078f6e0 100644 --- a/wgpu-hal/src/noop/buffer.rs +++ b/wgpu-hal/src/noop/buffer.rs @@ -14,12 +14,6 @@ pub struct Buffer { /// This data is potentially accessed mutably in arbitrary non-overlapping slices, /// so we must store it in `UnsafeCell` to avoid making any too-strong no-aliasing claims. storage: Arc>, - - /// Size of the allocation. - /// - /// This is redundant with `storage.get().len()`, but that method is not - /// available until our MSRV is 1.79 or greater. - size: usize, } /// SAFETY: @@ -30,7 +24,9 @@ unsafe impl Send for Buffer {} unsafe impl Sync for Buffer {} impl Buffer { - pub(super) fn new(desc: &crate::BufferDescriptor) -> Result { + pub(super) fn new( + desc: &crate::BufferDescriptor, + ) -> Result<(Self, wgt::BufferAddress), crate::DeviceError> { let &crate::BufferDescriptor { label: _, size, @@ -54,7 +50,7 @@ impl Buffer { let storage: Arc> = unsafe { Arc::from_raw(Arc::into_raw(storage) as *mut UnsafeCell<[u8]>) }; - Ok(Buffer { storage, size }) + Ok((Buffer { storage }, desc.size)) } /// Returns a pointer to the memory owned by this buffer within the given `range`. @@ -64,7 +60,7 @@ impl Buffer { /// to a reference. pub(super) fn get_slice_ptr(&self, range: crate::MemoryRange) -> *mut [u8] { let base_ptr = self.storage.get(); - let range = range_to_usize(range, self.size); + let range = range_to_usize(range, self.storage.get().len()); // We must obtain a slice pointer without ever creating a slice reference // that could alias with another slice. diff --git a/wgpu-hal/src/noop/command.rs b/wgpu-hal/src/noop/command.rs index 4150a1bc6..7620ca22b 100644 --- a/wgpu-hal/src/noop/command.rs +++ b/wgpu-hal/src/noop/command.rs @@ -172,14 +172,14 @@ impl crate::CommandEncoder for CommandBuffer { unsafe fn set_index_buffer<'a>( &mut self, - binding: crate::BufferBinding<'a, Buffer>, + binding: crate::BufferBinding<'a, Buffer, wgt::BufferSize>, format: wgt::IndexFormat, ) { } unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: crate::BufferBinding<'a, Buffer>, + binding: crate::BufferBinding<'a, Buffer, wgt::BufferSize>, ) { } unsafe fn set_viewport(&mut self, rect: &crate::Rect, depth_range: Range) {} diff --git a/wgpu-hal/src/noop/mod.rs b/wgpu-hal/src/noop/mod.rs index c8491ded4..4d3f45c77 100644 --- a/wgpu-hal/src/noop/mod.rs +++ b/wgpu-hal/src/noop/mod.rs @@ -295,7 +295,10 @@ impl crate::Queue for Context { impl crate::Device for Context { type A = Api; - unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult { + unsafe fn create_buffer( + &self, + desc: &crate::BufferDescriptor, + ) -> DeviceResult<(Buffer, wgt::BufferAddress)> { Buffer::new(desc) } diff --git a/wgpu-hal/src/vulkan/command.rs b/wgpu-hal/src/vulkan/command.rs index 71d1b54da..4bce740e7 100644 --- a/wgpu-hal/src/vulkan/command.rs +++ b/wgpu-hal/src/vulkan/command.rs @@ -995,7 +995,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_index_buffer<'a>( &mut self, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferSize>, format: wgt::IndexFormat, ) { unsafe { @@ -1010,7 +1010,7 @@ impl crate::CommandEncoder for super::CommandEncoder { unsafe fn set_vertex_buffer<'a>( &mut self, index: u32, - binding: crate::BufferBinding<'a, super::Buffer>, + binding: crate::BufferBinding<'a, super::Buffer, wgt::BufferSize>, ) { let vk_buffers = [binding.buffer.raw]; let vk_offsets = [binding.offset]; diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index c7c8129ad..63ba06899 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -911,7 +911,7 @@ impl crate::Device for super::Device { unsafe fn create_buffer( &self, desc: &crate::BufferDescriptor, - ) -> Result { + ) -> Result<(super::Buffer, wgt::BufferAddress), crate::DeviceError> { let vk_info = vk::BufferCreateInfo::default() .size(desc.size) .usage(conv::map_buffer_usage(desc.usage)) @@ -989,12 +989,15 @@ impl crate::Device for super::Device { self.counters.buffer_memory.add(allocation.size() as isize); self.counters.buffers.add(1); - Ok(super::Buffer { - raw, - ownership: super::BufferOwnership::Managed(Mutex::new( - super::BufferMemoryBacking::Managed(allocation), - )), - }) + Ok(( + super::Buffer { + raw, + ownership: super::BufferOwnership::Managed(Mutex::new( + super::BufferMemoryBacking::Managed(allocation), + )), + }, + desc.size, + )) } unsafe fn destroy_buffer(&self, buffer: super::Buffer) { match buffer.ownership { @@ -1713,9 +1716,7 @@ impl crate::Device for super::Device { vk::DescriptorBufferInfo::default() .buffer(binding.buffer.raw) .offset(binding.offset) - .range( - binding.size.map_or(vk::WHOLE_SIZE, wgt::BufferSize::get), - ) + .range(binding.size.get()) }, )); writes.push(