Derive Macro encase::ShaderType

source ·
#[derive(ShaderType)]
{
    // Attributes available to this derive:
    #[align]
    #[size]
}
Expand description

Used to implement ShaderType for structs

§Attributes

Field attributes

  • #[align(X)] where X is a power of 2 u32 literal (equivalent to WGSL align attribute)

    Used to increase the alignment of the field

  • #[size(X)] where X is a u32 literal (equivalent to WGSL size attribute)

    Used to increase the size of the field

  • #[size(runtime)] can only be attached to the last field of the struct

    Used to denote the fact that the field it is attached to is a runtime-sized array

§Note about generics

While structs using generic type parameters are supported by this derive macro

  • the #[align(X)] and #[size(X)] attributes will only work if they are attached to fields whose type contains no generic type parameters

§Examples

Simple

#[derive(ShaderType)]
struct AffineTransform2D {
    matrix: mint::ColumnMatrix2<f32>,
    translate: mint::Vector2<f32>
}

Contains a runtime-sized array

The ArrayLength type can be used to explicitly write or read the length of the contained runtime-sized array

#[derive(ShaderType)]
struct Positions {
    length: ArrayLength,
    #[size(runtime)]
    positions: Vec<mint::Point2<f32>>
}

Complex

#[derive(ShaderType)]
struct Complex<
    'a,
    'b: 'a,
    E: 'a + ShaderType + ShaderSize,
    T: 'b + ShaderType + ShaderSize,
    const N: usize,
> {
    array: [&'a mut E; N],
    #[size(runtime)]
    rts_array: &'a mut Vec<&'b T>,
}