Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added convenience, build buffer from slice, leveraging rust generics … #243

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>()) 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::<u32>()) as u64,
device.new_buffer_from_slice(
&data[..],
MTLResourceOptions::CPUCacheModeDefaultCache,
)
};
Expand Down
12 changes: 12 additions & 0 deletions src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,6 +1965,18 @@ impl DeviceRef {
options:options]
}
}

pub fn new_buffer_from_slice<T:Copy>(
&self,
data:&[T],
options:MTLResourceOptions
)-> Buffer {
self.new_buffer_with_data(
data.as_ptr().cast::<std::ffi::c_void>(),
(data.len() * std::mem::size_of::<T>()) as NSUInteger,
options
)
}

pub fn new_texture(&self, descriptor: &TextureDescriptorRef) -> Texture {
unsafe { msg_send![self, newTextureWithDescriptor: descriptor] }
Expand Down