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