[go: up one dir, main page]

ShaderType

Derive Macro ShaderType 

Source
#[derive(ShaderType)]
{
    // Attributes available to this derive:
    #[shader]
}
Expand description

Used to implement ShaderType for structs

§Attributes

Field attributes

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

    Used to increase the alignment of the field

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

    Used to increase the size of the field

  • #[shader(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 #[shader(align(X))] and #[shader(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: test_impl::Mat2x2f,
    translate: test_impl::Vec2f,
}

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,
    #[shader(size(runtime))]
    positions: Vec<test_impl::Vec2f>
}

Complex

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