Crate wgsl_bindgen
source ·Expand description
§wgsl_bindgen
wgsl_bindgen is an experimental library for generating typesafe Rust bindings from WGSL shaders to wgpu.
§Features
The WgslBindgenOptionBuilder is used to configure the generation of Rust bindings from WGSL shaders. 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.
Writing Rust code to interact with WGSL shaders can be tedious and error prone, especially when the types and functions in the shader code change during development. wgsl_bindgen is not a rendering library and does not offer high level abstractions like a scene graph or material system. However, using generated code still has a number of advantages compared to writing the code by hand.
The code generated by wgsl_bindgen can help with valid API usage like:
- setting all bind groups and bind group bindings
- setting correct struct fields and field types for vertex input buffers
- setting correct struct struct fields and field types for storage and uniform buffers
- configuring shader initialization
- getting vertex attribute offsets for vertex buffers
- const validation of struct memory layouts when using bytemuck
Here’s an example of how to use WgslBindgenOptionBuilder to generate Rust bindings from WGSL shaders:
use miette::{IntoDiagnostic, Result};
use wgsl_bindgen::{WgslTypeSerializeStrategy, WgslBindgenOptionBuilder, GlamWgslTypeMap};
fn main() -> Result<()> {
WgslBindgenOptionBuilder::default()
.add_entry_point("src/shader/testbed.wgsl")
.add_entry_point("src/shader/triangle.wgsl")
.skip_hash_check(true)
.serialization_strategy(WgslTypeSerializeStrategy::Bytemuck)
.wgsl_type_map(GlamWgslTypeMap)
.derive_serde(false)
.output_file("src/shader.rs")
.build()?
.generate()
.into_diagnostic()
}Modules§
Structs§
- A struct representing a directory to scan for additional source files.
glamtypes likeglam::Vec4orglam::Mat4. Types not representable byglamlikemat2x3<f32>will use the output from [WgslRustTypeMap::map].- Path used in the import statement
nalgebratypes likenalgebra::SVector<f64, 4>ornalgebra::SMatrix<f32, 2, 3>.- Builder for
WgslBindgenOption. - Rust types like
[f32; 4]or[[f32; 4]; 4]. - Allowed IR capabilities.
- An iterator over the variants of WgslType
Enums§
- Errors while generating Rust source for a WGSl shader module.
- Enum representing the possible errors that can occur in the
wgsl_bindgenprocess. - Error type for WgslBindgenOptionBuilder
- The
WgslTypeenum represents various WGSL types, such as vectors and matrices. See spec - Enum representing the possible serialization strategies for WGSL types.
Traits§
- A trait for building
WgslTypetoTokenStreammap.