[][src]Trait web_glitz::pipeline::interface_block::InterfaceBlock

pub unsafe trait InterfaceBlock: StableRepr {
    const MEMORY_UNITS: &'static [MemoryUnit];
}

Trait that may be implemented on a type which, when stored in a Buffer, may safely be used to back a device interface block (uniform block) that has a compatible memory layout.

Unsafe

Types that implement this type must have a fixed memory layout and any instance of the type must match the memory layout specified, otherwise a pipeline that uses an interface block backed by the instance may produce unexpected results.

Safely Deriving

This trait may be automatically derived on a struct when all of its members implement InterfaceBlockComponent. Notably, InterfaceBlockComponent is implemented for all basic std140 memory units defined in the std140 crate:

use web_glitz::pipeline::interface_block::InterfaceBlock;

#[std140::repr_std140]
#[derive(web_glitz::derive::InterfaceBlock)]
struct MyUniforms {
    transform: std140::mat4x4,
    base_color: std140::vec4,
}

The InterfaceBlockComponent trait is also implemented for any type that implements InterfaceBlock:

use web_glitz::pipeline::interface_block::InterfaceBlock;

#[std140::repr_std140]
#[derive(web_glitz::derive::InterfaceBlock)]
struct PointLight {
    position: std140::vec3,
    color: std140::vec3,
    constant_attenuation: std140::float,
    linear_attenuation: std140::float,
    quadratic_attenuation: std140::float,
}

#[std140::repr_std140]
#[derive(web_glitz::derive::InterfaceBlock)]
struct MyUniforms {
    transform: std140::mat4x4,
    base_color: std140::vec4,
    light: PointLight,
}

Note that both MyUniforms and PointLight in the example above are also marked with the #[std140::repr_std140] attribute to ensure that the struct layout matches the std140 convention; this is not a strict requirement for automatically deriving the InterfaceBlock trait. However, the InterfaceBlock (and InterfaceBlockComponent) trait(s) do rely on the struct having a stable representation across builds; the Rust compiler gives no consistency guarantees for non-primitive type representations between builds (that is: if a type seems compatible with a memory layout on your current build, it is not guaranteed to remain compatible on any future builds), unless a specific representation is specified with the #[repr] attribute (e.g. #[repr(C, align(16))]). The marker trait StableRepr may be unsafely implemented on types to mark them as having a stable representation. StableRepr is already implemented for all std140::ReprStd140 types, including any struct marked with #[std140::repr_std140].

Associated Constants

Loading content...

Implementors

Loading content...