Compile-time checks to ensure structs conform to WGSL's memory layout rules
The core of this crate is the [ShaderLayout], [ShaderLayoutCompat] derive proc-macros
(or declarative macros if derive feature is disabled), 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 ;
// Use declarative macro
shader_layout!
// Or use proc-macro
See https://github.com/beicause/const_shader_layout/tree/master/tests for what this supports and checks.
Features
| Feature | Description |
|---|---|
derive(default) |
Enable ShaderLayout, ShaderLayoutCompat derive macros |
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.