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()
.workspace_root("src/shader")
.add_entry_point("src/shader/testbed.wgsl")
.add_entry_point("src/shader/triangle.wgsl")
.skip_hash_check(true)
.serialization_strategy(WgslTypeSerializeStrategy::Bytemuck)
.type_map(GlamWgslTypeMap)
.derive_serde(false)
.output("src/shader.rs".to_string())
.build()?
.generate()
.into_diagnostic()
}
Modules§
Structs§
- Additional
Scan Directory - A struct representing a directory to scan for additional source files.
- Bind
Group Layout Generator - Represents a generator for creating WGSL bind group layout structures.
- Binding
Generator - Glam
Wgsl Type Map glam
types likeglam::Vec4
orglam::Mat4
. Types not representable byglam
likemat2x3<f32>
will use the output from RustWgslTypeMap.- Import
Path Part - Import part path used in the import statement
- Nalgebra
Wgsl Type Map nalgebra
types likenalgebra::SVector<f64, 4>
ornalgebra::SMatrix<f32, 2, 3>
.- Override
Bind Group Entry Module Path - Struct for overriding binding module path of bindgroup entry
- Override
Struct - This struct is used to create a custom mapping from the wgsl side to rust side, skipping generation of the struct and using the custom one instead. This also means skipping checks for alignment and size when using bytemuck for the struct. This is useful for core primitive types you would want to model in Rust side
- Override
Struct Alignment - Struct for overriding alignment of specific structs.
- Override
Struct Field Type - Struct for overriding the field type of specific structs.
- Pipeline
Layout Generator - Regex
- A compiled regular expression for searching Unicode haystacks.
- Rust
Wgsl Type Map - Rust types like
[f32; 4]
or[[f32; 4]; 4]
. - Source
File Dir - Source
File Path - Source
Location - Source
Module Name - WGSL
Bindgen - Wgpu
GetBindings Generator Config - Wgsl
Bindgen Option - Wgsl
Bindgen Option Builder - Builder for
WgslBindgenOption
. - Wgsl
MatType Iter - An iterator over the variants of WgslMatType
- Wgsl
Shader IrCapabilities - Allowed IR capabilities.
- Wgsl
VecType Iter - An iterator over the variants of WgslVecType
Enums§
- Bind
Resource Type - Create
Module Error - Errors while generating Rust source for a WGSl shader module.
- Wgsl
Bindgen Error - Enum representing the possible errors that can occur in the
wgsl_bindgen
process. - Wgsl
Bindgen Option Builder Error - Error type for WgslBindgenOptionBuilder
- Wgsl
MatType - The
WgslType
enum represents various Wgsl matrices. See spec - Wgsl
Shader Source Type - An enum representing the source type that will be generated for the output.
- Wgsl
Type - The
WgslType
enum represents various WGSL types, such as vectors and matrices. See spec - Wgsl
Type Serialize Strategy - Enum representing the possible serialization strategies for WGSL types.
- Wgsl
Type Visibility - An enum representing the visibility of the type generated in the output
- Wgsl
VecType - The
WgslType
enum represents various WGSL vectors. See spec
Traits§
- GetBindings
Generator Config - Wgsl
Type MapBuild - A trait for building
WgslType
toTokenStream
map.
Type Aliases§
- Fast
Index Map - Insertion-order-preserving hash map (
IndexMap<K, V>
), but with the same hasher asFastHashMap<K, V>
(faster but not resilient to DoS attacks). - FxIndex
Map - FxIndex
Set - Wgsl
Type Map