]> git.tdb.fi Git - libs/gl.git/blob - source/programcompiler.h
Add another constructor to RenderTarget to resolve an ambiguity
[libs/gl.git] / source / programcompiler.h
1 #ifndef MSP_GL_PROGRAMCOMPILER_H_
2 #define MSP_GL_PROGRAMCOMPILER_H_
3
4 #include <set>
5 #include "programparser.h"
6 #include "programsyntax.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Program;
12 class Resources;
13
14 class ProgramCompiler
15 {
16 private:
17         struct Visitor: ProgramSyntax::TraversingVisitor
18         {
19                 typedef void ResultType;
20
21                 ProgramSyntax::Stage *stage;
22
23                 Visitor();
24
25                 virtual void apply(ProgramSyntax::Stage &);
26                 void get_result() const { }
27         };
28
29         struct BlockModifier: Visitor
30         {
31                 bool remove_node;
32                 std::vector<RefPtr<ProgramSyntax::Statement> > insert_nodes;
33
34                 BlockModifier();
35
36                 void flatten_block(ProgramSyntax::Block &);
37                 void apply_and_increment(ProgramSyntax::Block &, ProgramSyntax::NodeList<ProgramSyntax::Statement>::iterator &);
38                 using Visitor::visit;
39                 virtual void visit(ProgramSyntax::Block &);
40         };
41
42         struct Formatter: Visitor
43         {
44                 typedef std::string ResultType;
45
46                 std::string formatted;
47                 unsigned source_index;
48                 unsigned source_line;
49                 unsigned indent;
50                 bool parameter_list;
51                 std::string block_interface;
52
53                 Formatter();
54
55                 virtual void apply(ProgramSyntax::Stage &);
56                 const std::string &get_result() const { return formatted; }
57                 using Visitor::visit;
58                 void append(const std::string &);
59                 void append(char);
60                 void set_source(unsigned, unsigned);
61                 virtual void visit(ProgramSyntax::Block &);
62                 virtual void visit(ProgramSyntax::Literal &);
63                 virtual void visit(ProgramSyntax::ParenthesizedExpression &);
64                 virtual void visit(ProgramSyntax::VariableReference &);
65                 virtual void visit(ProgramSyntax::MemberAccess &);
66                 virtual void visit(ProgramSyntax::UnaryExpression &);
67                 virtual void visit(ProgramSyntax::BinaryExpression &);
68                 virtual void visit(ProgramSyntax::Assignment &);
69                 virtual void visit(ProgramSyntax::FunctionCall &);
70                 virtual void visit(ProgramSyntax::ExpressionStatement &);
71                 virtual void visit(ProgramSyntax::Import &);
72                 virtual void visit(ProgramSyntax::Precision &);
73                 virtual void visit(ProgramSyntax::Layout &);
74                 virtual void visit(ProgramSyntax::InterfaceLayout &);
75                 virtual void visit(ProgramSyntax::StructDeclaration &);
76                 virtual void visit(ProgramSyntax::VariableDeclaration &);
77                 virtual void visit(ProgramSyntax::InterfaceBlock &);
78                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
79                 virtual void visit(ProgramSyntax::Conditional &);
80                 virtual void visit(ProgramSyntax::Iteration &);
81                 virtual void visit(ProgramSyntax::Return &);
82                 virtual void visit(ProgramSyntax::Jump &);
83         };
84
85         template<typename T>
86         struct NodeGatherer: Visitor
87         {
88                 typedef std::vector<T *> ResultType;
89
90                 std::vector<T *> nodes;
91
92                 const ResultType &get_result() const { return nodes; }
93                 using Visitor::visit;
94                 virtual void visit(T &n) { nodes.push_back(&n); }
95         };
96
97         struct DeclarationCombiner: BlockModifier
98         {
99                 bool toplevel;
100                 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
101                 std::map<std::string, ProgramSyntax::VariableDeclaration *> variables;
102
103                 DeclarationCombiner();
104
105                 using Visitor::visit;
106                 virtual void visit(ProgramSyntax::Block &);
107                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
108                 virtual void visit(ProgramSyntax::VariableDeclaration &);
109         };
110
111         struct VariableResolver: Visitor
112         {
113                 std::vector<ProgramSyntax::Block *> blocks;
114                 ProgramSyntax::StructDeclaration *type;
115                 bool anonymous;
116                 std::string block_interface;
117                 bool record_target;
118                 ProgramSyntax::VariableDeclaration *assignment_target;
119                 bool self_referencing;
120
121                 VariableResolver();
122
123                 virtual void apply(ProgramSyntax::Stage &);
124                 using Visitor::visit;
125                 virtual void visit(ProgramSyntax::Block &);
126                 virtual void visit(ProgramSyntax::VariableReference &);
127                 virtual void visit(ProgramSyntax::MemberAccess &);
128                 virtual void visit(ProgramSyntax::BinaryExpression &);
129                 virtual void visit(ProgramSyntax::Assignment &);
130                 virtual void visit(ProgramSyntax::StructDeclaration &);
131                 virtual void visit(ProgramSyntax::VariableDeclaration &);
132                 virtual void visit(ProgramSyntax::InterfaceBlock &);
133         };
134
135         struct FunctionResolver: Visitor
136         {
137                 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
138
139                 using Visitor::visit;
140                 virtual void visit(ProgramSyntax::FunctionCall &);
141                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
142         };
143
144         struct InterfaceGenerator: BlockModifier
145         {
146                 std::string in_prefix;
147                 std::string out_prefix;
148                 unsigned scope_level;
149                 std::map<std::string, RefPtr<ProgramSyntax::VariableDeclaration> > iface_declarations;
150
151                 InterfaceGenerator();
152
153                 static std::string get_out_prefix(ProgramSyntax::StageType);
154                 virtual void apply(ProgramSyntax::Stage &);
155                 using Visitor::visit;
156                 virtual void visit(ProgramSyntax::Block &);
157                 std::string change_prefix(const std::string &, const std::string &) const;
158                 bool generate_interface(ProgramSyntax::VariableDeclaration &, const std::string &, const std::string &);
159                 ProgramSyntax::ExpressionStatement &insert_assignment(const std::string &, ProgramSyntax::Expression *);
160                 virtual void visit(ProgramSyntax::VariableReference &);
161                 virtual void visit(ProgramSyntax::VariableDeclaration &);
162                 virtual void visit(ProgramSyntax::Passthrough &);
163         };
164
165         struct DeclarationReorderer: Visitor
166         {
167                 enum DeclarationKind
168                 {
169                         NO_DECLARATION,
170                         LAYOUT,
171                         STRUCT,
172                         VARIABLE,
173                         FUNCTION
174                 };
175
176                 unsigned scope_level;
177                 DeclarationKind kind;
178                 std::set<ProgramSyntax::Node *> ordered_funcs;
179                 std::set<ProgramSyntax::Node *> needed_funcs;
180
181                 DeclarationReorderer();
182
183                 using Visitor::visit;
184                 virtual void visit(ProgramSyntax::Block &);
185                 virtual void visit(ProgramSyntax::FunctionCall &);
186                 virtual void visit(ProgramSyntax::InterfaceLayout &) { kind = LAYOUT; }
187                 virtual void visit(ProgramSyntax::StructDeclaration &) { kind = STRUCT; }
188                 virtual void visit(ProgramSyntax::VariableDeclaration &);
189                 virtual void visit(ProgramSyntax::InterfaceBlock &) { kind = VARIABLE; }
190                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
191         };
192
193         struct InlineableFunctionLocator: Visitor
194         {
195                 typedef std::set<ProgramSyntax::FunctionDeclaration *> ResultType;
196
197                 std::map<ProgramSyntax::FunctionDeclaration *, unsigned> refcounts;
198                 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
199                 ProgramSyntax::FunctionDeclaration *in_function;
200
201                 InlineableFunctionLocator();
202
203                 const ResultType &get_result() const { return inlineable; }
204                 using Visitor::visit;
205                 virtual void visit(ProgramSyntax::FunctionCall &);
206                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
207         };
208
209         struct FunctionInliner: Visitor
210         {
211                 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
212                 unsigned extract_result;
213                 RefPtr<ProgramSyntax::Expression> inline_result;
214
215                 FunctionInliner();
216                 FunctionInliner(const std::set<ProgramSyntax::FunctionDeclaration *> &);
217
218                 void visit_and_inline(RefPtr<ProgramSyntax::Expression> &);
219                 using Visitor::visit;
220                 virtual void visit(ProgramSyntax::Block &);
221                 virtual void visit(ProgramSyntax::UnaryExpression &);
222                 virtual void visit(ProgramSyntax::BinaryExpression &);
223                 virtual void visit(ProgramSyntax::MemberAccess &);
224                 virtual void visit(ProgramSyntax::FunctionCall &);
225                 virtual void visit(ProgramSyntax::VariableDeclaration &);
226                 virtual void visit(ProgramSyntax::Return &);
227         };
228
229         struct ExpressionEvaluator: ProgramSyntax::NodeVisitor
230         {
231                 typedef std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Expression *> ValueMap;
232
233                 const ValueMap *variable_values;
234                 float result;
235                 bool result_valid;
236
237                 ExpressionEvaluator();
238                 ExpressionEvaluator(const ValueMap &);
239
240                 using ProgramSyntax::NodeVisitor::visit;
241                 virtual void visit(ProgramSyntax::Literal &);
242                 virtual void visit(ProgramSyntax::ParenthesizedExpression &);
243                 virtual void visit(ProgramSyntax::VariableReference &);
244                 virtual void visit(ProgramSyntax::UnaryExpression &);
245                 virtual void visit(ProgramSyntax::BinaryExpression &);
246         };
247
248         struct ConstantConditionEliminator: BlockModifier
249         {
250                 unsigned scope_level;
251                 bool record_only;
252                 ExpressionEvaluator::ValueMap variable_values;
253
254                 ConstantConditionEliminator();
255
256                 using Visitor::visit;
257                 virtual void visit(ProgramSyntax::Block &);
258                 virtual void visit(ProgramSyntax::UnaryExpression &);
259                 virtual void visit(ProgramSyntax::Assignment &);
260                 virtual void visit(ProgramSyntax::VariableDeclaration &);
261                 virtual void visit(ProgramSyntax::Conditional &);
262                 virtual void visit(ProgramSyntax::Iteration &);
263         };
264
265         struct UnusedVariableLocator: Visitor
266         {
267                 struct VariableInfo
268                 {
269                         bool local;
270                         std::vector<ProgramSyntax::Node *> assignments;
271                         bool conditionally_assigned;
272                         bool referenced;
273
274                         VariableInfo();
275                 };
276
277                 typedef std::set<ProgramSyntax::Node *> ResultType;
278                 typedef std::map<ProgramSyntax::VariableDeclaration *, VariableInfo> BlockVariableMap;
279
280                 std::set<ProgramSyntax::Node *> unused_nodes;
281                 std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Node *> aggregates;
282                 ProgramSyntax::Node *aggregate;
283                 std::vector<BlockVariableMap> variables;
284                 ProgramSyntax::Assignment *assignment;
285                 bool assignment_target;
286                 bool assign_to_subscript;
287                 bool global_scope;
288
289                 UnusedVariableLocator();
290
291                 virtual void apply(ProgramSyntax::Stage &);
292                 const ResultType &get_result() const { return unused_nodes; }
293                 using Visitor::visit;
294                 virtual void visit(ProgramSyntax::VariableReference &);
295                 virtual void visit(ProgramSyntax::MemberAccess &);
296                 virtual void visit(ProgramSyntax::BinaryExpression &);
297                 virtual void visit(ProgramSyntax::Assignment &);
298                 void record_assignment(ProgramSyntax::VariableDeclaration &, ProgramSyntax::Node &, bool);
299                 void clear_assignments(VariableInfo &, bool);
300                 virtual void visit(ProgramSyntax::ExpressionStatement &);
301                 virtual void visit(ProgramSyntax::StructDeclaration &);
302                 virtual void visit(ProgramSyntax::VariableDeclaration &);
303                 virtual void visit(ProgramSyntax::InterfaceBlock &);
304                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
305                 void merge_down_variables();
306                 virtual void visit(ProgramSyntax::Conditional &);
307                 virtual void visit(ProgramSyntax::Iteration &);
308         };
309
310         struct UnusedFunctionLocator: Visitor
311         {
312                 typedef std::set<ProgramSyntax::Node *> ResultType;
313
314                 std::set<ProgramSyntax::Node *> unused_nodes;
315                 std::set<ProgramSyntax::FunctionDeclaration *> used_definitions;
316
317                 const ResultType &get_result() const { return unused_nodes; }
318                 using Visitor::visit;
319                 virtual void visit(ProgramSyntax::FunctionCall &);
320                 virtual void visit(ProgramSyntax::FunctionDeclaration &);
321         };
322
323         struct NodeRemover: Visitor
324         {
325                 std::set<ProgramSyntax::Node *> to_remove;
326
327                 NodeRemover() { }
328                 NodeRemover(const std::set<ProgramSyntax::Node *> &);
329
330                 using Visitor::visit;
331                 virtual void visit(ProgramSyntax::Block &);
332                 virtual void visit(ProgramSyntax::VariableDeclaration &);
333                 virtual void visit(ProgramSyntax::Iteration &);
334         };
335
336         struct PrecisionRemover: BlockModifier
337         {
338                 using Visitor::visit;
339                 virtual void visit(ProgramSyntax::Precision &);
340                 virtual void visit(ProgramSyntax::VariableDeclaration &);
341         };
342
343         struct DefaultPrecisionGenerator: BlockModifier
344         {
345                 bool toplevel;
346                 std::set<std::string> have_default;
347
348                 DefaultPrecisionGenerator();
349
350                 using Visitor::visit;
351                 virtual void visit(ProgramSyntax::Block &);
352                 virtual void visit(ProgramSyntax::Precision &);
353                 virtual void visit(ProgramSyntax::VariableDeclaration &);
354         };
355
356         struct LegacyConverter: BlockModifier
357         {
358                 GLApi target_api;
359                 Version target_version;
360                 std::string type;
361                 ProgramSyntax::VariableDeclaration *frag_out;
362
363                 LegacyConverter();
364                 LegacyConverter(const Version &);
365
366                 bool check_version(const Version &) const;
367                 bool check_extension(const Extension &) const;
368                 using Visitor::visit;
369                 bool supports_unified_interface_syntax() const;
370                 virtual void visit(ProgramSyntax::VariableReference &);
371                 virtual void visit(ProgramSyntax::Assignment &);
372                 bool supports_unified_sampling_functions() const;
373                 virtual void visit(ProgramSyntax::FunctionCall &);
374                 bool supports_interface_layouts() const;
375                 bool supports_centroid_sampling() const;
376                 bool supports_sample_sampling() const;
377                 virtual void visit(ProgramSyntax::VariableDeclaration &);
378                 bool supports_interface_blocks(const std::string &) const;
379                 virtual void visit(ProgramSyntax::InterfaceBlock &);
380         };
381
382         Resources *resources;
383         ProgramSyntax::Module *module;
384         std::vector<std::string> imported_names;
385
386 public:
387         ProgramCompiler();
388         ~ProgramCompiler();
389
390         void compile(const std::string &, const std::string & = "<string>");
391         void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
392         void compile(IO::Base &, const std::string &);
393         void add_shaders(Program &);
394
395 private:
396         static ProgramSyntax::Module *create_builtins_module();
397         static ProgramSyntax::Module &get_builtins_module();
398         static ProgramSyntax::Stage *get_builtins(ProgramSyntax::StageType);
399         void append_module(ProgramSyntax::Module &);
400         void append_stage(ProgramSyntax::Stage &);
401         void process();
402         void import(const std::string &);
403         void generate(ProgramSyntax::Stage &);
404         bool optimize(ProgramSyntax::Stage &);
405         void finalize(ProgramSyntax::Stage &);
406         static void inject_block(ProgramSyntax::Block &, const ProgramSyntax::Block &);
407         template<typename T>
408         static typename T::ResultType apply(ProgramSyntax::Stage &);
409         template<typename T, typename A>
410         static typename T::ResultType apply(ProgramSyntax::Stage &, const A &);
411 };
412
413 } // namespace GL
414 } // namespace Msp
415
416 #endif