Merge pull request #1620 from akien-mga/unix-executable-bit
[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 2.7][python]: for executing SPIRV-Tools scripts. (Optional if not using SPIRV-Tools.)
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 #### 4) Build and Install
131
132 ```bash
133 # for Linux:
134 make -j4 install
135
136 # for Windows:
137 cmake --build . --config Release --target install
138 # "Release" (for --config) could also be "Debug", "MinSizeRel", or "RelWithDebInfo"
139 ```
140
141 If using MSVC, after running CMake to configure, use the
142 Configuration Manager to check the `INSTALL` project.
143
144 ### If you need to change the GLSL grammar
145
146 The grammar in `glslang/MachineIndependent/glslang.y` has to be recompiled with
147 bison if it changes, the output files are committed to the repo to avoid every
148 developer needing to have bison configured to compile the project when grammar
149 changes are quite infrequent. For windows you can get binaries from
150 [GnuWin32][bison-gnu-win32].
151
152 The command to rebuild is:
153
154 ```bash
155 bison --defines=MachineIndependent/glslang_tab.cpp.h
156       -t MachineIndependent/glslang.y
157       -o MachineIndependent/glslang_tab.cpp
158 ```
159
160 The above command is also available in the bash script at
161 `glslang/updateGrammar`.
162
163 Testing
164 -------
165
166 Right now, there are two test harnesses existing in glslang: one is [Google
167 Test](gtests/), one is the [`runtests` script](Test/runtests). The former
168 runs unit tests and single-shader single-threaded integration tests, while
169 the latter runs multiple-shader linking tests and multi-threaded tests.
170
171 ### Running tests
172
173 The [`runtests` script](Test/runtests) requires compiled binaries to be
174 installed into `$BUILD_DIR/install`. Please make sure you have supplied the
175 correct configuration to CMake (using `-DCMAKE_INSTALL_PREFIX`) when building;
176 otherwise, you may want to modify the path in the `runtests` script.
177
178 Running Google Test-backed tests:
179
180 ```bash
181 cd $BUILD_DIR
182
183 # for Linux:
184 ctest
185
186 # for Windows:
187 ctest -C {Debug|Release|RelWithDebInfo|MinSizeRel}
188
189 # or, run the test binary directly
190 # (which gives more fine-grained control like filtering):
191 <dir-to-glslangtests-in-build-dir>/glslangtests
192 ```
193
194 Running `runtests` script-backed tests:
195
196 ```bash
197 cd $SOURCE_DIR/Test && ./runtests
198 ```
199
200 ### Contributing tests
201
202 Test results should always be included with a pull request that modifies
203 functionality.
204
205 If you are writing unit tests, please use the Google Test framework and
206 place the tests under the `gtests/` directory.
207
208 Integration tests are placed in the `Test/` directory. It contains test input
209 and a subdirectory `baseResults/` that contains the expected results of the
210 tests.  Both the tests and `baseResults/` are under source-code control.
211
212 Google Test runs those integration tests by reading the test input, compiling
213 them, and then compare against the expected results in `baseResults/`. The
214 integration tests to run via Google Test is registered in various
215 `gtests/*.FromFile.cpp` source files. `glslangtests` provides a command-line
216 option `--update-mode`, which, if supplied, will overwrite the golden files
217 under the `baseResults/` directory with real output from that invocation.
218 For more information, please check `gtests/` directory's
219 [README](gtests/README.md).
220
221 For the `runtests` script, it will generate current results in the
222 `localResults/` directory and `diff` them against the `baseResults/`.
223 When you want to update the tracked test results, they need to be
224 copied from `localResults/` to `baseResults/`.  This can be done by
225 the `bump` shell script.
226
227 You can add your own private list of tests, not tracked publicly, by using
228 `localtestlist` to list non-tracked tests.  This is automatically read
229 by `runtests` and included in the `diff` and `bump` process.
230
231 Programmatic Interfaces
232 -----------------------
233
234 Another piece of software can programmatically translate shaders to an AST
235 using one of two different interfaces:
236 * A new C++ class-oriented interface, or
237 * The original C functional interface
238
239 The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
240
241 ### C++ Class Interface (new, preferred)
242
243 This interface is in roughly the last 1/3 of `ShaderLang.h`.  It is in the
244 glslang namespace and contains the following.
245
246 ```cxx
247 const char* GetEsslVersionString();
248 const char* GetGlslVersionString();
249 bool InitializeProcess();
250 void FinalizeProcess();
251
252 class TShader
253     setStrings(...);
254     setEnvInput(EShSourceHlsl or EShSourceGlsl, stage,  EShClientVulkan or EShClientOpenGL, 100);
255     setEnvClient(EShClientVulkan or EShClientOpenGL, EShTargetVulkan_1_0 or EShTargetVulkan_1_1 or EShTargetOpenGL_450);
256     setEnvTarget(EShTargetSpv, EShTargetSpv_1_0 or EShTargetSpv_1_3);
257     bool parse(...);
258     const char* getInfoLog();
259
260 class TProgram
261     void addShader(...);
262     bool link(...);
263     const char* getInfoLog();
264     Reflection queries
265 ```
266
267 See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
268 details.
269
270 ### C Functional Interface (orignal)
271
272 This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
273 as the `Sh*()` interface, as all the entry points start `Sh`.
274
275 The `Sh*()` interface takes a "compiler" call-back object, which it calls after
276 building call back that is passed the AST and can then execute a backend on it.
277
278 The following is a simplified resulting run-time call stack:
279
280 ```c
281 ShCompile(shader, compiler) -> compiler(AST) -> <back end>
282 ```
283
284 In practice, `ShCompile()` takes shader strings, default version, and
285 warning/error and other options for controlling compilation.
286
287 Basic Internal Operation
288 ------------------------
289
290 * Initial lexical analysis is done by the preprocessor in
291   `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
292   in `MachineIndependent/Scan.cpp`.  There is currently no use of flex.
293
294 * Code is parsed using bison on `MachineIndependent/glslang.y` with the
295   aid of a symbol table and an AST.  The symbol table is not passed on to
296   the back-end; the intermediate representation stands on its own.
297   The tree is built by the grammar productions, many of which are
298   offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
299
300 * The intermediate representation is very high-level, and represented
301   as an in-memory tree.   This serves to lose no information from the
302   original program, and to have efficient transfer of the result from
303   parsing to the back-end.  In the AST, constants are propogated and
304   folded, and a very small amount of dead code is eliminated.
305
306   To aid linking and reflection, the last top-level branch in the AST
307   lists all global symbols.
308
309 * The primary algorithm of the back-end compiler is to traverse the
310   tree (high-level intermediate representation), and create an internal
311   object code representation.  There is an example of how to do this
312   in `MachineIndependent/intermOut.cpp`.
313
314 * Reduction of the tree to a linear byte-code style low-level intermediate
315   representation is likely a good way to generate fully optimized code.
316
317 * There is currently some dead old-style linker-type code still lying around.
318
319 * Memory pool: parsing uses types derived from C++ `std` types, using a
320   custom allocator that puts them in a memory pool.  This makes allocation
321   of individual container/contents just few cycles and deallocation free.
322   This pool is popped after the AST is made and processed.
323
324   The use is simple: if you are going to call `new`, there are three cases:
325
326   - the object comes from the pool (its base class has the macro
327     `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
328
329   - it is a `TString`, in which case call `NewPoolTString()`, which gets
330     it from the pool, and there is no corresponding `delete`
331
332   - the object does not come from the pool, and you have to do normal
333     C++ memory management of what you `new`
334
335
336 [cmake]: https://cmake.org/
337 [python]: https://www.python.org/
338 [bison]: https://www.gnu.org/software/bison/
339 [googletest]: https://github.com/google/googletest
340 [bison-gnu-win32]: http://gnuwin32.sourceforge.net/packages/bison.htm
341 [master-tot-release]: https://github.com/KhronosGroup/glslang/releases/tag/master-tot