2021-12-23 22:49:12 +00:00
|
|
|
# Bevy Crevice
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-12-23 22:49:12 +00:00
|
|
|
This is a fork of [Crevice](https://crates.io/crates/crevice) for
|
|
|
|
[Bevy](https://bevyengine.org).
|
|
|
|
|
|
|
|
For use outside of Bevy, you should consider
|
|
|
|
using [Crevice](https://crates.io/crates/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](https://github.com/LPGhatguy/crevice/pull/27/).
|
|
|
|
* Renaming of traits and macros to better match Bevy API.
|
|
|
|
|
|
|
|
## Crevice
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
Crevice is similar to [`glsl-layout`][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.
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
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
|
2021-12-23 22:49:12 +00:00
|
|
|
* glam 0.20, using the `glam` feature
|
2021-11-12 01:39:25 +00:00
|
|
|
|
|
|
|
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.
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-07-26 23:44:23 +00:00
|
|
|
## Examples
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-07-26 23:44:23 +00:00
|
|
|
### Single Value
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
Uploading many types can be done by deriving [`AsStd140`][std140::AsStd140] and
|
|
|
|
using [`as_std140`][std140::AsStd140::as_std140] and
|
2021-04-11 20:13:07 +00:00
|
|
|
[`as_bytes`][std140::Std140::as_bytes] to turn the result into bytes.
|
|
|
|
|
|
|
|
```glsl
|
|
|
|
uniform MAIN {
|
|
|
|
mat3 orientation;
|
|
|
|
vec3 position;
|
|
|
|
float scale;
|
|
|
|
} main;
|
|
|
|
```
|
|
|
|
|
|
|
|
```rust
|
2021-12-23 22:49:12 +00:00
|
|
|
use bevy_crevice::std140::{AsStd140, Std140};
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
#[derive(AsStd140)]
|
|
|
|
struct MainUniform {
|
|
|
|
orientation: mint::ColumnMatrix3<f32>,
|
|
|
|
position: mint::Vector3<f32>,
|
|
|
|
scale: f32,
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = MainUniform {
|
2021-11-12 01:39:25 +00:00
|
|
|
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(),
|
2021-04-11 20:13:07 +00:00
|
|
|
scale: 4.0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let value_std140 = value.as_std140();
|
|
|
|
|
|
|
|
upload_data_to_gpu(value_std140.as_bytes());
|
|
|
|
```
|
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
### Sequential Types
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
More complicated data can be uploaded using the std140
|
|
|
|
[`Writer`][std140::Writer] type.
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
```glsl
|
|
|
|
struct PointLight {
|
|
|
|
vec3 position;
|
|
|
|
vec3 color;
|
|
|
|
float brightness;
|
|
|
|
};
|
|
|
|
|
|
|
|
buffer POINT_LIGHTS {
|
|
|
|
uint len;
|
|
|
|
PointLight[] lights;
|
|
|
|
} point_lights;
|
|
|
|
```
|
|
|
|
|
|
|
|
```rust
|
2021-12-23 22:49:12 +00:00
|
|
|
use bevy_crevice::std140::{self, AsStd140};
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
#[derive(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;
|
|
|
|
writer.write(&light_count)?;
|
|
|
|
|
|
|
|
// 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())?;
|
|
|
|
|
|
|
|
unmap_gpu_buffer();
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
## Features
|
|
|
|
|
|
|
|
* `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.
|
2021-04-11 20:13:07 +00:00
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
## Minimum Supported Rust Version (MSRV)
|
|
|
|
|
|
|
|
Crevice supports Rust 1.52.1 and newer due to use of new `const fn` features.
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
[glsl-layout]: https://github.com/rustgd/glsl-layout
|
2021-11-12 01:39:25 +00:00
|
|
|
|
|
|
|
[std140::AsStd140]: https://docs.rs/crevice/latest/crevice/std140/trait.AsStd140.html
|
|
|
|
[std140::AsStd140::as_std140]: https://docs.rs/crevice/latest/crevice/std140/trait.AsStd140.html#method.as_std140
|
|
|
|
[std140::Std140::as_bytes]: https://docs.rs/crevice/latest/crevice/std140/trait.Std140.html#method.as_bytes
|
|
|
|
[std140::Writer]: https://docs.rs/crevice/latest/crevice/std140/struct.Writer.html
|
|
|
|
|
|
|
|
[`std::io::Write`]: https://doc.rust-lang.org/stable/std/io/trait.Write.html
|
|
|
|
|
|
|
|
[`bytemuck::Pod`]: https://docs.rs/bytemuck/latest/bytemuck/trait.Pod.html
|
|
|
|
[`bytemuck::Zeroable`]: https://docs.rs/bytemuck/latest/bytemuck/trait.Zeroable.html
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
Licensed under either of
|
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
* Apache License, Version 2.0, ([LICENSE-APACHE](http://www.apache.org/licenses/LICENSE-2.0))
|
|
|
|
* MIT license ([LICENSE-MIT](http://opensource.org/licenses/MIT))
|
2021-04-11 20:13:07 +00:00
|
|
|
|
|
|
|
at your option.
|
|
|
|
|
|
|
|
### Contribution
|
2021-07-26 23:44:23 +00:00
|
|
|
|
2021-11-12 01:39:25 +00:00
|
|
|
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.
|