#glsl #std140 #std430 #macro-derive

no-std crevice

Create GLSL-compatible versions of structs with explicitly-initialized padding

16 breaking releases

0.17.0 Oct 8, 2024
0.16.0 May 6, 2024
0.15.0 Mar 16, 2024
0.14.0 Sep 8, 2023
0.5.0 Oct 18, 2020

#32 in Graphics APIs

Download history 627/week @ 2024-07-29 578/week @ 2024-08-05 1014/week @ 2024-08-12 637/week @ 2024-08-19 734/week @ 2024-08-26 828/week @ 2024-09-02 812/week @ 2024-09-09 705/week @ 2024-09-16 1168/week @ 2024-09-23 760/week @ 2024-09-30 408/week @ 2024-10-07 815/week @ 2024-10-14 876/week @ 2024-10-21 920/week @ 2024-10-28 818/week @ 2024-11-04 454/week @ 2024-11-11

3,109 downloads per month
Used in 25 crates (9 directly)

MIT/Apache

60KB
1K SLoC

Crevice

GitHub CI Status crevice on crates.io crevice docs

Crevice creates GLSL-compatible versions of types through the power of derive macros. Generated structures provide an as_bytes method to allow safely packing data into buffers for uploading.

Generated structs also implement bytemuck::Zeroable and bytemuck::Pod for use with other libraries.

Crevice is similar to glsl-layout, but supports types from many math crates, can generate GLSL source from structs, and explicitly initializes padding to remove one source of undefined behavior.

Crevice has support for many Rust math libraries via feature flags, and most other math libraries by use of the mint crate. Crevice currently supports:

  • mint 0.5, enabled by default
  • cgmath 0.18, using the cgmath feature
  • nalgebra 0.33, using the nalgebra feature
  • glam 0.29, using the glam feature

PRs are welcome to add or update math libraries to Crevice.

If your math library is not supported, it's possible to define structs using the types from mint and convert your math library's types into mint types. This is supported by most Rust math libraries.

Your math library may require you to turn on a feature flag to get mint support. For example, cgmath requires the "mint" feature to be enabled to allow conversions to and from mint types.

Examples

Single Value

Uploading many types can be done by deriving AsStd140 and using as_std140 and as_bytes to turn the result into bytes.

uniform MAIN {
    mat3 orientation;
    vec3 position;
    float scale;
} main;
use crevice::std140::AsStd140;

#[derive(AsStd140)]
struct MainUniform {
    orientation: mint::ColumnMatrix3<f32>,
    position: mint::Point3<f32>,
    scale: f32,
}

let value = MainUniform {
    orientation: [
        [1.0, 0.0, 0.0],
        [0.0, 1.0, 0.0],
        [0.0, 0.0, 1.0],
    ].into(),
    position: [1.0, 2.0, 3.0].into(),
    scale: 4.0,
};

let value_std140 = value.as_std140();

upload_data_to_gpu(value_std140.as_bytes());

Sequential Types

More complicated data can be uploaded using the std140 Writer type.

struct PointLight {
    vec3 position;
    vec3 color;
    float brightness;
};

buffer POINT_LIGHTS {
    uint len;
    PointLight[] lights;
} point_lights;

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.4–3.5MB
~82K SLoC