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