Skip to content

Commit

Permalink
add UnalignedSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcaff committed Apr 18, 2024
1 parent 7827b6b commit 8bb1603
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion vulkano/autogen/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn properties_output(members: &[PropertiesMember]) -> TokenStream {
quote! {
properties_ffi.#ffi_member.map(|s|
unsafe {
std::slice::from_raw_parts(
UnalignedSlice::new(
s #ffi_member_field .#ffi_name .cast_const(),
s #ffi_member_field .#len_field_name as _,
)
Expand Down
28 changes: 22 additions & 6 deletions vulkano/src/device/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,29 @@ use crate::{
shader::ShaderStages,
DeviceSize, Version,
};
use std::ffi::c_char;
use std::{ffi::c_char, mem};

// Generated by build.rs
include!(concat!(env!("OUT_DIR"), "/properties.rs"));

/// Wrapper for holding and accessing a potentially unaligned sequence of pointers
struct UnalignedSlice<T> {
ptr: *const T,
len: usize,
}

impl<T> UnalignedSlice<T> {
pub unsafe fn new(ptr: *const T, len: usize) -> UnalignedSlice<T> {
UnalignedSlice { ptr, len }
}

pub fn iter(&self) -> impl Iterator<Item = &T> {
(0..self.len)
.into_iter()
.map(|idx| unsafe { &*self.ptr.add(idx * mem::size_of::<*const T>()) })
}
}

// A bit of a hack...
// TODO: integrate into autogen?
pub(crate) trait FromVulkan<F>
Expand Down Expand Up @@ -307,12 +325,10 @@ impl FromVulkan<ash::vk::ImageLayout> for ImageLayout {
}
}

impl<U: for<'a> FromVulkan<&'a T>, T> FromVulkan<&[T]> for Vec<U> {
impl<U: for<'a> FromVulkan<&'a T>, T> FromVulkan<UnalignedSlice<T>> for Vec<U> {
#[inline]
fn from_vulkan(val: &[T]) -> Option<Vec<U>> {
val.iter()
.map(|it| U::from_vulkan(it))
.collect::<Option<Vec<_>>>()
fn from_vulkan(val: UnalignedSlice<T>) -> Option<Vec<U>> {
val.iter().map(U::from_vulkan).collect::<Option<Vec<_>>>()
}
}

Expand Down

0 comments on commit 8bb1603

Please sign in to comment.