From 80e59f179f0d0b5a67a076089c854c6bf55383af Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Thu, 24 Sep 2026 15:17:23 +0200 Subject: [PATCH] less in wgpu Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- wgpu-core/src/resource.rs | 9 +++- wgpu/src/backend/webgpu.rs | 4 -- wgpu/src/backend/wgpu_core.rs | 81 +++++++++++++++++++---------------- wgpu/src/dispatch.rs | 4 -- 4 files changed, 52 insertions(+), 46 deletions(-) diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 1926a9ebb..516f4034d 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -1496,12 +1496,17 @@ impl StagingBuffer { Ok(staging_buffer) } - /// SAFETY: You must not call any functions of `self` + /// # Safety + /// You must not call any functions of `self` /// until you stopped using the returned pointer. - pub(crate) unsafe fn ptr(&self) -> NonNull { + pub unsafe fn ptr(&self) -> NonNull { self.ptr } + pub fn size(&self) -> wgt::BufferSize { + self.size + } + #[cfg(feature = "trace")] pub(crate) fn get_data(&self) -> &[u8] { unsafe { core::slice::from_raw_parts(self.ptr.as_ptr(), self.size.get() as usize) } diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index a9ec15b9a..7bf97a70c 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -4640,10 +4640,6 @@ impl WebBufferMappedRange { } } impl dispatch::BufferMappedRangeInterface for WebBufferMappedRange { - fn len(&self) -> usize { - self.get_temporary_mapping().len() - } - #[inline] unsafe fn read_slice(&self) -> &[u8] { self.get_temporary_mapping() diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 516bb0e67..e08829685 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -25,7 +25,7 @@ use wgt::WasmNotSendSync; use crate::{ api, - dispatch::{self, BlasCompactCallback, BufferMappedRangeInterface}, + dispatch::{self, BlasCompactCallback}, BindingResource, Blas, BufferBinding, BufferDescriptor, Features, LoadOp, Operations, ShaderSource, SurfaceTargetUnsafe, TextureDescriptor, Tlas, WriteOnly, }; @@ -673,16 +673,10 @@ impl fmt::Debug for CoreSurfaceOutputDetail { #[derive(Debug)] pub struct CoreQueueWriteBuffer { wgpu_staging_buffer: wgc::resource::StagingBuffer, - mapping: CoreBufferMappedRange, } #[derive(Debug)] -pub struct CoreBufferMappedRange { - ptr: NonNull, - size: usize, - // Stagging - _guard: Option, -} +pub struct CoreBufferMappedRange(BufferMapping); #[cfg(send_sync)] unsafe impl Send for CoreBufferMappedRange {} @@ -723,8 +717,32 @@ crate::cmp::impl_eq_ord_hash_box_address!(CoreRenderBundleEncoder => .encoder); crate::cmp::impl_eq_ord_hash_arc_address!(CoreRenderBundle => .wgpu_render_bundle); crate::cmp::impl_eq_ord_hash_arc_address!(CoreSurface => .wgpu_surface); crate::cmp::impl_eq_ord_hash_arc_address!(CoreSurfaceOutputDetail => .wgpu_surface); -crate::cmp::impl_eq_ord_hash_proxy!(CoreQueueWriteBuffer => .mapping.ptr); -crate::cmp::impl_eq_ord_hash_proxy!(CoreBufferMappedRange => .ptr); +impl PartialEq for CoreQueueWriteBuffer { + fn eq(&self, other: &Self) -> bool { + unsafe { self.wgpu_staging_buffer.ptr() == other.wgpu_staging_buffer.ptr() } + } +} +impl Eq for CoreQueueWriteBuffer {} +impl PartialOrd for CoreQueueWriteBuffer { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} +impl Ord for CoreQueueWriteBuffer { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + unsafe { + self.wgpu_staging_buffer + .ptr() + .cmp(&other.wgpu_staging_buffer.ptr()) + } + } +} +impl core::hash::Hash for CoreQueueWriteBuffer { + fn hash(&self, state: &mut H) { + unsafe { self.wgpu_staging_buffer.ptr().hash(state) } + } +} +crate::cmp::impl_eq_ord_hash_proxy!(CoreBufferMappedRange => .0.ptr()); impl dispatch::InstanceInterface for ContextWgpuCore { fn new(desc: wgt::InstanceDescriptor) -> Self @@ -1620,16 +1638,9 @@ impl dispatch::QueueInterface for CoreQueue { size: crate::BufferSize, ) -> Option { match self.wgpu_queue.create_staging_buffer(size) { - Ok((wgpu_staging_buffer, ptr)) => Some( + Ok((wgpu_staging_buffer, _)) => Some( CoreQueueWriteBuffer { wgpu_staging_buffer, - mapping: CoreBufferMappedRange { - ptr, - size: size.get() as usize, - // stagging buffer is the one keeping the mapping alive - // so no guard is needed here - _guard: None, - }, } .into(), ), @@ -1830,14 +1841,7 @@ impl dispatch::BufferInterface for CoreBuffer { let size = sub_range.end - sub_range.start; self.wgpu_buffer .get_mapped_range(sub_range.start, Some(size)) - .map(|mapping| { - CoreBufferMappedRange { - ptr: mapping.ptr(), - size: mapping.len() as usize, - _guard: Some(mapping), - } - .into() - }) + .map(|mapping| CoreBufferMappedRange(mapping).into()) .map_err(|err| crate::MapRangeError(format_error(&err))) } @@ -2959,31 +2963,36 @@ impl dispatch::SurfaceOutputDetailInterface for CoreSurfaceOutputDetail { impl dispatch::QueueWriteBufferInterface for CoreQueueWriteBuffer { #[inline] fn len(&self) -> usize { - self.mapping.len() + self.wgpu_staging_buffer.size().get() as usize } #[inline] unsafe fn write_slice(&mut self) -> WriteOnly<'_, [u8]> { - unsafe { self.mapping.write_slice() } + unsafe { + WriteOnly::new(NonNull::slice_from_raw_parts( + self.wgpu_staging_buffer.ptr(), + self.len(), + )) + } } } impl dispatch::BufferMappedRangeInterface for CoreBufferMappedRange { - #[inline] - fn len(&self) -> usize { - self.size - } - #[inline] unsafe fn read_slice(&self) -> &[u8] { // FIXME: We currently tolerate this - // https://github.com/gfx-rs/wgpu/pull/10307#discussion_r4093841117 - unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.size) } + // https://github.com/gfx-rs/wgpu/issues/10549 + unsafe { slice::from_raw_parts(self.0.ptr().as_ptr(), self.0.len() as usize) } } #[inline] unsafe fn write_slice(&mut self) -> WriteOnly<'_, [u8]> { - unsafe { WriteOnly::new(NonNull::slice_from_raw_parts(self.ptr, self.size)) } + unsafe { + WriteOnly::new(NonNull::slice_from_raw_parts( + self.0.ptr(), + self.0.len() as usize, + )) + } } #[cfg(webgpu)] diff --git a/wgpu/src/dispatch.rs b/wgpu/src/dispatch.rs index 17d1975ae..60ec54f86 100644 --- a/wgpu/src/dispatch.rs +++ b/wgpu/src/dispatch.rs @@ -667,10 +667,6 @@ pub trait QueueWriteBufferInterface: CommonTraits { } pub trait BufferMappedRangeInterface: CommonTraits { - // Used only in wgpu_core's `impl QueueWriteBufferInterface` - #[cfg_attr(not(wgpu_core), expect(unused))] - fn len(&self) -> usize; - /// # Safety /// /// Must only be used on read, not write, mappings.