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 and create_shader_modules functions are 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();
}
§Modules and Preprocessors
There are a number of useful processing crates that extend or modify WGSL to add features like module imports or conditional compilation. wgsl_to_wgpu does not provide support for any of these crates directly. Instead, pass the final processed WGSL to create_shader_modules and specify the approriate name demangling logic if needed. See the function documentation for details.
Structs§
- Module
- Generated code for a Rust module and its submodules.
- Module
Path - A fully qualified absolute module path like
a::b
split into["a", "b"]
. - Type
Path - A fully qualified absolute path like
a::b::Item
split into["a", "b"]
and"Item"
. - Validation
Options - Options for semantic validation.
- Wgsl
Capabilities - Allowed IR capabilities.
- Write
Options - Options for configuring the generated bindings to work with additional dependencies. Use WriteOptions::default for only requiring WGPU itself.
Enums§
- Create
Module Error - Errors while generating Rust source for a WGSL shader module.
- Matrix
Vector Types - 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§
- create_
shader_ module - Create a Rust module for a WGSL shader included via include_str.
- create_
shader_ modules - Create Rust module(s) for a WGSL shader included as a string literal.
- demangle_
identity - An identity demangling function that treats
name
as an item in the root module.