15 个重大版本发布
| 0.16.0 | 2024 年 5 月 6 日 | 
|---|---|
| 0.15.0 | 2024 年 3 月 16 日 | 
| 0.14.0 | 2023 年 9 月 8 日 | 
| 0.13.0 | 2023 年 3 月 21 日 | 
| 0.5.0 | 2020 年 10 月 18 日 | 
98 在 Rust 模式 中
每月下载量 3,119
在 22 个 crate 中使用 (8 个直接使用)
61KB
 1K  SLoC
Crevice
Crevice 通过 derive 宏的力量创建与 GLSL 兼容的类型版本。生成的结构体提供了 as_bytes 方法,允许将数据安全地打包到缓冲区中进行上传。
生成的结构体还实现了 bytemuck::Zeroable 和 bytemuck::Pod,以便与其他库一起使用。
Crevice 与 glsl-layout 类似,但支持许多数学 crate 的类型,可以从结构体生成 GLSL 源代码,并显式初始化填充以消除未定义行为的一个来源。
Crevice 通过功能标志支持许多 Rust 数学库,并通过 mint crate 使用其他大多数数学库。Crevice 目前支持
- mint 0.5,默认启用
- cgmath 0.18,使用 cgmath功能
- nalgebra 0.31,使用 nalgebra功能
- glam 0.23,使用 glam功能
欢迎 PR 添加或更新 Crevice 的数学库。
如果您的数学库不受支持,您可以使用 mint 的类型定义结构体,并将您的数学库类型转换为 mint 类型。大多数 Rust 数学库都支持此操作。
您的数学库可能需要您启用一个功能标志才能获得 mint 支持。例如,cgmath 需要启用 "mint" 功能才能允许在 mint 类型之间进行转换。
示例
单个值
通过 derive AsStd140 并使用 as_std140 和 as_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());
序列类型
可以使用 std140 Writer 类型上传更复杂的数据。
struct PointLight {
    vec3 position;
    vec3 color;
    float brightness;
};
buffer POINT_LIGHTS {
    uint len;
    PointLight[] lights;
} point_lights;
use crevice::std140::{self, AsStd140};
#[derive(AsStd140)]
struct PointLight {
    position: mint::Point3<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();
功能
- std(默认): 启用基于- std::io::Write的结构体。
- cgmath: 启用对 cgmath 类型支持。
- nalgebra: 启用对 nalgebra 类型支持。
- glam: 启用对 glam 类型支持。
支持的最小 Rust 版本 (MSRV)
由于使用了新的 const fn 功能,Crevice 支持 Rust 1.58.0 及更高版本。
许可证
许可方式为以下之一:
- Apache License,版本 2.0,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 https://open-source.org.cn/licenses/MIT)
您可选其一。
贡献
除非您明确声明,否则根据 Apache-2.0 许可证定义,您有意提交以包含在作品中的任何贡献,将按上述方式双许可,不附加任何额外条款或条件。
依赖
~1.3–3MB
~77K SLoC