Compile-time checks to ensure structs conform to WGSL's memory layout rules
All validation is performed at compile time using declarative macros (macro_rules!) — no proc macros,
no build dependencies, no compile-time overhead beyond the checks themselves.
The core of this crate is the [shader_layout], [shader_layout_compat] macros and the
[ShaderLayout], [ShaderLayoutCompat] traits.
ShaderLayout corresponds to https://www.w3.org/TR/WGSL/#alignment-and-size.
ShaderLayoutCompat is a stricter subset that enforces the
uniform address space layout constraints
(without the uniform_buffer_standard_layout extension). Every type implementing ShaderLayoutCompat
also implements ShaderLayout.
Both macros validate every field's alignment and the overall struct size at compile time. If a constraint is violated, compilation fails with a clear error message:
shader_layout! {
pub struct OffsetUnaligned {
a1: f32, // align 4
a4: glam::Vec3, // align 16 — offset 4 is not a multiple of 16!
}
}
// error[E0080]: evaluation panicked: Failed to implement `ShaderLayout`: field `OffsetUnaligned::a4` (`glam::Vec3`) is not properly aligned. The offset is 4 but required align is 16
use ;
use ;
shader_layout!
shader_layout_compat!
shader_layout_compat!
See https://github.com/beicause/const_shader_layout/tree/master/tests for what this supports and checks.
Features
| Feature | Description |
|---|---|
glam (default) |
Implements ShaderLayout/ShaderLayoutCompat for glam types |
half |
Implements ShaderLayout/ShaderLayoutCompat for half::f16 and array of it |
std (default), libm, nostd-libm |
Re-export glam's corresponding features |
This crate focuses on layout validation only. For safe cast/transmute utilities, pair it with other crates like bytemuck.