#glsl #std140 #std430 #io-write

no-std bevy_crevice

Create GLSL-compatible versions of structs with explicitly-initialized padding (Bevy version)

3 unstable releases

0.7.0 Apr 15, 2022
0.6.1 Feb 14, 2022
0.6.0 Jan 8, 2022

#606 in Algorithms

Download history 672/week @ 2024-03-13 1144/week @ 2024-03-20 1148/week @ 2024-03-27 983/week @ 2024-04-03 861/week @ 2024-04-10 1032/week @ 2024-04-17 1054/week @ 2024-04-24 940/week @ 2024-05-01 920/week @ 2024-05-08 947/week @ 2024-05-15 969/week @ 2024-05-22 1020/week @ 2024-05-29 882/week @ 2024-06-05 695/week @ 2024-06-12 808/week @ 2024-06-19 804/week @ 2024-06-26

3,331 downloads per month



Bevy Crevice

This is a fork of Crevice for Bevy.

For use outside of Bevy, you should consider using Crevice directly.

It was forked to allow better integration in Bevy:

  • Easier derive macro usage, without needing to depend on Crevice directly.
  • Use of unmerged features (as of the fork), like Array Support.
  • Renaming of traits and macros to better match Bevy API.


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.29, using the nalgebra feature
  • glam 0.20, 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.


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 bevy_crevice::std140::{AsStd140, Std140};

struct MainUniform {
    orientation: mint::ColumnMatrix3<f32>,
    position: mint::Vector3<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],
    position: [1.0, 2.0, 3.0].into(),
    scale: 4.0,

let value_std140 = value.as_std140();


Sequential Types

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

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

    uint len;
    PointLight[] lights;
} point_lights;
use bevy_crevice::std140::{self, AsStd140};

struct PointLight {
    position: mint::Vector3<f32>,
    color: mint::Vector3<f32>,
    brightness: f32,

let lights = vec![
    PointLight {
        position: [0.0, 1.0, 0.0].into(),
        color: [1.0, 0.0, 0.0].into(),
        brightness: 0.6,
    PointLight {
        position: [0.0, 4.0, 3.0].into(),
        color: [1.0, 1.0, 1.0].into(),
        brightness: 1.0,

let target_buffer = map_gpu_buffer_for_write();
let mut writer = std140::Writer::new(target_buffer);

let light_count = lights.len() as u32;

// Crevice will automatically insert the required padding to align the
// PointLight structure correctly. In this case, there will be 12 bytes of
// padding between the length field and the light list.




  • std (default): Enables std::io::Write-based structs.
  • cgmath: Enables support for types from cgmath.
  • nalgebra: Enables support for types from nalgebra.
  • glam: Enables support for types from glam.

Minimum Supported Rust Version (MSRV)

Crevice supports Rust 1.52.1 and newer due to use of new const fn features.


Licensed under either of

at your option.


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.


~80K SLoC