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