| Crates.io | wgsl-macro |
| lib.rs | wgsl-macro |
| version | 0.3.2 |
| created_at | 2025-07-11 03:13:48.482513+00 |
| updated_at | 2025-07-14 02:47:06.779956+00 |
| description | A WGSL shader preprocessor supporting #import, #ifdef, and compile-time constants. |
| homepage | |
| repository | https://github.com/Eddieg26/wgsl-macro |
| max_upload_size | |
| id | 1747367 |
| size | 31,093 |
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.
#import other shader modules by path or scheme (e.g. embedded://...)#const injection for compile-time constants#if, #ifdef, #ifndef, #else, #end logic blocksAdd to your Cargo.toml:
wgsl-macro = "0.3.0"
use wgsl_preprocessor::{ShaderProcessor, ShaderConstant, ShaderConstants};
let mut processor = ShaderProcessor::new();
processor.add_module("embedded://shaders/utils.wgsl", "fn util() {}");
let mut constants = ShaderConstants::new();
constants.set("LIGHT_COUNT", ShaderConstant::U32(50));
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(source, &constants).unwrap();
println!("{}", result);
#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 existYou can collect all #import directives asynchronously using get_imports:
use wgsl_preprocessor::ShaderProcessor;
let imports = ShaderProcessor::get_imports(shader_src, |path| async move {
// Fetch from disk, server, or embedded assets
Ok(load_shader_from_source(path))
}).await?;
Licensed under MIT or Apache-2.0 (your choice).
PRs, issues, and suggestions are welcome! Whether you want to support #define-style macros, improve error reporting, or just fix typos โ contributions are appreciated!