1 Also see the Khronos landing page for glslang as a reference front end:
3 https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
5 The above page includes where to get binaries, and is kept up to date
6 regarding the feature level of glslang.
11 [![Build Status](https://travis-ci.org/KhronosGroup/glslang.svg?branch=master)](https://travis-ci.org/KhronosGroup/glslang)
12 [![Build status](https://ci.appveyor.com/api/projects/status/q6fi9cb0qnhkla68/branch/master?svg=true)](https://ci.appveyor.com/project/Khronoswebmaster/glslang/branch/master)
14 An OpenGL and OpenGL ES shader front end and validator.
16 There are several components:
18 1. A GLSL/ESSL front-end for reference validation and translation of GLSL/ESSL into an AST.
20 2. An HLSL front-end for translation of a broad generic HLL into the AST. See [issue 362](https://github.com/KhronosGroup/glslang/issues/362) and [issue 701](https://github.com/KhronosGroup/glslang/issues/701) for current status.
22 3. A SPIR-V back end for translating the AST to SPIR-V.
24 4. A standalone wrapper, `glslangValidator`, that can be used as a command-line tool for the above.
26 How to add a feature protected by a version/extension/stage/profile: See the
27 comment in `glslang/MachineIndependent/Versions.cpp`.
29 Tasks waiting to be done are documented as GitHub issues.
31 Execution of Standalone Wrapper
32 -------------------------------
34 To use the standalone binary form, execute `glslangValidator`, and it will print
35 a usage statement. Basic operation is to give it a file containing a shader,
36 and it will print out warnings/errors and optionally an AST.
38 The applied stage-specific rules are based on the file extension:
39 * `.vert` for a vertex shader
40 * `.tesc` for a tessellation control shader
41 * `.tese` for a tessellation evaluation shader
42 * `.geom` for a geometry shader
43 * `.frag` for a fragment shader
44 * `.comp` for a compute shader
46 There is also a non-shader extension
47 * `.conf` for a configuration file of limits, see usage statement for example
54 * [CMake][cmake]: for generating compilation targets.
55 * [Python 2.7][python]: for executing SPIRV-Tools scripts. (Optional if not using SPIRV-Tools.)
56 * [bison][bison]: _optional_, but needed when changing the grammar (glslang.y).
57 * [googletest][googletest]: _optional_, but should use if making any changes to glslang.
61 #### 1) Check-Out this project
64 cd <parent of where you want glslang to be>
66 git clone git@github.com:KhronosGroup/glslang.git
68 git clone https://github.com/KhronosGroup/glslang.git
71 #### 2) Check-Out External Projects
74 cd <the directory glslang was cloned to, "External" will be a subdirectory>
75 git clone https://github.com/google/googletest.git External/googletest
78 If you wish to assure that SPIR-V generated from HLSL is legal for Vulkan,
79 or wish to invoke -Os to reduce SPIR-V size from HLSL or GLSL, install
80 spirv-tools with this:
83 ./update_glslang_sources.py
86 For running the CMake GUI or Visual Studio with python dependencies, you will,
87 in addition to python within the cygwin environment, need a Windows [python][python]
88 installation, including selecting the `PATH` update.
92 Assume the source directory is `$SOURCE_DIR` and
93 the build directory is `$BUILD_DIR`:
95 For building on Linux (assuming using the Ninja generator):
100 cmake -GNinja -DCMAKE_BUILD_TYPE={Debug|Release|RelWithDebInfo} \
101 -DCMAKE_INSTALL_PREFIX=`pwd`/install $SOURCE_DIR
104 For building on Windows:
107 cmake $SOURCE_DIR -DCMAKE_INSTALL_PREFIX=`pwd`/install
108 # The CMAKE_INSTALL_PREFIX part is for testing (explained later).
111 The CMake GUI also works for Windows (version 3.4.1 tested).
113 #### 4) Build and Install
120 cmake --build . --config {Release|Debug|MinSizeRel|RelWithDebInfo} \
124 If using MSVC, after running CMake to configure, use the
125 Configuration Manager to check the `INSTALL` project.
127 ### If you need to change the GLSL grammar
129 The grammar in `glslang/MachineIndependent/glslang.y` has to be recompiled with
130 bison if it changes, the output files are committed to the repo to avoid every
131 developer needing to have bison configured to compile the project when grammar
132 changes are quite infrequent. For windows you can get binaries from
133 [GnuWin32][bison-gnu-win32].
135 The command to rebuild is:
138 bison --defines=MachineIndependent/glslang_tab.cpp.h
139 -t MachineIndependent/glslang.y
140 -o MachineIndependent/glslang_tab.cpp
143 The above command is also available in the bash script at
144 `glslang/updateGrammar`.
149 Right now, there are two test harnesses existing in glslang: one is [Google
150 Test](gtests/), one is the [`runtests` script](Test/runtests). The former
151 runs unit tests and single-shader single-threaded integration tests, while
152 the latter runs multiple-shader linking tests and multi-threaded tests.
156 The [`runtests` script](Test/runtests) requires compiled binaries to be
157 installed into `$BUILD_DIR/install`. Please make sure you have supplied the
158 correct configuration to CMake (using `-DCMAKE_INSTALL_PREFIX`) when building;
159 otherwise, you may want to modify the path in the `runtests` script.
161 Running Google Test-backed tests:
170 ctest -C {Debug|Release|RelWithDebInfo|MinSizeRel}
172 # or, run the test binary directly
173 # (which gives more fine-grained control like filtering):
174 <dir-to-glslangtests-in-build-dir>/glslangtests
177 Running `runtests` script-backed tests:
180 cd $SOURCE_DIR/Test && ./runtests
183 ### Contributing tests
185 Test results should always be included with a pull request that modifies
188 If you are writing unit tests, please use the Google Test framework and
189 place the tests under the `gtests/` directory.
191 Integration tests are placed in the `Test/` directory. It contains test input
192 and a subdirectory `baseResults/` that contains the expected results of the
193 tests. Both the tests and `baseResults/` are under source-code control.
195 Google Test runs those integration tests by reading the test input, compiling
196 them, and then compare against the expected results in `baseResults/`. The
197 integration tests to run via Google Test is registered in various
198 `gtests/*.FromFile.cpp` source files. `glslangtests` provides a command-line
199 option `--update-mode`, which, if supplied, will overwrite the golden files
200 under the `baseResults/` directory with real output from that invocation.
201 For more information, please check `gtests/` directory's
202 [README](gtests/README.md).
204 For the `runtests` script, it will generate current results in the
205 `localResults/` directory and `diff` them against the `baseResults/`.
206 When you want to update the tracked test results, they need to be
207 copied from `localResults/` to `baseResults/`. This can be done by
208 the `bump` shell script.
210 You can add your own private list of tests, not tracked publicly, by using
211 `localtestlist` to list non-tracked tests. This is automatically read
212 by `runtests` and included in the `diff` and `bump` process.
214 Programmatic Interfaces
215 -----------------------
217 Another piece of software can programmatically translate shaders to an AST
218 using one of two different interfaces:
219 * A new C++ class-oriented interface, or
220 * The original C functional interface
222 The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
224 ### C++ Class Interface (new, preferred)
226 This interface is in roughly the last 1/3 of `ShaderLang.h`. It is in the
227 glslang namespace and contains the following.
230 const char* GetEsslVersionString();
231 const char* GetGlslVersionString();
232 bool InitializeProcess();
233 void FinalizeProcess();
237 void setStrings(...);
238 const char* getInfoLog();
243 const char* getInfoLog();
247 See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
250 ### C Functional Interface (orignal)
252 This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
253 as the `Sh*()` interface, as all the entry points start `Sh`.
255 The `Sh*()` interface takes a "compiler" call-back object, which it calls after
256 building call back that is passed the AST and can then execute a backend on it.
258 The following is a simplified resulting run-time call stack:
261 ShCompile(shader, compiler) -> compiler(AST) -> <back end>
264 In practice, `ShCompile()` takes shader strings, default version, and
265 warning/error and other options for controlling compilation.
267 Basic Internal Operation
268 ------------------------
270 * Initial lexical analysis is done by the preprocessor in
271 `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
272 in `MachineIndependent/Scan.cpp`. There is currently no use of flex.
274 * Code is parsed using bison on `MachineIndependent/glslang.y` with the
275 aid of a symbol table and an AST. The symbol table is not passed on to
276 the back-end; the intermediate representation stands on its own.
277 The tree is built by the grammar productions, many of which are
278 offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
280 * The intermediate representation is very high-level, and represented
281 as an in-memory tree. This serves to lose no information from the
282 original program, and to have efficient transfer of the result from
283 parsing to the back-end. In the AST, constants are propogated and
284 folded, and a very small amount of dead code is eliminated.
286 To aid linking and reflection, the last top-level branch in the AST
287 lists all global symbols.
289 * The primary algorithm of the back-end compiler is to traverse the
290 tree (high-level intermediate representation), and create an internal
291 object code representation. There is an example of how to do this
292 in `MachineIndependent/intermOut.cpp`.
294 * Reduction of the tree to a linear byte-code style low-level intermediate
295 representation is likely a good way to generate fully optimized code.
297 * There is currently some dead old-style linker-type code still lying around.
299 * Memory pool: parsing uses types derived from C++ `std` types, using a
300 custom allocator that puts them in a memory pool. This makes allocation
301 of individual container/contents just few cycles and deallocation free.
302 This pool is popped after the AST is made and processed.
304 The use is simple: if you are going to call `new`, there are three cases:
306 - the object comes from the pool (its base class has the macro
307 `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
309 - it is a `TString`, in which case call `NewPoolTString()`, which gets
310 it from the pool, and there is no corresponding `delete`
312 - the object does not come from the pool, and you have to do normal
313 C++ memory management of what you `new`
316 [cmake]: https://cmake.org/
317 [python]: https://www.python.org/
318 [bison]: https://www.gnu.org/software/bison/
319 [googletest]: https://github.com/google/googletest
320 [bison-gnu-win32]: http://gnuwin32.sourceforge.net/packages/bison.htm