| 1 |
GLSL optimizer |
| 2 |
============== |
| 3 |
|
| 4 |
> :warning: As of mid-2016, the project is unlikely to have any significant developments. At Unity we are moving to a different |
| 5 |
shader compilation pipeline, with glsl-optimizer is not used. So from my side there won't be significant work done on it. :warning: |
| 6 |
|
| 7 |
|
| 8 |
A C++ library that takes GLSL shaders, does some GPU-independent optimizations on them |
| 9 |
and outputs GLSL or Metal source back. Optimizations are function inlining, dead code removal, copy propagation, |
| 10 |
constant folding, constant propagation, arithmetic optimizations and so on. |
| 11 |
|
| 12 |
Apparently quite a few mobile platforms are pretty bad at optimizing shaders; and |
| 13 |
unfortunately they *also* lack offline shader compilers. So using a GLSL optimizer offline |
| 14 |
before can make the shader run much faster on a platform like that. See performance numbers |
| 15 |
in [this blog post](http://aras-p.info/blog/2010/09/29/glsl-optimizer/). |
| 16 |
|
| 17 |
Even for drivers that have decent shader optimization, GLSL optimizer could be useful to just strip away |
| 18 |
dead code, make shaders smaller and do uniform/input reflection offline. |
| 19 |
|
| 20 |
Almost all actual code is [Mesa 3D's GLSL](http://cgit.freedesktop.org/mesa/mesa/log/) |
| 21 |
compiler; all this library does is spits out optimized GLSL or Metal back, and adds GLES type precision |
| 22 |
handling to the optimizer. |
| 23 |
|
| 24 |
This GLSL optimizer is made for [Unity's](http://unity3d.com/) purposes and is built-in |
| 25 |
starting with Unity 3.0. |
| 26 |
|
| 27 |
GLSL Optimizer is licensed according to the terms of the MIT license. |
| 28 |
|
| 29 |
See [change log here](Changelog.md). |
| 30 |
|
| 31 |
|
| 32 |
Usage |
| 33 |
----- |
| 34 |
|
| 35 |
Visual Studio 2010 (Windows, x86/x64) and Xcode 5+ (Mac, i386) project files for a static |
| 36 |
library are provided in `projects/vs2010/glsl_optimizer.sln` and `projects/xcode5/glsl_optimizer_lib` |
| 37 |
respectively. |
| 38 |
|
| 39 |
> Note: only the VS and Xcode project files are maintained and should work at any time. |
| 40 |
> There's also a cmake and gyp build system for Linux et al., and some stuff in contrib folder - |
| 41 |
> all that may or might not work. |
| 42 |
|
| 43 |
For Linux you can use cmake. Just type "cmake . && make" in the root directory. |
| 44 |
This will build the optimizer library and some executable binaries. |
| 45 |
|
| 46 |
Interface for the library is `src/glsl/glsl_optimizer.h`. General usage is: |
| 47 |
|
| 48 |
ctx = glslopt_initialize(targetVersion); |
| 49 |
for (lots of shaders) { |
| 50 |
shader = glslopt_optimize (ctx, shaderType, shaderSource, options); |
| 51 |
if (glslopt_get_status (shader)) { |
| 52 |
newSource = glslopt_get_output (shader); |
| 53 |
} else { |
| 54 |
errorLog = glslopt_get_log (shader); |
| 55 |
} |
| 56 |
glslopt_shader_delete (shader); |
| 57 |
} |
| 58 |
glslopt_cleanup (ctx); |
| 59 |
|
| 60 |
|
| 61 |
Tests |
| 62 |
----- |
| 63 |
|
| 64 |
There's a testing suite for catching regressions, see `tests` folder. In VS, build |
| 65 |
and run `glsl_optimizer_tests` project; in Xcode use `projects/xcode5/glsl_optimizer_tests` |
| 66 |
project. The test executable requires path to the `tests` folder as an argument. |
| 67 |
|
| 68 |
Each test comes as three text files; input, expected IR dump and expected optimized |
| 69 |
GLSL dump. GLES3 tests are also converted into Metal. |
| 70 |
|
| 71 |
If you're making changes to the project and want pull requests accepted easier, I'd |
| 72 |
appreciate if there would be no test suite regressions. If you are implementing a |
| 73 |
feature, it would be cool to add tests to cover it as well! |
| 74 |
|
| 75 |
|
| 76 |
Notes |
| 77 |
----- |
| 78 |
|
| 79 |
* GLSL versions 1.10 and 1.20 are supported. 1.10 is the default, use #version 120 to specify |
| 80 |
1.20. Higher GLSL versions might work, but aren't tested now. |
| 81 |
* GLSL ES versions 1.00 and 3.00 are supported. |
| 82 |
|
| 83 |
|
| 84 |
Dev Notes |
| 85 |
--------- |
| 86 |
|
| 87 |
Pulling Mesa upstream: |
| 88 |
|
| 89 |
git fetch upstream |
| 90 |
git merge upstream/master |
| 91 |
sh removeDeletedByUs.sh |
| 92 |
# inspect files, git rm unneeded ones, fix conflicts etc. |
| 93 |
# git commit |
| 94 |
|
| 95 |
Rebuilding flex/bison parsers: |
| 96 |
|
| 97 |
* When .y/.l files are changed, the parsers are *not* rebuilt automatically, |
| 98 |
* Run ./generateParsers.sh to do that. You'll need bison & flex (on Mac, do "Install Command Line Tools" from Xcode) |
| 99 |
* I use bison 2.3 and flex 2.5.35 (in OS X 10.8/10.9) |
| 100 |
|