Crate wgsl_to_wgpu

Source
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.
ModulePath
A fully qualified absolute module path like a::b split into ["a", "b"].
TypePath
A fully qualified absolute path like a::b::Item split into ["a", "b"] and "Item".
ValidationOptions
Options for semantic validation.
WgslCapabilities
Allowed IR capabilities.
WriteOptions
Options for configuring the generated bindings to work with additional dependencies. Use WriteOptions::default for only requiring WGPU itself.

Enums§

CreateModuleError
Errors while generating Rust source for a WGSL shader module.
MatrixVectorTypes
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.