]> git.tdb.fi Git - libs/gl.git/blob - source/programsyntax.h
Share shader data between copied RenderPasses
[libs/gl.git] / source / programsyntax.h
1 #ifndef MSP_GL_PROGRAMSYNTAX_H_
2 #define MSP_GL_PROGRAMSYNTAX_H_
3
4 #include <list>
5 #include <map>
6 #include <string>
7 #include <vector>
8 #include <msp/core/refptr.h>
9 #include "extension.h"
10
11 namespace Msp {
12 namespace GL {
13 namespace ProgramSyntax {
14
15 struct NodeVisitor;
16
17 struct Node
18 {
19 private:
20         Node &operator=(const Node &);
21 public:
22         virtual ~Node() { }
23
24         virtual Node *clone() const = 0;
25         virtual void visit(NodeVisitor &) = 0;
26 };
27
28 template<typename T>
29 class NodePtr: public RefPtr<T>
30 {
31 public:
32         NodePtr() { }
33         NodePtr(T *p): RefPtr<T>(p) { }
34         NodePtr(const NodePtr &p): RefPtr<T>(p ? p->clone() : 0) { }
35
36         template<typename U>
37         NodePtr(const RefPtr<U> &p): RefPtr<T>(p) { }
38
39         template<typename U>
40         NodePtr(const NodePtr<U> &p): RefPtr<T>(p ? p->clone() : 0) { }
41 };
42
43 template<typename C>
44 class NodeContainer: public C
45 {
46 public:
47         NodeContainer() { }
48         NodeContainer(const NodeContainer &);
49 };
50
51 template<typename T>
52 class NodeList: public NodeContainer<std::list<RefPtr<T> > >
53 { };
54
55 template<typename T>
56 class NodeArray: public NodeContainer<std::vector<RefPtr<T> > >
57 { };
58
59 struct StructDeclaration;
60 struct VariableDeclaration;
61 struct FunctionDeclaration;
62
63 struct Block: Node
64 {
65         NodeList<Node> body;
66         bool use_braces;
67         std::map<std::string, StructDeclaration *> types;
68         std::map<std::string, VariableDeclaration *> variables;
69
70         Block();
71
72         virtual Block *clone() const { return new Block(*this); }
73         virtual void visit(NodeVisitor &);
74 };
75
76 struct Expression: Node
77 {
78         virtual Expression *clone() const = 0;
79 };
80
81 struct Literal: Expression
82 {
83         std::string token;
84
85         virtual Literal *clone() const { return new Literal(*this); }
86         virtual void visit(NodeVisitor &);
87 };
88
89 struct ParenthesizedExpression: Expression
90 {
91         NodePtr<Expression> expression;
92
93         virtual ParenthesizedExpression *clone() const { return new ParenthesizedExpression(*this); }
94         virtual void visit(NodeVisitor &);
95 };
96
97 struct VariableReference: Expression
98 {
99         std::string name;
100         VariableDeclaration *declaration;
101
102         VariableReference();
103
104         virtual VariableReference *clone() const { return new VariableReference(*this); }
105         virtual void visit(NodeVisitor &);
106 };
107
108 struct MemberAccess: Expression
109 {
110         NodePtr<Expression> left;
111         std::string member;
112         VariableDeclaration *declaration;
113
114         virtual MemberAccess *clone() const { return new MemberAccess(*this); }
115         virtual void visit(NodeVisitor &);
116 };
117
118 struct UnaryExpression: Expression
119 {
120         std::string oper;
121         NodePtr<Expression> expression;
122         bool prefix;
123
124         UnaryExpression();
125
126         virtual UnaryExpression *clone() const { return new UnaryExpression(*this); }
127         virtual void visit(NodeVisitor &);
128 };
129
130 struct BinaryExpression: Expression
131 {
132         NodePtr<Expression> left;
133         std::string oper;
134         NodePtr<Expression> right;
135         std::string after;
136
137         virtual BinaryExpression *clone() const { return new BinaryExpression(*this); }
138         virtual void visit(NodeVisitor &);
139 };
140
141 struct Assignment: BinaryExpression
142 {
143         bool self_referencing;
144         VariableDeclaration *target_declaration;
145
146         Assignment();
147
148         virtual Assignment *clone() const { return new Assignment(*this); }
149         virtual void visit(NodeVisitor &);
150 };
151
152 struct FunctionCall: Expression
153 {
154         std::string name;
155         FunctionDeclaration *declaration;
156         bool constructor;
157         NodeArray<Expression> arguments;
158
159         FunctionCall();
160
161         virtual FunctionCall *clone() const { return new FunctionCall(*this); }
162         virtual void visit(NodeVisitor &);
163 };
164
165 struct ExpressionStatement: Node
166 {
167         NodePtr<Expression> expression;
168
169         virtual ExpressionStatement *clone() const { return new ExpressionStatement(*this); }
170         virtual void visit(NodeVisitor &);
171 };
172
173 struct Import: Node
174 {
175         std::string module;
176
177         virtual Import *clone() const { return new Import(*this); }
178         virtual void visit(NodeVisitor &);
179 };
180
181 struct Precision: Node
182 {
183         std::string precision;
184         std::string type;
185
186         virtual Precision *clone() const { return new Precision(*this); }
187         virtual void visit(NodeVisitor &);
188 };
189
190 struct Layout: Node
191 {
192         struct Qualifier
193         {
194                 // TODO the standard calls this name, not identifier
195                 std::string identifier;
196                 std::string value;
197         };
198
199         std::vector<Qualifier> qualifiers;
200
201         virtual Layout *clone() const { return new Layout(*this); }
202         virtual void visit(NodeVisitor &);
203 };
204
205 struct InterfaceLayout: Node
206 {
207         std::string interface;
208         Layout layout;
209
210         virtual InterfaceLayout *clone() const { return new InterfaceLayout(*this); }
211         virtual void visit(NodeVisitor &);
212 };
213
214 struct StructDeclaration: Node
215 {
216         std::string name;
217         Block members;
218
219         StructDeclaration();
220
221         virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
222         virtual void visit(NodeVisitor &);
223 };
224
225 struct VariableDeclaration: Node
226 {
227         bool constant;
228         std::string sampling;
229         std::string interface;
230         std::string precision;
231         std::string type;
232         StructDeclaration *type_declaration;
233         std::string name;
234         bool array;
235         NodePtr<Expression> array_size;
236         NodePtr<Expression> init_expression;
237         VariableDeclaration *linked_declaration;
238         NodePtr<Layout> layout;
239
240         VariableDeclaration();
241
242         virtual VariableDeclaration *clone() const { return new VariableDeclaration(*this); }
243         virtual void visit(NodeVisitor &);
244 };
245
246 struct InterfaceBlock: Node
247 {
248         std::string interface;
249         std::string name;
250         Block members;
251         std::string instance_name;
252         bool array;
253
254         InterfaceBlock();
255
256         virtual InterfaceBlock *clone() const { return new InterfaceBlock(*this); }
257         virtual void visit(NodeVisitor &);
258 };
259
260 struct FunctionDeclaration: Node
261 {
262         std::string return_type;
263         std::string name;
264         NodeArray<VariableDeclaration> parameters;
265         FunctionDeclaration *definition;
266         Block body;
267
268         FunctionDeclaration();
269         FunctionDeclaration(const FunctionDeclaration &);
270
271         virtual FunctionDeclaration *clone() const { return new FunctionDeclaration(*this); }
272         virtual void visit(NodeVisitor &);
273 };
274
275 struct Conditional: Node
276 {
277         NodePtr<Expression> condition;
278         Block body;
279         Block else_body;
280
281         virtual Conditional *clone() const { return new Conditional(*this); }
282         virtual void visit(NodeVisitor &);
283 };
284
285 struct Iteration: Node
286 {
287         NodePtr<Node> init_statement;
288         NodePtr<Expression> condition;
289         NodePtr<Expression> loop_expression;
290         Block body;
291
292         virtual Iteration *clone() const { return new Iteration(*this); }
293         virtual void visit(NodeVisitor &);
294 };
295
296 struct Passthrough: Node
297 {
298         NodePtr<Expression> subscript;
299
300         virtual Passthrough *clone() const { return new Passthrough(*this); }
301         virtual void visit(NodeVisitor &);
302 };
303
304 struct Return: Node
305 {
306         NodePtr<Expression> expression;
307
308         virtual Return *clone() const { return new Return(*this); }
309         virtual void visit(NodeVisitor &);
310 };
311
312 struct Jump: Node
313 {
314         std::string keyword;
315
316         virtual Jump *clone() const { return new Jump(*this); }
317         virtual void visit(NodeVisitor &);
318 };
319
320 struct NodeVisitor
321 {
322         virtual ~NodeVisitor() { }
323
324         virtual void visit(Block &) { }
325         virtual void visit(Literal &) { }
326         virtual void visit(ParenthesizedExpression &) { }
327         virtual void visit(VariableReference &) { }
328         virtual void visit(MemberAccess &) { }
329         virtual void visit(UnaryExpression &) { }
330         virtual void visit(BinaryExpression &) { }
331         virtual void visit(Assignment &);
332         virtual void visit(FunctionCall &) { }
333         virtual void visit(ExpressionStatement &) { }
334         virtual void visit(Import &) { }
335         virtual void visit(Precision &) { }
336         virtual void visit(Layout &) { }
337         virtual void visit(InterfaceLayout &) { }
338         virtual void visit(StructDeclaration &) { }
339         virtual void visit(VariableDeclaration &) { }
340         virtual void visit(InterfaceBlock &) { }
341         virtual void visit(FunctionDeclaration &) { }
342         virtual void visit(Conditional &) { }
343         virtual void visit(Iteration &) { }
344         virtual void visit(Passthrough &) { }
345         virtual void visit(Return &) { }
346         virtual void visit(Jump &) { }
347 };
348
349 struct TraversingVisitor: NodeVisitor
350 {
351         using NodeVisitor::visit;
352         virtual void visit(Block &);
353         virtual void visit(ParenthesizedExpression &);
354         virtual void visit(MemberAccess &);
355         virtual void visit(UnaryExpression &);
356         virtual void visit(BinaryExpression &);
357         virtual void visit(FunctionCall &);
358         virtual void visit(ExpressionStatement &);
359         virtual void visit(InterfaceLayout &);
360         virtual void visit(StructDeclaration &);
361         virtual void visit(VariableDeclaration &);
362         virtual void visit(InterfaceBlock &);
363         virtual void visit(FunctionDeclaration &);
364         virtual void visit(Conditional &);
365         virtual void visit(Iteration &);
366         virtual void visit(Passthrough &);
367         virtual void visit(Return &);
368 };
369
370 enum StageType
371 {
372         SHARED,
373         VERTEX,
374         GEOMETRY,
375         FRAGMENT
376 };
377
378 struct Stage
379 {
380         StageType type;
381         Stage *previous;
382         ProgramSyntax::Block content;
383         std::map<std::string, VariableDeclaration *> in_variables;
384         std::map<std::string, VariableDeclaration *> out_variables;
385         std::map<std::string, unsigned> locations;
386         Version required_version;
387
388         Stage(StageType);
389 };
390
391 struct Module
392 {
393         Stage shared;
394         std::list<Stage> stages;
395
396         Module();
397 };
398
399 } // namespace ProgramSyntax
400 } // namespace GL
401 } // namespace Msp
402
403 #endif