Home

Awesome

rollup-plugin-glsl-optimize

NPM Package Changelog Node Version Types Maintainability
Dependencies Dev Dependencies Coverage Status Node.js CI

Import GLSL source files as strings. Pre-processed, validated and optimized with Khronos Group SPIRV-Tools.

Primary use-case is processing WebGL2 / GLSL ES 300 shaders.

import frag from './shaders/myShader.frag';
console.log(frag);

Features

GLSL Optimizer

For WebGL2 / GLSL ES >= 300

With optimize: true (default) shaders will be compiled to SPIR-V (opengl semantics) and optimized for performance using the Khronos SPIR-V Tools Optimizer before being cross-compiled back to GLSL.

Shader Preprocessor

Shaders are preprocessed and validated using the Khronos Glslang Validator.

Macros are run at build time with support for C-style #include directives: *

#version 300 es
#include "postProcessingShared.glsl"
#include "dofCircle.glsl"

void main() {
  outColor = CircleDof(UVAndScreenPos, Color, ColorCoc);
}

* Via the GL_GOOGLE_include_directive extension. But an #extension directive is not required nor recommended in your final inlined code.

Supports glslify

Specify glslify: true to process shader sources with glslify (a node.js-style module system for GLSL).

And install glslify in your devDependencies with npm i -D glslify

Installation

npm i rollup-plugin-glsl-optimize -D

Khronos tools

This plugin uses the Khronos Glslang Validator, Khronos SPIRV-Tools Optimizer and Khronos SPIRV Cross compiler.

Binaries are automatically installed for:

Paths can be manually provided / overridden with the GLSLANG_VALIDATOR, GLSLANG_OPTIMIZER, GLSLANG_CROSS environment variables.

Usage

// rollup.config.mjs
import {default as glslOptimize} from 'rollup-plugin-glsl-optimize';

export default {
    // ...
    plugins: [
        glslOptimize(),
    ]
};

Shader stages

The following shader stages are supported by the Khronos tools and recognized by file extension:

Shader StageFile Extensions
Vertex.vs, .vert, .vs.glsl, .vert.glsl
Fragment.fs, .frag, .fs.glsl, .frag.glsl
Geometry*.geom, .geom.glsl
Compute*.comp, .comp.glsl
Tess Control*.tesc, .tesc.glsl
Tess Eval*.tese, .tese.glsl

* Unsupported in WebGL2

Options

Features

Debugging

Preprocessor

glslify

Advanced Options

Changelog

Available in CHANGES.md.

Caveats & Known Issues

License

Released under the MIT license.
Strip whitespace function adapted from code by Vincent Wochnik (rollup-plugin-glsl).

Khronos tool binaries (built by the upstream projects) are distributed and installed with this plugin under the terms of the Apache License Version 2.0. See the corresponding LICENSE files installed in the bin folder and the binary releases.