use crate::vk;
use std::iter::Iterator;
use std::marker::PhantomData;
use std::mem::size_of;
use std::os::raw::c_void;
use std::{io, slice};
#[derive(Debug, Clone)]
pub struct Align<T> {
ptr: *mut c_void,
elem_size: vk::DeviceSize,
size: vk::DeviceSize,
_m: PhantomData<T>,
}
#[derive(Debug)]
pub struct AlignIter<'a, T: 'a> {
align: &'a mut Align<T>,
current: vk::DeviceSize,
}
impl<T: Copy> Align<T> {
pub fn copy_from_slice(&mut self, slice: &[T]) {
use std::slice::from_raw_parts_mut;
if self.elem_size == size_of::<T>() as u64 {
unsafe {
let mapped_slice = from_raw_parts_mut(self.ptr as *mut T, slice.len());
mapped_slice.copy_from_slice(slice);
}
} else {
for (i, val) in self.iter_mut().enumerate().take(slice.len()) {
*val = slice[i];
}
}
}
}
fn calc_padding(adr: vk::DeviceSize, align: vk::DeviceSize) -> vk::DeviceSize {
(align - adr % align) % align
}
impl<T> Align<T> {
pub unsafe fn new(ptr: *mut c_void, alignment: vk::DeviceSize, size: vk::DeviceSize) -> Self {
let padding = calc_padding(size_of::<T>() as vk::DeviceSize, alignment);
let elem_size = size_of::<T>() as vk::DeviceSize + padding;
assert!(calc_padding(size, alignment) == 0, "size must be aligned");
Self {
ptr,
elem_size,
size,
_m: PhantomData,
}
}
pub fn iter_mut(&mut self) -> AlignIter<T> {
AlignIter {
current: 0,
align: self,
}
}
}
impl<'a, T: Copy + 'a> Iterator for AlignIter<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
if self.current == self.align.size {
return None;
}
unsafe {
let ptr = (self.align.ptr as *mut u8).offset(self.current as isize) as *mut T;
self.current += self.align.elem_size;
Some(&mut *ptr)
}
}
}
pub fn read_spv<R: io::Read + io::Seek>(x: &mut R) -> io::Result<Vec<u32>> {
let size = x.seek(io::SeekFrom::End(0))?;
if size % 4 != 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"input length not divisible by 4",
));
}
if size > usize::max_value() as u64 {
return Err(io::Error::new(io::ErrorKind::InvalidData, "input too long"));
}
let words = (size / 4) as usize;
let mut result = vec![0u32; words];
x.seek(io::SeekFrom::Start(0))?;
x.read_exact(unsafe { slice::from_raw_parts_mut(result.as_mut_ptr() as *mut u8, words * 4) })?;
const MAGIC_NUMBER: u32 = 0x0723_0203;
if !result.is_empty() && result[0] == MAGIC_NUMBER.swap_bytes() {
for word in &mut result {
*word = word.swap_bytes();
}
}
if result.is_empty() || result[0] != MAGIC_NUMBER {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"input missing SPIR-V magic number",
));
}
Ok(result)
}