1 #ifndef MSP_GL_PROGRAMCOMPILER_H_
2 #define MSP_GL_PROGRAMCOMPILER_H_
5 #include "programparser.h"
6 #include "programsyntax.h"
17 struct Visitor: ProgramSyntax::TraversingVisitor
19 typedef void ResultType;
21 ProgramSyntax::Stage *stage;
25 virtual void apply(ProgramSyntax::Stage &);
26 void get_result() const { }
29 struct BlockModifier: Visitor
32 std::vector<RefPtr<ProgramSyntax::Statement> > insert_nodes;
36 void flatten_block(ProgramSyntax::Block &);
37 void apply_and_increment(ProgramSyntax::Block &, ProgramSyntax::NodeList<ProgramSyntax::Statement>::iterator &);
39 virtual void visit(ProgramSyntax::Block &);
42 struct Formatter: Visitor
44 typedef std::string ResultType;
46 std::string formatted;
47 unsigned source_index;
51 std::string block_interface;
55 virtual void apply(ProgramSyntax::Stage &);
56 const std::string &get_result() const { return formatted; }
58 void append(const std::string &);
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 &);
86 struct NodeGatherer: Visitor
88 typedef std::vector<T *> ResultType;
90 std::vector<T *> nodes;
92 const ResultType &get_result() const { return nodes; }
94 virtual void visit(T &n) { nodes.push_back(&n); }
97 struct DeclarationCombiner: BlockModifier
100 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
101 std::map<std::string, ProgramSyntax::VariableDeclaration *> variables;
103 DeclarationCombiner();
105 using Visitor::visit;
106 virtual void visit(ProgramSyntax::Block &);
107 virtual void visit(ProgramSyntax::FunctionDeclaration &);
108 virtual void visit(ProgramSyntax::VariableDeclaration &);
111 struct VariableResolver: Visitor
113 std::vector<ProgramSyntax::Block *> blocks;
114 ProgramSyntax::StructDeclaration *type;
116 std::string block_interface;
118 ProgramSyntax::VariableDeclaration *assignment_target;
119 bool self_referencing;
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 &);
135 struct FunctionResolver: Visitor
137 std::map<std::string, std::vector<ProgramSyntax::FunctionDeclaration *> > functions;
139 using Visitor::visit;
140 virtual void visit(ProgramSyntax::FunctionCall &);
141 virtual void visit(ProgramSyntax::FunctionDeclaration &);
144 struct InterfaceGenerator: BlockModifier
146 std::string in_prefix;
147 std::string out_prefix;
148 unsigned scope_level;
149 std::map<std::string, RefPtr<ProgramSyntax::VariableDeclaration> > iface_declarations;
151 InterfaceGenerator();
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 &);
165 struct DeclarationReorderer: Visitor
176 unsigned scope_level;
177 DeclarationKind kind;
178 std::set<ProgramSyntax::Node *> ordered_funcs;
179 std::set<ProgramSyntax::Node *> needed_funcs;
181 DeclarationReorderer();
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 &);
193 struct InlineableFunctionLocator: Visitor
195 typedef std::set<ProgramSyntax::FunctionDeclaration *> ResultType;
197 std::map<ProgramSyntax::FunctionDeclaration *, unsigned> refcounts;
198 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
199 ProgramSyntax::FunctionDeclaration *in_function;
201 InlineableFunctionLocator();
203 const ResultType &get_result() const { return inlineable; }
204 using Visitor::visit;
205 virtual void visit(ProgramSyntax::FunctionCall &);
206 virtual void visit(ProgramSyntax::FunctionDeclaration &);
209 struct FunctionInliner: Visitor
211 std::set<ProgramSyntax::FunctionDeclaration *> inlineable;
212 unsigned extract_result;
213 RefPtr<ProgramSyntax::Expression> inline_result;
216 FunctionInliner(const std::set<ProgramSyntax::FunctionDeclaration *> &);
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 &);
229 struct ExpressionEvaluator: ProgramSyntax::NodeVisitor
231 typedef std::map<ProgramSyntax::VariableDeclaration *, ProgramSyntax::Expression *> ValueMap;
233 const ValueMap *variable_values;
237 ExpressionEvaluator();
238 ExpressionEvaluator(const ValueMap &);
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 &);
248 struct ConstantConditionEliminator: BlockModifier
250 unsigned scope_level;
252 ExpressionEvaluator::ValueMap variable_values;
254 ConstantConditionEliminator();
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 &);
265 struct UnusedVariableLocator: Visitor
270 std::vector<ProgramSyntax::Node *> assignments;
271 bool conditionally_assigned;
277 typedef std::set<ProgramSyntax::Node *> ResultType;
278 typedef std::map<ProgramSyntax::VariableDeclaration *, VariableInfo> BlockVariableMap;
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;
289 UnusedVariableLocator();
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 &);
310 struct UnusedFunctionLocator: Visitor
312 typedef std::set<ProgramSyntax::Node *> ResultType;
314 std::set<ProgramSyntax::Node *> unused_nodes;
315 std::set<ProgramSyntax::FunctionDeclaration *> used_definitions;
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 &);
323 struct NodeRemover: Visitor
325 std::set<ProgramSyntax::Node *> to_remove;
328 NodeRemover(const std::set<ProgramSyntax::Node *> &);
330 using Visitor::visit;
331 virtual void visit(ProgramSyntax::Block &);
332 virtual void visit(ProgramSyntax::VariableDeclaration &);
333 virtual void visit(ProgramSyntax::Iteration &);
336 struct PrecisionRemover: BlockModifier
338 using Visitor::visit;
339 virtual void visit(ProgramSyntax::Precision &);
340 virtual void visit(ProgramSyntax::VariableDeclaration &);
343 struct DefaultPrecisionGenerator: BlockModifier
346 std::set<std::string> have_default;
348 DefaultPrecisionGenerator();
350 using Visitor::visit;
351 virtual void visit(ProgramSyntax::Block &);
352 virtual void visit(ProgramSyntax::Precision &);
353 virtual void visit(ProgramSyntax::VariableDeclaration &);
356 struct LegacyConverter: BlockModifier
359 Version target_version;
361 ProgramSyntax::VariableDeclaration *frag_out;
364 LegacyConverter(const Version &);
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 &);
382 Resources *resources;
383 ProgramSyntax::Module *module;
384 std::vector<std::string> imported_names;
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 &);
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 &);
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 &);
408 static typename T::ResultType apply(ProgramSyntax::Stage &);
409 template<typename T, typename A>
410 static typename T::ResultType apply(ProgramSyntax::Stage &, const A &);