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.
This commit is contained in:
Andy Leiserson committed 2026-09-08 15:07:15 -07:00
1 parent 4f4dc63098
commit ea0afc6216
34 files changed
+304 -246

No files matched your search

+4
View File
@@ -153,6 +153,10 @@ By @sagudev in [#10109](https://github.com/gfx-rs/wgpu/pull/10109).
- Added `naga::valid::Capabilities::LINEAR_INTERPOLATION`, which is now required in order to use `@interpolate(linear)`. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972).
- The GLSL backend's `MissingFeatures` error now names the GLSL version that lacks the features, e.g. `GLSL 300 es doesn't support the required feature(s): NOPERSPECTIVE_QUALIFIER`. By @emilk in [#9972](https://github.com/gfx-rs/wgpu/pull/9972).
#### Metal
- Removed the `size` argument to `wgpu_hal::metal::Device::buffer_from_raw`. The passed size value was previously used only to resolve vertex buffer bindings without an explicit size, possibly incorrectly. Binding sizes are now resolved in `wgpu-core`. By @andyleiserson in [#9848](https://github.com/gfx-rs/wgpu/pull/9848).
### Bug Fixes
#### General
+1 -1
View File
@@ -2921,7 +2921,7 @@ async fn check_mark_externally_initialized_case<A: hal::Api>(ctx: &TestingContex
let hal_device = ctx.device.as_hal::<A>().expect("adapter backend mismatch");
let hal_texture = texture.as_hal::<A>().expect("adapter backend mismatch");
let staging_buffer = hal_device
let (staging_buffer, _allocated_size) = hal_device
.create_buffer(&hal::BufferDescriptor {
label: Some("mark_externally_initialized staging"),
size: buffer_size,
+12
View File
@@ -120,6 +120,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.")]
@@ -132,6 +143,7 @@ impl WebGpuError for BindingError {
Self::DestroyedResource(e) => e.webgpu_error_type(),
Self::BindingRangeTooLarge { .. }
| Self::BindingOffsetTooLarge { .. }
| Self::BindingOffsetEqualsSize { .. }
| BindingError::UnbindingVertexBufferOffsetNotZero { .. }
| BindingError::UnbindingVertexBufferSizeNotZero { .. } => ErrorType::Validation,
}
+20 -5
View File
@@ -1024,7 +1024,10 @@ fn set_index_buffer(
}
.into());
}
let end = offset + buffer.resolve_binding_size(offset, size)?;
let end = offset
+ buffer
.resolve_binding_size::<NonZeroU64>(offset, size)?
.get();
state
.buffer_memory_init_actions
@@ -1069,7 +1072,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
@@ -1535,7 +1538,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 {
@@ -1547,7 +1556,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 { offset, data } => {
@@ -1828,7 +1843,7 @@ impl State {
slot,
buffer: Some(buffer.clone()),
offset,
size,
size: Some(size),
});
});
}
+6 -5
View File
@@ -636,7 +636,7 @@ impl VertexState {
/// Call `f` for each dirty slot with `(slot_index, buffer, offset, size)` and mark them clean.
pub(crate) fn flush<F>(&mut self, mut f: F)
where
F: FnMut(u32, &Arc<Buffer>, BufferAddress, Option<BufferSize>),
F: FnMut(u32, &Arc<Buffer>, BufferAddress, BufferSize),
{
for (i, slot) in self.slots.iter_mut().enumerate() {
let Some(slot) = slot.as_mut() else { continue };
@@ -649,7 +649,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"),
);
}
}
@@ -2836,10 +2837,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(
@@ -2897,7 +2898,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
+2 -2
View File
@@ -23,13 +23,13 @@ pub enum RenderCommand<R: ReferenceType> {
buffer: R::Buffer,
index_format: wgt::IndexFormat,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferSize>, // optional in encoder; must be `Some` in a finished render bundle
},
SetVertexBuffer {
slot: u32,
buffer: Option<R::Buffer>,
offset: BufferAddress,
size: Option<BufferSize>,
size: Option<BufferSize>, // optional in encoder; must be `Some` in a finished render bundle
},
SetBlendConstant(Color),
SetStencilReference(u32),
+2 -1
View File
@@ -207,6 +207,7 @@ impl Device {
memory_flags: hal::MemoryFlags::PREFER_COHERENT,
})
.map_err(DeviceError::from_hal)?
.0
}))
} else {
None
@@ -317,7 +318,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),
+57 -36
View File
@@ -8,9 +8,8 @@ use alloc::{
use core::{
fmt,
mem::{self, ManuallyDrop},
num::NonZeroU32,
num::{NonZeroU32, NonZeroU64},
};
use hal::ShouldBeNonZeroExt;
use arrayvec::ArrayVec;
use bitflags::Flags;
@@ -172,6 +171,9 @@ pub struct ExternalTextureParams {
pub _padding: [u8; 4],
}
const _: () =
assert!(size_of::<ExternalTextureParams>() == wgpu_hal::EXTERNAL_TEXTURE_PARAMS_SIZE as usize);
impl ExternalTextureParams {
pub fn from_desc<L>(desc: &wgt::ExternalTextureDescriptor<L>) -> Self {
let gamut_conversion_matrix = [
@@ -525,7 +527,8 @@ impl Device {
memory_flags: hal::MemoryFlags::empty(),
})
}
.map_err(DeviceError::from_hal)?;
.map_err(DeviceError::from_hal)?
.0;
let zero_buffer = guard(zero_buffer, |buffer| unsafe { raw.destroy_buffer(buffer) });
let empty_bgl = unsafe {
@@ -546,12 +549,13 @@ impl Device {
Some("(wgpu internal) default external texture params buffer"),
instance_flags,
),
size: size_of::<ExternalTextureParams>() as _,
size: align_to(size_of::<ExternalTextureParams>() 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;
let default_external_texture_params_buffer =
guard(default_external_texture_params_buffer, |buffer| unsafe {
raw.destroy_buffer(buffer)
@@ -1217,34 +1221,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()
@@ -1285,7 +1300,7 @@ impl Device {
let mapping = map_buffer(
buffer,
tail_start,
actual_size - tail_start,
final_size - tail_start,
HostMap::Write,
snatch_guard,
)?;
@@ -1294,11 +1309,13 @@ impl Device {
.expect("newly-created buffer cannot be destroyed");
unsafe {
// SAFETY: The buffer tail is valid and aligned.
mapping.ptr.cast::<u32>().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);
}
@@ -1306,7 +1323,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
@@ -1323,7 +1340,7 @@ impl Device {
pending_writes.clear_buffer(
self,
&buffer,
tail_start..actual_size,
tail_start..final_size,
&snatch_guard,
)?;
return Ok(buffer);
@@ -1336,7 +1353,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;
@@ -1363,12 +1380,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
@@ -1568,7 +1585,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 unsafe fn create_buffer_from_hal(
@@ -1595,16 +1613,15 @@ 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 be initialized.
/// - `hal_buffer` must not have zero size.
/// Same safety requirements as `create_buffer_from_hal`.
pub(crate) unsafe fn create_buffer_from_hal_inner(
self: &Arc<Self>,
hal_buffer: Box<dyn hal::DynBuffer>,
desc: &resource::BufferDescriptor,
) -> Result<Arc<Buffer>, resource::CreateBufferError> {
let timestamp_normalization_bind_group = Snatchable::new(unsafe {
// SAFETY: The size passed here must be 4B aligned, >= the application size, and
// <= the buffer size.
self.timestamp_normalizer
.get()
.unwrap()
@@ -1654,6 +1671,10 @@ impl Device {
Ok(buffer)
}
/// 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,
@@ -3234,7 +3255,7 @@ impl Device {
late_buffer_binding_sizes: &mut FastHashMap<u32, wgt::BufferSize>,
used: &mut BindGroupStates,
snatch_guard: &'a SnatchGuard<'a>,
) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer>, CreateBindGroupError> {
) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer, NonZeroU64>, CreateBindGroupError> {
use crate::binding_model::CreateBindGroupError as Error;
let (binding_ty, dynamic, min_size) = match decl.ty {
@@ -3296,7 +3317,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
@@ -3566,8 +3588,7 @@ impl Device {
);
let params = external_texture_state
.params
.binding(0, None, snatch_guard)?
.0;
.binding(0, None, snatch_guard)?;
Ok(hal::ExternalTextureBinding { planes, params })
}
@@ -3620,7 +3641,7 @@ impl Device {
let params = hal::BufferBinding::new_unchecked(
self.default_external_texture_params_buffer.as_ref(),
0,
None,
NonZeroU64::new(size_of::<ExternalTextureParams>() as u64).unwrap(),
);
Ok(hal::ExternalTextureBinding { planes, params })
@@ -249,7 +249,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_buffer = guard(dst_buffer, |buffer| unsafe {
device.destroy_buffer(buffer)
@@ -270,7 +270,7 @@ impl Dispatch {
buffers: &[hal::BufferBinding::new_unchecked(
dst_buffer.as_ref(),
0,
Some(DST_BUFFER_SIZE),
DST_BUFFER_SIZE,
)],
samplers: &[],
textures: &[],
+1 -1
View File
@@ -770,7 +770,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,
+4
View File
@@ -80,6 +80,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,
+32 -19
View File
@@ -601,32 +601,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<wgt::BufferAddress> + TryFrom<wgt::BufferAddress>,
>(
&self,
offset: wgt::BufferAddress,
binding_size: Option<wgt::BufferSize>,
) -> Result<u64, BindingError> {
binding_size: Option<S>,
) -> Result<S, BindingError> {
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,
}
}
}),
}
}
@@ -655,13 +667,14 @@ impl Buffer {
offset: wgt::BufferAddress,
binding_size: Option<wgt::BufferSize>,
snatch_guard: &'a SnatchGuard,
) -> Result<(hal::BufferBinding<'a, dyn hal::DynBuffer>, u64), BindingError> {
) -> Result<hal::BufferBinding<'a, dyn hal::DynBuffer, wgt::BufferSize>, 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,
))
}
@@ -1341,7 +1354,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))?;
+1 -1
View File
@@ -14,7 +14,7 @@ pub struct ScratchBuffer {
impl ScratchBuffer {
pub(crate) fn new(device: &Arc<Device>, size: wgt::BufferSize) -> Result<Self, DeviceError> {
let raw = unsafe {
let (raw, _) = unsafe {
device
.raw()
.create_buffer(&hal::BufferDescriptor {
+10 -2
View File
@@ -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,
+5 -5
View File
@@ -300,7 +300,7 @@ impl<A: hal::Api> Example<A> {
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)
@@ -421,7 +421,7 @@ impl<A: hal::Api> Example<A> {
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();
@@ -446,7 +446,7 @@ impl<A: hal::Api> Example<A> {
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,
@@ -463,7 +463,7 @@ impl<A: hal::Api> Example<A> {
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,
@@ -503,7 +503,7 @@ impl<A: hal::Api> Example<A> {
let local_buffer_binding = hal::BufferBinding::new_unchecked(
&local_buffer,
0,
wgpu_types::BufferSize::new(size_of::<Locals>() as _),
wgpu_types::BufferSize::new(size_of::<Locals>() as _).unwrap(),
);
let local_group_desc = hal::BindGroupDescriptor {
label: Some("local"),
@@ -429,7 +429,7 @@ impl<A: hal::Api> Example<A> {
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,
@@ -455,7 +455,7 @@ impl<A: hal::Api> Example<A> {
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,
@@ -565,7 +565,7 @@ impl<A: hal::Api> Example<A> {
let instances_buffer_size = instances.len() * size_of::<AccelerationStructureInstance>();
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,
@@ -631,7 +631,7 @@ impl<A: hal::Api> Example<A> {
let uniforms_size = size_of::<Uniforms>();
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,
@@ -722,7 +722,7 @@ impl<A: hal::Api> Example<A> {
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"),
+5 -5
View File
@@ -479,7 +479,7 @@ impl super::CommandEncoder {
));
super::Buffer {
resource,
size: size.get(),
allocated_size: size.get(),
allocation,
}
};
@@ -1425,12 +1425,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),
};
@@ -1439,11 +1439,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;
}
+18 -10
View File
@@ -491,7 +491,7 @@ impl super::Device {
) -> super::Buffer {
super::Buffer {
resource,
size,
allocated_size: size,
allocation: suballocation::Allocation::none(
suballocation::AllocationType::Buffer,
size,
@@ -506,7 +506,7 @@ impl crate::Device for super::Device {
unsafe fn create_buffer(
&self,
desc: &crate::BufferDescriptor,
) -> Result<super::Buffer, crate::DeviceError> {
) -> Result<(super::Buffer, wgt::BufferAddress), crate::DeviceError> {
let mut desc = desc.clone();
if desc.usage.contains(wgt::BufferUses::UNIFORM) {
@@ -520,11 +520,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) {
@@ -1595,7 +1598,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 {
@@ -1607,8 +1610,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);
}
}
@@ -1789,7 +1797,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);
+9 -12
View File
@@ -917,10 +917,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,
}
@@ -935,14 +939,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<S> crate::BufferBinding<'_, Buffer, S> {
// TODO: Return GPU handle directly?
fn resolve_address(&self) -> wgt::BufferAddress {
(unsafe { self.buffer.resource.GetGPUVirtualAddress() }) + self.offset
+4 -4
View File
@@ -101,14 +101,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<f32>, depth_range: Range<f32>);
unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>);
@@ -645,7 +645,7 @@ impl<C: CommandEncoder + DynResource> 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();
@@ -655,7 +655,7 @@ impl<C: CommandEncoder + DynResource> 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) };
+4 -3
View File
@@ -21,7 +21,7 @@ pub trait DynDevice: DynResource {
unsafe fn create_buffer(
&self,
desc: &BufferDescriptor,
) -> Result<Box<dyn DynBuffer>, DeviceError>;
) -> Result<(Box<dyn DynBuffer>, wgt::BufferAddress), DeviceError>;
unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>);
unsafe fn add_raw_buffer(&self, buffer: &dyn DynBuffer);
@@ -186,8 +186,9 @@ impl<D: Device + DynResource> DynDevice for D {
unsafe fn create_buffer(
&self,
desc: &BufferDescriptor,
) -> Result<Box<dyn DynBuffer>, DeviceError> {
unsafe { D::create_buffer(self, desc) }.map(|b| -> Box<dyn DynBuffer> { Box::new(b) })
) -> Result<(Box<dyn DynBuffer>, wgt::BufferAddress), DeviceError> {
unsafe { D::create_buffer(self, desc) }
.map(|(b, size)| -> (Box<dyn DynBuffer>, _) { (Box::new(b), size) })
}
unsafe fn destroy_buffer(&self, buffer: Box<dyn DynBuffer>) {
+2 -2
View File
@@ -116,8 +116,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<B: DynBuffer>(self) -> BufferBinding<'a, B> {
impl<'a, S> BufferBinding<'a, dyn DynBuffer, S> {
pub fn expect_downcast<B: DynBuffer>(self) -> BufferBinding<'a, B, S> {
BufferBinding {
buffer: self.buffer.expect_downcast_ref(),
offset: self.offset,
+2 -2
View File
@@ -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];
+30 -30
View File
@@ -301,7 +301,6 @@ impl super::Device {
super::Buffer {
raw: Some(glow::NativeBuffer(name)),
target,
size: desc.size,
map_flags,
map_state: Arc::new(Mutex::new(super::BufferMapState {
mapped: false,
@@ -685,7 +684,7 @@ impl crate::Device for super::Device {
unsafe fn create_buffer(
&self,
desc: &crate::BufferDescriptor,
) -> Result<super::Buffer, crate::DeviceError> {
) -> Result<(super::Buffer, wgt::BufferAddress), crate::DeviceError> {
let target = if desc.usage.contains(wgt::BufferUses::INDEX) {
glow::ELEMENT_ARRAY_BUFFER
} else {
@@ -702,18 +701,20 @@ 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(Mutex::new(super::BufferMapState {
mapped: false,
data: Some(vec![0; desc.size as usize]),
offset_of_current_mapping: 0,
})),
drop_guard: None,
});
return Ok((
super::Buffer {
raw: None,
target,
map_flags: 0,
map_state: Arc::new(Mutex::new(super::BufferMapState {
mapped: false,
data: Some(vec![0; desc.size as usize]),
offset_of_current_mapping: 0,
})),
drop_guard: None,
},
desc.size,
));
}
let gl = &self.shared.context.lock();
@@ -806,18 +807,20 @@ 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(Mutex::new(super::BufferMapState {
mapped: false,
data,
offset_of_current_mapping: 0,
})),
drop_guard: None,
})
Ok((
super::Buffer {
raw,
target,
map_flags,
map_state: Arc::new(Mutex::new(super::BufferMapState {
mapped: false,
data,
offset_of_current_mapping: 0,
})),
drop_guard: None,
},
desc.size,
))
}
unsafe fn destroy_buffer(&self, buffer: super::Buffer) {
@@ -1491,10 +1494,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 { .. } => {
-1
View File
@@ -384,7 +384,6 @@ impl Drop for Queue {
pub struct Buffer {
raw: Option<glow::Buffer>,
target: BindTarget,
size: wgt::BufferAddress,
/// Flags to use within calls to [`Device::map_buffer`](crate::Device::map_buffer).
map_flags: u32,
/// Buffer mapping state.
+23 -25
View File
@@ -323,6 +323,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<wgt::BufferAddress>;
@@ -924,7 +925,7 @@ pub trait Device: WasmNotSendSync {
unsafe fn create_buffer(
&self,
desc: &BufferDescriptor,
) -> Result<<Self::A as Api>::Buffer, DeviceError>;
) -> Result<(<Self::A as Api>::Buffer, wgt::BufferAddress), DeviceError>;
/// Free `buffer` and any GPU resources it owns.
///
@@ -1676,13 +1677,13 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
unsafe fn set_index_buffer<'a>(
&mut self,
binding: BufferBinding<'a, <Self::A as Api>::Buffer>,
binding: BufferBinding<'a, <Self::A as Api>::Buffer, wgt::BufferSize>,
format: wgt::IndexFormat,
);
unsafe fn set_vertex_buffer<'a>(
&mut self,
index: u32,
binding: BufferBinding<'a, <Self::A as Api>::Buffer>,
binding: BufferBinding<'a, <Self::A as Api>::Buffer, wgt::BufferSize>,
);
unsafe fn set_viewport(&mut self, rect: &Rect<f32>, depth_range: Range<f32>);
unsafe fn set_scissor_rect(&mut self, rect: &Rect<u32>);
@@ -2211,6 +2212,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,
@@ -2360,7 +2370,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
@@ -2375,15 +2385,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<wgt::BufferSize>,
pub size: S,
}
// We must implement this manually because `B` is not necessarily `Clone`.
impl<B: DynBuffer + ?Sized> Clone for BufferBinding<'_, B> {
impl<B: DynBuffer + ?Sized, S: Copy> Clone for BufferBinding<'_, B, S> {
fn clone(&self) -> Self {
BufferBinding {
buffer: self.buffer,
@@ -2421,7 +2427,7 @@ impl ShouldBeNonZeroExt for Option<NonZeroU64> {
}
}
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
@@ -2432,21 +2438,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(<https://github.com/gfx-rs/wgpu/issues/3170>): remove the parameter
pub fn new_unchecked<S: Into<Option<NonZeroU64>>>(
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,
}
}
@@ -2474,7 +2472,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
@@ -2516,7 +2514,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],
+10 -11
View File
@@ -1408,7 +1408,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);
@@ -1423,7 +1423,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 = MAX_BUFFERS - 1 - index;
let encoder = self.state.render.as_ref().unwrap();
@@ -1435,15 +1435,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
+9 -22
View File
@@ -447,11 +447,8 @@ impl super::Device {
}
}
pub unsafe fn buffer_from_raw(
raw: Retained<ProtocolObject<dyn MTLBuffer>>,
size: wgt::BufferAddress,
) -> super::Buffer {
super::Buffer { raw, size }
pub unsafe fn buffer_from_raw(raw: Retained<ProtocolObject<dyn MTLBuffer>>) -> super::Buffer {
super::Buffer { raw }
}
pub fn raw_device(&self) -> &Retained<ProtocolObject<dyn MTLDevice>> {
@@ -462,7 +459,10 @@ impl super::Device {
impl crate::Device for super::Device {
type A = super::Api;
unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<super::Buffer> {
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);
@@ -487,10 +487,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) {
@@ -1147,12 +1144,7 @@ impl crate::Device for super::Device {
}
if let wgt::BufferBindingType::Storage { .. } = ty {
let remaining_size = wgt::BufferSize::new(
source.buffer.size - source.offset,
);
if let Some(binding_size) = source.size.or(remaining_size) {
array_element_sizes.push((idx as u32, binding_size));
}
array_element_sizes.push((idx as u32, source.size));
}
let use_info = bg
@@ -1241,14 +1233,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,
};
-10
View File
@@ -844,7 +844,6 @@ impl crate::Queue for Queue {
#[derive(Debug)]
pub struct Buffer {
raw: Retained<ProtocolObject<dyn MTLBuffer>>,
size: wgt::BufferAddress,
}
unsafe impl Send for Buffer {}
@@ -858,15 +857,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<ProtocolObject<dyn MTLTexture>>,
+5 -9
View File
@@ -8,12 +8,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<UnsafeCell<[u8]>>,
/// 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:
@@ -24,7 +18,9 @@ unsafe impl Send for Buffer {}
unsafe impl Sync for Buffer {}
impl Buffer {
pub(super) fn new(desc: &crate::BufferDescriptor) -> Result<Self, crate::DeviceError> {
pub(super) fn new(
desc: &crate::BufferDescriptor,
) -> Result<(Self, wgt::BufferAddress), crate::DeviceError> {
let &crate::BufferDescriptor {
label: _,
size,
@@ -48,7 +44,7 @@ impl Buffer {
let storage: Arc<UnsafeCell<[u8]>> =
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`.
@@ -58,7 +54,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.
+2 -2
View File
@@ -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<f32>, depth_range: Range<f32>) {}
+4 -1
View File
@@ -299,7 +299,10 @@ impl crate::Queue for Context {
impl crate::Device for Context {
type A = Api;
unsafe fn create_buffer(&self, desc: &crate::BufferDescriptor) -> DeviceResult<Buffer> {
unsafe fn create_buffer(
&self,
desc: &crate::BufferDescriptor,
) -> DeviceResult<(Buffer, wgt::BufferAddress)> {
Buffer::new(desc)
}
+2 -2
View File
@@ -1033,7 +1033,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 {
@@ -1048,7 +1048,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];
+11 -10
View File
@@ -997,7 +997,7 @@ impl crate::Device for super::Device {
unsafe fn create_buffer(
&self,
desc: &crate::BufferDescriptor,
) -> Result<super::Buffer, crate::DeviceError> {
) -> Result<(super::Buffer, wgt::BufferAddress), crate::DeviceError> {
let vk_info = vk::BufferCreateInfo::default()
.size(desc.size)
.usage(conv::map_buffer_usage(desc.usage))
@@ -1073,12 +1073,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 {
@@ -1826,9 +1829,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(