Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Layout Traits

WgslLayout

WgslLayout is implemented for every built-in WGSL type: scalars, vectors, matrices, arrays, and atomics.

#![allow(unused)]
fn main() {
pub trait WgslLayout {
    const SIZE: usize;
    const ALIGN: usize;
}
}
TypeSIZEALIGN
f3244
u3244
vec2<f32>88
vec3<f32>1216
vec4<f32>1616
mat4x4<f32>6416
array<f32, 4>164

These constants are the source of truth for all downstream layout computation.

Layout

Layout extends WgslLayout with per-field metadata for composite types:

#![allow(unused)]
fn main() {
pub trait Layout: WgslLayout {
    const FIELDS: &'static [FieldLayout];
}
}

FieldLayout is described in Field Layout.

Generic Structs

Generic structs are supported. Each type parameter receives a T: WgslLayout bound in the generated impl, so SIZE, ALIGN, and FIELDS are computed in terms of the substituted type's constants:

#![allow(unused)]
fn main() {
#[derive(Layout)]
struct Cell<T> {
    value: T,
    next: u32,
}
}

The generated impl is roughly:

#![allow(unused)]
fn main() {
impl<T: WgslLayout> Layout for Cell<T> {
    const FIELDS: &'static [FieldLayout] = &[ /* computed from T::SIZE, T::ALIGN */ ];
}
}

Because the bounds propagate WgslLayout, generic structs compose freely with other layout-annotated types and built-in WGSL types.