Crate wgsl_to_wgpu

source ·
Expand description

§wgsl_to_wgpu

wgsl_to_wgpu is an experimental library for generating typesafe Rust bindings from WGSL shaders to wgpu.

§Getting Started

The create_shader_module function is 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();
}

Structs§

Enums§

  • Errors while generating Rust source for a WGSl shader module.
  • 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§