#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg), doc(auto_cfg = false))]
#![no_std]
use core::num::{NonZero, Wrapping};
pub trait ShaderLayout: Clone + Copy + 'static {
const ALIGN: NonZero<u64>;
}
#[macro_export]
macro_rules! impl_shader_layout_raw {
($($ty:ty),+$(,)?) => {
$(impl $crate::ShaderLayout for $ty {
const ALIGN: ::core::num::NonZero<u64> = ::core::num::NonZero::new(align_of::<$ty>() as u64).unwrap();
})+
};
}
#[macro_export]
macro_rules! impl_shader_layout {
($align:expr $(, $ty:ty)+$(,)?) => {
$(impl $crate::ShaderLayout for $ty {
const ALIGN: ::core::num::NonZero<u64> = ::core::num::NonZero::new($align).unwrap();
})+
};
}
#[macro_export]
macro_rules! impl_shader_layout_array {
($($ty:ty),+$(,)?) => {
$(
impl<const N: usize> $crate::ShaderLayout for [$ty; N]
{
const ALIGN: ::core::num::NonZero<u64> = <$ty as $crate::ShaderLayout>::ALIGN;
}
const _ : () = {
const N: usize = 1;
const SIZE: u64 = (size_of::<$ty>() as u64).next_multiple_of(<$ty as $crate::ShaderLayout>::ALIGN.get()) * N as u64;
assert!(
SIZE == size_of::<[$ty; N]>() as u64,
concat!(
"Size of `[",
stringify!($ty),
"; N]` must be equal to its shader size, i.e. `N × roundUp(AlignOf(E), SizeOf(E))`",
),
);
};
)+
};
}
impl_shader_layout_raw!(
i16,
u16,
NonZero<i16>,
NonZero<u16>,
Wrapping<i16>,
Wrapping<u16>,
f32,
i32,
u32,
NonZero<i32>,
NonZero<u32>,
Wrapping<f32>,
Wrapping<i32>,
Wrapping<u32>,
);
impl_shader_layout_array!(
i16,
u16,
NonZero<i16>,
NonZero<u16>,
Wrapping<i16>,
Wrapping<u16>,
f32,
i32,
u32,
NonZero<i32>,
NonZero<u32>,
Wrapping<f32>,
Wrapping<i32>,
Wrapping<u32>,
);
#[macro_export]
macro_rules! shader_layout {
(
$(#[$attr:meta])*
$vis:vis struct $struct_name:ident {
$(
$(#[$field_attr:meta])*
$field_vis:vis $field_name:ident : $field_ty:ty
),* $(,)?
}
) => {
#[derive(Copy, Clone)]
#[repr(C)]
$(#[$attr])*
$vis struct $struct_name {
$(
$(#[$field_attr])*
$field_vis $field_name: $field_ty
),*
}
$(
const _ : () = {
const OFFSET: u64 = core::mem::offset_of!($struct_name, $field_name) as u64;
const ALIGN: u64 = <$field_ty as $crate::ShaderLayout>::ALIGN.get();
assert!(
OFFSET.is_multiple_of(ALIGN),
concat!(
"In a `shader_layout!`, field `",
stringify!($struct_name), "::", stringify!($field_name),
"` is not properly aligned",
),
);
};
)*
impl $crate::ShaderLayout for $struct_name {
const ALIGN: ::core::num::NonZero<u64> = {
const MEMBER_ALIGNS: &[u64] = &[$(
(<$field_ty as $crate::ShaderLayout>::ALIGN.get())
),*];
let mut max = MEMBER_ALIGNS[0];
let mut i = 1;
while i < MEMBER_ALIGNS.len() {
if MEMBER_ALIGNS[i] > max {
max = MEMBER_ALIGNS[i];
}
i += 1;
}
::core::num::NonZero::new(max).unwrap()
};
}
const _ : () = {
const SIZE: u64 = (size_of::<$struct_name>() as u64).next_multiple_of(<$struct_name as $crate::ShaderLayout>::ALIGN.get());
assert!(
(size_of::<$struct_name>() as u64) == SIZE,
concat!(
"In a `shader_layout!`, struct `",
stringify!($struct_name),
"` size must be equal to its shader size, i.e. `roundUp(AlignOf(S), SizeOf(S)))`",
),
);
};
};
}
#[cfg(feature = "glam")]
#[cfg_attr(docsrs, doc(cfg(feature = "glam")))]
mod glam;
#[cfg(feature = "half")]
#[cfg_attr(docsrs, doc(cfg(feature = "half")))]
mod half;