Crevice creates GLSL-compatible versions of types through the power of derive
macros. Generated structures provide an [as_bytes
][std140::Std140::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 mint
types
and explicitly initializes padding to remove one source of undefined behavior.
Examples in this crate use cgmath, but any math crate that works with the mint crate will also work. Some other crates include nalgebra, ultraviolet, glam, and vek.
Uploading many types can be done by deriving AsStd140
and using
[as_std140
][std140::AsStd140::asstd140] and
[as_bytes
][std140::Std140::asbytes] to turn the result into bytes.
glsl
uniform MAIN {
mat3 orientation;
vec3 position;
float scale;
} main;
```rust use crevice::std140::{AsStd140, Std140}; use cgmath::prelude::*; use cgmath::{Matrix3, Vector3};
struct MainUniform {
orientation: mint::ColumnMatrix3
let value = MainUniform { orientation: Matrix3::identity().into(), position: Vector3::new(1.0, 2.0, 3.0).into(), scale: 4.0, };
let valuestd140 = value.asstd140();
uploaddatatogpu(valuestd140.as_bytes()); ```
More complicated data can be uploaded using the std140 Writer
type.
```glsl struct PointLight { vec3 position; vec3 color; float brightness; };
buffer POINTLIGHTS { uint len; PointLight[] lights; } pointlights; ```
```rust use crevice::std140::{self, AsStd140};
struct PointLight {
position: mint::Vector3
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 targetbuffer = mapgpubufferforwrite(); let mut writer = std140::Writer::new(targetbuffer);
let lightcount = lights.len() as u32; writer.write(&lightcount)?;
// 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.
writer.write(lights.as_slice())?;
unmapgpubuffer();
```
std
(default): Enables std::io::Write
-based structs.Crevice supports Rust 1.46.0 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.