From 9142e37344b8fb178da5d0c601300836b34b4e7e Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Tue, 20 Sep 2022 21:31:28 +0100 Subject: [PATCH 1/2] added convenience, build buffer from slice, leveraging rust generics & slice type --- examples/compute/main.rs | 9 +++------ src/device.rs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/examples/compute/main.rs b/examples/compute/main.rs index 6497c790..02cc442b 100644 --- a/examples/compute/main.rs +++ b/examples/compute/main.rs @@ -19,17 +19,14 @@ fn main() { 24, 25, 26, 27, 28, 29, 30, ]; - let buffer = device.new_buffer_with_data( - unsafe { mem::transmute(data.as_ptr()) }, - (data.len() * mem::size_of::()) as u64, + let buffer = device.new_buffer_from_slice(&data[..], MTLResourceOptions::CPUCacheModeDefaultCache, ); let sum = { let data = [0u32]; - device.new_buffer_with_data( - unsafe { mem::transmute(data.as_ptr()) }, - (data.len() * mem::size_of::()) as u64, + device.new_buffer_from_slice( + &data[..], MTLResourceOptions::CPUCacheModeDefaultCache, ) }; diff --git a/src/device.rs b/src/device.rs index 1cb0a407..ed698d83 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1965,6 +1965,18 @@ impl DeviceRef { options:options] } } + + pub fn new_buffer_from_slice( + &self, + data:&[T], + options:MTLResourceOptions + )-> Buffer { + self.new_buffer_with_data( + &data[0] as *const _ as *const std::ffi::c_void, + (data.len() * std::mem::size_of::()) as NSUInteger, + options + ) + } pub fn new_texture(&self, descriptor: &TextureDescriptorRef) -> Texture { unsafe { msg_send![self, newTextureWithDescriptor: descriptor] } From 3dc3727d56e4a2094c0cef1b23c2dfc84c3a7951 Mon Sep 17 00:00:00 2001 From: dobkeratops Date: Tue, 20 Sep 2022 23:45:40 +0100 Subject: [PATCH 2/2] Update src/device.rs Co-authored-by: Mads Marquart --- src/device.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/device.rs b/src/device.rs index ed698d83..4eb3e718 100644 --- a/src/device.rs +++ b/src/device.rs @@ -1972,7 +1972,7 @@ impl DeviceRef { options:MTLResourceOptions )-> Buffer { self.new_buffer_with_data( - &data[0] as *const _ as *const std::ffi::c_void, + data.as_ptr().cast::(), (data.len() * std::mem::size_of::()) as NSUInteger, options )