wgsl-macro

Crates.iowgsl-macro
lib.rswgsl-macro
version0.3.2
created_at2025-07-11 03:13:48.482513+00
updated_at2025-07-14 02:47:06.779956+00
descriptionA WGSL shader preprocessor supporting #import, #ifdef, and compile-time constants.
homepage
repositoryhttps://github.com/Eddieg26/wgsl-macro
max_upload_size
id1747367
size31,093
Eddie Goodman (Eddieg26)

documentation

README

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

  • #import other shader modules by path or scheme (e.g. embedded://...)
  • #const injection for compile-time constants
  • #if, #ifdef, #ifndef, #else, #end logic 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:

wgsl-macro = "0.3.0"

๐Ÿงช Example

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);

๐Ÿ”ง 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 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?;

๐Ÿšง 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!

Commit count: 0

cargo fmt