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