use std::mem;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[repr(u8)]
pub enum Usage {
Data,
Dynamic,
Upload,
Download,
}
bitflags!(
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Access: u8 {
const READ = 0x1;
const WRITE = 0x2;
const RW = 0x3;
}
);
bitflags!(
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
pub struct Bind: u8 {
const RENDER_TARGET = 0x1;
const DEPTH_STENCIL = 0x2;
const SHADER_RESOURCE = 0x4;
const UNORDERED_ACCESS = 0x8;
const TRANSFER_SRC = 0x10;
const TRANSFER_DST = 0x20;
}
);
impl Bind {
pub fn is_mutable(&self) -> bool {
let mutable = Self::TRANSFER_DST | Self::UNORDERED_ACCESS |
Self::RENDER_TARGET | Self::DEPTH_STENCIL;
self.intersects(mutable)
}
}
#[doc(hidden)]
pub trait Typed: Sized {
type Raw;
fn new(raw: Self::Raw) -> Self;
fn raw(&self) -> &Self::Raw;
}
pub unsafe trait Pod {}
macro_rules! impl_pod {
( ty = $($ty:ty)* ) => { $( unsafe impl Pod for $ty {} )* };
( ar = $($tt:expr)* ) => { $( unsafe impl<T: Pod> Pod for [T; $tt] {} )* };
}
impl_pod! { ty = isize usize i8 u8 i16 u16 i32 u32 i64 u64 f32 f64 }
impl_pod! { ar =
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
}
unsafe impl<T: Pod, U: Pod> Pod for (T, U) {}
pub fn cast_slice<A: Pod, B: Pod>(slice: &[A]) -> &[B] {
use std::slice;
let raw_len = mem::size_of::<A>().wrapping_mul(slice.len());
let len = raw_len / mem::size_of::<B>();
assert_eq!(raw_len, mem::size_of::<B>().wrapping_mul(len));
unsafe {
slice::from_raw_parts(slice.as_ptr() as *const B, len)
}
}