From 5266bd1f08662710a57ff412f2cf86f419ef99cb Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Thu, 4 Jul 2024 12:46:24 +0200 Subject: [PATCH] change `prepare_staging_buffer` to return a non null u8 pointer --- deno_webgpu/buffer.rs | 10 +++++++--- player/tests/test.rs | 2 +- wgpu-core/src/device/global.rs | 19 +++++++++++++++---- wgpu-core/src/device/queue.rs | 15 ++++++++------- wgpu/src/backend/wgpu_core.rs | 7 ++++--- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/deno_webgpu/buffer.rs b/deno_webgpu/buffer.rs index 5b7d20880..e0b0e50d3 100644 --- a/deno_webgpu/buffer.rs +++ b/deno_webgpu/buffer.rs @@ -9,6 +9,7 @@ use deno_core::Resource; use deno_core::ResourceId; use std::borrow::Cow; use std::cell::RefCell; +use std::ptr::NonNull; use std::rc::Rc; use std::time::Duration; use wgpu_core::resource::BufferAccessResult; @@ -30,7 +31,7 @@ impl Resource for WebGpuBuffer { } } -struct WebGpuBufferMapped(*mut u8, usize); +struct WebGpuBufferMapped(NonNull, usize); impl Resource for WebGpuBufferMapped { fn name(&self) -> Cow { "webGPUBufferMapped".into() @@ -164,7 +165,8 @@ pub fn op_webgpu_buffer_get_mapped_range( .map_err(|e| DomExceptionOperationError::new(&e.to_string()))?; // SAFETY: guarantee to be safe from wgpu - let slice = unsafe { std::slice::from_raw_parts_mut(slice_pointer, range_size as usize) }; + let slice = + unsafe { std::slice::from_raw_parts_mut(slice_pointer.as_ptr(), range_size as usize) }; buf.copy_from_slice(slice); let rid = state @@ -191,7 +193,9 @@ pub fn op_webgpu_buffer_unmap( if let Some(buf) = buf { // SAFETY: guarantee to be safe from wgpu - let slice = unsafe { std::slice::from_raw_parts_mut(mapped_resource.0, mapped_resource.1) }; + let slice = unsafe { + std::slice::from_raw_parts_mut(mapped_resource.0.as_ptr(), mapped_resource.1) + }; slice.copy_from_slice(buf); } diff --git a/player/tests/test.rs b/player/tests/test.rs index 2aca181c8..a5aba15bd 100644 --- a/player/tests/test.rs +++ b/player/tests/test.rs @@ -154,7 +154,7 @@ impl Test<'_> { let (ptr, size) = wgc::gfx_select!(device_id => global.buffer_get_mapped_range(buffer, expect.offset, Some(expect.data.len() as wgt::BufferAddress))) .unwrap(); - let contents = unsafe { slice::from_raw_parts(ptr, size as usize) }; + let contents = unsafe { slice::from_raw_parts(ptr.as_ptr(), size as usize) }; let expected_data = match expect.data { ExpectedData::Raw(vec) => vec, ExpectedData::File(name, size) => { diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index 0e2a22e88..2be55a48b 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -31,7 +31,8 @@ use wgt::{BufferAddress, TextureFormat}; use std::{ borrow::Cow, - iter, ptr, + iter, + ptr::{self, NonNull}, sync::{atomic::Ordering, Arc}, }; @@ -2611,7 +2612,7 @@ impl Global { buffer_id: id::BufferId, offset: BufferAddress, size: Option, - ) -> Result<(*mut u8, u64), BufferAccessError> { + ) -> Result<(NonNull, u64), BufferAccessError> { profiling::scope!("Buffer::get_mapped_range"); api_log!("Buffer::get_mapped_range {buffer_id:?} offset {offset:?} size {size:?}"); @@ -2651,7 +2652,12 @@ impl Global { max: buffer.size, }); } - unsafe { Ok((ptr.as_ptr().offset(offset as isize), range_size)) } + unsafe { + Ok(( + NonNull::new_unchecked(ptr.as_ptr().offset(offset as isize)), + range_size, + )) + } } resource::BufferMapState::Active { ref ptr, ref range, .. @@ -2671,7 +2677,12 @@ impl Global { // ptr points to the beginning of the range we mapped in map_async // rather than the beginning of the buffer. let relative_offset = (offset - range.start) as isize; - unsafe { Ok((ptr.as_ptr().offset(relative_offset), range_size)) } + unsafe { + Ok(( + NonNull::new_unchecked(ptr.as_ptr().offset(relative_offset)), + range_size, + )) + } } resource::BufferMapState::Idle | resource::BufferMapState::Waiting(_) => { Err(BufferAccessError::NotMapped) diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 59d46ac0e..5acc7c17d 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -29,7 +29,8 @@ use hal::{CommandEncoder as _, Device as _, Queue as _}; use smallvec::SmallVec; use std::{ - iter, mem, ptr, + iter, mem, + ptr::{self, NonNull}, sync::{atomic::Ordering, Arc}, }; use thiserror::Error; @@ -320,7 +321,7 @@ fn prepare_staging_buffer( device: &Arc>, size: wgt::BufferAddress, instance_flags: wgt::InstanceFlags, -) -> Result<(StagingBuffer, *mut u8), DeviceError> { +) -> Result<(StagingBuffer, NonNull), DeviceError> { profiling::scope!("prepare_staging_buffer"); let stage_desc = hal::BufferDescriptor { label: hal_label(Some("(wgpu internal) Staging"), instance_flags), @@ -340,7 +341,7 @@ fn prepare_staging_buffer( is_coherent: mapping.is_coherent, }; - Ok((staging_buffer, mapping.ptr.as_ptr())) + Ok((staging_buffer, mapping.ptr)) } impl StagingBuffer { @@ -457,7 +458,7 @@ impl Global { if let Err(flush_error) = unsafe { profiling::scope!("copy"); - ptr::copy_nonoverlapping(data.as_ptr(), staging_buffer_ptr, data.len()); + ptr::copy_nonoverlapping(data.as_ptr(), staging_buffer_ptr.as_ptr(), data.len()); staging_buffer.flush(device.raw()) } { pending_writes.consume(staging_buffer); @@ -482,7 +483,7 @@ impl Global { queue_id: QueueId, buffer_size: wgt::BufferSize, id_in: Option, - ) -> Result<(id::StagingBufferId, *mut u8), QueueWriteError> { + ) -> Result<(id::StagingBufferId, NonNull), QueueWriteError> { profiling::scope!("Queue::create_staging_buffer"); let hub = A::hub(self); @@ -845,7 +846,7 @@ impl Global { unsafe { ptr::copy_nonoverlapping( data.as_ptr().offset(data_layout.offset as isize), - staging_buffer_ptr, + staging_buffer_ptr.as_ptr(), stage_size as usize, ); } @@ -862,7 +863,7 @@ impl Global { data_layout.offset as isize + (rows_offset + row) as isize * bytes_per_row as isize, ), - staging_buffer_ptr.offset( + staging_buffer_ptr.as_ptr().offset( (rows_offset + row) as isize * stage_bytes_per_row as isize, ), copy_bytes_per_row, diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index dc9ee58cc..961f4970b 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -20,6 +20,7 @@ use std::{ fmt, future::{ready, Ready}, ops::Range, + ptr::NonNull, slice, sync::Arc, }; @@ -3471,7 +3472,7 @@ impl crate::context::QueueWriteBuffer for QueueWriteBuffer { #[derive(Debug)] pub struct BufferMappedRange { - ptr: *mut u8, + ptr: NonNull, size: usize, } @@ -3483,12 +3484,12 @@ unsafe impl Sync for BufferMappedRange {} impl crate::context::BufferMappedRange for BufferMappedRange { #[inline] fn slice(&self) -> &[u8] { - unsafe { slice::from_raw_parts(self.ptr, self.size) } + unsafe { slice::from_raw_parts(self.ptr.as_ptr(), self.size) } } #[inline] fn slice_mut(&mut self) -> &mut [u8] { - unsafe { slice::from_raw_parts_mut(self.ptr, self.size) } + unsafe { slice::from_raw_parts_mut(self.ptr.as_ptr(), self.size) } } }