Nonfunctional: Remove stray ';' and fix Google Test sentence in README.
[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 An OpenGL and OpenGL ES shader front end and validator.
12
13 There are two components:
14
15 1. A front-end library for programmatic parsing of GLSL/ESSL into an AST.
16
17 2. A standalone wrapper, `glslangValidator`, that can be used as a shader
18    validation tool.
19
20 How to add a feature protected by a version/extension/stage/profile:  See the
21 comment in `glslang/MachineIndependent/Versions.cpp`.
22
23 Things left to do:  See `Todo.txt`
24
25 Execution of Standalone Wrapper
26 -------------------------------
27
28 To use the standalone binary form, execute `glslangValidator`, and it will print
29 a usage statement.  Basic operation is to give it a file containing a shader,
30 and it will print out warnings/errors and optionally an AST.
31
32 The applied stage-specific rules are based on the file extension:
33 * `.vert` for a vertex shader
34 * `.tesc` for a tessellation control shader
35 * `.tese` for a tessellation evaluation shader
36 * `.geom` for a geometry shader
37 * `.frag` for a fragment shader
38 * `.comp` for a compute shader
39
40 There is also a non-shader extension
41 * `.conf` for a configuration file of limits, see usage statement for example
42
43 Building
44 --------
45
46 CMake: The currently maintained and preferred way of building is through CMake.
47 In MSVC, after running CMake, you may need to use the Configuration Manager to
48 check the INSTALL project.
49
50 The grammar in glslang/MachineIndependent/glslang.y has to be recompiled with
51 bison if it changes, the output files are committed to the repo to avoid every
52 developer needing to have bison configured to compile the project when grammar
53 changes are quite infrequent. For windows you can get binaries from
54 [GnuWin32](http://gnuwin32.sourceforge.net/packages/bison.htm).
55
56 The command to rebuild is:
57
58 ```
59 bison --defines=MachineIndependent/glslang_tab.cpp.h
60       -t MachineIndependent/glslang.y
61       -o MachineIndependent/glslang_tab.cpp
62 ```
63
64 Glslang is adding the ability to test with
65 [Google Test](https://github.com/google/googletest) framework. If you want to
66 build and run those tests, please make sure you have a copy of Google Tests
67 checked out in the `External/` directory:
68 `git clone https://github.com/google/googletest.git`.
69
70 Programmatic Interfaces
71 -----------------------
72
73 Another piece of software can programmatically translate shaders to an AST
74 using one of two different interfaces:
75 * A new C++ class-oriented interface, or
76 * The original C functional interface
77
78 The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
79
80 ### C++ Class Interface (new, preferred)
81
82 This interface is in roughly the last 1/3 of `ShaderLang.h`.  It is in the
83 glslang namespace and contains the following.
84
85 ```cxx
86 const char* GetEsslVersionString();
87 const char* GetGlslVersionString();
88 bool InitializeProcess();
89 void FinalizeProcess();
90
91 class TShader
92     bool parse(...);
93     void setStrings(...);
94     const char* getInfoLog();
95
96 class TProgram
97     void addShader(...);
98     bool link(...);
99     const char* getInfoLog();
100     Reflection queries
101 ```
102
103 See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
104 details.
105
106 ### C Functional Interface (orignal)
107
108 This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
109 as the `Sh*()` interface, as all the entry points start `Sh`.
110
111 The `Sh*()` interface takes a "compiler" call-back object, which it calls after
112 building call back that is passed the AST and can then execute a backend on it.
113
114 The following is a simplified resulting run-time call stack:
115
116 ```c
117 ShCompile(shader, compiler) -> compiler(AST) -> <back end>
118 ```
119
120 In practice, `ShCompile()` takes shader strings, default version, and
121 warning/error and other options for controlling compilation.
122
123 Testing
124 -------
125
126 Test results should always be included with a pull request that modifies
127 functionality. And since glslang added the ability to test with
128 [Google Test](https://github.com/google/googletest) framework,
129 please write your new tests using Google Test.
130
131 The old (deprecated) testing process is:
132
133 `Test` is an active test directory that contains test input and a
134 subdirectory `baseResults` that contains the expected results of the
135 tests.  Both the tests and `baseResults` are under source-code control.
136 Executing the script `./runtests` will generate current results in
137 the `localResults` directory and `diff` them against the `baseResults`.
138
139 When you want to update the tracked test results, they need to be
140 copied from `localResults` to `baseResults`.  This can be done by
141 the `bump` shell script.
142
143 The list of files tested comes from `testlist`, and lists input shaders
144 in this directory, which must all be public for this to work.  However,
145 you can add your own private list of tests, not tracked here, by using
146 `localtestlist` to list non-tracked tests.  This is automatically read
147 by `runtests` and included in the `diff` and `bump` process.
148
149 Basic Internal Operation
150 ------------------------
151
152 * Initial lexical analysis is done by the preprocessor in
153   `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
154   in `MachineIndependent/Scan.cpp`.  There is currently no use of flex.
155
156 * Code is parsed using bison on `MachineIndependent/glslang.y` with the
157   aid of a symbol table and an AST.  The symbol table is not passed on to
158   the back-end; the intermediate representation stands on its own.
159   The tree is built by the grammar productions, many of which are
160   offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
161
162 * The intermediate representation is very high-level, and represented
163   as an in-memory tree.   This serves to lose no information from the
164   original program, and to have efficient transfer of the result from
165   parsing to the back-end.  In the AST, constants are propogated and
166   folded, and a very small amount of dead code is eliminated.
167
168   To aid linking and reflection, the last top-level branch in the AST
169   lists all global symbols.
170
171 * The primary algorithm of the back-end compiler is to traverse the
172   tree (high-level intermediate representation), and create an internal
173   object code representation.  There is an example of how to do this
174   in `MachineIndependent/intermOut.cpp`.
175
176 * Reduction of the tree to a linear byte-code style low-level intermediate
177   representation is likely a good way to generate fully optimized code.
178
179 * There is currently some dead old-style linker-type code still lying around.
180
181 * Memory pool: parsing uses types derived from C++ `std` types, using a
182   custom allocator that puts them in a memory pool.  This makes allocation
183   of individual container/contents just few cycles and deallocation free.
184   This pool is popped after the AST is made and processed.
185
186   The use is simple: if you are going to call `new`, there are three cases:
187
188   - the object comes from the pool (its base class has the macro
189     `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
190
191   - it is a `TString`, in which case call `NewPoolTString()`, which gets
192     it from the pool, and there is no corresponding `delete`
193
194   - the object does not come from the pool, and you have to do normal
195     C++ memory management of what you `new`