[go: up one dir, main page]

logo

Derive Macro encase::WgslType

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

Used to implement WgslType for structs

Attributes

Struct 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 #[assert_uniform_compat] attribute won’t work with such a struct

  • 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(WgslType)]
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(WgslType)]
struct Positions {
    length: ArrayLength,
    #[size(runtime)]
    positions: Vec<mint::Point2<f32>>
}

Assert uniform address space requirements

Will not compile since runtime-sized arrays are not compatible with the uniform address space restrictions on stored values

#[derive(WgslType)]
#[assert_uniform_compat]
struct Invalid {
    #[size(runtime)]
    vec: Vec<mint::Vector4<f32>>
}

Will not compile

#[derive(WgslType)]
#[assert_uniform_compat]
struct Invalid {
    a: f32,
    b: f32, // invalid: offset of b is 4 bytes, but must be at least 16
}

Will compile (fixed via align attribute)

#[derive(WgslType)]
#[assert_uniform_compat]
struct Valid {
    a: f32,
    #[align(16)]
    b: f32, // valid: offset of b is 16 bytes
}

Will compile (fixed via size attribute)

#[derive(WgslType)]
#[assert_uniform_compat]
struct Valid {
    #[size(16)]
    a: f32,
    b: f32, // valid: offset of b is 16 bytes
}

Complex

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