less in wgpu

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
This commit is contained in:
sagudev authored and mergify[bot] committed 2026-10-08 11:08:30 +00:00
1 parent 322e22792d
commit 80e59f179f
4 files changed
+52 -46

No files matched your search

+7 -2
View File
@@ -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<u8> {
pub unsafe fn ptr(&self) -> NonNull<u8> {
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) }
-4
View File
@@ -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()
+45 -36
View File
@@ -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<u8>,
size: usize,
// Stagging
_guard: Option<BufferMapping>,
}
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<core::cmp::Ordering> {
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<H: core::hash::Hasher>(&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<dispatch::DispatchQueueWriteBuffer> {
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)]
-4
View File
@@ -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.