Merge pull request #5 from KhronosGroup/master
[platform/upstream/glslang.git] / README.md
1 Also see the Khronos landing page for glslang as a reference front end:
2
3 https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
4
5 The above page includes where to get binaries, and is kept up to date
6 regarding the feature level of glslang.
7
8 glslang
9 =======
10
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)
13
14 An OpenGL and OpenGL ES shader front end and validator.
15
16 There are several components:
17
18 1. A GLSL/ESSL front-end for reference validation and translation of GLSL/ESSL into an AST.
19
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.
21
22 3. A SPIR-V back end for translating the AST to SPIR-V.
23
24 4. A standalone wrapper, `glslangValidator`, that can be used as a command-line tool for the above.
25
26 How to add a feature protected by a version/extension/stage/profile:  See the
27 comment in `glslang/MachineIndependent/Versions.cpp`.
28
29 Tasks waiting to be done are documented as GitHub issues.
30
31 Execution of Standalone Wrapper
32 -------------------------------
33
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.
37
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
45
46 There is also a non-shader extension
47 * `.conf` for a configuration file of limits, see usage statement for example
48
49 Building
50 --------
51
52 Instead of building manually, you can also download the binaries for your
53 platform directly from the [master-tot release][master-tot-release] on GitHub.
54 Those binaries are automatically uploaded by the buildbots after successful
55 testing and they always reflect the current top of the tree of the master
56 branch.
57
58 ### Dependencies
59
60 * A C++11 compiler.
61   (For MSVS: 2015 is recommended, 2013 is fully supported/tested, and 2010 support is attempted, but not tested.)
62 * [CMake][cmake]: for generating compilation targets.
63 * make: _Linux_, ninja is an alternative, if configured.
64 * [Python 3.x][python]: for executing SPIRV-Tools scripts. (Optional if not using SPIRV-Tools and the 'External' subdirectory does not exist.)
65 * [bison][bison]: _optional_, but needed when changing the grammar (glslang.y).
66 * [googletest][googletest]: _optional_, but should use if making any changes to glslang.
67
68 ### Build steps
69
70 The following steps assume a Bash shell. On Windows, that could be the Git Bash
71 shell or some other shell of your choosing.
72
73 #### 1) Check-Out this project 
74
75 ```bash
76 cd <parent of where you want glslang to be>
77 git clone https://github.com/KhronosGroup/glslang.git
78 ```
79
80 #### 2) Check-Out External Projects
81
82 ```bash
83 cd <the directory glslang was cloned to, "External" will be a subdirectory>
84 git clone https://github.com/google/googletest.git External/googletest
85 ```
86
87 If you want to use googletest with Visual Studio 2013, you also need to check out an older version:
88
89 ```bash
90 # to use googletest with Visual Studio 2013
91 cd External/googletest
92 git checkout 440527a61e1c91188195f7de212c63c77e8f0a45
93 cd ../..
94 ```
95
96 If you wish to assure that SPIR-V generated from HLSL is legal for Vulkan,
97 or wish to invoke -Os to reduce SPIR-V size from HLSL or GLSL, install
98 spirv-tools with this:
99
100 ```bash
101 ./update_glslang_sources.py
102 ```
103
104 #### 3) Configure
105
106 Assume the source directory is `$SOURCE_DIR` and the build directory is
107 `$BUILD_DIR`. First ensure the build directory exists, then navigate to it:
108
109 ```bash
110 mkdir -p $BUILD_DIR
111 cd $BUILD_DIR
112 ```
113
114 For building on Linux:
115
116 ```bash
117 cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="$(pwd)/install" $SOURCE_DIR
118 # "Release" (for CMAKE_BUILD_TYPE) could also be "Debug" or "RelWithDebInfo"
119 ```
120
121 For building on Windows:
122
123 ```bash
124 cmake $SOURCE_DIR -DCMAKE_INSTALL_PREFIX="$(pwd)/install"
125 # The CMAKE_INSTALL_PREFIX part is for testing (explained later).
126 ```
127
128 The CMake GUI also works for Windows (version 3.4.1 tested).
129
130 Also, consider using `git config --global core.fileMode false` (or with `--local`) on Windows
131 to prevent the addition of execution permission on files.
132
133 #### 4) Build and Install
134
135 ```bash
136 # for Linux:
137 make -j4 install
138
139 # for Windows:
140 cmake --build . --config Release --target install
141 # "Release" (for --config) could also be "Debug", "MinSizeRel", or "RelWithDebInfo"
142 ```
143
144 If using MSVC, after running CMake to configure, use the
145 Configuration Manager to check the `INSTALL` project.
146
147 ### If you need to change the GLSL grammar
148
149 The grammar in `glslang/MachineIndependent/glslang.y` has to be recompiled with
150 bison if it changes, the output files are committed to the repo to avoid every
151 developer needing to have bison configured to compile the project when grammar
152 changes are quite infrequent. For windows you can get binaries from
153 [GnuWin32][bison-gnu-win32].
154
155 The command to rebuild is:
156
157 ```bash
158 m4 -P MachineIndependent/glslang.m4 > MachineIndependent/glslang.y
159 bison --defines=MachineIndependent/glslang_tab.cpp.h
160       -t MachineIndependent/glslang.y
161       -o MachineIndependent/glslang_tab.cpp
162 ```
163
164 The above commands are also available in the bash script in `updateGrammar`,
165 when executed from the glslang subdirectory of the glslang repository.
166 With no arguments it builds the full grammar, and with a "web" argument,
167 the web grammar subset (see more about the web subset in the next section).
168
169 ### WASM for the the Web
170
171 Use the steps in [Build Steps](#build-steps), which following notes/exceptions:
172 * For building the web subset of core glslang:
173   + `m4` also needs a `-DGLSLANG_WEB` argument, or simply execute `updateGrammar web` from the glslang subdirectory
174   + turn off the CMAKE options for `BUILD_TESTING`, `ENABLE_OPT`, and `INSTALL_GTEST`,
175     while turning on `ENABLE_GLSLANG_WEB`
176 * `emsdk` needs to be present in your executable search path, *PATH* for
177   Bash-like enivironments
178   + Instructions located
179     [here](https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install)
180 * Do not checkout SPIRV-Tools into `External`
181   + Does not work correctly with emscripten out of the box and we don't want it
182     in the build anyway. *TBD* Have build ignore SPIRV-Tools for web build
183 * Wrap call to `cmake` using `emconfigure` with ENABLE_GLSLANG_WEB=ON:
184   + e.g. For Linux, `emconfigure cmake -DCMAKE_BUILD_TYPE=Release
185     -DENABLE_GLSLANG_WEB=ON -DCMAKE_INSTALL_PREFIX="$(pwd)/install" ..`
186 * To get a 'true' minimized build, make sure to use `brotli` to compress the .js
187   and .wasm files
188
189 Testing
190 -------
191
192 Right now, there are two test harnesses existing in glslang: one is [Google
193 Test](gtests/), one is the [`runtests` script](Test/runtests). The former
194 runs unit tests and single-shader single-threaded integration tests, while
195 the latter runs multiple-shader linking tests and multi-threaded tests.
196
197 ### Running tests
198
199 The [`runtests` script](Test/runtests) requires compiled binaries to be
200 installed into `$BUILD_DIR/install`. Please make sure you have supplied the
201 correct configuration to CMake (using `-DCMAKE_INSTALL_PREFIX`) when building;
202 otherwise, you may want to modify the path in the `runtests` script.
203
204 Running Google Test-backed tests:
205
206 ```bash
207 cd $BUILD_DIR
208
209 # for Linux:
210 ctest
211
212 # for Windows:
213 ctest -C {Debug|Release|RelWithDebInfo|MinSizeRel}
214
215 # or, run the test binary directly
216 # (which gives more fine-grained control like filtering):
217 <dir-to-glslangtests-in-build-dir>/glslangtests
218 ```
219
220 Running `runtests` script-backed tests:
221
222 ```bash
223 cd $SOURCE_DIR/Test && ./runtests
224 ```
225
226 ### Contributing tests
227
228 Test results should always be included with a pull request that modifies
229 functionality.
230
231 If you are writing unit tests, please use the Google Test framework and
232 place the tests under the `gtests/` directory.
233
234 Integration tests are placed in the `Test/` directory. It contains test input
235 and a subdirectory `baseResults/` that contains the expected results of the
236 tests.  Both the tests and `baseResults/` are under source-code control.
237
238 Google Test runs those integration tests by reading the test input, compiling
239 them, and then compare against the expected results in `baseResults/`. The
240 integration tests to run via Google Test is registered in various
241 `gtests/*.FromFile.cpp` source files. `glslangtests` provides a command-line
242 option `--update-mode`, which, if supplied, will overwrite the golden files
243 under the `baseResults/` directory with real output from that invocation.
244 For more information, please check `gtests/` directory's
245 [README](gtests/README.md).
246
247 For the `runtests` script, it will generate current results in the
248 `localResults/` directory and `diff` them against the `baseResults/`.
249 When you want to update the tracked test results, they need to be
250 copied from `localResults/` to `baseResults/`.  This can be done by
251 the `bump` shell script.
252
253 You can add your own private list of tests, not tracked publicly, by using
254 `localtestlist` to list non-tracked tests.  This is automatically read
255 by `runtests` and included in the `diff` and `bump` process.
256
257 Programmatic Interfaces
258 -----------------------
259
260 Another piece of software can programmatically translate shaders to an AST
261 using one of two different interfaces:
262 * A new C++ class-oriented interface, or
263 * The original C functional interface
264
265 The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
266
267 ### C++ Class Interface (new, preferred)
268
269 This interface is in roughly the last 1/3 of `ShaderLang.h`.  It is in the
270 glslang namespace and contains the following, here with suggested calls
271 for generating SPIR-V:
272
273 ```cxx
274 const char* GetEsslVersionString();
275 const char* GetGlslVersionString();
276 bool InitializeProcess();
277 void FinalizeProcess();
278
279 class TShader
280     setStrings(...);
281     setEnvInput(EShSourceHlsl or EShSourceGlsl, stage,  EShClientVulkan or EShClientOpenGL, 100);
282     setEnvClient(EShClientVulkan or EShClientOpenGL, EShTargetVulkan_1_0 or EShTargetVulkan_1_1 or EShTargetOpenGL_450);
283     setEnvTarget(EShTargetSpv, EShTargetSpv_1_0 or EShTargetSpv_1_3);
284     bool parse(...);
285     const char* getInfoLog();
286
287 class TProgram
288     void addShader(...);
289     bool link(...);
290     const char* getInfoLog();
291     Reflection queries
292 ```
293
294 For just validating (not generating code), subsitute these calls:
295
296 ```cxx
297     setEnvInput(EShSourceHlsl or EShSourceGlsl, stage,  EShClientNone, 0);
298     setEnvClient(EShClientNone, 0);
299     setEnvTarget(EShTargetNone, 0);
300 ```
301
302 See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
303 details. There is a block comment giving more detail above the calls for
304 `setEnvInput, setEnvClient, and setEnvTarget`.
305
306 ### C Functional Interface (orignal)
307
308 This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
309 as the `Sh*()` interface, as all the entry points start `Sh`.
310
311 The `Sh*()` interface takes a "compiler" call-back object, which it calls after
312 building call back that is passed the AST and can then execute a backend on it.
313
314 The following is a simplified resulting run-time call stack:
315
316 ```c
317 ShCompile(shader, compiler) -> compiler(AST) -> <back end>
318 ```
319
320 In practice, `ShCompile()` takes shader strings, default version, and
321 warning/error and other options for controlling compilation.
322
323 Basic Internal Operation
324 ------------------------
325
326 * Initial lexical analysis is done by the preprocessor in
327   `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
328   in `MachineIndependent/Scan.cpp`.  There is currently no use of flex.
329
330 * Code is parsed using bison on `MachineIndependent/glslang.y` with the
331   aid of a symbol table and an AST.  The symbol table is not passed on to
332   the back-end; the intermediate representation stands on its own.
333   The tree is built by the grammar productions, many of which are
334   offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
335
336 * The intermediate representation is very high-level, and represented
337   as an in-memory tree.   This serves to lose no information from the
338   original program, and to have efficient transfer of the result from
339   parsing to the back-end.  In the AST, constants are propogated and
340   folded, and a very small amount of dead code is eliminated.
341
342   To aid linking and reflection, the last top-level branch in the AST
343   lists all global symbols.
344
345 * The primary algorithm of the back-end compiler is to traverse the
346   tree (high-level intermediate representation), and create an internal
347   object code representation.  There is an example of how to do this
348   in `MachineIndependent/intermOut.cpp`.
349
350 * Reduction of the tree to a linear byte-code style low-level intermediate
351   representation is likely a good way to generate fully optimized code.
352
353 * There is currently some dead old-style linker-type code still lying around.
354
355 * Memory pool: parsing uses types derived from C++ `std` types, using a
356   custom allocator that puts them in a memory pool.  This makes allocation
357   of individual container/contents just few cycles and deallocation free.
358   This pool is popped after the AST is made and processed.
359
360   The use is simple: if you are going to call `new`, there are three cases:
361
362   - the object comes from the pool (its base class has the macro
363     `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
364
365   - it is a `TString`, in which case call `NewPoolTString()`, which gets
366     it from the pool, and there is no corresponding `delete`
367
368   - the object does not come from the pool, and you have to do normal
369     C++ memory management of what you `new`
370
371
372 [cmake]: https://cmake.org/
373 [python]: https://www.python.org/
374 [bison]: https://www.gnu.org/software/bison/
375 [googletest]: https://github.com/google/googletest
376 [bison-gnu-win32]: http://gnuwin32.sourceforge.net/packages/bison.htm
377 [master-tot-release]: https://github.com/KhronosGroup/glslang/releases/tag/master-tot