Building README: Update to CMake information.
[platform/upstream/glslang.git] / README.md
1 glslang
2 =======
3
4 An OpenGL and OpenGL ES shader front end and validator.
5
6 There are two components:
7
8 1. A front-end library for programmatic parsing of GLSL/ESSL into an AST.
9
10 2. A standalone wrapper, `glslangValidator`, that can be used as a shader
11    validation tool.
12
13 How to add a feature protected by a version/extension/stage/profile:  See the
14 comment in `glslang/MachineIndependent/Versions.cpp`.
15
16 Things left to do:  See `Todo.txt`
17
18 Execution of Standalone Wrapper
19 -------------------------------
20
21 There are binaries in the `Install/Windows` and `Install/Linux` directories.
22
23 To use the standalone binary form, execute `glslangValidator`, and it will print
24 a usage statement.  Basic operation is to give it a file containing a shader,
25 and it will print out warnings/errors and optionally an AST.
26
27 The applied stage-specific rules are based on the file extension:
28 * `.vert` for a vertex shader
29 * `.tesc` for a tessellation control shader
30 * `.tese` for a tessellation evaluation shader
31 * `.geom` for a geometry shader
32 * `.frag` for a fragment shader
33 * `.comp` for a compute shader
34
35 There is also a non-shader extension
36 * `.conf` for a configuration file of limits, see usage statement for example
37
38 Building
39 --------
40
41 CMake: The currently maintained and preferred way of building is through CMake.
42 In MSVC, after running CMake, you may need to use the Configuration Manager to
43 check the INSTALL project.
44
45 Note there are some legacy build methods still intermingled within the directory
46 structure (make, MSVC), but these are no longer maintained, having been
47 replaced with CMake.
48
49 Programmatic Interfaces
50 -----------------------
51
52 Another piece of software can programmatically translate shaders to an AST
53 using one of two different interfaces:
54 * A new C++ class-oriented interface, or
55 * The original C functional interface
56
57 The `main()` in `StandAlone/StandAlone.cpp` shows examples using both styles.
58
59 ### C++ Class Interface (new, preferred)
60
61 This interface is in roughly the last 1/3 of `ShaderLang.h`.  It is in the
62 glslang namespace and contains the following.
63
64 ```cxx
65 const char* GetEsslVersionString();
66 const char* GetGlslVersionString();
67 bool InitializeProcess();
68 void FinalizeProcess();
69
70 class TShader
71     bool parse(...);
72     void setStrings(...);
73     const char* getInfoLog();
74
75 class TProgram
76     void addShader(...);
77     bool link(...);
78     const char* getInfoLog();
79     Reflection queries
80 ```
81
82 See `ShaderLang.h` and the usage of it in `StandAlone/StandAlone.cpp` for more
83 details.
84
85 ### C Functional Interface (orginal)
86
87 This interface is in roughly the first 2/3 of `ShaderLang.h`, and referred to
88 as the `Sh*()` interface, as all the entry points start `Sh`.
89
90 The `Sh*()` interface takes a "compiler" call-back object, which it calls after
91 building call back that is passed the AST and can then execute a backend on it.
92
93 The following is a simplified resulting run-time call stack:
94
95 ```c
96 ShCompile(shader, compiler) -> compiler(AST) -> <back end>
97 ```
98
99 In practice, `ShCompile()` takes shader strings, default version, and
100 warning/error and other options for controling compilation.
101
102 Testing
103 -------
104
105 `Test` is an active test directory that contains test input and a
106 subdirectory `baseResults` that contains the expected results of the
107 tests.  Both the tests and `baseResults` are under source-code control.
108 Executing the script `./runtests` will generate current results in
109 the `localResults` directory and `diff` them against the `baseResults`.
110 When you want to update the tracked test results, they need to be
111 copied from `localResults` to `baseResults`.
112
113 There are some tests borrowed from LunarGLASS.  If LunarGLASS is
114 missing, those tests just won't run.
115
116 Basic Internal Operation
117 ------------------------
118
119 * Initial lexical analysis is done by the preprocessor in
120   `MachineIndependent/Preprocessor`, and then refined by a GLSL scanner
121   in `MachineIndependent/Scan.cpp`.  There is currently no use of flex.
122
123 * Code is parsed using bison on `MachineIndependent/glslang.y` with the
124   aid of a symbol table and an AST.  The symbol table is not passed on to
125   the back-end; the intermediate representation stands on its own.
126   The tree is built by the grammar productions, many of which are
127   offloaded into `ParseHelper.cpp`, and by `Intermediate.cpp`.
128
129 * The intermediate representation is very high-level, and represented
130   as an in-memory tree.   This serves to lose no information from the
131   original program, and to have efficient transfer of the result from
132   parsing to the back-end.  In the AST, constants are propogated and
133   folded, and a very small amount of dead code is eliminated.
134
135   To aid linking and reflection, the last top-level branch in the AST
136   lists all global symbols.
137
138 * The primary algorithm of the back-end compiler is to traverse the
139   tree (high-level intermediate representation), and create an internal
140   object code representation.  There is an example of how to do this
141   in `MachineIndependent/intermOut.cpp`.
142
143 * Reduction of the tree to a linear byte-code style low-level intermediate
144   representation is likely a good way to generate fully optimized code.
145
146 * There is currently some dead old-style linker-type code still lying around.
147
148 * Memory pool: parsing uses types derived from C++ `std` types, using a
149   custom allocator that puts them in a memory pool.  This makes allocation
150   of individual container/contents just few cycles and deallocation free.
151   This pool is popped after the AST is made and processed.
152
153   The use is simple: if you are going to call `new`, there are three cases:
154
155   - the object comes from the pool (its base class has the macro
156     `POOL_ALLOCATOR_NEW_DELETE` in it) and you do not have to call `delete`
157
158   - it is a `TString`, in which case call `NewPoolTString()`, which gets
159     it from the pool, and there is no corresponding `delete`
160
161   - the object does not come from the pool, and you have to do normal
162     C++ memory management of what you `new`