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