#wgsl-shader #wgpu #minify #gamedev #graphics #shader #wgsl

bin+lib wgsl-minifier

A command-line tool for minifying WGSL shaders

11 releases (6 breaking)

0.7.0 Apr 10, 2025
0.6.0 Aug 15, 2024
0.5.0 Jun 22, 2024
0.4.1 Mar 11, 2024
0.3.2 Nov 20, 2023

#1181 in Game dev

Download history 37/week @ 2025-01-04 85/week @ 2025-01-11 7/week @ 2025-01-18 2/week @ 2025-01-25 17/week @ 2025-02-01 56/week @ 2025-02-08 27/week @ 2025-02-15 12/week @ 2025-02-22 42/week @ 2025-03-01 7/week @ 2025-03-08 81/week @ 2025-03-15 24/week @ 2025-03-22 160/week @ 2025-03-29 281/week @ 2025-04-05 64/week @ 2025-04-12 29/week @ 2025-04-19

534 downloads per month
Used in 4 crates (2 directly)

MIT license

17KB
312 lines

WGSL Minifier

crates.io docs.rs crates.io

A small tool built on top of Naga that makes WGSL shaders smaller by performing simple dead code elimination, stripping names of non-exported functions and local variables, and removing as much whitespace as possible.

Usage

To minify your WGSL shader, simply run the following:

cargo install --features 'bin' wgsl-minifier
wgsl-minifier path/to/your/shader.wgsl path/to/minified/output.wgsl

As a library

To use this crate as a library, for example in a game engine or larger preprocessor, two calls must be made. The first strips identifiers to smaller ones where possible. The second removes unnecessary whitespace, commas, and parentheses in a source string:

let mut module = /* your source here, or */ naga::Module::default();

// Now minify!
wgsl_minifier::minify_module(&mut module);

// Write to WGSL string
let mut validator = naga::valid::Validator::new(
    naga::valid::ValidationFlags::all(),
    naga::valid::Capabilities::all(),
);
let info = validator.validate(&module).unwrap();
let output = naga::back::wgsl::write_string(&module, &info, naga::back::wgsl::WriterFlags::empty()).unwrap();

// Minify string
let output = wgsl_minifier::minify_wgsl_source(&output);

Dependencies

~7–14MB
~176K SLoC