README: Update the component list.
[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 . A GLSL/ESSL front-end for reference validation and translation of GLSL/ESSL into an AST.
19
20 . An HLSL front-end for translation of a broad generic HLL into the AST.
21
22 . A SPIR-V back end for translating the AST to SPIR-V.
23
24 . 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 ### Dependencies
53
54 * [CMake][cmake]: for generating compilation targets.
55 * [bison][bison]: _optional_, but needed when changing the grammar (glslang.y).
56 * [googletest][googletest]: _optional_, but should use if making any changes to glslang.
57
58 ### Build steps
59
60 #### 1) Check-Out External Projects
61
62 ```bash
63 cd <the directory glslang was cloned to, External will be a subdirectory>
64 git clone https://github.com/google/googletest.git External/googletest
65 ```
66
67 #### 2) Configure
68
69 Assume the source directory is `$SOURCE_DIR` and
70 the build directory is `$BUILD_DIR`:
71
72 For building on Linux (assuming using the Ninja generator):
73
74 ```bash
75 cd $BUILD_DIR
76
77 cmake -GNinja -DCMAKE_BUILD_TYPE={Debug|Release|RelWithDebInfo} \
78       -DCMAKE_INSTALL_PREFIX=`pwd`/install $SOURCE_DIR
79 ```
80
81 For building on Windows:
82
83 ```bash
84 cmake $SOURCE_DIR -DCMAKE_INSTALL_PREFIX=`pwd`/install
85 # The CMAKE_INSTALL_PREFIX part is for testing (explained later).
86 ```
87
88 The CMake GUI also works for Windows (version 3.4.1 tested).
89
90 #### 3) Build and Install
91
92 ```bash
93 # for Linux:
94 ninja install
95
96 # for Windows:
97 cmake --build . --config {Release|Debug|MinSizeRel|RelWithDebInfo} \
98       --target install
99 ```
100
101 If using MSVC, after running CMake to configure, use the
102 Configuration Manager to check the `INSTALL` project.
103
104 ### If you need to change the GLSL grammar
105
106 The grammar in `glslang/MachineIndependent/glslang.y` has to be recompiled with
107 bison if it changes, the output files are committed to the repo to avoid every
108 developer needing to have bison configured to compile the project when grammar
109 changes are quite infrequent. For windows you can get binaries from
110 [GnuWin32][bison-gnu-win32].
111
112 The command to rebuild is:
113
114 ```bash
115 bison --defines=MachineIndependent/glslang_tab.cpp.h
116       -t MachineIndependent/glslang.y
117       -o MachineIndependent/glslang_tab.cpp
118 ```
119
120 The above command is also available in the bash script at
121 `glslang/updateGrammar`.
122
123 Testing
124 -------
125
126 Right now, there are two test harnesses existing in glslang: one is [Google
127 Test](gtests/), one is the [`runtests` script](Test/runtests). The former
128 runs unit tests and single-shader single-threaded integration tests, while
129 the latter runs multiple-shader linking tests and multi-threaded tests.
130
131 ### Running tests
132
133 The [`runtests` script](Test/runtests) requires compiled binaries to be
134 installed into `$BUILD_DIR/install`. Please make sure you have supplied the
135 correct configuration to CMake (using `-DCMAKE_INSTALL_PREFIX`) when building;
136 otherwise, you may want to modify the path in the `runtests` script.
137
138 Running Google Test-backed tests:
139
140 ```bash
141 cd $BUILD_DIR
142
143 # for Linux:
144 ctest
145
146 # for Windows:
147 ctest -C {Debug|Release|RelWithDebInfo|MinSizeRel}
148
149 # or, run the test binary directly
150 # (which gives more fine-grained control like filtering):
151 <dir-to-glslangtests-in-build-dir>/glslangtests
152 ```
153
154 Running `runtests` script-backed tests:
155
156 ```bash
157 cd $SOURCE_DIR/Test && ./runtests
158 ```
159
160 ### Contributing tests
161
162 Test results should always be included with a pull request that modifies
163 functionality.
164
165 If you are writing unit tests, please use the Google Test framework and
166 place the tests under the `gtests/` directory.
167
168 Integration tests are placed in the `Test/` directory. It contains test input
169 and a subdirectory `baseResults/` that contains the expected results of the
170 tests.  Both the tests and `baseResults/` are under source-code control.
171
172 Google Test runs those integration tests by reading the test input, compiling
173 them, and then compare against the expected results in `baseResults/`. The
174 integration tests to run via Google Test is registered in various
175 `gtests/*.FromFile.cpp` source files. `glslangtests` provides a command-line
176 option `--update-mode`, which, if supplied, will overwrite the golden files
177 under the `baseResults/` directory with real output from that invocation.
178 For more information, please check `gtests/` directory's
179 [README](gtests/README.md).
180
181 For the `runtests` script, it will generate current results in the
182 `localResults/` directory and `diff` them against the `baseResults/`.
183 When you want to update the tracked test results, they need to be
184 copied from `localResults/` to `baseResults/`.  This can be done by
185 the `bump` shell script.
186
187 You can add your own private list of tests, not tracked publicly, by using
188 `localtestlist` to list non-tracked tests.  This is automatically read
189 by `runtests` and included in the `diff` and `bump` process.
190
191 Programmatic Interfaces
192 -----------------------
193
194 Another piece of software can programmatically translate shaders to an AST
195 using one of two different interfaces:
196 * A new C++ class-oriented interface, or
197 * The original C functional interface
198
199 The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
200
201 ### C++ Class Interface (new, preferred)
202
203 This interface is in roughly the last 1/3 of `ShaderLang.h`.  It is in the
204 glslang namespace and contains the following.
205
206 ```cxx
207 const char* GetEsslVersionString();
208 const char* GetGlslVersionString();
209 bool InitializeProcess();
210 void FinalizeProcess();
211
212 class TShader
213     bool parse(...);
214     void setStrings(...);
215     const char* getInfoLog();
216
217 class TProgram
218     void addShader(...);
219     bool link(...);
220     const char* getInfoLog();
221     Reflection queries
222 ```
223
224 See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
225 details.
226
227 ### C Functional Interface (orignal)
228
229 This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
230 as the `Sh*()` interface, as all the entry points start `Sh`.
231
232 The `Sh*()` interface takes a "compiler" call-back object, which it calls after
233 building call back that is passed the AST and can then execute a backend on it.
234
235 The following is a simplified resulting run-time call stack:
236
237 ```c
238 ShCompile(shader, compiler) -> compiler(AST) -> <back end>
239 ```
240
241 In practice, `ShCompile()` takes shader strings, default version, and
242 warning/error and other options for controlling compilation.
243
244 Basic Internal Operation
245 ------------------------
246
247 * Initial lexical analysis is done by the preprocessor in
248   `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
249   in `MachineIndependent/Scan.cpp`.  There is currently no use of flex.
250
251 * Code is parsed using bison on `MachineIndependent/glslang.y` with the
252   aid of a symbol table and an AST.  The symbol table is not passed on to
253   the back-end; the intermediate representation stands on its own.
254   The tree is built by the grammar productions, many of which are
255   offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
256
257 * The intermediate representation is very high-level, and represented
258   as an in-memory tree.   This serves to lose no information from the
259   original program, and to have efficient transfer of the result from
260   parsing to the back-end.  In the AST, constants are propogated and
261   folded, and a very small amount of dead code is eliminated.
262
263   To aid linking and reflection, the last top-level branch in the AST
264   lists all global symbols.
265
266 * The primary algorithm of the back-end compiler is to traverse the
267   tree (high-level intermediate representation), and create an internal
268   object code representation.  There is an example of how to do this
269   in `MachineIndependent/intermOut.cpp`.
270
271 * Reduction of the tree to a linear byte-code style low-level intermediate
272   representation is likely a good way to generate fully optimized code.
273
274 * There is currently some dead old-style linker-type code still lying around.
275
276 * Memory pool: parsing uses types derived from C++ `std` types, using a
277   custom allocator that puts them in a memory pool.  This makes allocation
278   of individual container/contents just few cycles and deallocation free.
279   This pool is popped after the AST is made and processed.
280
281   The use is simple: if you are going to call `new`, there are three cases:
282
283   - the object comes from the pool (its base class has the macro
284     `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
285
286   - it is a `TString`, in which case call `NewPoolTString()`, which gets
287     it from the pool, and there is no corresponding `delete`
288
289   - the object does not come from the pool, and you have to do normal
290     C++ memory management of what you `new`
291
292
293 [cmake]: https://cmake.org/
294 [bison]: https://www.gnu.org/software/bison/
295 [googletest]: https://github.com/google/googletest
296 [bison-gnu-win32]: http://gnuwin32.sourceforge.net/packages/bison.htm