wgsl_to_wgpu icon indicating copy to clipboard operation
wgsl_to_wgpu copied to clipboard

Generate typesafe Rust bindings from WGSL shaders to wgpu

wgsl_to_wgpu

Latest Version docs.rs
An experimental library for generating typesafe Rust bindings from WGSL shaders to wgpu.

Usage

The WGSL shaders are parsed using naga to generate corresponding types in Rust. The provided functions can be incorporated into the compilation process using a build script. This enables catching many instances of invalid API usage at compile time such as incorrectly configuring group and binding indices. The amount of boilerplate code needed to initialize data in WGSL shaders is greatly reduced since binding layouts and descriptor code is generated automatically.

See the example crate for how to use the generated code. Run the example with cargo run.

Limitations

This project supports most WGSL types but doesn't enforce certain key properties such as field alignment. It may be necessary to disable running this function for shaders with unsupported types or features. The current implementation assumes all shader stages are part of a single WGSL source file. Vertex attributes using floating point types in WGSL like vec2<f32> are assumed to use float inputs instead of normalized attributes like unorm or snorm integers. Insufficient or innaccurate generated code should be replaced by handwritten implementations as needed.

Credits

  • naga - WGSL parser and syntax
  • wgpu - Rust implementation of WebGPU