# Compile-time checks to ensure structs conform to WGSL's [memory layout rules](https://www.w3.org/TR/WGSL/#alignment-and-size)
[](https://github.com/beicause/const_shader_layout/actions)
[](https://github.com/beicause/const_shader_layout)
[](https://crates.io/crates/const_shader_layout)
[](https://docs.rs/const_shader_layout)
The core of this crate is the [`shader_layout`], [`shader_layout_compat`] macros and the [`ShaderLayout`], [`ShaderLayoutCompat`] traits.
`ShaderLayout` is corresponding to <https://www.w3.org/TR/WGSL/#alignment-and-size>.
`ShaderLayoutCompat` is a subset of `ShaderLayout` and is corresponding to [address space layout constraints](https://www.w3.org/TR/WGSL/#address-space-layout-constraints) of uniform without [uniform_buffer_standard_layout](https://www.w3.org/TR/WGSL/#language_extension-uniform_buffer_standard_layout) language extension.
```rust
use const_shader_layout::{shader_layout, shader_layout_compat, ShaderLayout, ShaderLayoutCompat};
use glam::{Vec2, Vec3, Vec4};
shader_layout! {
pub struct MyStorage {
a1: f32,
a2: [f32; 2],
a3: [f32; 1],
a4: Vec3,
a5: f32,
a6: Vec3,
p1: f32, // Padding needed otherwise struct size (44) won't match shader size (48).
}
}
const {
assert!(<MyStorage as ShaderLayout>::ALIGN.get() == 16);
assert!(size_of::<MyStorage>() == 48);
}
shader_layout_compat! {
pub struct Nested {
a1: [Vec4; 2],
a2: Vec3,
a3: f32
}
}
shader_layout_compat! {
pub struct MyUniform {
a1: Nested,
a2: Vec3,
// Padding is not needed, because struct size (64) aligned to 16 in `repr(C)`, so it matches shader size (64).
}
}
const {
assert!(<Nested as ShaderLayoutCompat>::ALIGN_COMPAT.get() == 16);
assert!(<Nested as ShaderLayoutCompat>::SIZE_COMPAT.get() == 48);
assert!(size_of::<Nested>() == 48);
assert!(<MyUniform as ShaderLayoutCompat>::ALIGN_COMPAT.get() == 16);
assert!(<MyUniform as ShaderLayoutCompat>::SIZE_COMPAT.get() == 64);
assert!(size_of::<MyUniform>() == 64);
}
```
See <https://github.com/beicause/const_shader_layout/tree/master/tests> for what this supports and checks.
This doesn't provide any byte conversion methods. Instead, it's intended to be used with other libraries such as [bytemuck](https://docs.rs/bytemuck/latest/bytemuck/index.html).