#wgsl #shader #graphics #wgpu #gamedev #validation

macro wgsl-inline

A macro used to embed WGSL within Rust

5 releases

0.2.1 Jun 22, 2024
0.2.0 Nov 2, 2023
0.1.5 Oct 27, 2023
0.1.3 Aug 6, 2023

#1926 in Game dev

Download history 5/week @ 2024-08-26 6/week @ 2024-09-09 8/week @ 2024-09-16 47/week @ 2024-09-23 15/week @ 2024-09-30 38/week @ 2024-10-07

473 downloads per month

MIT license

22KB
401 lines

WGSL Inline

crates.io docs.rs crates.io

WGSL Inline adds a macro, wgsl!, which takes WGSL sourcecode and validates it, reporting any errors to the Rust compiler.

Note that this crate is intended for small shaders - if you have a large program, with many shaders, you may prefer the include-wgsl-oil crate, which allows shaders to import other shader files using the naga-oil preprocessor.

Example

In your Cargo.toml:

wgsl-inline = "0.2"

Then in your Rust source:

mod my_shader {
    wgsl_inline::wgsl!{
        struct VertexOutput {
            @builtin(position) position: vec4<f32>,
            @location(0) frag_uv: vec2<f32>,
        }

        @vertex
        fn main(
            @location(0) position: vec4<f32>,
            @location(1) uv: vec2<f32>
        ) -> VertexOutput {
            var output: VertexOutput;
            output.position = position;
            output.frag_uv = uv;
            return output;
        }
    }
}

fn main() {
    // The generated `SOURCE` constant contains the source code,
    // with the added guarantee that the shader is valid.
    println!("shader source: {}", my_shader::SOURCE);
}

Error Checking

Error scopes are propogated to the token in the macro that caused the error. That is to say, your IDE should be able to tell you exactly which bit of the shader code isn't valid, without ever leaving Rust! For example, my IDE shows me something like the following:

Image of a WGSL compile error in an IDE

Exported items

This crate uses naga-to-tokenstream to extract information other than just the shader source into your Rust program. For a full list of the generated items, see naga-to-tokenstream. The encase, glam and naga features of the naga-to-tokenstream crate can be enabled using feature flags of the same name on this crate.

Minification

This crate comes with a "minification" feature flag minify. When enabled, all of your included shader source code will be reduced in size at compile time (removing variable names and excess whitespace). This is intended to be used on release builds, stripping debug information to increase shader parsing startup time and decrease read latency.

wgsl-inline = { version = "0.2", features = ["minify"] }

Dependencies

~6–14MB
~175K SLoC