]> git.nega.tv - josh/narcissus/commitdiff
narcissus-gpu: Remove an unsafe fn from `PersistentBuffer`
authorJosh Simmons <josh@nega.tv>
Fri, 29 Nov 2024 22:11:31 +0000 (23:11 +0100)
committerJosh Simmons <josh@nega.tv>
Fri, 29 Nov 2024 22:11:31 +0000 (23:11 +0100)
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
title/shark/tests/radix_sort.rs

index 0a4994400e1e4fe9e758ad395d30b31f09666b8c..638c429da06dc1bc46a3f5f7884c256407f5dc4f 100644 (file)
@@ -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<T: ?Sized>(&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<T: ?Sized>(&mut self, offset: usize, src: &T) {
         unsafe { copy_from_with_offset(self.ptr, self.len, offset, src) }
     }
-
-    pub unsafe fn copy_to_slice<T>(&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.
index e1be7aaf46746184490a1b2a2369ce1cf30ba182..9f3acc2441b820ac50457055c7344a4f823dcb86 100644 (file)
@@ -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.