#wgsl #preprocessor #proc-macro #c-style #define #validation #include-wgsl

nightly macro wgpu-pp

Simple, C-style preprocessor for WGSL using proc macros

1 stable release

1.0.0 Aug 30, 2024

#1721 in Procedural macros

MIT license

20KB
371 lines

wgpu-pp

Simple, C-style preprocessor for WGSL using proc macros. It also runs compile-time validation via Naga.

include_wgsl!("../shader.wgsl")

shader.wgsl:

#include "common/color.wgsl"

#define PI f32(3.14159)

@fragment
fn fs_main(
    @builtin(position) position: vec4f,
) -> @location(0) vec4f {
    let x = 2.0 * (position.x - 0.5) * PI
    let y = 2.0 * (position.y - 0.5) * PI

    return srgb_to_linear(vec4(
        0.5 + 0.5 * sin(x),
        0.0,
        0.5 + 0.5 * cos(x),
        1.0
    ));
}

Preprocessor statements:

  • #include: Ability to share code between shaders.
  • #define: Work around WGSL 1.0 limitations (such as passing arrays to functions). Works for both constants and macros.

wgpu-pp does not aim to output human-readable WGSL, there may be extraneous newlines—comments are also stripped.

License

This work is distributed under the MIT License.

Dependencies

~8–16MB
~223K SLoC