Function wgsl_to_wgpu::create_shader_module_embedded

source ·
pub fn create_shader_module_embedded(
    wgsl_source: &str,
    options: WriteOptions
) -> Result<String, CreateModuleError>
Expand description

Generates a Rust module for a WGSL shader embedded as a string literal.

§Examples

This function is intended to be called at build time such as in a build script. The source string does not need to be from an actual file on disk. This allows applying build time operations like preprocessor defines.

// build.rs
fn main() {
    // Generate the shader at build time.
    let wgsl_source = generate_wgsl_source_string();

    // Configure the output based on the dependencies for the project.
    let options = wgsl_to_wgpu::WriteOptions {
        derive_bytemuck_vertex: true,
        derive_encase_host_shareable: true,
        matrix_vector_types: wgsl_to_wgpu::MatrixVectorTypes::Glam,
        ..Default::default()
    };

    // Generate the bindings.
    let text = wgsl_to_wgpu::create_shader_module_embedded(&wgsl_source, options).unwrap();
    std::fs::write("src/shader.rs", text.as_bytes()).unwrap();
}