change prepare_staging_buffer to return a non null u8 pointer

This commit is contained in:
teoxoy 2024-07-04 12:46:24 +02:00 committed by Teodor Tanasoaia
parent bd2b284a26
commit 5266bd1f08
5 changed files with 35 additions and 18 deletions

View File

@ -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<u8>, usize);
impl Resource for WebGpuBufferMapped {
fn name(&self) -> Cow<str> {
"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);
}

View File

@ -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) => {

View File

@ -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<BufferAddress>,
) -> Result<(*mut u8, u64), BufferAccessError> {
) -> Result<(NonNull<u8>, 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)

View File

@ -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<A: HalApi>(
device: &Arc<Device<A>>,
size: wgt::BufferAddress,
instance_flags: wgt::InstanceFlags,
) -> Result<(StagingBuffer<A>, *mut u8), DeviceError> {
) -> Result<(StagingBuffer<A>, NonNull<u8>), 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<A: HalApi>(
is_coherent: mapping.is_coherent,
};
Ok((staging_buffer, mapping.ptr.as_ptr()))
Ok((staging_buffer, mapping.ptr))
}
impl<A: HalApi> StagingBuffer<A> {
@ -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<id::StagingBufferId>,
) -> Result<(id::StagingBufferId, *mut u8), QueueWriteError> {
) -> Result<(id::StagingBufferId, NonNull<u8>), 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,

View File

@ -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<u8>,
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) }
}
}