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);
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.
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.
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
npm i rollup-plugin-glsl-optimize -D
This plugin uses the Khronos Glslang Validator, Khronos SPIRV-Tools Optimizer and Khronos SPIRV Cross compiler.
Binaries are automatically installed for:
- Windows 64bit (MSVC 2017)
- MacOS x86_64 (clang)
- Ubuntu Trusty / Debian Buster amd64 (clang)
Paths can be manually provided / overridden with the GLSLANG_VALIDATOR
, GLSLANG_OPTIMIZER
, GLSLANG_CROSS
environment variables.
// rollup.config.mjs
import {default as glslOptimize} from 'rollup-plugin-glsl-optimize';
export default {
// ...
plugins: [
glslOptimize(),
]
};
The following shader stages are supported by the Khronos tools and recognized by file extension:
Shader Stage | File 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
include
:PathFilter
(default table above) File extensions within rollup to include. Though this option can be reconfigured, shader stage detection still operates based on the table above.exclude
:PathFilter
(defaultundefined
) File extensions within rollup to exclude.includePaths
:string[]
(default undefined) Additional search paths for#include
directive (source file directory is always searched)
optimize
:boolean
(default true) Optimize via SPIR-V as described in the Optimization section [requires WebGL2 / GLSL ES >= 300]. When disabled simply runs the preprocessor [all supported GLSL versions].compress
:boolean
(default true) Strip all whitespace in the sources
sourceMap
:boolean
(default true) Emit source maps. These contain the final preprocessed/optimized GLSL source (but not stripped of whitespace) to aid debugging.emitLineDirectives
:boolean
(default false) Emit#line NN "original.file"
directives for debugging - useful with#include
. Note this requires theGL_GOOGLE_cpp_style_line_directive
extension so the shader will fail to run in drivers that lack support.
optimizerPreserveUnusedBindings
:boolean
(default true) Ensure that the optimizer preserves all declared bindings, even when those bindings are unused.preamble
:string
(default undefined) Prepended to the shader source (after the #version directive, before the preprocessor runs)
glslify
:boolean
(default false) Process sources using glslify prior to all preprocessing, validation and optimization.glslifyOptions
(default undefined) Whenglslify
enabled, pass these additional options toglslify.compile()
.
optimizerDebugSkipOptimizer
:boolean
(default false) Whenoptimize
enabled, skip the SPIR-V optimizer - compiles to SPIR-V then cross-compiles back to GLSL immediately.suppressLineExtensionDirective
:boolean
(default false) WhenemitLineDirectives
enabled, suppress theGL_GOOGLE_cpp_style_line_directive
directive.extraValidatorParams
,extraOptimizerParams
,extraCrossParams
:string[]
(default undefined) Additional parameters for the Khronos Glslang Validator here, the Khronos SPIR-V Optimizer here, and the Khronos SPIR-V Cross compiler here.glslangValidatorPath
,glslangOptimizerPath
,glslangCrossPath
:string
(default undefined) Provide / override binary tool paths.
It's recommended to instead use the environment variablesGLSLANG_VALIDATOR
,GLSLANG_OPTIMIZER
,GLSLANG_CROSS
where needed. They always take precedence if set.
Available in CHANGES.md.
- This plugin handles glsl and glslify by itself. Use with conflicting plugins (e.g. rollup-plugin-glsl, rollup-plugin-glslify) will cause unpredictable results.
- Optimizer:
lowp
precision qualifier - emitted asmediump
SPIR-V has a singleRelaxedPrecision
decoration for 16-32bit precision. However most implementations actually treatmediump
andlowp
equivalently, hence the lack of need for it in SPIR-V.
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.