From 4b467d474b3b3b840715bf14244605675f860d1e Mon Sep 17 00:00:00 2001 From: Josh Simmons Date: Fri, 29 Nov 2024 23:11:31 +0100 Subject: [PATCH] narcissus-gpu: Remove an unsafe fn from `PersistentBuffer` It was pretty sus and the test can just do the unsafe thing it wants to do directly. --- engine/narcissus-gpu/src/mapped_buffer.rs | 13 ++++++++----- title/shark/tests/radix_sort.rs | 9 ++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/engine/narcissus-gpu/src/mapped_buffer.rs b/engine/narcissus-gpu/src/mapped_buffer.rs index 0a49944..638c429 100644 --- a/engine/narcissus-gpu/src/mapped_buffer.rs +++ b/engine/narcissus-gpu/src/mapped_buffer.rs @@ -68,6 +68,14 @@ impl PersistentBuffer<'_> { BufferArg::Persistent(self) } + pub fn len(&self) -> usize { + self.len + } + + pub fn as_ptr(&self) -> *mut u8 { + self.ptr.as_ptr() + } + pub fn copy_from(&mut self, src: &T) { unsafe { copy_from_with_offset(self.ptr, self.len, 0, src) } } @@ -75,11 +83,6 @@ impl PersistentBuffer<'_> { pub fn copy_with_offset(&mut self, offset: usize, src: &T) { unsafe { copy_from_with_offset(self.ptr, self.len, offset, src) } } - - pub unsafe fn copy_to_slice(&self, dst: &mut [T]) { - assert!(std::mem::size_of_val(dst) == self.len); - std::ptr::copy(self.ptr.cast().as_ptr(), dst.as_mut_ptr(), dst.len()); - } } /// Transient mapped buffer that is tied to the lifetime of the current frame. diff --git a/title/shark/tests/radix_sort.rs b/title/shark/tests/radix_sort.rs index e1be7aa..9f3acc2 100644 --- a/title/shark/tests/radix_sort.rs +++ b/title/shark/tests/radix_sort.rs @@ -139,7 +139,14 @@ fn gpu_sort(values: &mut [u32]) { gpu.wait_idle(); - unsafe { sort_buffer.copy_to_slice(values) }; + unsafe { + assert!(sort_buffer.len() == std::mem::size_of_val(values)); + std::ptr::copy( + sort_buffer.as_ptr().cast_const(), + values.as_mut_ptr().cast(), + std::mem::size_of_val(values), + ); + }; } // This test requires a GPU, so ignore the test by default. -- 2.49.0