]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
Improve support for interface blocks
[libs/gl.git] / source / glsl / generate.h
1 #ifndef MSP_GL_SL_GENERATE_H_
2 #define MSP_GL_SL_GENERATE_H_
3
4 #include <map>
5 #include <set>
6 #include <string>
7 #include <vector>
8 #include "visitor.h"
9
10 namespace Msp {
11 namespace GL {
12 namespace SL {
13
14 class DeclarationCombiner: private TraversingVisitor
15 {
16 private:
17         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
18         std::map<std::string, VariableDeclaration *> variables;
19         std::set<Node *> nodes_to_remove;
20
21 public:
22         void apply(Stage &);
23
24 private:
25         virtual void visit(Block &);
26         virtual void visit(FunctionDeclaration &);
27         virtual void visit(VariableDeclaration &);
28 };
29
30 class BlockResolver: private TraversingVisitor
31 {
32 public:
33         void apply(Stage &s) { s.content.visit(*this); }
34
35 private:
36         virtual void enter(Block &);
37         virtual void visit(InterfaceBlock &);
38 };
39
40 class VariableResolver: private TraversingVisitor
41 {
42 private:
43         Block *builtins;
44         std::map<std::string, VariableDeclaration *> *members;
45         RefPtr<InterfaceBlockReference> iface_ref;
46         std::string block_interface;
47         bool record_target;
48         VariableDeclaration *assignment_target;
49         bool self_referencing;
50
51 public:
52         VariableResolver();
53
54         void apply(Stage &);
55
56 private:
57         Block *next_block(Block &);
58
59         virtual void enter(Block &);
60         virtual void visit(VariableReference &);
61         virtual void visit(InterfaceBlockReference &);
62         virtual void visit(MemberAccess &);
63         virtual void visit(BinaryExpression &);
64         virtual void visit(Assignment &);
65         virtual void visit(StructDeclaration &);
66         virtual void visit(VariableDeclaration &);
67         virtual void visit(InterfaceBlock &);
68 };
69
70 class FunctionResolver: private TraversingVisitor
71 {
72 private:
73         std::map<std::string, std::vector<FunctionDeclaration *> > functions;
74
75 public:
76         void apply(Stage &s) { s.content.visit(*this); }
77
78 private:
79         virtual void visit(FunctionCall &);
80         virtual void visit(FunctionDeclaration &);
81 };
82
83 class InterfaceGenerator: private TraversingVisitor
84 {
85 private:
86         Stage *stage;
87         std::string in_prefix;
88         std::string out_prefix;
89         bool function_scope;
90         InterfaceBlock *iface_block;
91         NodeList<Statement>::iterator iface_insert_point;
92         NodeList<Statement>::iterator assignment_insert_point;
93         std::set<Node *> nodes_to_remove;
94
95 public:
96         InterfaceGenerator();
97
98         void apply(Stage &);
99
100 private:
101         static std::string get_out_prefix(Stage::Type);
102         std::string change_prefix(const std::string &, const std::string &) const;
103         virtual void visit(Block &);
104         bool generate_interface(VariableDeclaration &, const std::string &, const std::string &);
105         ExpressionStatement &insert_assignment(const std::string &, Expression *);
106         virtual void visit(VariableReference &);
107         virtual void visit(VariableDeclaration &);
108         virtual void visit(InterfaceBlock &);
109         virtual void visit(FunctionDeclaration &);
110         virtual void visit(Passthrough &);
111 };
112
113 class DeclarationReorderer: private TraversingVisitor
114 {
115 private:
116         enum DeclarationKind
117         {
118                 NO_DECLARATION,
119                 LAYOUT,
120                 STRUCT,
121                 VARIABLE,
122                 FUNCTION
123         };
124
125         DeclarationKind kind;
126         std::set<Node *> ordered_funcs;
127         std::set<Node *> needed_funcs;
128
129 public:
130         DeclarationReorderer();
131
132         void apply(Stage &s) { s.content.visit(*this); }
133
134 private:
135         virtual void visit(Block &);
136         virtual void visit(FunctionCall &);
137         virtual void visit(InterfaceLayout &) { kind = LAYOUT; }
138         virtual void visit(StructDeclaration &) { kind = STRUCT; }
139         virtual void visit(VariableDeclaration &);
140         virtual void visit(InterfaceBlock &) { kind = VARIABLE; }
141         virtual void visit(FunctionDeclaration &);
142 };
143
144 } // namespace SL
145 } // namespace GL
146 } // namespace Msp
147
148 #endif