Expand description
§wgsl_to_wgpu
wgsl_to_wgpu is a library for generating typesafe Rust bindings from WGSL shaders to wgpu.
§Getting Started
The create_shader_module function is intended for use in build scripts. This facilitates a shader focused workflow where edits to WGSL code are automatically reflected in the corresponding Rust file. For example, changing the type of a uniform in WGSL will raise a compile error in Rust code using the generated struct to initialize the buffer.
// build.rs
use wgsl_to_wgpu::{create_shader_module, MatrixVectorTypes, WriteOptions};
fn main() {
println!("cargo:rerun-if-changed=src/shader.wgsl");
// Read the shader source file.
let wgsl_source = std::fs::read_to_string("src/shader.wgsl").unwrap();
// Configure the output based on the dependencies for the project.
let options = WriteOptions {
derive_bytemuck_vertex: true,
derive_encase_host_shareable: true,
matrix_vector_types: MatrixVectorTypes::Glam,
..Default::default()
};
// Generate the bindings.
let text = create_shader_module(&wgsl_source, "shader.wgsl", options).unwrap();
std::fs::write("src/shader.rs", text.as_bytes()).unwrap();
}
Structs§
- Options for semantic validation.
- Allowed IR capabilities.
- Options for configuring the generated bindings to work with additional dependencies. Use WriteOptions::default for only requiring WGPU itself.
Enums§
- Errors while generating Rust source for a WGSL shader module.
- The format to use for matrix and vector types. Note that the generated types for the same WGSL type may differ in size or alignment.
Functions§
- Generates a Rust module for a WGSL shader included via include_str.
- Generates a Rust module for a WGSL shader embedded as a string literal.