wgsl-macro
A modular and async-capable preprocessor for WGSL (WebGPU Shading Language) shader source files.
Supports powerful directives like #import, #ifdef, #const, and conditionals to enable reusable, compile-time evaluated shader logic.
Use this crate to modularize your WGSL code, inject constants, and evaluate conditions in shaders before compilation.
โจ Features
#importother shader modules by path or scheme (e.g.embedded://...)#constinjection for compile-time constants#if,#ifdef,#ifndef,#else,#endlogic blocks- Nested and multi-branch conditional support
- Async import resolution (ideal for Web/WebGPU environments)
- Strong error handling and detailed diagnostics
๐ Installation
Add to your Cargo.toml:
= "0.1"
๐งช Example
use ;
let mut processor = new;
processor.add_module;
let mut constants = new;
constants.set;
let source = r#"
#import embedded://shaders/utils.wgsl
const LIGHT_COUNT: u32 = 10;
#if LIGHT_COUNT == 50
fn main() {
util();
}
#end
"#;
let result = processor.build.unwrap;
println!;
๐ง Supported Directives
#import pathโ Load another module by path#const NAME: TYPE = VALUE;โ Define constants#if,#else if,#else,#endโ Conditional logic#ifdef,#ifndefโ Check if constants exist
๐ Async Imports
You can collect all #import directives asynchronously using get_imports:
use ShaderProcessor;
let imports = get_imports.await?;
๐ง Roadmap
- Expression parser for more advanced constant evaluation
- GLSL support
- CLI tool for shader preprocessing
๐ License
Licensed under MIT or Apache-2.0 (your choice).
โค๏ธ Contributions
PRs, issues, and suggestions are welcome! Whether you want to support #define-style macros, improve error reporting, or just fix typos โ contributions are appreciated!