Skip to content

jrachele/wgsl-preprocessor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WGSL Preprocessor

Does what it says on the tin. Written in Zig.

Inspired by Bevy's preprocessor.

Features

Import other files using the #import directive

shader.wgsl:

#import "other_shader.wgsl"

var b = 10;

other_shader.wgsl:

var a = 5;

Result:

var a = 5;

var b = 10;

#if directives with conditions at shader compile time

// ... somewhere in your Zig code
const processed_wgsl = ShaderPreprocessor.process("conditional.wgsl", .{
    .conditions = .{
        .full_screen = global.config.full_screen,
        .mouse_captured = false,
        .foo = true,
    }
});

conditional.wgsl:

#if full_screen
var screen_dims = vec2<u32>(1920, 1080);
#else
var screen_dims = vec2<u32>(1280, 720);
#endif

@compute @workgroup_size(8, 8, 1)
fn main() {
    #if mouse_captured
    // Do something
    #endif

    #if foo
    // Do something else
    #endif 
}

Constants to inject into the shader at compile time using #(constant_ident)

// ... somewhere in your Zig code
const processed_wgsl = ShaderPreprocessor.process("constants.wgsl", .{
    .constants = .{
        .workgroup_x = app.config.workgroup_size,
        .workgroup_y = app.config.workgroup_size, 
    }
});

constants.wgsl:

@compute @workgroup_size(#(workgroup_x), #(workgroup_y), 1)
fn main() {
}

Removing comments and whitespace

There are options available for removing comments and whitespace:

// ... somewhere in your Zig code
const processed_wgsl = ShaderPreprocessor.process("comment.wgsl", .{
    .options = .{
      .remove_comments = true,
      .remove_whitespace = true,
    }
});

comment.wgsl:

var a = 1;
/*
* This is a block comment
*
*/

var b = 2;

// This is a line comment
// Here's another

var c = 3;
// Comment at the end!

Result with only remove_comments = true:

var a = 1;

var b = 2;


var c = 3;

Result with both remove_comments = true and remove_whitespace = true:

var a = 1;var b = 2;var c = 3;

Dependencies

  • Mecha - parser combinator library

About

Shader preprocessor for WGSL, written in Zig

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published