]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.h
58bc4ca202e6bdbf60f5d625c1f5b5ba2004ef14
[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 /** Manipulates specialization constants.  If values are specified, turns
15 specialization constants into normal constants.  Without values assigns
16 automatic constant_ids to specialization constants. */
17 class ConstantSpecializer: private TraversingVisitor
18 {
19 private:
20         const std::map<std::string, int> *values;
21
22 public:
23         ConstantSpecializer();
24
25         void apply(Stage &, const std::map<std::string, int> *);
26
27 private:
28         virtual void visit(VariableDeclaration &);
29 };
30
31 /** Forms links between nested blocks in the syntax tree. */
32 class BlockHierarchyResolver: private TraversingVisitor
33 {
34 private:
35         bool r_any_resolved;
36
37 public:
38         BlockHierarchyResolver(): r_any_resolved(false) { }
39
40         bool apply(Stage &s) { r_any_resolved = false; s.content.visit(*this); return r_any_resolved; }
41
42 private:
43         virtual void enter(Block &);
44 };
45
46 /** Resolves types of variables and base types of other types. */
47 class TypeResolver: private TraversingVisitor
48 {
49 private:
50         Stage *stage;
51         std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
52         std::map<TypeDeclaration *, TypeDeclaration *> array_types;
53         NodeList<Statement>::iterator type_insert_point;
54         InterfaceBlock *iface_block;
55         bool r_any_resolved;
56
57 public:
58         TypeResolver();
59
60         bool apply(Stage &);
61
62 private:
63         TypeDeclaration *get_or_create_array_type(TypeDeclaration &);
64         void resolve_type(TypeDeclaration *&, const std::string &, bool);
65         virtual void visit(Block &);
66         virtual void visit(BasicTypeDeclaration &);
67         virtual void visit(ImageTypeDeclaration &);
68         virtual void visit(StructDeclaration &);
69         virtual void visit(VariableDeclaration &);
70         virtual void visit(InterfaceBlock &);
71         virtual void visit(FunctionDeclaration &);
72 };
73
74 /** Resolves variable references.  Variable references which match the name
75 of an interface block are turned into interface block references. */
76 class VariableResolver: private TraversingVisitor
77 {
78 private:
79         Stage *stage;
80         RefPtr<Expression> r_replacement_expr;
81         bool r_any_resolved;
82         bool record_target;
83         bool r_self_referencing;
84         Assignment::Target r_assignment_target;
85         std::vector<VariableDeclaration *> redeclared_builtins;
86         std::set<Node *> nodes_to_remove;
87
88 public:
89         VariableResolver();
90
91         bool apply(Stage &);
92
93 private:
94         virtual void enter(Block &);
95         virtual void visit(RefPtr<Expression> &);
96         void check_assignment_target(Statement *);
97         virtual void visit(VariableReference &);
98         virtual void visit(InterfaceBlockReference &);
99         void add_to_chain(Assignment::Target::ChainType, unsigned);
100         virtual void visit(MemberAccess &);
101         virtual void visit(Swizzle &);
102         virtual void visit(BinaryExpression &);
103         virtual void visit(Assignment &);
104         void merge_layouts(Layout &, const Layout &);
105         virtual void visit(VariableDeclaration &);
106         virtual void visit(InterfaceBlock &);
107 };
108
109 /** Resolves types and lvalueness of expressions. */
110 class ExpressionResolver: private TraversingVisitor
111 {
112 private:
113         enum Compatibility
114         {
115                 NOT_COMPATIBLE,
116                 LEFT_CONVERTIBLE,
117                 RIGHT_CONVERTIBLE,
118                 SAME_TYPE
119         };
120
121         struct ArgumentInfo
122         {
123                 BasicTypeDeclaration *type;
124                 unsigned component_count;
125         };
126
127         Stage *stage;
128         std::vector<BasicTypeDeclaration *> basic_types;
129         NodeList<Statement>::iterator insert_point;
130         bool r_any_resolved;
131
132 public:
133         ExpressionResolver();
134
135         bool apply(Stage &);
136
137 private:
138         static bool is_scalar(BasicTypeDeclaration &);
139         static bool is_vector_or_matrix(BasicTypeDeclaration &);
140         static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
141         static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
142         static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
143         BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
144         BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
145         void convert_to(RefPtr<Expression> &, BasicTypeDeclaration &);
146         bool convert_to_element(RefPtr<Expression> &, BasicTypeDeclaration &);
147         bool truncate_vector(RefPtr<Expression> &, unsigned);
148         void resolve(Expression &, TypeDeclaration *, bool);
149
150         virtual void visit(Block &);
151         virtual void visit(Literal &);
152         virtual void visit(VariableReference &);
153         virtual void visit(InterfaceBlockReference &);
154         virtual void visit(MemberAccess &);
155         virtual void visit(Swizzle &);
156         virtual void visit(UnaryExpression &);
157         void visit(BinaryExpression &, bool);
158         virtual void visit(BinaryExpression &);
159         virtual void visit(Assignment &);
160         virtual void visit(TernaryExpression &);
161         void visit_constructor(FunctionCall &);
162         virtual void visit(FunctionCall &);
163         virtual void visit(BasicTypeDeclaration &);
164         virtual void visit(VariableDeclaration &);
165 };
166
167 /** Resolves function declarations and calls. */
168 class FunctionResolver: private TraversingVisitor
169 {
170 private:
171         Stage *stage;
172         std::map<std::string, std::vector<FunctionDeclaration *> > declarations;
173         bool r_any_resolved;
174
175 public:
176         bool apply(Stage &);
177
178 private:
179         virtual void visit(FunctionCall &);
180         virtual void visit(FunctionDeclaration &);
181 };
182
183 /** Materializes implicitly declared interfaces.
184
185 Out variable declarations inside functions are moved to the global scope.
186
187 Passthrough statements are processed, generating out variables to match in
188 variables and copying values.
189
190 Unresolved variables are looked up in the previous stage's out variables. */
191 class InterfaceGenerator: private TraversingVisitor
192 {
193 private:
194         Stage *stage;
195         std::string in_prefix;
196         std::string out_prefix;
197         bool function_scope;
198         bool copy_block;
199         std::vector<VariableDeclaration *> declared_inputs;
200         Block *iface_target_block;
201         NodeList<Statement>::iterator iface_insert_point;
202         NodeList<Statement>::iterator assignment_insert_point;
203         std::set<Node *> nodes_to_remove;
204
205 public:
206         InterfaceGenerator();
207
208         void apply(Stage &);
209
210 private:
211         static std::string get_out_prefix(Stage::Type);
212         std::string change_prefix(const std::string &, const std::string &) const;
213         virtual void visit(Block &);
214         VariableDeclaration *generate_interface(VariableDeclaration &, const std::string &, const std::string &);
215         InterfaceBlock *generate_interface(InterfaceBlock &);
216         ExpressionStatement &insert_assignment(const std::string &, Expression *);
217         virtual void visit(VariableReference &);
218         virtual void visit(VariableDeclaration &);
219         virtual void visit(InterfaceBlock &);
220         virtual void visit(FunctionDeclaration &);
221         virtual void visit(Passthrough &);
222 };
223
224 } // namespace SL
225 } // namespace GL
226 } // namespace Msp
227
228 #endif