Skip to content

Commit 335b236

Browse files
committed
Documentation, fixes
1 parent c2038d6 commit 335b236

14 files changed

+1099
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ node_modules/
55

66
bin/
77
build/
8+
types-gen/
89

910
# Logs
1011
logs

README.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,104 @@
11
# rollup-plugin-glsl-optimize
2+
[![Tool Binaries][tool-binaries]][tool-binaries-url]
23

3-
Import GLSL source files, optimized with [Khronos Group SPIRV-Tools](https://github.com/KhronosGroup/SPIRV-Tools).
4+
Import GLSL source files. Pre-processed, validated and optimized with [Khronos Group SPIRV-Tools](https://github.com/KhronosGroup/SPIRV-Tools).
45

56
```js
67
import frag from './shaders/myShader.frag';
78
console.log(frag);
89
```
10+
## Installation
11+
12+
```sh
13+
npm i rollup-plugin-glsl-optimize -D
14+
```
15+
### Khronos tool binaries
16+
This plugin requires binaries from the [Khronos Glslang Validator](https://github.com/KhronosGroup/glslang), [Khronos SPIRV-Tools Optimizer](https://github.com/KhronosGroup/SPIRV-Tools) and [Khronos SPIRV Cross compiler](https://github.com/KhronosGroup/SPIRV-Cross).
17+
18+
Upstream builds are automatically installed for:
19+
* Windows 64bit (MSVC 2017)
20+
* Ubuntu Trusty / Debian amd64 (clang)
21+
* MacOS x86_64 (clang)
22+
23+
Otherwise tool paths are provided / overridden with the ``GLSLANG_VALIDATOR``, ``GLSLANG_OPTIMIZER``, ``GLSLANG_CROSS`` environment variables.
24+
25+
## Usage
26+
```js
27+
// rollup.config.js
28+
import {default as glslOptimize} from 'rollup-plugin-glsl-optimize';
29+
30+
export default {
31+
// ...
32+
plugins: [
33+
glslOptimize(),
34+
]
35+
};
36+
```
37+
38+
## Features
39+
40+
### Preprocessing and Validation
41+
Shaders are pre-processed and validated using the [Khronos Glslang Validator](https://github.com/KhronosGroup/glslang) which helps identify syntactic and semantic errors.
42+
43+
Macros like ``#define`` are now run at build time. There's also support for C-style ``#include`` directives*:
44+
45+
```glsl
46+
#version 300 es
47+
48+
#include "postProcessingShared.glsl"
49+
#include "dofCircle.glsl"
50+
51+
void main() {
52+
outColor = CircleDof(UVAndScreenPos, Color, ColorCoc);
53+
}
54+
```
55+
*Via the ``GL_GOOGLE_include_directive`` extension. But an ``#extension`` directive is neither required nor recommended since most browsers/drivers will throw an error.*
56+
57+
### Optimization
58+
**Requires WebGL2 / GLSL ES >= 300**
59+
60+
With ``optimize: true`` (default) shaders will also be compiled to SPIR-V (opengl semantics) and optimized for performance using the [Khronos SPIR-V Tools Optimizer](https://github.com/KhronosGroup/SPIRV-Tools) before being cross-compiled back to GLSL.
61+
62+
## Shader stages
63+
64+
The following shader stages are supported by the Khronos tools and recognized by file extension:
65+
66+
| Shader Stage | File Extensions |
67+
| ------------ | ------------------------------------- |
68+
| Vertex | ``.vs, .vert, .vs.glsl, .vert.glsl`` |
69+
| Fragment | ``.fs, .frag, .fs.glsl, .frag.glsl`` |
70+
| Geometry* | ``.geom, .geom.glsl`` |
71+
| Compute* | ``.comp, .comp.glsl`` |
72+
| Tess Control* | ``.tesc, .tesc.glsl`` |
73+
| Tess Eval* | ``.tese, .tese.glsl`` |
74+
75+
*\* Unsupported in WebGL and therefore untested*
76+
77+
## Options
78+
- `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.
79+
- `exclude` : `PathFilter` (default ``undefined``) File extensions within rollup to exclude.
80+
- `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].
81+
- ``compress`` : ``boolean`` (default true) Strip all whitespace in the sources
82+
- ``includePaths`` : ``string[]`` (default undefined) Additional search paths for ``#include`` directive (source file directory is always searched)
83+
- ``sourceMap`` : ``boolean`` (default true) Emit source maps (for the final GLSL source within Javascript)
84+
- ``emitLineDirectives`` : ``boolean`` (default false) Emit ``#line NN "original.file"`` directives for debugging - useful with ``#include``. Note this requires the ``GL_GOOGLE_cpp_style_line_directive`` extension so the shader will fail to run in drivers that lack support.
85+
- ``optimizerPreserveUnusedBindings`` : ``boolean`` (default true) Ensure that the optimizer preserves all declared bindings, even when those bindings are unused.
86+
- ``preamble`` : ``string`` (default undefined) Prepended to the shader source (after the #version directive, before the preprocessor runs)
87+
### Advanced Options
88+
- ``optimizerDebugSkipOptimizer`` : ``boolean`` (default false) When ``optimize`` enabled, skip the SPIR-V optimizer - compiles to SPIR-V then cross-compiles back to GLSL immediately.
89+
- ``suppressLineExtensionDirective`` : ``boolean`` (default false) When `emitLineDirectives` enabled, suppress the ``GL_GOOGLE_cpp_style_line_directive`` directive.
90+
- ``extraValidatorParams``, ``extraOptimizerParams``, ``extraCrossParams`` : ``string[]`` (default undefined) Additional parameters for the Khronos Glslang Validator [here](doc/glslangValidator.md), the Khronos SPIR-V Optimizer [here](doc/spirv-opt.md), and the Khronos SPIR-V Cross compiler [here](doc/spirv-cross.md).
91+
- ``glslangValidatorPath``, ``glslangOptimizerPath``, ``glslangCrossPath`` : ``string`` (default undefined) Provide / override binary tool paths. Note the environment variables always take precedence if set.
992

1093
## License
1194

1295
Released under the [MIT license](LICENSE).
1396

97+
*Khronos tool binaries (built and made available by the upstream projects) are distributed and installed with this plugin under the terms of the Apache License Version 2.0 (consult the corresponding LICENSE files in the ``bin`` folder).*
98+
1499
## See also
15100

16101
* [rollup-plugin-glsl](https://github.com/vwochnik/rollup-plugin-glsl)
102+
103+
[tool-binaries]: https://github.com/docd27/rollup-plugin-glsl-optimize/actions/workflows/release-binaries.yml/badge.svg
104+
[tool-binaries-url]: https://github.com/docd27/rollup-plugin-glsl-optimize/actions/workflows/release-binaries.yml

doc/glslangValidator.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# glslangValidator
2+
3+
```
4+
Usage: glslangValidator [option]... [file]...
5+
6+
'file' can end in .<stage> for auto-stage classification, where <stage> is:
7+
.conf to provide a config file that replaces the default configuration
8+
(see -c option below for generating a template)
9+
.vert for a vertex shader
10+
.tesc for a tessellation control shader
11+
.tese for a tessellation evaluation shader
12+
.geom for a geometry shader
13+
.frag for a fragment shader
14+
.comp for a compute shader
15+
.mesh for a mesh shader
16+
.task for a task shader
17+
.rgen for a ray generation shader
18+
.rint for a ray intersection shader
19+
.rahit for a ray any hit shader
20+
.rchit for a ray closest hit shader
21+
.rmiss for a ray miss shader
22+
.rcall for a ray callable shader
23+
.glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes
24+
.hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes
25+
26+
Options:
27+
-C cascading errors; risk crash from accumulation of error recoveries
28+
-D input is HLSL (this is the default when any suffix is .hlsl)
29+
-D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>
30+
define a pre-processor macro
31+
-E print pre-processed GLSL; cannot be used with -l;
32+
errors will appear on stderr
33+
-G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;
34+
default file name is <stage>.spv (-o overrides this);
35+
'ver', when present, is the version of the input semantics,
36+
which will appear in #define GL_SPIRV ver;
37+
'--client opengl100' is the same as -G100;
38+
a '--target-env' for OpenGL will also imply '-G';
39+
currently only supports GLSL
40+
-H print human readable form of SPIR-V; turns on -V
41+
-I<dir> add dir to the include search path; includer's directory
42+
is searched first, followed by left-to-right order of -I
43+
-Od disables optimization; may cause illegal SPIR-V for HLSL
44+
-Os optimizes SPIR-V to minimize size
45+
-S <stage> uses specified stage rather than parsing the file extension
46+
choices for <stage> are vert, tesc, tese, geom, frag, or comp
47+
-U<name> | --undef-macro <name> | --U <name>
48+
undefine a pre-processor macro
49+
-V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;
50+
default file name is <stage>.spv (-o overrides this)
51+
'ver', when present, is the version of the input semantics,
52+
which will appear in #define VULKAN ver
53+
'--client vulkan100' is the same as -V100
54+
a '--target-env' for Vulkan will also imply '-V'
55+
-c configuration dump;
56+
creates the default configuration file (redirect to a .conf file)
57+
-d default to desktop (#version 110) when there is no shader #version
58+
(default is ES version 100)
59+
-e <name> | --entry-point <name>
60+
specify <name> as the entry-point function name
61+
-f{hlsl_functionality1}
62+
'hlsl_functionality1' enables use of the
63+
SPV_GOOGLE_hlsl_functionality1 extension
64+
-g generate debug information
65+
-g0 strip debug information
66+
-h print this usage message
67+
-i intermediate tree (glslang AST) is printed out
68+
-l link all input files together to form a single module
69+
-m memory leak mode
70+
-o <file> save binary to <file>, requires a binary option (e.g., -V)
71+
-q dump reflection query database; requires -l for linking
72+
-r | --relaxed-errors relaxed GLSL semantic error-checking mode
73+
-s silence syntax and semantic error reporting
74+
-t multi-threaded mode
75+
-v | --version
76+
print version strings
77+
-w | --suppress-warnings
78+
suppress GLSL warnings, except as required by "#extension : warn"
79+
-x save binary output as text-based 32-bit hexadecimal numbers
80+
-u<name>:<loc> specify a uniform location override for --aml
81+
--uniform-base <base> set a base to use for generated uniform locations
82+
--auto-map-bindings | --amb automatically bind uniform variables
83+
without explicit bindings
84+
--auto-map-locations | --aml automatically locate input/output lacking
85+
'location' (fragile, not cross stage)
86+
--client {vulkan<ver>|opengl<ver>} see -V and -G
87+
--dump-builtin-symbols prints builtin symbol table prior each compile
88+
-dumpfullversion | -dumpversion print bare major.minor.patchlevel
89+
--flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to
90+
scalars
91+
--hlsl-offsets allow block offsets to follow HLSL rules
92+
works independently of source language
93+
--hlsl-iomap perform IO mapping in HLSL register space
94+
--hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL
95+
--hlsl-dx9-compatible interprets sampler declarations as a
96+
texture/sampler combo like DirectX9 would,
97+
and recognizes DirectX9-specific semantics
98+
--invert-y | --iy invert position.Y output in vertex shader
99+
--keep-uncalled | --ku don't eliminate uncalled functions
100+
--nan-clamp favor non-NaN operand in min, max, and clamp
101+
--no-storage-format | --nsf use Unknown image format
102+
--quiet do not print anything to stdout, unless
103+
requested by another option
104+
--reflect-strict-array-suffix use strict array suffix rules when
105+
reflecting
106+
--reflect-basic-array-suffix arrays of basic types will have trailing [0]
107+
--reflect-intermediate-io reflection includes inputs/outputs of linked
108+
shaders rather than just vertex/fragment
109+
--reflect-separate-buffers reflect buffer variables and blocks
110+
separately to uniforms
111+
--reflect-all-block-variables reflect all variables in blocks, whether
112+
inactive or active
113+
--reflect-unwrap-io-blocks unwrap input/output blocks the same as
114+
uniform blocks
115+
--resource-set-binding [stage] name set binding
116+
set descriptor set and binding for
117+
individual resources
118+
--resource-set-binding [stage] set
119+
set descriptor set for all resources
120+
--rsb synonym for --resource-set-binding
121+
--shift-image-binding [stage] num
122+
base binding number for images (uav)
123+
--shift-image-binding [stage] [num set]...
124+
per-descriptor-set shift values
125+
--sib synonym for --shift-image-binding
126+
--shift-sampler-binding [stage] num
127+
base binding number for samplers
128+
--shift-sampler-binding [stage] [num set]...
129+
per-descriptor-set shift values
130+
--ssb synonym for --shift-sampler-binding
131+
--shift-ssbo-binding [stage] num base binding number for SSBOs
132+
--shift-ssbo-binding [stage] [num set]...
133+
per-descriptor-set shift values
134+
--sbb synonym for --shift-ssbo-binding
135+
--shift-texture-binding [stage] num
136+
base binding number for textures
137+
--shift-texture-binding [stage] [num set]...
138+
per-descriptor-set shift values
139+
--stb synonym for --shift-texture-binding
140+
--shift-uav-binding [stage] num base binding number for UAVs
141+
--shift-uav-binding [stage] [num set]...
142+
per-descriptor-set shift values
143+
--suavb synonym for --shift-uav-binding
144+
--shift-UBO-binding [stage] num base binding number for UBOs
145+
--shift-UBO-binding [stage] [num set]...
146+
per-descriptor-set shift values
147+
--sub synonym for --shift-UBO-binding
148+
--shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding
149+
--spirv-dis output standard-form disassembly; works only
150+
when a SPIR-V generation option is also used
151+
--spirv-val execute the SPIRV-Tools validator
152+
--source-entrypoint <name> the given shader source function is
153+
renamed to be the <name> given in -e
154+
--sep synonym for --source-entrypoint
155+
--stdin read from stdin instead of from a file;
156+
requires providing the shader stage using -S
157+
--target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl |
158+
spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}
159+
Set the execution environment that the
160+
generated code will be executed in.
161+
Defaults to:
162+
* vulkan1.0 under --client vulkan<ver>
163+
* opengl under --client opengl<ver>
164+
* spirv1.0 under --target-env vulkan1.0
165+
* spirv1.3 under --target-env vulkan1.1
166+
* spirv1.5 under --target-env vulkan1.2
167+
Multiple --target-env can be specified.
168+
--variable-name <name>
169+
--vn <name> creates a C header file that contains a
170+
uint32_t array named <name>
171+
initialized with the shader binary code
172+
```

0 commit comments

Comments
 (0)