1 #include <msp/core/raii.h>
2 #include <msp/gl/extensions/ext_gpu_shader4.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
7 #include "programcompiler.h"
17 const char builtins_src[] =
18 "#pragma MSP stage(vertex)\n"
19 "out gl_PerVertex {\n"
20 " vec4 gl_Position;\n"
21 " float gl_ClipDistance[];\n"
23 "#pragma MSP stage(geometry)\n"
25 " vec4 gl_Position;\n"
26 " float gl_ClipDistance[];\n"
28 "out gl_PerVertex {\n"
29 " vec4 gl_Position;\n"
30 " float gl_ClipDistance[];\n"
38 using namespace ProgramSyntax;
40 ProgramCompiler::ProgramCompiler():
45 ProgramCompiler::~ProgramCompiler()
50 void ProgramCompiler::compile(const string &source, const string &src_name)
54 module = new Module();
56 imported_names.insert(src_name);
57 append_module(parser.parse(source, src_name));
61 void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
65 module = new Module();
67 imported_names.insert(src_name);
68 append_module(parser.parse(io, src_name));
72 void ProgramCompiler::compile(IO::Base &io, const string &src_name)
74 compile(io, 0, src_name);
77 void ProgramCompiler::add_shaders(Program &program)
80 throw invalid_operation("ProgramCompiler::add_shaders");
82 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
86 program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
87 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
88 program.bind_attribute(j->second, j->first);
90 else if(i->type==GEOMETRY)
91 program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
92 else if(i->type==FRAGMENT)
94 program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
97 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
98 program.bind_fragment_data(j->second, j->first);
104 Module *ProgramCompiler::create_builtins_module()
106 ProgramParser parser;
107 Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
108 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
110 VariableResolver resolver;
111 i->content.visit(resolver);
112 for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
113 j->second->linked_declaration = j->second;
118 Module &ProgramCompiler::get_builtins_module()
120 static RefPtr<Module> builtins_module = create_builtins_module();
121 return *builtins_module;
124 Stage *ProgramCompiler::get_builtins(StageType type)
126 Module &module = get_builtins_module();
127 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
133 void ProgramCompiler::append_module(ProgramSyntax::Module &mod)
135 vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
136 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
137 import((*i)->module);
138 apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
140 append_stage(mod.shared);
141 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
145 void ProgramCompiler::append_stage(Stage &stage)
148 if(stage.type==SHARED)
149 target = &module->shared;
152 list<Stage>::iterator i;
153 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
154 if(i==module->stages.end() || i->type>stage.type)
156 list<Stage>::iterator j = module->stages.insert(i, stage.type);
157 if(i!=module->stages.end())
160 if(i!=module->stages.begin())
167 for(NodeList<Node>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
168 target->content.body.push_back(*i);
169 apply<DeclarationCombiner>(*target);
172 void ProgramCompiler::process()
174 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
176 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
179 i = module->stages.begin();
183 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
187 void ProgramCompiler::import(const string &name)
189 string fn = name+".glsl";
190 if(imported_names.count(fn))
192 imported_names.insert(fn);
194 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
196 throw runtime_error(format("module %s not found", name));
197 ProgramParser import_parser;
198 append_module(import_parser.parse(*io, fn));
201 void ProgramCompiler::generate(Stage &stage)
203 inject_block(stage.content, module->shared.content);
205 apply<DeclarationReorderer>(stage);
206 apply<FunctionResolver>(stage);
207 apply<VariableResolver>(stage);
208 apply<InterfaceGenerator>(stage);
209 apply<VariableResolver>(stage);
210 apply<DeclarationReorderer>(stage);
211 apply<FunctionResolver>(stage);
212 apply<LegacyConverter>(stage);
215 bool ProgramCompiler::optimize(Stage &stage)
217 apply<ConstantConditionEliminator>(stage);
219 set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
220 apply<FunctionInliner>(stage, inlineable);
222 set<Node *> unused = apply<UnusedVariableLocator>(stage);
223 set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
224 unused.insert(unused2.begin(), unused2.end());
225 apply<NodeRemover>(stage, unused);
227 return !unused.empty();
230 void ProgramCompiler::finalize(Stage &stage)
232 if(get_gl_api()==OPENGL_ES2)
233 apply<DefaultPrecisionGenerator>(stage);
235 apply<PrecisionRemover>(stage);
238 void ProgramCompiler::inject_block(Block &target, const Block &source)
240 list<RefPtr<Node> >::iterator insert_point = target.body.begin();
241 for(list<RefPtr<Node> >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
242 target.body.insert(insert_point, (*i)->clone());
246 typename T::ResultType ProgramCompiler::apply(Stage &stage)
249 visitor.apply(stage);
250 return visitor.get_result();
253 template<typename T, typename A>
254 typename T::ResultType ProgramCompiler::apply(Stage &stage, const A &arg)
257 visitor.apply(stage);
258 return visitor.get_result();
262 ProgramCompiler::Visitor::Visitor():
266 void ProgramCompiler::Visitor::apply(Stage &s)
268 SetForScope<Stage *> set(stage, &s);
269 stage->content.visit(*this);
273 ProgramCompiler::BlockModifier::BlockModifier():
277 void ProgramCompiler::BlockModifier::flatten_block(Block &block)
279 insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
283 void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
285 block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
286 insert_nodes.clear();
289 block.body.erase(i++);
295 void ProgramCompiler::BlockModifier::visit(Block &block)
297 for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
300 apply_and_increment(block, i);
305 ProgramCompiler::Formatter::Formatter():
307 parameter_list(false),
311 void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s)
313 GLApi api = get_gl_api();
314 const Version &ver = s.required_version;
318 formatted += format("#version %d%02d", ver.major, ver.minor);
319 if(api==OPENGL_ES2 && ver>=Version(3, 0))
327 void ProgramCompiler::Formatter::visit(Literal &literal)
329 formatted += literal.token;
332 void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
335 parexpr.expression->visit(*this);
339 void ProgramCompiler::Formatter::visit(VariableReference &var)
341 formatted += var.name;
344 void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
346 memacc.left->visit(*this);
347 formatted += format(".%s", memacc.member);
350 void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
353 formatted += unary.oper;
354 unary.expression->visit(*this);
356 formatted += unary.oper;
359 void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
361 binary.left->visit(*this);
362 formatted += binary.oper;
363 binary.right->visit(*this);
364 formatted += binary.after;
367 void ProgramCompiler::Formatter::visit(Assignment &assign)
369 assign.left->visit(*this);
370 formatted += format(" %s ", assign.oper);
371 assign.right->visit(*this);
374 void ProgramCompiler::Formatter::visit(FunctionCall &call)
376 formatted += format("%s(", call.name);
377 for(vector<RefPtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
379 if(i!=call.arguments.begin())
386 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
388 expr.expression->visit(*this);
392 void ProgramCompiler::Formatter::visit(Block &block)
397 unsigned brace_indent = indent;
398 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
400 formatted += format("%s{\n", string(brace_indent*2, ' '));
402 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
403 string spaces(indent*2, ' ');
404 for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
406 if(i!=block.body.begin())
414 formatted += format("\n%s}", string(brace_indent*2, ' '));
417 void ProgramCompiler::Formatter::visit(Import &import)
419 formatted += format("import %s;", import.module);
422 void ProgramCompiler::Formatter::visit(Precision &prec)
424 formatted += format("precision %s %s;", prec.precision, prec.type);
427 void ProgramCompiler::Formatter::visit(Layout &layout)
429 formatted += "layout(";
430 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
432 if(i!=layout.qualifiers.begin())
434 formatted += i->identifier;
435 if(!i->value.empty())
436 formatted += format("=%s", i->value);
441 void ProgramCompiler::Formatter::visit(InterfaceLayout &layout)
443 layout.layout.visit(*this);
444 formatted += format(" %s;", layout.interface);
447 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
449 formatted += format("struct %s\n", strct.name);
450 strct.members.visit(*this);
454 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
458 var.layout->visit(*this);
462 formatted += "const ";
463 if(!var.interpolation.empty())
464 formatted += format("%s ", var.interpolation);
465 if(!var.sampling.empty())
466 formatted += format("%s ", var.sampling);
467 if(!var.interface.empty() && var.interface!=block_interface)
469 string interface = var.interface;
470 if(stage->required_version<Version(1, 30))
472 if(stage->type==VERTEX && var.interface=="in")
473 interface = "attribute";
474 else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in"))
475 interface = "varying";
477 formatted += format("%s ", interface);
479 if(!var.precision.empty())
480 formatted += format("%s ", var.precision);
481 formatted += format("%s %s", var.type, var.name);
486 var.array_size->visit(*this);
489 if(var.init_expression)
492 var.init_expression->visit(*this);
498 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
500 SetForScope<string> set(block_interface, iface.interface);
501 formatted += format("%s %s\n", iface.interface, iface.name);
502 iface.members.visit(*this);
506 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
508 formatted += format("%s %s(", func.return_type, func.name);
509 for(vector<RefPtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
511 if(i!=func.parameters.begin())
513 SetFlag set(parameter_list);
517 if(func.definition==&func)
520 func.body.visit(*this);
526 void ProgramCompiler::Formatter::visit(Conditional &cond)
529 formatted.replace(formatted.rfind('\n'), string::npos, 1, ' ');
534 cond.condition->visit(*this);
537 cond.body.visit(*this);
538 if(!cond.else_body.body.empty())
540 formatted += format("\n%selse\n", string(indent*2, ' '));
541 SetForScope<unsigned> set(else_if, 2);
542 cond.else_body.visit(*this);
546 void ProgramCompiler::Formatter::visit(Iteration &iter)
548 if(!iter.init_statement && iter.condition && !iter.loop_expression)
550 formatted += "while(";
551 iter.condition->visit(*this);
557 if(iter.init_statement)
558 iter.init_statement->visit(*this);
564 iter.condition->visit(*this);
567 if(iter.loop_expression)
570 iter.loop_expression->visit(*this);
575 if(iter.body.body.empty())
580 iter.body.visit(*this);
584 void ProgramCompiler::Formatter::visit(Return &ret)
586 formatted += "return";
590 ret.expression->visit(*this);
595 void ProgramCompiler::Formatter::visit(Jump &jump)
597 formatted += jump.keyword;
602 ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
606 void ProgramCompiler::DeclarationCombiner::visit(Block &block)
611 SetForScope<bool> set(toplevel, false);
612 BlockModifier::visit(block);
615 void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
617 vector<FunctionDeclaration *> &decls = functions[func.name];
620 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
622 (*i)->definition = func.definition;
623 (*i)->body.body.clear();
626 decls.push_back(&func);
629 void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
631 VariableDeclaration *&ptr = variables[var.name];
634 ptr->type = var.type;
635 if(var.init_expression)
636 ptr->init_expression = var.init_expression;
644 ProgramCompiler::VariableResolver::VariableResolver():
646 record_target(false),
647 assignment_target(0),
648 self_referencing(false)
651 void ProgramCompiler::VariableResolver::apply(Stage &s)
653 SetForScope<Stage *> set(stage, &s);
654 Stage *builtins = get_builtins(stage->type);
656 blocks.push_back(&builtins->content);
657 stage->content.visit(*this);
662 void ProgramCompiler::VariableResolver::visit(Block &block)
664 blocks.push_back(&block);
665 block.variables.clear();
666 TraversingVisitor::visit(block);
670 void ProgramCompiler::VariableResolver::visit(VariableReference &var)
674 for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
677 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
678 if(j!=(*i)->variables.end())
680 var.declaration = j->second;
681 type = j->second->type_declaration;
688 if(assignment_target)
690 record_target = false;
691 assignment_target = 0;
694 assignment_target = var.declaration;
696 else if(var.declaration && var.declaration==assignment_target)
697 self_referencing = true;
700 void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
703 TraversingVisitor::visit(memacc);
704 memacc.declaration = 0;
707 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
708 if(i!=type->members.variables.end())
710 memacc.declaration = i->second;
711 type = i->second->type_declaration;
718 void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
723 SetForScope<bool> set(record_target, false);
724 binary.right->visit(*this);
727 binary.left->visit(*this);
731 TraversingVisitor::visit(binary);
736 void ProgramCompiler::VariableResolver::visit(Assignment &assign)
739 SetFlag set(record_target);
740 assignment_target = 0;
741 assign.left->visit(*this);
744 self_referencing = false;
745 assign.right->visit(*this);
747 assign.self_referencing = (self_referencing || assign.oper!="=");
748 assign.target_declaration = assignment_target;
751 void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
753 TraversingVisitor::visit(strct);
754 blocks.back()->types[strct.name] = &strct;
757 void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
759 for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
762 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
763 if(j!=(*i)->types.end())
764 var.type_declaration = j->second;
767 if(!block_interface.empty() && var.interface.empty())
768 var.interface = block_interface;
770 TraversingVisitor::visit(var);
771 blocks.back()->variables[var.name] = &var;
772 if(anonymous && blocks.size()>1)
773 blocks[blocks.size()-2]->variables[var.name] = &var;
776 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
778 SetFlag set(anonymous);
779 SetForScope<string> set2(block_interface, iface.interface);
780 TraversingVisitor::visit(iface);
784 void ProgramCompiler::FunctionResolver::visit(FunctionCall &call)
786 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
787 if(i!=functions.end())
788 call.declaration = i->second.back();
790 TraversingVisitor::visit(call);
793 void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
795 vector<FunctionDeclaration *> &decls = functions[func.name];
798 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
799 (*i)->definition = func.definition;
801 decls.push_back(&func);
803 else if(!decls.empty() && decls.back()->definition)
804 func.definition = decls.back()->definition;
806 decls.push_back(&func);
808 TraversingVisitor::visit(func);
812 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
816 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
820 else if(type==GEOMETRY)
826 void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
828 SetForScope<Stage *> set(stage, &s);
830 in_prefix = get_out_prefix(stage->previous->type);
831 out_prefix = get_out_prefix(stage->type);
832 stage->content.visit(*this);
835 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
837 SetForScope<unsigned> set(scope_level, scope_level+1);
838 for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
844 for(map<string, RefPtr<VariableDeclaration> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
846 list<RefPtr<Node> >::iterator k = block.body.insert(i, j->second);
849 iface_declarations.clear();
852 apply_and_increment(block, i);
856 string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
858 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
859 return prefix+name.substr(offset);
862 bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
864 const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
865 if(stage_vars.count(name) || iface_declarations.count(name))
868 VariableDeclaration* iface_var = new VariableDeclaration;
869 iface_var->sampling = var.sampling;
870 iface_var->interface = iface;
871 iface_var->type = var.type;
872 iface_var->type_declaration = var.type_declaration;
873 iface_var->name = name;
874 if(stage->type==GEOMETRY)
875 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
877 iface_var->array = var.array;
879 iface_var->array_size = var.array_size;
881 iface_var->linked_declaration = &var;
882 iface_declarations[name] = iface_var;
887 void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
889 Assignment *assign = new Assignment;
890 VariableReference *ref = new VariableReference;
894 assign->right = right;
896 ExpressionStatement *stmt = new ExpressionStatement;
897 stmt->expression = assign;
899 insert_nodes.push_back(stmt);
902 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
904 if(var.declaration || !stage->previous)
906 if(iface_declarations.count(var.name))
909 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
910 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
911 if(i==prev_out.end())
912 i = prev_out.find(in_prefix+var.name);
913 if(i!=prev_out.end())
915 generate_interface(*i->second, "in", i->second->name);
916 var.name = i->second->name;
920 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
922 if(var.interface=="out")
925 stage->out_variables[var.name] = &var;
926 else if(generate_interface(var, "out", change_prefix(var.name, string())))
929 if(var.init_expression)
931 insert_assignment(var.name, var.init_expression->clone());
936 else if(var.interface=="in")
938 stage->in_variables[var.name] = &var;
939 if(var.linked_declaration)
940 var.linked_declaration->linked_declaration = &var;
941 else if(stage->previous)
943 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
944 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
945 if(i!=prev_out.end())
947 var.linked_declaration = i->second;
948 i->second->linked_declaration = &var;
953 TraversingVisitor::visit(var);
956 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
958 vector<VariableDeclaration *> pass_vars;
960 for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
961 pass_vars.push_back(i->second);
962 for(map<string, RefPtr<VariableDeclaration> >::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i)
963 if(i->second->interface=="in")
964 pass_vars.push_back(i->second.get());
968 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
969 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
972 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
973 linked = ((*j)->linked_declaration==i->second);
975 if(!linked && generate_interface(*i->second, "in", i->second->name))
976 pass_vars.push_back(i->second);
980 if(stage->type==GEOMETRY)
982 VariableReference *ref = new VariableReference;
985 BinaryExpression *subscript = new BinaryExpression;
986 subscript->left = ref;
987 subscript->oper = "[";
988 subscript->right = pass.subscript;
989 subscript->after = "]";
991 MemberAccess *memacc = new MemberAccess;
992 memacc->left = subscript;
993 memacc->member = "gl_Position";
995 insert_assignment("gl_Position", memacc);
998 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1000 string out_name = change_prefix((*i)->name, out_prefix);
1001 generate_interface(**i, "out", out_name);
1003 VariableReference *ref = new VariableReference;
1004 ref->name = (*i)->name;
1007 BinaryExpression *subscript = new BinaryExpression;
1008 subscript->left = ref;
1009 subscript->oper = "[";
1010 subscript->right = pass.subscript;
1011 subscript->after = "]";
1012 insert_assignment(out_name, subscript);
1015 insert_assignment(out_name, ref);
1022 ProgramCompiler::DeclarationReorderer::DeclarationReorderer():
1024 kind(NO_DECLARATION)
1027 void ProgramCompiler::DeclarationReorderer::visit(FunctionCall &call)
1029 FunctionDeclaration *def = call.declaration;
1031 def = def->definition;
1032 if(def && !ordered_funcs.count(def))
1033 needed_funcs.insert(def);
1036 void ProgramCompiler::DeclarationReorderer::visit(Block &block)
1038 SetForScope<unsigned> set(scope_level, scope_level+1);
1040 return Visitor::visit(block);
1042 list<RefPtr<Node> >::iterator struct_insert_point = block.body.end();
1043 list<RefPtr<Node> >::iterator variable_insert_point = block.body.end();
1044 list<RefPtr<Node> >::iterator function_insert_point = block.body.end();
1045 unsigned unordered_func_count = 0;
1046 bool ordered_any_funcs = false;
1048 for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
1050 kind = NO_DECLARATION;
1054 if(kind==STRUCT && struct_insert_point!=block.body.end())
1056 block.body.insert(struct_insert_point, *i);
1059 else if(kind>STRUCT && struct_insert_point==block.body.end())
1060 struct_insert_point = i;
1062 if(kind==VARIABLE && variable_insert_point!=block.body.end())
1064 block.body.insert(variable_insert_point, *i);
1067 else if(kind>VARIABLE && variable_insert_point==block.body.end())
1068 variable_insert_point = i;
1072 if(function_insert_point==block.body.end())
1073 function_insert_point = i;
1075 if(needed_funcs.empty())
1077 ordered_funcs.insert(i->get());
1078 if(i!=function_insert_point)
1080 block.body.insert(function_insert_point, *i);
1084 ++function_insert_point;
1085 ordered_any_funcs = true;
1088 ++unordered_func_count;
1093 if(function_insert_point==i)
1094 ++function_insert_point;
1095 block.body.erase(i++);
1100 if(i==block.body.end() && unordered_func_count)
1102 if(!ordered_any_funcs)
1103 // A subset of the remaining functions forms a recursive loop
1104 /* TODO pick a function and move it up, adding any necessary
1108 i = function_insert_point;
1109 unordered_func_count = 0;
1114 void ProgramCompiler::DeclarationReorderer::visit(ProgramSyntax::VariableDeclaration &var)
1116 Visitor::visit(var);
1120 void ProgramCompiler::DeclarationReorderer::visit(FunctionDeclaration &func)
1122 needed_funcs.clear();
1123 func.body.visit(*this);
1124 needed_funcs.erase(&func);
1129 ProgramCompiler::InlineableFunctionLocator::InlineableFunctionLocator():
1133 void ProgramCompiler::InlineableFunctionLocator::visit(FunctionCall &call)
1135 FunctionDeclaration *def = call.declaration;
1136 if(def && def->definition!=def)
1137 def = def->definition;
1141 unsigned &count = refcounts[def];
1143 if(count>1 || def==in_function)
1144 inlineable.erase(def);
1147 TraversingVisitor::visit(call);
1150 void ProgramCompiler::InlineableFunctionLocator::visit(FunctionDeclaration &func)
1152 unsigned &count = refcounts[func.definition];
1153 if(!count && func.parameters.empty())
1154 inlineable.insert(func.definition);
1156 SetForScope<FunctionDeclaration *> set(in_function, &func);
1157 TraversingVisitor::visit(func);
1161 ProgramCompiler::FunctionInliner::FunctionInliner():
1165 ProgramCompiler::FunctionInliner::FunctionInliner(const set<FunctionDeclaration *> &in):
1170 void ProgramCompiler::FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
1175 ptr = inline_result;
1178 void ProgramCompiler::FunctionInliner::visit(Block &block)
1183 for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1191 void ProgramCompiler::FunctionInliner::visit(UnaryExpression &unary)
1193 visit_and_inline(unary.expression);
1197 void ProgramCompiler::FunctionInliner::visit(BinaryExpression &binary)
1199 visit_and_inline(binary.left);
1200 visit_and_inline(binary.right);
1204 void ProgramCompiler::FunctionInliner::visit(MemberAccess &memacc)
1206 visit_and_inline(memacc.left);
1210 void ProgramCompiler::FunctionInliner::visit(FunctionCall &call)
1212 for(vector<RefPtr<Expression> >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
1213 visit_and_inline(*i);
1215 FunctionDeclaration *def = call.declaration;
1216 if(def && def->definition!=def)
1217 def = def->definition;
1219 if(def && inlineable.count(def))
1228 void ProgramCompiler::FunctionInliner::visit(VariableDeclaration &var)
1230 if(var.init_expression)
1231 visit_and_inline(var.init_expression);
1235 void ProgramCompiler::FunctionInliner::visit(Return &ret)
1237 TraversingVisitor::visit(ret);
1240 inline_result = ret.expression->clone();
1244 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator():
1250 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator(const ValueMap &v):
1251 variable_values(&v),
1256 void ProgramCompiler::ExpressionEvaluator::visit(Literal &literal)
1258 if(literal.token=="true")
1260 else if(literal.token=="false")
1263 result = lexical_cast<float>(literal.token);
1264 result_valid = true;
1267 void ProgramCompiler::ExpressionEvaluator::visit(ParenthesizedExpression &parexp)
1269 parexp.expression->visit(*this);
1272 void ProgramCompiler::ExpressionEvaluator::visit(VariableReference &var)
1274 if(!var.declaration)
1279 ValueMap::const_iterator i = variable_values->find(var.declaration);
1280 if(i!=variable_values->end())
1281 i->second->visit(*this);
1283 else if(var.declaration->init_expression)
1284 var.declaration->init_expression->visit(*this);
1287 void ProgramCompiler::ExpressionEvaluator::visit(UnaryExpression &unary)
1289 result_valid = false;
1290 unary.expression->visit(*this);
1297 result_valid = false;
1300 void ProgramCompiler::ExpressionEvaluator::visit(BinaryExpression &binary)
1302 result_valid = false;
1303 binary.left->visit(*this);
1307 float left_result = result;
1308 result_valid = false;
1309 binary.right->visit(*this);
1313 if(binary.oper=="<")
1314 result = (left_result<result);
1315 else if(binary.oper=="<=")
1316 result = (left_result<=result);
1317 else if(binary.oper==">")
1318 result = (left_result>result);
1319 else if(binary.oper==">=")
1320 result = (left_result>=result);
1321 else if(binary.oper=="==")
1322 result = (left_result==result);
1323 else if(binary.oper=="!=")
1324 result = (left_result!=result);
1325 else if(binary.oper=="&&")
1326 result = (left_result && result);
1327 else if(binary.oper=="||")
1328 result = (left_result || result);
1330 result_valid = false;
1334 ProgramCompiler::ConstantConditionEliminator::ConstantConditionEliminator():
1339 void ProgramCompiler::ConstantConditionEliminator::visit(Block &block)
1341 SetForScope<unsigned> set(scope_level, scope_level+1);
1342 BlockModifier::visit(block);
1344 for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
1345 variable_values.erase(i->second);
1348 void ProgramCompiler::ConstantConditionEliminator::visit(UnaryExpression &unary)
1350 if(VariableReference *var = dynamic_cast<VariableReference *>(unary.expression.get()))
1351 if(unary.oper=="++" || unary.oper=="--")
1352 variable_values.erase(var->declaration);
1355 void ProgramCompiler::ConstantConditionEliminator::visit(Assignment &assign)
1357 variable_values.erase(assign.target_declaration);
1360 void ProgramCompiler::ConstantConditionEliminator::visit(VariableDeclaration &var)
1362 if(var.constant || scope_level>1)
1363 variable_values[&var] = var.init_expression.get();
1366 void ProgramCompiler::ConstantConditionEliminator::visit(Conditional &cond)
1370 ExpressionEvaluator eval(variable_values);
1371 cond.condition->visit(eval);
1372 if(eval.result_valid)
1374 flatten_block(eval.result ? cond.body : cond.else_body);
1379 TraversingVisitor::visit(cond);
1382 void ProgramCompiler::ConstantConditionEliminator::visit(Iteration &iter)
1388 /* If the loop condition is always false on the first iteration, the
1389 entire loop can be removed */
1390 if(iter.init_statement)
1391 iter.init_statement->visit(*this);
1392 ExpressionEvaluator eval(variable_values);
1393 iter.condition->visit(eval);
1394 if(eval.result_valid && !eval.result)
1401 /* Record all assignments that occur inside the loop body so those
1402 variables won't be considered as constant */
1403 SetFlag set_record(record_only);
1404 TraversingVisitor::visit(iter);
1407 TraversingVisitor::visit(iter);
1409 if(VariableDeclaration *init_decl = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
1410 variable_values.erase(init_decl);
1414 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
1417 assignment_target(false),
1421 void ProgramCompiler::UnusedVariableLocator::apply(Stage &s)
1423 variables.push_back(BlockVariableMap());
1425 BlockVariableMap &global_variables = variables.back();
1426 for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
1428 if(i->first->interface=="out" && (s.type==FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
1430 if(!i->second.referenced)
1432 unused_nodes.insert(i->first);
1433 clear_assignments(i->second, true);
1436 variables.pop_back();
1439 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
1441 map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
1442 if(i!=aggregates.end())
1443 unused_nodes.erase(i->second);
1445 if(var.declaration && !assignment_target)
1447 VariableInfo &var_info = variables.back()[var.declaration];
1448 var_info.assignments.clear();
1449 var_info.referenced = true;
1453 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
1455 TraversingVisitor::visit(memacc);
1456 unused_nodes.erase(memacc.declaration);
1459 void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
1461 if(binary.oper=="[")
1463 binary.left->visit(*this);
1464 SetForScope<bool> set(assignment_target, false);
1465 binary.right->visit(*this);
1468 TraversingVisitor::visit(binary);
1471 void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
1474 SetForScope<bool> set(assignment_target, !assign.self_referencing);
1475 assign.left->visit(*this);
1477 assign.right->visit(*this);
1478 assignment = &assign;
1481 void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool self_ref)
1483 VariableInfo &var_info = variables.back()[&var];
1485 clear_assignments(var_info, true);
1486 var_info.assignments.push_back(&node);
1487 var_info.conditionally_assigned = false;
1490 void ProgramCompiler::UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_unused)
1494 for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
1495 unused_nodes.insert(*i);
1497 var_info.assignments.clear();
1500 void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
1503 TraversingVisitor::visit(expr);
1504 if(assignment && assignment->target_declaration)
1505 record_assignment(*assignment->target_declaration, expr, assignment->self_referencing);
1508 void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
1510 SetForScope<Node *> set(aggregate, &strct);
1511 unused_nodes.insert(&strct);
1512 TraversingVisitor::visit(strct);
1515 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
1518 aggregates[&var] = aggregate;
1521 variables.back()[&var].local = true;
1522 if(var.init_expression)
1523 record_assignment(var, *var.init_expression, false);
1525 unused_nodes.erase(var.type_declaration);
1526 TraversingVisitor::visit(var);
1529 void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface)
1531 SetForScope<Node *> set(aggregate, &iface);
1532 unused_nodes.insert(&iface);
1533 TraversingVisitor::visit(iface);
1536 void ProgramCompiler::UnusedVariableLocator::visit(FunctionDeclaration &func)
1538 variables.push_back(BlockVariableMap());
1541 SetForScope<bool> set(global_scope, false);
1542 for(vector<RefPtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
1544 func.body.visit(*this);
1547 BlockVariableMap &block_variables = variables.back();
1548 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
1549 i->second.conditionally_assigned = true;
1550 for(vector<RefPtr<VariableDeclaration> >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
1551 block_variables[i->get()].referenced = true;
1552 merge_down_variables();
1555 void ProgramCompiler::UnusedVariableLocator::merge_down_variables()
1557 BlockVariableMap &parent_variables = variables[variables.size()-2];
1558 BlockVariableMap &block_variables = variables.back();
1559 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
1563 if(!i->second.referenced)
1564 unused_nodes.insert(i->first);
1565 clear_assignments(i->second, true);
1569 BlockVariableMap::iterator j = parent_variables.find(i->first);
1570 if(j==parent_variables.end())
1571 parent_variables.insert(*i);
1574 if(i->second.referenced || !i->second.conditionally_assigned)
1575 clear_assignments(j->second, !i->second.referenced);
1576 j->second.conditionally_assigned = i->second.conditionally_assigned;
1577 j->second.referenced |= i->second.referenced;
1578 j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end());
1581 variables.pop_back();
1584 void ProgramCompiler::UnusedVariableLocator::visit(Conditional &cond)
1586 cond.condition->visit(*this);
1587 variables.push_back(BlockVariableMap());
1588 cond.body.visit(*this);
1590 BlockVariableMap if_variables;
1591 swap(variables.back(), if_variables);
1592 cond.else_body.visit(*this);
1594 BlockVariableMap &else_variables = variables.back();
1595 for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
1597 BlockVariableMap::iterator j = if_variables.find(i->first);
1598 if(j!=if_variables.end())
1600 i->second.assignments.insert(i->second.assignments.end(), j->second.assignments.begin(), j->second.assignments.end());
1601 i->second.conditionally_assigned |= j->second.conditionally_assigned;
1602 if_variables.erase(j);
1605 i->second.conditionally_assigned = true;
1608 for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
1610 i->second.conditionally_assigned = true;
1611 else_variables.insert(*i);
1614 merge_down_variables();
1617 void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter)
1619 variables.push_back(BlockVariableMap());
1620 TraversingVisitor::visit(iter);
1622 BlockVariableMap &block_variables = variables.back();
1623 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
1624 if(!i->second.local && i->second.referenced)
1625 i->second.assignments.clear();
1627 merge_down_variables();
1631 ProgramCompiler::UnusedVariableLocator::VariableInfo::VariableInfo():
1633 conditionally_assigned(false),
1638 void ProgramCompiler::UnusedFunctionLocator::visit(FunctionCall &call)
1640 TraversingVisitor::visit(call);
1642 unused_nodes.erase(call.declaration);
1643 if(call.declaration && call.declaration->definition!=call.declaration)
1644 used_definitions.insert(call.declaration->definition);
1647 void ProgramCompiler::UnusedFunctionLocator::visit(FunctionDeclaration &func)
1649 TraversingVisitor::visit(func);
1651 if((func.name!="main" || func.body.body.empty()) && !used_definitions.count(&func))
1652 unused_nodes.insert(&func);
1656 ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
1660 void ProgramCompiler::NodeRemover::visit(Block &block)
1662 for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
1665 if(to_remove.count(i->get()))
1666 block.body.erase(i++);
1672 void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
1674 if(to_remove.count(&var))
1676 stage->in_variables.erase(var.name);
1677 stage->out_variables.erase(var.name);
1678 stage->locations.erase(var.name);
1679 if(var.linked_declaration)
1680 var.linked_declaration->linked_declaration = 0;
1682 else if(var.init_expression && to_remove.count(var.init_expression.get()))
1683 var.init_expression = 0;
1686 void ProgramCompiler::NodeRemover::visit(Iteration &iter)
1688 if(to_remove.count(iter.init_statement.get()))
1689 iter.init_statement = 0;
1690 TraversingVisitor::visit(iter);
1694 void ProgramCompiler::PrecisionRemover::visit(Precision &)
1699 void ProgramCompiler::PrecisionRemover::visit(VariableDeclaration &var)
1701 var.precision.clear();
1705 ProgramCompiler::DefaultPrecisionGenerator::DefaultPrecisionGenerator():
1709 void ProgramCompiler::DefaultPrecisionGenerator::visit(Block &block)
1713 SetForScope<bool> set(toplevel, false);
1714 BlockModifier::visit(block);
1717 Visitor::visit(block);
1720 void ProgramCompiler::DefaultPrecisionGenerator::visit(Precision &prec)
1722 have_default.insert(prec.type);
1725 void ProgramCompiler::DefaultPrecisionGenerator::visit(VariableDeclaration &var)
1727 if(var.type_declaration)
1730 string type = var.type;
1731 if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
1733 else if(!type.compare(0, 3, "ivec") || type=="uint")
1736 if(!have_default.count(type))
1738 Precision *prec = new Precision;
1739 if(!type.compare(0, 7, "sampler"))
1740 prec->precision = "lowp";
1741 else if(stage->type==FRAGMENT)
1742 prec->precision = "mediump";
1744 prec->precision = "highp";
1746 insert_nodes.push_back(prec);
1748 have_default.insert(type);
1753 ProgramCompiler::LegacyConverter::LegacyConverter():
1754 target_api(get_gl_api()),
1755 target_version(get_glsl_version()),
1759 ProgramCompiler::LegacyConverter::LegacyConverter(const Version &v):
1760 target_api(get_gl_api()),
1765 bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_version) const
1767 if(target_version<feature_version)
1769 else if(stage->required_version<feature_version)
1770 stage->required_version = feature_version;
1775 bool ProgramCompiler::LegacyConverter::supports_unified_interface_syntax() const
1777 if(target_api==OPENGL_ES2)
1778 return check_version(Version(3, 0));
1780 return check_version(Version(1, 30));
1783 void ProgramCompiler::LegacyConverter::visit(VariableReference &var)
1785 if(var.declaration==frag_out && !supports_unified_interface_syntax())
1787 var.name = "gl_FragColor";
1788 var.declaration = 0;
1791 else if(var.declaration)
1792 type = var.declaration->type;
1797 void ProgramCompiler::LegacyConverter::visit(Assignment &assign)
1799 TraversingVisitor::visit(assign);
1800 if(assign.target_declaration==frag_out && !supports_unified_interface_syntax())
1801 assign.target_declaration = 0;
1804 bool ProgramCompiler::LegacyConverter::supports_unified_sampling_functions() const
1806 if(target_api==OPENGL_ES2)
1807 return check_version(Version(3, 0));
1809 return check_version(Version(1, 30));
1812 void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
1814 if(call.name=="texture" && !call.declaration && !supports_unified_sampling_functions())
1816 vector<RefPtr<Expression> >::iterator i = call.arguments.begin();
1817 if(i!=call.arguments.end())
1820 if(type=="sampler1D")
1821 call.name = "texture1D";
1822 else if(type=="sampler2D")
1823 call.name = "texture2D";
1824 else if(type=="sampler3D")
1825 call.name = "texture3D";
1826 else if(type=="sampler1DShadow")
1827 call.name = "shadow1D";
1828 else if(type=="sampler2DShadow")
1829 call.name = "shadow2D";
1831 for(; i!=call.arguments.end(); ++i)
1836 TraversingVisitor::visit(call);
1839 bool ProgramCompiler::LegacyConverter::supports_interface_layouts() const
1841 if(target_api==OPENGL_ES2)
1842 return check_version(Version(3, 0));
1844 return check_version(Version(3, 30));
1847 bool ProgramCompiler::LegacyConverter::supports_centroid_sampling() const
1849 if(target_api==OPENGL_ES2)
1850 return check_version(Version(3, 0));
1852 return check_version(Version(1, 20));
1855 bool ProgramCompiler::LegacyConverter::supports_sample_sampling() const
1857 if(target_api==OPENGL_ES2)
1858 return check_version(Version(3, 20));
1860 return check_version(Version(4, 0));
1863 void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
1865 if(var.layout && !supports_interface_layouts())
1867 vector<Layout::Qualifier>::iterator i;
1868 for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
1869 if(i!=var.layout->qualifiers.end())
1871 unsigned location = lexical_cast<unsigned>(i->value);
1872 if(stage->type==VERTEX && var.interface=="in")
1874 stage->locations[var.name] = location;
1875 var.layout->qualifiers.erase(i);
1877 else if(stage->type==FRAGMENT && var.interface=="out")
1880 static Require _req(EXT_gpu_shader4);
1881 stage->locations[var.name] = location;
1882 var.layout->qualifiers.erase(i);
1885 if(var.layout->qualifiers.empty())
1890 if(var.sampling=="centroid")
1892 if(!supports_centroid_sampling())
1893 var.sampling = string();
1895 else if(var.sampling=="sample")
1897 if(!supports_sample_sampling())
1898 var.sampling = string();
1901 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
1903 if(stage->type==FRAGMENT && var.interface=="out")
1910 TraversingVisitor::visit(var);
1913 bool ProgramCompiler::LegacyConverter::supports_interface_blocks(const string &iface) const
1915 if(target_api==OPENGL_ES2)
1917 if(iface=="uniform")
1918 return check_version(Version(3, 0));
1920 return check_version(Version(3, 20));
1923 return check_version(Version(1, 50));
1926 void ProgramCompiler::LegacyConverter::visit(InterfaceBlock &iface)
1928 if(!supports_interface_blocks(iface.interface))
1929 flatten_block(iface.members);