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