#tools #gamedev

bin+lib shader-crusher

A little helper to optimize glsl shaders for size

10 unstable releases (3 breaking)

Uses new Rust 2021

0.4.1-alpha Aug 27, 2022
0.4.0-alpha Aug 26, 2022
0.3.12-alpha Aug 26, 2022
0.3.11-alpha Jul 22, 2022
0.1.3 Apr 7, 2021

#713 in Command line utilities

Download history 10/week @ 2022-08-12 5/week @ 2022-08-19 82/week @ 2022-08-26 17/week @ 2022-09-02 22/week @ 2022-09-09 6/week @ 2022-09-16 9/week @ 2022-09-23 13/week @ 2022-09-30 5/week @ 2022-10-07 1/week @ 2022-10-14 11/week @ 2022-10-21 6/week @ 2022-10-28 22/week @ 2022-11-04 8/week @ 2022-11-11 31/week @ 2022-11-18 2/week @ 2022-11-25

64 downloads per month
Used in omt

MIT license

26KB
620 lines

Danger - Status

Works for me, but might wipe your harddisk.

Windows Status MacOS Status Linux Status

What?

Takes a glsl shader, removes white space, comments, etc, and replaces symbols/identifiers/type names by shorter ones that compress better.

Why?

I got tired of installing mono to get shader-minifier working, and needed something that I could embedd into my tools.

Why rust?

Because. And I want to learn rust.

And it's portable, and fast, and future proof.

Usage

Commandline

cargo run --help

cargo run -- --input shader.glsl --output shader_crushed.glsl

Use --blacklist "dont,crush,these" or


// code

#pragma SHADER_CRUSHER_OFF

// code

#pragma SHADER_CRUSHER_ON

// code

to keep certain identifiers untouched, e.g. uniforms that you need to resolve externaly. Keywords, built-in functions, and 'main' are automatically blacklisted.

Embedded/Linked

From C/C++

shader_crusher::ShaderCrusher* pShaderCrusher = shader_crusher::shadercrusher_new();
shader_crusher::shadercrusher_set_input( pShaderCrusher, fragmentString.c_str() );
shader_crusher::shadercrusher_crush( pShaderCrusher );
char* pOutput = shader_crusher::shadercrusher_get_ouput( pShaderCrusher );
fragmentString = std::string( pOutput );
shader_crusher::shadercrusher_free_ouput( pShaderCrusher, pOutput );
shader_crusher::shadercrusher_free( pShaderCrusher );

Don't forget do include the cbindgen generated header file, and link against the lib.

Stats

I only used it on my shaders so far, but on average the crushed size is 60%, or 40% if you further compress (e.g. with UPX/Crinkler/kkrunchy).

Soon

(because I want it)

  • Allow blacklist via c-api.
  • Add revalidation of output.
  • Run against piglet suite.
  • CI system with testing

Future

(aka not going to happen anytime soon)

  • Dead code removal
  • Smart replacement of repeated blocks
  • Multishader passes for smart extraction of shared code

Help

  • Run this against your shader, and see if it breaks anything, and what compression ratio you get.
  • Fix #52 in the glsl crate (too many braces).
  • Fix #110 in the glsl crate (whitespace in defines).
  • The swizzling blacklist is totally wrong, but gets the job done for now.
  • '#define's could be fixed, but I was lazy.

Dependencies

~3.5MB
~78K SLoC