From 468632f20747079b242c46fae97e43bf67014ddd Mon Sep 17 00:00:00 2001 From: Andy Leiserson Date: Wed, 9 Jul 2025 11:31:06 -0700 Subject: [PATCH] Restore plumbing of implicit remainder-of-buffer size to backends --- cts_runner/test.lst | 2 + wgpu-core/src/command/bundle.rs | 26 ++++-------- wgpu-core/src/command/render.rs | 40 ++++++++---------- wgpu-core/src/command/render_command.rs | 17 +++----- wgpu-core/src/device/resource.rs | 10 ++--- wgpu-core/src/indirect_validation/dispatch.rs | 2 +- wgpu-core/src/resource.rs | 41 ++++++++++--------- wgpu-hal/examples/halmark/main.rs | 12 ++++-- wgpu-hal/src/dx12/command.rs | 4 +- wgpu-hal/src/dx12/device.rs | 2 +- wgpu-hal/src/dx12/mod.rs | 7 ++++ wgpu-hal/src/gles/device.rs | 10 +++-- wgpu-hal/src/gles/mod.rs | 1 + wgpu-hal/src/lib.rs | 25 ++++++++--- wgpu-hal/src/metal/command.rs | 16 +++++--- wgpu-hal/src/metal/device.rs | 21 +++++++--- wgpu-hal/src/metal/mod.rs | 10 +++++ wgpu-hal/src/vulkan/device.rs | 6 +-- wgpu-types/src/lib.rs | 6 --- 19 files changed, 144 insertions(+), 114 deletions(-) diff --git a/cts_runner/test.lst b/cts_runner/test.lst index 163944db9..6432ffdd3 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -27,6 +27,8 @@ webgpu:api,validation,encoding,cmds,copyTextureToTexture:sample_count:* //FAIL: webgpu:api,validation,encoding,cmds,copyTextureToTexture:copy_ranges_with_compressed_texture_formats:* webgpu:api,validation,encoding,cmds,index_access:* //FAIL: webgpu:api,validation,encoding,cmds,render,draw:* +webgpu:api,validation,encoding,cmds,render,draw:index_buffer_OOB:* +webgpu:api,validation,encoding,cmds,render,draw:unused_buffer_bound:* webgpu:api,validation,encoding,encoder_state:* webgpu:api,validation,encoding,encoder_open_state:non_pass_commands:* webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:* diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 6a9a0b75b..d883f3711 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -505,7 +505,7 @@ impl RenderBundleEncoder { buffer_id, index_format, offset, - size: size.map(NonZeroU64::get), + size, }); } } @@ -610,7 +610,7 @@ fn set_index_buffer( buffer_id: id::Id, index_format: wgt::IndexFormat, offset: u64, - size: Option, + size: Option, ) -> Result<(), RenderBundleErrorInner> { let buffer = buffer_guard.get(buffer_id).get()?; @@ -642,7 +642,7 @@ fn set_vertex_buffer( slot: u32, buffer_id: id::Id, offset: u64, - size: Option, + size: Option, ) -> Result<(), RenderBundleErrorInner> { let max_vertex_buffers = state.device.limits.max_vertex_buffers; if slot >= max_vertex_buffers { @@ -967,11 +967,7 @@ impl RenderBundle { let bb = unsafe { // SAFETY: The binding size was checked against the buffer size // in `set_index_buffer` and again in `IndexState::flush`. - hal::BufferBinding::new_unchecked( - buffer, - *offset, - size.expect("size was resolved in `RenderBundleEncoder::finish`"), - ) + hal::BufferBinding::new_unchecked(buffer, *offset, *size) }; unsafe { raw.set_index_buffer(bb, *index_format) }; } @@ -985,11 +981,7 @@ impl RenderBundle { let bb = unsafe { // SAFETY: The binding size was checked against the buffer size // in `set_vertex_buffer` and again in `VertexState::flush`. - hal::BufferBinding::new_unchecked( - buffer, - *offset, - size.expect("size was resolved in `RenderBundleEncoder::finish`"), - ) + hal::BufferBinding::new_unchecked(buffer, *offset, *size) }; unsafe { raw.set_vertex_buffer(*slot, bb) }; } @@ -1176,7 +1168,7 @@ impl IndexState { buffer: self.buffer.clone(), index_format: self.format, offset: self.range.start, - size: Some(binding_size), + size: NonZeroU64::new(binding_size), }) } else { None @@ -1232,7 +1224,7 @@ impl VertexState { slot, buffer: self.buffer.clone(), offset: self.range.start, - size: Some(binding_size), + size: NonZeroU64::new(binding_size), }) } else { None @@ -1596,7 +1588,7 @@ where pub mod bundle_ffi { use super::{RenderBundleEncoder, RenderCommand}; use crate::{id, RawString}; - use core::{convert::TryInto, num::NonZeroU64, slice}; + use core::{convert::TryInto, slice}; use wgt::{BufferAddress, BufferSize, DynamicOffset, IndexFormat}; /// # Safety @@ -1655,7 +1647,7 @@ pub mod bundle_ffi { slot, buffer_id, offset, - size: size.map(NonZeroU64::get), + size, }); } diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 58bda99c4..96c09c3c1 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -1,17 +1,12 @@ use alloc::{borrow::Cow, sync::Arc, vec::Vec}; -use core::{ - fmt, - num::{NonZeroU32, NonZeroU64}, - str, -}; -use hal::ShouldBeNonZeroExt; +use core::{fmt, num::NonZeroU32, ops::Range, str}; use arrayvec::ArrayVec; use thiserror::Error; use wgt::{ error::{ErrorType, WebGpuError}, - BufferAddress, BufferSize, BufferSizeOrZero, BufferUsages, Color, DynamicOffset, IndexFormat, - ShaderStages, TextureSelector, TextureUsages, TextureViewDimension, VertexStepMode, + BufferAddress, BufferSize, BufferUsages, Color, DynamicOffset, IndexFormat, ShaderStages, + TextureSelector, TextureUsages, TextureViewDimension, VertexStepMode, }; use crate::command::{ @@ -362,17 +357,13 @@ struct IndexState { } impl IndexState { - fn update_buffer( - &mut self, - binding: &hal::BufferBinding<'_, B>, - format: IndexFormat, - ) { + fn update_buffer(&mut self, range: Range, format: IndexFormat) { self.buffer_format = Some(format); let shift = match format { IndexFormat::Uint16 => 1, IndexFormat::Uint32 => 2, }; - self.limit = binding.size.get() >> shift; + self.limit = (range.end - range.start) >> shift; } fn reset(&mut self) { @@ -2339,7 +2330,7 @@ fn set_index_buffer( buffer: Arc, index_format: IndexFormat, offset: u64, - size: Option, + size: Option, ) -> Result<(), RenderPassErrorInner> { api_log!("RenderPass::set_index_buffer {}", buffer.error_ident()); @@ -2353,15 +2344,16 @@ fn set_index_buffer( buffer.check_usage(BufferUsages::INDEX)?; - let binding = buffer + let (binding, resolved_size) = buffer .binding(offset, size, state.general.snatch_guard) .map_err(RenderCommandError::from)?; - state.index.update_buffer(&binding, index_format); + let end = offset + resolved_size; + state.index.update_buffer(offset..end, index_format); state.general.buffer_memory_init_actions.extend( buffer.initialization_status.read().create_action( &buffer, - offset..(offset + binding.size.get()), + offset..end, MemoryInitKind::NeedsInitializedMemory, ), ); @@ -2379,7 +2371,7 @@ fn set_vertex_buffer( slot: u32, buffer: Arc, offset: u64, - size: Option, + size: Option, ) -> Result<(), RenderPassErrorInner> { api_log!( "RenderPass::set_vertex_buffer {slot} {}", @@ -2405,15 +2397,15 @@ fn set_vertex_buffer( buffer.check_usage(BufferUsages::VERTEX)?; - let binding = buffer + let (binding, buffer_size) = buffer .binding(offset, size, state.general.snatch_guard) .map_err(RenderCommandError::from)?; - state.vertex.buffer_sizes[slot as usize] = Some(binding.size.get()); + state.vertex.buffer_sizes[slot as usize] = Some(buffer_size); state.general.buffer_memory_init_actions.extend( buffer.initialization_status.read().create_action( &buffer, - offset..(offset + binding.size.get()), + offset..(offset + buffer_size), MemoryInitKind::NeedsInitializedMemory, ), ); @@ -3090,7 +3082,7 @@ impl Global { buffer: pass_try!(base, scope, self.resolve_render_pass_buffer_id(buffer_id)), index_format, offset, - size: size.map(NonZeroU64::get), + size, }); Ok(()) @@ -3111,7 +3103,7 @@ impl Global { slot, buffer: pass_try!(base, scope, self.resolve_render_pass_buffer_id(buffer_id)), offset, - size: size.map(NonZeroU64::get), + size, }); Ok(()) diff --git a/wgpu-core/src/command/render_command.rs b/wgpu-core/src/command/render_command.rs index f57ec026e..656423854 100644 --- a/wgpu-core/src/command/render_command.rs +++ b/wgpu-core/src/command/render_command.rs @@ -1,6 +1,6 @@ use alloc::sync::Arc; -use wgt::{BufferAddress, BufferSizeOrZero, Color}; +use wgt::{BufferAddress, BufferSize, Color}; use super::{Rect, RenderBundle}; use crate::{ @@ -24,13 +24,13 @@ pub enum RenderCommand { buffer_id: id::BufferId, index_format: wgt::IndexFormat, offset: BufferAddress, - size: Option, + size: Option, }, SetVertexBuffer { slot: u32, buffer_id: id::BufferId, offset: BufferAddress, - size: Option, + size: Option, }, SetBlendConstant(Color), SetStencilReference(u32), @@ -416,20 +416,13 @@ pub enum ArcRenderCommand { buffer: Arc, index_format: wgt::IndexFormat, offset: BufferAddress, - - // For a render pass, this reflects the argument passed by the - // application, which may be `None`. For a finished render bundle, this - // reflects the validated size of the binding, and will be populated - // even in the case that the application omitted the size. - size: Option, + size: Option, }, SetVertexBuffer { slot: u32, buffer: Arc, offset: BufferAddress, - - // See comment in `SetIndexBuffer`. - size: Option, + size: Option, }, SetBlendConstant(Color), SetStencilReference(u32), diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 5afc4d6b3..302a74e34 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -8,7 +8,7 @@ use alloc::{ use core::{ fmt, mem::{self, ManuallyDrop}, - num::{NonZeroU32, NonZeroU64}, + num::NonZeroU32, sync::atomic::{AtomicBool, Ordering}, }; use hal::ShouldBeNonZeroExt; @@ -2198,8 +2198,8 @@ impl Device { buffer.check_usage(pub_usage)?; - let bb = buffer.binding(bb.offset, bb.size.map(NonZeroU64::get), snatch_guard)?; - let bind_size = bb.size.get(); + let (bb, bind_size) = buffer.binding(bb.offset, bb.size, snatch_guard)?; + let bind_end = bb.offset + bind_size; if bind_size > range_limit as u64 { return Err(Error::BufferRangeTooLarge { @@ -2214,8 +2214,8 @@ impl Device { dynamic_binding_info.push(binding_model::BindGroupDynamicBindingData { binding_idx: binding, buffer_size: buffer.size, - binding_range: bb.offset..bb.offset + bind_size, - maximum_dynamic_offset: buffer.size - bb.offset - bind_size, + binding_range: bb.offset..bind_end, + maximum_dynamic_offset: buffer.size - bind_end, binding_type: binding_ty, }); } diff --git a/wgpu-core/src/indirect_validation/dispatch.rs b/wgpu-core/src/indirect_validation/dispatch.rs index e9fe4971b..aa89619b7 100644 --- a/wgpu-core/src/indirect_validation/dispatch.rs +++ b/wgpu-core/src/indirect_validation/dispatch.rs @@ -234,7 +234,7 @@ impl Dispatch { }], buffers: &[unsafe { // SAFETY: We just created the buffer with this size. - hal::BufferBinding::new_unchecked(dst_buffer.as_ref(), 0, DST_BUFFER_SIZE) + hal::BufferBinding::new_unchecked(dst_buffer.as_ref(), 0, Some(DST_BUFFER_SIZE)) }], samplers: &[], textures: &[], diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 1bfde1db7..eaf3a3778 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -508,17 +508,17 @@ impl Buffer { pub fn resolve_binding_size( &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) { - Some(end) if end <= buffer_size => Ok(binding_size), + Some(binding_size) => match offset.checked_add(binding_size.get()) { + Some(end) if end <= buffer_size => Ok(binding_size.get()), _ => Err(BindingError::BindingRangeTooLarge { buffer: self.error_ident(), offset, - binding_size, + binding_size: binding_size.get(), buffer_size, }), }, @@ -535,35 +535,38 @@ impl Buffer { } /// Create a new [`hal::BufferBinding`] for the buffer with `offset` and - /// `size`. + /// `binding_size`. /// - /// If `size` is `None`, then the remainder of the buffer starting from - /// `offset` is used. + /// If `binding_size` is `None`, then the remainder of the buffer starting + /// from `offset` is used. /// /// If the binding would overflow the buffer, then an error is returned. /// - /// Zero-size bindings are permitted here for historical reasons. Although + /// A zero-size binding at the end of the buffer is permitted here for historical reasons. Although /// zero-size bindings are permitted by WebGPU, they are not permitted by - /// some backends. Previous documentation for `hal::BufferBinding` - /// disallowed zero-size bindings, but this restriction was not honored - /// elsewhere in the code. Zero-size bindings need to be quashed or remapped - /// to a non-zero size, either universally in wgpu-core, or in specific - /// backends that do not support them. See + /// some backends. The zero-size binding need to be quashed or remapped to a + /// non-zero size, either universally in wgpu-core, or in specific backends + /// that do not support them. See /// [#3170](https://github.com/gfx-rs/wgpu/issues/3170). + /// + /// Although it seems like it would be simpler and safer to use the resolved + /// size in the returned [`hal::BufferBinding`], doing this (and removing + /// redundant logic in backends to resolve the implicit size) was observed + /// to cause problems in certain CTS tests, so an implicit size + /// specification is preserved in the output. pub fn binding<'a>( &'a self, offset: wgt::BufferAddress, - binding_size: Option, + binding_size: Option, snatch_guard: &'a SnatchGuard, - ) -> Result, BindingError> { + ) -> Result<(hal::BufferBinding<'a, dyn hal::DynBuffer>, u64), BindingError> { let buf_raw = self.try_raw(snatch_guard)?; let resolved_size = self.resolve_binding_size(offset, binding_size)?; unsafe { // 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, + Ok(( + hal::BufferBinding::new_unchecked(buf_raw, offset, binding_size), resolved_size, )) } diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 60eb3d59b..0b1c3c28d 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -14,7 +14,9 @@ use winit::{ use std::{ borrow::{Borrow, Cow}, - iter, ptr, + iter, + num::NonZeroU64, + ptr, time::Instant, }; @@ -447,7 +449,11 @@ impl Example { let global_group = { let global_buffer_binding = unsafe { // SAFETY: This is the same size that was specified for buffer creation. - hal::BufferBinding::new_unchecked(&global_buffer, 0, global_buffer_desc.size) + hal::BufferBinding::new_unchecked( + &global_buffer, + 0, + NonZeroU64::new(global_buffer_desc.size), + ) }; let texture_binding = hal::TextureBinding { view: &texture_view, @@ -487,7 +493,7 @@ impl Example { hal::BufferBinding::new_unchecked( &local_buffer, 0, - wgpu_types::BufferSize::new(size_of::() as _).unwrap(), + wgpu_types::BufferSize::new(size_of::() as _), ) }; let local_group_desc = hal::BindGroupDescriptor { diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index 39e18af13..329534559 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -1136,7 +1136,7 @@ impl crate::CommandEncoder for super::CommandEncoder { ) { let ibv = Direct3D12::D3D12_INDEX_BUFFER_VIEW { BufferLocation: binding.resolve_address(), - SizeInBytes: binding.size.try_into().unwrap(), + SizeInBytes: binding.resolve_size().try_into().unwrap(), Format: auxil::dxgi::conv::map_index_format(format), }; @@ -1149,7 +1149,7 @@ impl crate::CommandEncoder for super::CommandEncoder { ) { let vb = &mut self.pass.vertex_buffers[index as usize]; vb.BufferLocation = binding.resolve_address(); - vb.SizeInBytes = binding.size.try_into().unwrap(); + vb.SizeInBytes = binding.resolve_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 e4b8e449e..19e7743d6 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1442,7 +1442,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.size.try_into().unwrap(); + let mut size = data.resolve_size().try_into().unwrap(); if has_dynamic_offset { match ty { diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index cb5b1974f..c8e6c3e0c 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -865,6 +865,13 @@ 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, + } + } + // TODO: Return GPU handle directly? fn resolve_address(&self) -> wgt::BufferAddress { (unsafe { self.buffer.resource.GetGPUVirtualAddress() }) + self.offset diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index 17a2fe722..0f36f734b 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -534,6 +534,7 @@ impl crate::Device for super::Device { return Ok(super::Buffer { raw: None, target, + size: desc.size, map_flags: 0, data: Some(Arc::new(MaybeMutex::new(vec![0; desc.size as usize]))), offset_of_current_mapping: Arc::new(MaybeMutex::new(0)), @@ -633,6 +634,7 @@ impl crate::Device for super::Device { Ok(super::Buffer { raw, target, + size: desc.size, map_flags, data, offset_of_current_mapping: Arc::new(MaybeMutex::new(0)), @@ -1261,11 +1263,13 @@ impl crate::Device for super::Device { let binding = match layout.ty { wgt::BindingType::Buffer { .. } => { let bb = &desc.buffers[entry.resource_index as usize]; - assert!(bb.size != 0, "zero-size bindings are not supported"); super::RawBinding::Buffer { raw: bb.buffer.raw.unwrap(), - offset: bb.offset.try_into().unwrap(), - size: bb.size.try_into().unwrap(), + offset: bb.offset as i32, + size: match bb.size { + Some(s) => s.get() as i32, + None => (bb.buffer.size - bb.offset) as i32, + }, } } wgt::BindingType::Sampler { .. } => { diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index c1b226f8c..a6073b4ec 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -342,6 +342,7 @@ impl Drop for Queue { pub struct Buffer { raw: Option, target: BindTarget, + size: wgt::BufferAddress, map_flags: u32, data: Option>>>, offset_of_current_mapping: Arc>, diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 0ffdac35e..83f88690f 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -1972,8 +1972,8 @@ pub struct PipelineLayoutDescriptor<'a, B: DynBindGroupLayout + ?Sized> { /// /// The recommended way to construct a `BufferBinding` is using the `binding` /// method on a wgpu-core `Buffer`, which will validate the binding size -/// against the buffer size. An unsafe `new_unchecked` constructor is also -/// provided for cases where direct construction is necessary. +/// against the buffer size. A `new_unchecked` constructor is also provided for +/// cases where direct construction is necessary. /// /// ## Accessible region /// @@ -2035,7 +2035,11 @@ pub struct BufferBinding<'a, B: DynBuffer + ?Sized> { pub offset: wgt::BufferAddress, /// The size of the region bound, in bytes. - pub size: wgt::BufferSizeOrZero, + /// + /// 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, } // We must implement this manually because `B` is not necessarily `Clone`. @@ -2068,6 +2072,15 @@ impl ShouldBeNonZeroExt for u64 { } } +impl ShouldBeNonZeroExt for Option { + fn get(&self) -> u64 { + match *self { + Some(non_zero) => non_zero.get(), + None => 0, + } + } +} + impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> { /// Construct a `BufferBinding` with the given contents. /// @@ -2082,10 +2095,10 @@ impl<'a, B: DynBuffer + ?Sized> BufferBinding<'a, B> { /// bytes starting at `offset` is contained within the buffer. /// /// The `S` type parameter is a temporary convenience to allow callers to - /// pass either a `u64` or a `NonZeroU64`. When the zero-size binding issue - /// is resolved, the argument should just match the type of the member. + /// 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 unsafe fn new_unchecked>( + pub unsafe fn new_unchecked>>( buffer: &'a B, offset: wgt::BufferAddress, size: S, diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index 9f331fd46..72a799a02 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -4,7 +4,7 @@ use alloc::{ borrow::{Cow, ToOwned as _}, vec::Vec, }; -use core::{num::NonZeroU64, ops::Range}; +use core::ops::Range; use metal::{ MTLIndexType, MTLLoadAction, MTLPrimitiveType, MTLScissorRect, MTLSize, MTLStoreAction, MTLViewport, MTLVisibilityResultMode, NSRange, @@ -977,11 +977,15 @@ impl crate::CommandEncoder for super::CommandEncoder { let encoder = self.state.render.as_ref().unwrap(); encoder.set_vertex_buffer(buffer_index, Some(&binding.buffer.raw), binding.offset); - // https://github.com/gfx-rs/wgpu/issues/3170 - let size = - NonZeroU64::new(binding.size).expect("zero-size vertex buffers are not supported"); - - self.state.vertex_buffer_size_map.insert(buffer_index, size); + 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); + } if let Some((index, sizes)) = self .state diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index ba40854dd..ef8e7c83a 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -1,5 +1,5 @@ use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec}; -use core::{num::NonZeroU64, ptr::NonNull, sync::atomic}; +use core::{ptr::NonNull, sync::atomic}; use std::{thread, time}; use parking_lot::Mutex; @@ -340,6 +340,10 @@ impl super::Device { } } + pub unsafe fn buffer_from_raw(raw: metal::Buffer, size: wgt::BufferAddress) -> super::Buffer { + super::Buffer { raw, size } + } + pub fn raw_device(&self) -> &Mutex { &self.shared.device } @@ -369,7 +373,10 @@ impl crate::Device for super::Device { raw.set_label(label); } self.counters.buffers.add(1); - Ok(super::Buffer { raw }) + Ok(super::Buffer { + raw, + size: desc.size, + }) }) } unsafe fn destroy_buffer(&self, _buffer: super::Buffer) { @@ -928,12 +935,14 @@ impl crate::Device for super::Device { let end = start + 1; bg.buffers .extend(desc.buffers[start..end].iter().map(|source| { - // https://github.com/gfx-rs/wgpu/issues/3170 - let source_size = NonZeroU64::new(source.size) - .expect("zero-size bindings are not supported"); + // 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 { .. } => { - Some(source_size) + source.size.or(remaining_size) } _ => None, }; diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 30af14a33..b5ae1dd5d 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -502,6 +502,7 @@ impl crate::Queue for Queue { #[derive(Debug)] pub struct Buffer { raw: metal::Buffer, + size: wgt::BufferAddress, } unsafe impl Send for Buffer {} @@ -515,6 +516,15 @@ 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: metal::Texture, diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index 5251d0e05..e760c4946 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -1804,12 +1804,12 @@ impl crate::Device for super::Device { (buffer_infos, local_buffer_infos) = buffer_infos.extend(desc.buffers[start as usize..end as usize].iter().map( |binding| { - // https://github.com/gfx-rs/wgpu/issues/3170 - assert!(binding.size != 0, "zero-size bindings are not supported"); vk::DescriptorBufferInfo::default() .buffer(binding.buffer.raw) .offset(binding.offset) - .range(binding.size) + .range( + binding.size.map_or(vk::WHOLE_SIZE, wgt::BufferSize::get), + ) }, )); write.buffer_info(local_buffer_infos) diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 0b676dd8c..ea821512e 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -61,12 +61,6 @@ pub type BufferAddress = u64; /// [`BufferSlice`]: ../wgpu/struct.BufferSlice.html pub type BufferSize = core::num::NonZeroU64; -/// Integral type used for buffer sizes that may be zero. -/// -/// Although the wgpu Rust API disallows zero-size `BufferSlice` and wgpu-hal -/// disallows zero-size bindings, WebGPU permits zero-size buffers and bindings. -pub type BufferSizeOrZero = u64; - /// Integral type used for binding locations in shaders. /// /// Used in [`VertexAttribute`]s and errors.