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