1 #include <msp/core/algorithm.h>
2 #include <msp/core/raii.h>
3 #include <msp/gl/extensions/arb_explicit_attrib_location.h>
4 #include <msp/gl/extensions/arb_gpu_shader5.h>
5 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
6 #include <msp/gl/extensions/ext_gpu_shader4.h>
7 #include <msp/gl/extensions/ext_texture_array.h>
8 #include <msp/strings/format.h>
9 #include <msp/strings/regex.h>
10 #include <msp/strings/utils.h>
13 #include "programcompiler.h"
14 #include "resources.h"
23 const char builtins_src[] =
24 "#pragma MSP stage(vertex)\n"
25 "out gl_PerVertex {\n"
26 " vec4 gl_Position;\n"
27 " float gl_ClipDistance[];\n"
29 "#pragma MSP stage(geometry)\n"
31 " vec4 gl_Position;\n"
32 " float gl_ClipDistance[];\n"
34 "out gl_PerVertex {\n"
35 " vec4 gl_Position;\n"
36 " float gl_ClipDistance[];\n"
44 using namespace ProgramSyntax;
46 ProgramCompiler::ProgramCompiler():
51 ProgramCompiler::~ProgramCompiler()
56 void ProgramCompiler::compile(const string &source, const string &src_name)
60 module = new Module();
62 imported_names.push_back(src_name);
63 append_module(parser.parse(source, src_name, 1));
67 void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
71 module = new Module();
73 imported_names.push_back(src_name);
74 append_module(parser.parse(io, src_name, 1));
78 void ProgramCompiler::compile(IO::Base &io, const string &src_name)
80 compile(io, 0, src_name);
83 void ProgramCompiler::add_shaders(Program &program)
86 throw invalid_operation("ProgramCompiler::add_shaders");
90 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
94 program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
95 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
96 program.bind_attribute(j->second, j->first);
98 else if(i->type==GEOMETRY)
99 program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
100 else if(i->type==FRAGMENT)
102 program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
105 for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
106 program.bind_fragment_data(j->second, j->first);
111 catch(const compile_error &e)
113 static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
114 vector<string> lines = split(e.what(), '\n');
116 for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
118 RegMatch m = r_message.match(*i);
125 index = lexical_cast<unsigned>(m[2].str);
126 line = lexical_cast<unsigned>(m[3].str);
130 index = lexical_cast<unsigned>(m[4].str);
131 line = lexical_cast<unsigned>(m[5].str);
133 const char *src = "<unknown>";
136 else if(index-1<imported_names.size())
137 src = imported_names[index-1].c_str();
138 translated += format("%s:%d: %s", src, line, m[6].str);
145 throw compile_error(translated);
149 Module *ProgramCompiler::create_builtins_module()
151 ProgramParser parser;
152 Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
153 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
155 VariableResolver resolver;
156 i->content.visit(resolver);
157 for(map<string, VariableDeclaration *>::iterator j=i->content.variables.begin(); j!=i->content.variables.end(); ++j)
158 j->second->linked_declaration = j->second;
163 Module &ProgramCompiler::get_builtins_module()
165 static RefPtr<Module> builtins_module = create_builtins_module();
166 return *builtins_module;
169 Stage *ProgramCompiler::get_builtins(StageType type)
171 Module &module = get_builtins_module();
172 for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
178 void ProgramCompiler::append_module(ProgramSyntax::Module &mod)
180 vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
181 for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
182 import((*i)->module);
183 apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
185 append_stage(mod.shared);
186 for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
190 void ProgramCompiler::append_stage(Stage &stage)
193 if(stage.type==SHARED)
194 target = &module->shared;
197 list<Stage>::iterator i;
198 for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
199 if(i==module->stages.end() || i->type>stage.type)
201 list<Stage>::iterator j = module->stages.insert(i, stage.type);
202 if(i!=module->stages.end())
205 if(i!=module->stages.begin())
212 if(stage.required_version>target->required_version)
213 target->required_version = stage.required_version;
214 for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
215 target->content.body.push_back(*i);
216 apply<DeclarationCombiner>(*target);
219 void ProgramCompiler::process()
221 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
223 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
226 i = module->stages.begin();
230 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
234 void ProgramCompiler::import(const string &name)
236 string fn = name+".glsl";
237 if(find(imported_names, fn)!=imported_names.end())
239 imported_names.push_back(fn);
241 RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
243 throw runtime_error(format("module %s not found", name));
244 ProgramParser import_parser;
245 append_module(import_parser.parse(*io, fn, imported_names.size()));
248 void ProgramCompiler::generate(Stage &stage)
250 if(module->shared.required_version>stage.required_version)
251 stage.required_version = module->shared.required_version;
252 inject_block(stage.content, module->shared.content);
254 apply<DeclarationReorderer>(stage);
255 apply<FunctionResolver>(stage);
256 apply<VariableResolver>(stage);
257 apply<InterfaceGenerator>(stage);
258 apply<VariableResolver>(stage);
259 apply<DeclarationReorderer>(stage);
260 apply<FunctionResolver>(stage);
261 apply<LegacyConverter>(stage);
264 bool ProgramCompiler::optimize(Stage &stage)
266 apply<ConstantConditionEliminator>(stage);
268 set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
269 apply<FunctionInliner>(stage, inlineable);
271 set<Node *> unused = apply<UnusedVariableLocator>(stage);
272 set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
273 unused.insert(unused2.begin(), unused2.end());
274 apply<NodeRemover>(stage, unused);
276 return !unused.empty();
279 void ProgramCompiler::finalize(Stage &stage)
281 if(get_gl_api()==OPENGL_ES2)
282 apply<DefaultPrecisionGenerator>(stage);
284 apply<PrecisionRemover>(stage);
287 void ProgramCompiler::inject_block(Block &target, const Block &source)
289 NodeList<Statement>::iterator insert_point = target.body.begin();
290 for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
291 target.body.insert(insert_point, (*i)->clone());
295 typename T::ResultType ProgramCompiler::apply(Stage &stage)
298 visitor.apply(stage);
299 return visitor.get_result();
302 template<typename T, typename A>
303 typename T::ResultType ProgramCompiler::apply(Stage &stage, const A &arg)
306 visitor.apply(stage);
307 return visitor.get_result();
311 ProgramCompiler::Visitor::Visitor():
315 void ProgramCompiler::Visitor::apply(Stage &s)
317 SetForScope<Stage *> set(stage, &s);
318 stage->content.visit(*this);
322 ProgramCompiler::BlockModifier::BlockModifier():
326 void ProgramCompiler::BlockModifier::flatten_block(Block &block)
328 insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
332 void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, NodeList<Statement>::iterator &i)
334 block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
335 insert_nodes.clear();
338 block.body.erase(i++);
344 void ProgramCompiler::BlockModifier::visit(Block &block)
346 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
349 apply_and_increment(block, i);
354 ProgramCompiler::Formatter::Formatter():
358 parameter_list(false)
361 void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s)
363 GLApi api = get_gl_api();
364 const Version &ver = s.required_version;
368 append(format("#version %d%02d", ver.major, ver.minor));
369 if(api==OPENGL_ES2 && ver>=Version(3, 0))
374 for(vector<const Extension *>::const_iterator i=s.required_extensions.begin(); i!=s.required_extensions.end(); ++i)
375 append(format("#extension %s: require\n", (*i)->get_name()));
376 if(!s.required_extensions.empty())
382 void ProgramCompiler::Formatter::append(const string &text)
385 for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
390 void ProgramCompiler::Formatter::append(char c)
397 void ProgramCompiler::Formatter::set_source(unsigned index, unsigned line)
399 if(index!=source_index || (index && line!=source_line))
401 if(index==source_index && line==source_line+1)
406 if(stage->required_version<Version(3, 30))
408 formatted += format("#line %d %d\n", l, index);
411 source_index = index;
415 void ProgramCompiler::Formatter::visit(Literal &literal)
417 append(literal.token);
420 void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
423 parexpr.expression->visit(*this);
427 void ProgramCompiler::Formatter::visit(VariableReference &var)
432 void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
434 memacc.left->visit(*this);
435 append(format(".%s", memacc.member));
438 void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
442 unary.expression->visit(*this);
447 void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
449 binary.left->visit(*this);
451 binary.right->visit(*this);
452 append(binary.after);
455 void ProgramCompiler::Formatter::visit(Assignment &assign)
457 assign.left->visit(*this);
458 append(format(" %s ", assign.oper));
459 assign.right->visit(*this);
462 void ProgramCompiler::Formatter::visit(FunctionCall &call)
464 append(format("%s(", call.name));
465 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
467 if(i!=call.arguments.begin())
474 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
476 expr.expression->visit(*this);
480 void ProgramCompiler::Formatter::visit(Block &block)
482 unsigned brace_indent = indent;
483 bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
485 append(format("%s{\n", string(brace_indent*2, ' ')));
487 SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
488 string spaces(indent*2, ' ');
489 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
491 if(i!=block.body.begin())
493 set_source((*i)->source, (*i)->line);
499 append(format("\n%s}", string(brace_indent*2, ' ')));
502 void ProgramCompiler::Formatter::visit(Import &import)
504 append(format("import %s;", import.module));
507 void ProgramCompiler::Formatter::visit(Precision &prec)
509 append(format("precision %s %s;", prec.precision, prec.type));
512 void ProgramCompiler::Formatter::visit(Layout &layout)
515 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
517 if(i!=layout.qualifiers.begin())
519 append(i->identifier);
520 if(!i->value.empty())
521 append(format("=%s", i->value));
526 void ProgramCompiler::Formatter::visit(InterfaceLayout &layout)
528 layout.layout.visit(*this);
529 append(format(" %s;", layout.interface));
532 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
534 append(format("struct %s\n", strct.name));
535 strct.members.visit(*this);
539 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
543 var.layout->visit(*this);
548 if(!var.interpolation.empty())
549 append(format("%s ", var.interpolation));
550 if(!var.sampling.empty())
551 append(format("%s ", var.sampling));
552 if(!var.interface.empty() && var.interface!=block_interface)
554 string interface = var.interface;
555 if(stage->required_version<Version(1, 30))
557 if(stage->type==VERTEX && var.interface=="in")
558 interface = "attribute";
559 else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in"))
560 interface = "varying";
562 append(format("%s ", interface));
564 if(!var.precision.empty())
565 append(format("%s ", var.precision));
566 append(format("%s %s", var.type, var.name));
571 var.array_size->visit(*this);
574 if(var.init_expression)
577 var.init_expression->visit(*this);
583 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
585 SetForScope<string> set(block_interface, iface.interface);
586 append(format("%s %s\n", iface.interface, iface.name));
587 iface.members.visit(*this);
591 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
593 append(format("%s %s(", func.return_type, func.name));
594 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
596 if(i!=func.parameters.begin())
598 SetFlag set(parameter_list);
602 if(func.definition==&func)
605 func.body.visit(*this);
611 void ProgramCompiler::Formatter::visit(Conditional &cond)
614 cond.condition->visit(*this);
617 cond.body.visit(*this);
618 if(!cond.else_body.body.empty())
620 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
621 if(cond.else_body.body.size()==1 && else_cond)
624 set_source(else_cond->source, else_cond->line);
625 append(format("%selse ", string(indent*2, ' ')));
626 else_cond->visit(*this);
630 append(format("\n%selse\n", string(indent*2, ' ')));
631 cond.else_body.visit(*this);
636 void ProgramCompiler::Formatter::visit(Iteration &iter)
638 if(!iter.init_statement && iter.condition && !iter.loop_expression)
641 iter.condition->visit(*this);
647 if(iter.init_statement)
648 iter.init_statement->visit(*this);
654 iter.condition->visit(*this);
657 if(iter.loop_expression)
660 iter.loop_expression->visit(*this);
665 if(iter.body.body.empty())
670 iter.body.visit(*this);
674 void ProgramCompiler::Formatter::visit(Return &ret)
680 ret.expression->visit(*this);
685 void ProgramCompiler::Formatter::visit(Jump &jump)
687 append(jump.keyword);
692 ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
696 void ProgramCompiler::DeclarationCombiner::visit(Block &block)
701 SetForScope<bool> set(toplevel, false);
702 BlockModifier::visit(block);
705 void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
707 vector<FunctionDeclaration *> &decls = functions[func.name];
710 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
712 (*i)->definition = func.definition;
713 (*i)->body.body.clear();
716 decls.push_back(&func);
719 void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
721 VariableDeclaration *&ptr = variables[var.name];
724 ptr->type = var.type;
725 if(var.init_expression)
726 ptr->init_expression = var.init_expression;
731 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
734 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
735 if(j->identifier==i->identifier)
742 ptr->layout->qualifiers.push_back(*i);
746 ptr->layout = var.layout;
755 ProgramCompiler::VariableResolver::VariableResolver():
757 record_target(false),
758 assignment_target(0),
759 self_referencing(false)
762 void ProgramCompiler::VariableResolver::apply(Stage &s)
764 SetForScope<Stage *> set(stage, &s);
765 Stage *builtins = get_builtins(stage->type);
767 blocks.push_back(&builtins->content);
768 stage->content.visit(*this);
773 void ProgramCompiler::VariableResolver::visit(Block &block)
775 blocks.push_back(&block);
776 block.variables.clear();
777 TraversingVisitor::visit(block);
781 void ProgramCompiler::VariableResolver::visit(VariableReference &var)
785 for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
788 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
789 if(j!=(*i)->variables.end())
791 var.declaration = j->second;
792 type = j->second->type_declaration;
799 if(assignment_target)
801 record_target = false;
802 assignment_target = 0;
805 assignment_target = var.declaration;
807 else if(var.declaration && var.declaration==assignment_target)
808 self_referencing = true;
811 void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
814 TraversingVisitor::visit(memacc);
815 memacc.declaration = 0;
818 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
819 if(i!=type->members.variables.end())
821 memacc.declaration = i->second;
822 type = i->second->type_declaration;
829 void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
834 SetForScope<bool> set(record_target, false);
835 binary.right->visit(*this);
838 binary.left->visit(*this);
842 TraversingVisitor::visit(binary);
847 void ProgramCompiler::VariableResolver::visit(Assignment &assign)
850 SetFlag set(record_target);
851 assignment_target = 0;
852 assign.left->visit(*this);
855 self_referencing = false;
856 assign.right->visit(*this);
858 assign.self_referencing = (self_referencing || assign.oper!="=");
859 assign.target_declaration = assignment_target;
862 void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
864 TraversingVisitor::visit(strct);
865 blocks.back()->types[strct.name] = &strct;
868 void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
870 for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
873 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
874 if(j!=(*i)->types.end())
875 var.type_declaration = j->second;
878 if(!block_interface.empty() && var.interface.empty())
879 var.interface = block_interface;
881 TraversingVisitor::visit(var);
882 blocks.back()->variables[var.name] = &var;
883 if(anonymous && blocks.size()>1)
884 blocks[blocks.size()-2]->variables[var.name] = &var;
887 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
889 SetFlag set(anonymous);
890 SetForScope<string> set2(block_interface, iface.interface);
891 TraversingVisitor::visit(iface);
895 void ProgramCompiler::FunctionResolver::visit(FunctionCall &call)
897 map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
898 if(i!=functions.end())
899 call.declaration = i->second.back();
901 TraversingVisitor::visit(call);
904 void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
906 vector<FunctionDeclaration *> &decls = functions[func.name];
909 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
910 (*i)->definition = func.definition;
912 decls.push_back(&func);
914 else if(!decls.empty() && decls.back()->definition)
915 func.definition = decls.back()->definition;
917 decls.push_back(&func);
919 TraversingVisitor::visit(func);
923 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
927 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
931 else if(type==GEOMETRY)
937 void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
939 SetForScope<Stage *> set(stage, &s);
941 in_prefix = get_out_prefix(stage->previous->type);
942 out_prefix = get_out_prefix(stage->type);
943 stage->content.visit(*this);
946 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
948 SetForScope<unsigned> set(scope_level, scope_level+1);
949 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
955 for(map<string, RefPtr<VariableDeclaration> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
957 NodeList<Statement>::iterator k = block.body.insert(i, j->second);
960 iface_declarations.clear();
963 apply_and_increment(block, i);
967 string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
969 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
970 return prefix+name.substr(offset);
973 bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
975 const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
976 if(stage_vars.count(name) || iface_declarations.count(name))
979 VariableDeclaration* iface_var = new VariableDeclaration;
980 iface_var->sampling = var.sampling;
981 iface_var->interface = iface;
982 iface_var->type = var.type;
983 iface_var->type_declaration = var.type_declaration;
984 iface_var->name = name;
985 if(stage->type==GEOMETRY)
986 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
988 iface_var->array = var.array;
990 iface_var->array_size = var.array_size;
992 iface_var->linked_declaration = &var;
993 iface_declarations[name] = iface_var;
998 ExpressionStatement &ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
1000 Assignment *assign = new Assignment;
1001 VariableReference *ref = new VariableReference;
1005 assign->right = right;
1007 ExpressionStatement *stmt = new ExpressionStatement;
1008 stmt->expression = assign;
1010 insert_nodes.push_back(stmt);
1015 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
1017 if(var.declaration || !stage->previous)
1019 if(iface_declarations.count(var.name))
1022 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
1023 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
1024 if(i==prev_out.end())
1025 i = prev_out.find(in_prefix+var.name);
1026 if(i!=prev_out.end())
1028 generate_interface(*i->second, "in", i->second->name);
1029 var.name = i->second->name;
1033 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
1035 if(var.interface=="out")
1038 stage->out_variables[var.name] = &var;
1039 else if(generate_interface(var, "out", change_prefix(var.name, string())))
1042 if(var.init_expression)
1044 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1045 stmt.source = var.source;
1046 stmt.line = var.line;
1051 else if(var.interface=="in")
1053 stage->in_variables[var.name] = &var;
1054 if(var.linked_declaration)
1055 var.linked_declaration->linked_declaration = &var;
1056 else if(stage->previous)
1058 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
1059 map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
1060 if(i!=prev_out.end())
1062 var.linked_declaration = i->second;
1063 i->second->linked_declaration = &var;
1068 TraversingVisitor::visit(var);
1071 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
1073 vector<VariableDeclaration *> pass_vars;
1075 for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
1076 pass_vars.push_back(i->second);
1077 for(map<string, RefPtr<VariableDeclaration> >::const_iterator i=iface_declarations.begin(); i!=iface_declarations.end(); ++i)
1078 if(i->second->interface=="in")
1079 pass_vars.push_back(i->second.get());
1083 const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
1084 for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
1086 bool linked = false;
1087 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
1088 linked = ((*j)->linked_declaration==i->second);
1090 if(!linked && generate_interface(*i->second, "in", i->second->name))
1091 pass_vars.push_back(i->second);
1095 if(stage->type==GEOMETRY)
1097 VariableReference *ref = new VariableReference;
1098 ref->name = "gl_in";
1100 BinaryExpression *subscript = new BinaryExpression;
1101 subscript->left = ref;
1102 subscript->oper = "[";
1103 subscript->right = pass.subscript;
1104 subscript->after = "]";
1106 MemberAccess *memacc = new MemberAccess;
1107 memacc->left = subscript;
1108 memacc->member = "gl_Position";
1110 insert_assignment("gl_Position", memacc);
1113 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1115 string out_name = change_prefix((*i)->name, out_prefix);
1116 generate_interface(**i, "out", out_name);
1118 VariableReference *ref = new VariableReference;
1119 ref->name = (*i)->name;
1122 BinaryExpression *subscript = new BinaryExpression;
1123 subscript->left = ref;
1124 subscript->oper = "[";
1125 subscript->right = pass.subscript;
1126 subscript->after = "]";
1127 insert_assignment(out_name, subscript);
1130 insert_assignment(out_name, ref);
1137 ProgramCompiler::DeclarationReorderer::DeclarationReorderer():
1139 kind(NO_DECLARATION)
1142 void ProgramCompiler::DeclarationReorderer::visit(FunctionCall &call)
1144 FunctionDeclaration *def = call.declaration;
1146 def = def->definition;
1147 if(def && !ordered_funcs.count(def))
1148 needed_funcs.insert(def);
1151 void ProgramCompiler::DeclarationReorderer::visit(Block &block)
1153 SetForScope<unsigned> set(scope_level, scope_level+1);
1155 return Visitor::visit(block);
1157 NodeList<Statement>::iterator struct_insert_point = block.body.end();
1158 NodeList<Statement>::iterator variable_insert_point = block.body.end();
1159 NodeList<Statement>::iterator function_insert_point = block.body.end();
1160 unsigned unordered_func_count = 0;
1161 bool ordered_any_funcs = false;
1163 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
1165 kind = NO_DECLARATION;
1169 if(kind==STRUCT && struct_insert_point!=block.body.end())
1171 block.body.insert(struct_insert_point, *i);
1174 else if(kind>STRUCT && struct_insert_point==block.body.end())
1175 struct_insert_point = i;
1177 if(kind==VARIABLE && variable_insert_point!=block.body.end())
1179 block.body.insert(variable_insert_point, *i);
1182 else if(kind>VARIABLE && variable_insert_point==block.body.end())
1183 variable_insert_point = i;
1187 if(function_insert_point==block.body.end())
1188 function_insert_point = i;
1190 if(needed_funcs.empty())
1192 ordered_funcs.insert(i->get());
1193 if(i!=function_insert_point)
1195 block.body.insert(function_insert_point, *i);
1199 ++function_insert_point;
1200 ordered_any_funcs = true;
1203 ++unordered_func_count;
1208 if(function_insert_point==i)
1209 ++function_insert_point;
1210 block.body.erase(i++);
1215 if(i==block.body.end() && unordered_func_count)
1217 if(!ordered_any_funcs)
1218 // A subset of the remaining functions forms a recursive loop
1219 /* TODO pick a function and move it up, adding any necessary
1223 i = function_insert_point;
1224 unordered_func_count = 0;
1229 void ProgramCompiler::DeclarationReorderer::visit(ProgramSyntax::VariableDeclaration &var)
1231 Visitor::visit(var);
1235 void ProgramCompiler::DeclarationReorderer::visit(FunctionDeclaration &func)
1237 needed_funcs.clear();
1238 func.body.visit(*this);
1239 needed_funcs.erase(&func);
1244 ProgramCompiler::InlineableFunctionLocator::InlineableFunctionLocator():
1248 void ProgramCompiler::InlineableFunctionLocator::visit(FunctionCall &call)
1250 FunctionDeclaration *def = call.declaration;
1251 if(def && def->definition!=def)
1252 def = def->definition;
1256 unsigned &count = refcounts[def];
1258 if(count>1 || def==in_function)
1259 inlineable.erase(def);
1262 TraversingVisitor::visit(call);
1265 void ProgramCompiler::InlineableFunctionLocator::visit(FunctionDeclaration &func)
1267 unsigned &count = refcounts[func.definition];
1268 if(!count && func.parameters.empty())
1269 inlineable.insert(func.definition);
1271 SetForScope<FunctionDeclaration *> set(in_function, &func);
1272 TraversingVisitor::visit(func);
1276 ProgramCompiler::FunctionInliner::FunctionInliner():
1280 ProgramCompiler::FunctionInliner::FunctionInliner(const set<FunctionDeclaration *> &in):
1285 void ProgramCompiler::FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
1290 ptr = inline_result;
1293 void ProgramCompiler::FunctionInliner::visit(Block &block)
1298 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1306 void ProgramCompiler::FunctionInliner::visit(UnaryExpression &unary)
1308 visit_and_inline(unary.expression);
1312 void ProgramCompiler::FunctionInliner::visit(BinaryExpression &binary)
1314 visit_and_inline(binary.left);
1315 visit_and_inline(binary.right);
1319 void ProgramCompiler::FunctionInliner::visit(MemberAccess &memacc)
1321 visit_and_inline(memacc.left);
1325 void ProgramCompiler::FunctionInliner::visit(FunctionCall &call)
1327 for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
1328 visit_and_inline(*i);
1330 FunctionDeclaration *def = call.declaration;
1331 if(def && def->definition!=def)
1332 def = def->definition;
1334 if(def && inlineable.count(def))
1343 void ProgramCompiler::FunctionInliner::visit(VariableDeclaration &var)
1345 if(var.init_expression)
1346 visit_and_inline(var.init_expression);
1350 void ProgramCompiler::FunctionInliner::visit(Return &ret)
1352 TraversingVisitor::visit(ret);
1355 inline_result = ret.expression->clone();
1359 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator():
1365 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator(const ValueMap &v):
1366 variable_values(&v),
1371 void ProgramCompiler::ExpressionEvaluator::visit(Literal &literal)
1373 if(literal.token=="true")
1375 else if(literal.token=="false")
1378 result = lexical_cast<float>(literal.token);
1379 result_valid = true;
1382 void ProgramCompiler::ExpressionEvaluator::visit(ParenthesizedExpression &parexp)
1384 parexp.expression->visit(*this);
1387 void ProgramCompiler::ExpressionEvaluator::visit(VariableReference &var)
1389 if(!var.declaration)
1394 ValueMap::const_iterator i = variable_values->find(var.declaration);
1395 if(i!=variable_values->end())
1396 i->second->visit(*this);
1398 else if(var.declaration->init_expression)
1399 var.declaration->init_expression->visit(*this);
1402 void ProgramCompiler::ExpressionEvaluator::visit(UnaryExpression &unary)
1404 result_valid = false;
1405 unary.expression->visit(*this);
1412 result_valid = false;
1415 void ProgramCompiler::ExpressionEvaluator::visit(BinaryExpression &binary)
1417 result_valid = false;
1418 binary.left->visit(*this);
1422 float left_result = result;
1423 result_valid = false;
1424 binary.right->visit(*this);
1428 if(binary.oper=="<")
1429 result = (left_result<result);
1430 else if(binary.oper=="<=")
1431 result = (left_result<=result);
1432 else if(binary.oper==">")
1433 result = (left_result>result);
1434 else if(binary.oper==">=")
1435 result = (left_result>=result);
1436 else if(binary.oper=="==")
1437 result = (left_result==result);
1438 else if(binary.oper=="!=")
1439 result = (left_result!=result);
1440 else if(binary.oper=="&&")
1441 result = (left_result && result);
1442 else if(binary.oper=="||")
1443 result = (left_result || result);
1445 result_valid = false;
1449 ProgramCompiler::ConstantConditionEliminator::ConstantConditionEliminator():
1454 void ProgramCompiler::ConstantConditionEliminator::visit(Block &block)
1456 SetForScope<unsigned> set(scope_level, scope_level+1);
1457 BlockModifier::visit(block);
1459 for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
1460 variable_values.erase(i->second);
1463 void ProgramCompiler::ConstantConditionEliminator::visit(UnaryExpression &unary)
1465 if(VariableReference *var = dynamic_cast<VariableReference *>(unary.expression.get()))
1466 if(unary.oper=="++" || unary.oper=="--")
1467 variable_values.erase(var->declaration);
1470 void ProgramCompiler::ConstantConditionEliminator::visit(Assignment &assign)
1472 variable_values.erase(assign.target_declaration);
1475 void ProgramCompiler::ConstantConditionEliminator::visit(VariableDeclaration &var)
1477 if(var.constant || scope_level>1)
1478 variable_values[&var] = var.init_expression.get();
1481 void ProgramCompiler::ConstantConditionEliminator::visit(Conditional &cond)
1485 ExpressionEvaluator eval(variable_values);
1486 cond.condition->visit(eval);
1487 if(eval.result_valid)
1489 flatten_block(eval.result ? cond.body : cond.else_body);
1494 TraversingVisitor::visit(cond);
1497 void ProgramCompiler::ConstantConditionEliminator::visit(Iteration &iter)
1503 /* If the loop condition is always false on the first iteration, the
1504 entire loop can be removed */
1505 if(iter.init_statement)
1506 iter.init_statement->visit(*this);
1507 ExpressionEvaluator eval(variable_values);
1508 iter.condition->visit(eval);
1509 if(eval.result_valid && !eval.result)
1516 /* Record all assignments that occur inside the loop body so those
1517 variables won't be considered as constant */
1518 SetFlag set_record(record_only);
1519 TraversingVisitor::visit(iter);
1522 TraversingVisitor::visit(iter);
1524 if(VariableDeclaration *init_decl = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
1525 variable_values.erase(init_decl);
1529 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
1532 assignment_target(false),
1533 assign_to_subscript(false),
1537 void ProgramCompiler::UnusedVariableLocator::apply(Stage &s)
1539 variables.push_back(BlockVariableMap());
1541 BlockVariableMap &global_variables = variables.back();
1542 for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
1544 if(i->first->interface=="out" && (s.type==FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
1546 if(!i->second.referenced)
1548 unused_nodes.insert(i->first);
1549 clear_assignments(i->second, true);
1552 variables.pop_back();
1555 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
1557 map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
1558 if(i!=aggregates.end())
1559 unused_nodes.erase(i->second);
1561 if(var.declaration && !assignment_target)
1563 VariableInfo &var_info = variables.back()[var.declaration];
1564 var_info.assignments.clear();
1565 var_info.referenced = true;
1569 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
1571 TraversingVisitor::visit(memacc);
1572 unused_nodes.erase(memacc.declaration);
1575 void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
1577 if(binary.oper=="[")
1579 if(assignment_target)
1580 assign_to_subscript = true;
1581 binary.left->visit(*this);
1582 SetForScope<bool> set(assignment_target, false);
1583 binary.right->visit(*this);
1586 TraversingVisitor::visit(binary);
1589 void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
1592 assign_to_subscript = false;
1593 SetForScope<bool> set(assignment_target, !assign.self_referencing);
1594 assign.left->visit(*this);
1596 assign.right->visit(*this);
1597 assignment = &assign;
1600 void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool chained)
1602 VariableInfo &var_info = variables.back()[&var];
1604 clear_assignments(var_info, true);
1605 var_info.assignments.push_back(&node);
1606 var_info.conditionally_assigned = false;
1609 void ProgramCompiler::UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_unused)
1613 for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
1614 unused_nodes.insert(*i);
1616 var_info.assignments.clear();
1619 void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
1622 TraversingVisitor::visit(expr);
1623 if(assignment && assignment->target_declaration)
1624 record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript));
1627 void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
1629 SetForScope<Node *> set(aggregate, &strct);
1630 unused_nodes.insert(&strct);
1631 TraversingVisitor::visit(strct);
1634 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
1637 aggregates[&var] = aggregate;
1640 variables.back()[&var].local = true;
1641 if(var.init_expression)
1642 record_assignment(var, *var.init_expression, false);
1644 unused_nodes.erase(var.type_declaration);
1645 TraversingVisitor::visit(var);
1648 void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface)
1650 SetForScope<Node *> set(aggregate, &iface);
1651 unused_nodes.insert(&iface);
1652 TraversingVisitor::visit(iface);
1655 void ProgramCompiler::UnusedVariableLocator::visit(FunctionDeclaration &func)
1657 variables.push_back(BlockVariableMap());
1660 SetForScope<bool> set(global_scope, false);
1661 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
1663 func.body.visit(*this);
1666 BlockVariableMap &block_variables = variables.back();
1667 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
1668 i->second.conditionally_assigned = true;
1669 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
1670 block_variables[i->get()].referenced = true;
1671 merge_down_variables();
1674 void ProgramCompiler::UnusedVariableLocator::merge_down_variables()
1676 BlockVariableMap &parent_variables = variables[variables.size()-2];
1677 BlockVariableMap &block_variables = variables.back();
1678 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
1682 if(!i->second.referenced)
1683 unused_nodes.insert(i->first);
1684 clear_assignments(i->second, i->first->interface!="out");
1688 BlockVariableMap::iterator j = parent_variables.find(i->first);
1689 if(j==parent_variables.end())
1690 parent_variables.insert(*i);
1693 if(i->second.referenced || !i->second.conditionally_assigned)
1694 clear_assignments(j->second, !i->second.referenced);
1695 j->second.conditionally_assigned = i->second.conditionally_assigned;
1696 j->second.referenced |= i->second.referenced;
1697 j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin(), i->second.assignments.end());
1700 variables.pop_back();
1703 void ProgramCompiler::UnusedVariableLocator::visit(Conditional &cond)
1705 cond.condition->visit(*this);
1706 variables.push_back(BlockVariableMap());
1707 cond.body.visit(*this);
1709 BlockVariableMap if_variables;
1710 swap(variables.back(), if_variables);
1711 cond.else_body.visit(*this);
1713 BlockVariableMap &else_variables = variables.back();
1714 for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
1716 BlockVariableMap::iterator j = if_variables.find(i->first);
1717 if(j!=if_variables.end())
1719 i->second.assignments.insert(i->second.assignments.end(), j->second.assignments.begin(), j->second.assignments.end());
1720 i->second.conditionally_assigned |= j->second.conditionally_assigned;
1721 if_variables.erase(j);
1724 i->second.conditionally_assigned = true;
1727 for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
1729 i->second.conditionally_assigned = true;
1730 else_variables.insert(*i);
1733 merge_down_variables();
1736 void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter)
1738 variables.push_back(BlockVariableMap());
1739 TraversingVisitor::visit(iter);
1741 BlockVariableMap &block_variables = variables.back();
1742 for(BlockVariableMap::iterator i=block_variables.begin(); i!=block_variables.end(); ++i)
1743 if(!i->second.local && i->second.referenced)
1744 i->second.assignments.clear();
1746 merge_down_variables();
1750 ProgramCompiler::UnusedVariableLocator::VariableInfo::VariableInfo():
1752 conditionally_assigned(false),
1757 void ProgramCompiler::UnusedFunctionLocator::visit(FunctionCall &call)
1759 TraversingVisitor::visit(call);
1761 unused_nodes.erase(call.declaration);
1762 if(call.declaration && call.declaration->definition!=call.declaration)
1763 used_definitions.insert(call.declaration->definition);
1766 void ProgramCompiler::UnusedFunctionLocator::visit(FunctionDeclaration &func)
1768 TraversingVisitor::visit(func);
1770 if((func.name!="main" || func.body.body.empty()) && !used_definitions.count(&func))
1771 unused_nodes.insert(&func);
1775 ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
1779 void ProgramCompiler::NodeRemover::visit(Block &block)
1781 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
1784 if(to_remove.count(i->get()))
1785 block.body.erase(i++);
1791 void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
1793 if(to_remove.count(&var))
1795 stage->in_variables.erase(var.name);
1796 stage->out_variables.erase(var.name);
1797 stage->locations.erase(var.name);
1798 if(var.linked_declaration)
1799 var.linked_declaration->linked_declaration = 0;
1801 else if(var.init_expression && to_remove.count(var.init_expression.get()))
1802 var.init_expression = 0;
1805 void ProgramCompiler::NodeRemover::visit(Iteration &iter)
1807 if(to_remove.count(iter.init_statement.get()))
1808 iter.init_statement = 0;
1809 TraversingVisitor::visit(iter);
1813 void ProgramCompiler::PrecisionRemover::visit(Precision &)
1818 void ProgramCompiler::PrecisionRemover::visit(VariableDeclaration &var)
1820 var.precision.clear();
1824 ProgramCompiler::DefaultPrecisionGenerator::DefaultPrecisionGenerator():
1828 void ProgramCompiler::DefaultPrecisionGenerator::visit(Block &block)
1832 SetForScope<bool> set(toplevel, false);
1833 BlockModifier::visit(block);
1836 Visitor::visit(block);
1839 void ProgramCompiler::DefaultPrecisionGenerator::visit(Precision &prec)
1841 have_default.insert(prec.type);
1844 void ProgramCompiler::DefaultPrecisionGenerator::visit(VariableDeclaration &var)
1846 if(var.type_declaration)
1849 string type = var.type;
1850 if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
1852 else if(!type.compare(0, 3, "ivec") || type=="uint")
1855 if(!have_default.count(type))
1857 Precision *prec = new Precision;
1858 if(!type.compare(0, 7, "sampler"))
1859 prec->precision = "lowp";
1860 else if(stage->type==FRAGMENT)
1861 prec->precision = "mediump";
1863 prec->precision = "highp";
1865 insert_nodes.push_back(prec);
1867 have_default.insert(type);
1872 ProgramCompiler::LegacyConverter::LegacyConverter():
1873 target_api(get_gl_api()),
1874 target_version(get_glsl_version()),
1878 ProgramCompiler::LegacyConverter::LegacyConverter(const Version &v):
1879 target_api(get_gl_api()),
1884 bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_version) const
1886 if(target_version<feature_version)
1888 else if(stage->required_version<feature_version)
1889 stage->required_version = feature_version;
1894 bool ProgramCompiler::LegacyConverter::check_extension(const Extension &extension) const
1899 vector<const Extension *>::iterator i = find(stage->required_extensions, &extension);
1900 if(i==stage->required_extensions.end())
1901 stage->required_extensions.push_back(&extension);
1906 bool ProgramCompiler::LegacyConverter::supports_unified_interface_syntax() const
1908 if(target_api==OPENGL_ES2)
1909 return check_version(Version(3, 0));
1911 return check_version(Version(1, 30));
1914 void ProgramCompiler::LegacyConverter::visit(VariableReference &var)
1916 if(var.declaration==frag_out && !supports_unified_interface_syntax())
1918 var.name = "gl_FragColor";
1919 var.declaration = 0;
1922 else if(var.declaration)
1923 type = var.declaration->type;
1928 void ProgramCompiler::LegacyConverter::visit(Assignment &assign)
1930 TraversingVisitor::visit(assign);
1931 if(assign.target_declaration==frag_out && !supports_unified_interface_syntax())
1932 assign.target_declaration = 0;
1935 bool ProgramCompiler::LegacyConverter::supports_unified_sampling_functions() const
1937 if(target_api==OPENGL_ES2)
1938 return check_version(Version(3, 0));
1940 return check_version(Version(1, 30));
1943 void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
1945 if(call.name=="texture" && !call.declaration && !supports_unified_sampling_functions())
1947 NodeArray<Expression>::iterator i = call.arguments.begin();
1948 if(i!=call.arguments.end())
1951 if(type=="sampler1D")
1952 call.name = "texture1D";
1953 else if(type=="sampler2D")
1954 call.name = "texture2D";
1955 else if(type=="sampler3D")
1956 call.name = "texture3D";
1957 else if(type=="samplerCube")
1958 call.name = "textureCube";
1959 else if(type=="sampler1DShadow")
1960 call.name = "shadow1D";
1961 else if(type=="sampler2DShadow")
1962 call.name = "shadow2D";
1963 else if(type=="sampler1DArray")
1965 check_extension(EXT_texture_array);
1966 call.name = "texture1DArray";
1968 else if(type=="sampler2DArray")
1970 check_extension(EXT_texture_array);
1971 call.name = "texture2DArray";
1973 else if(type=="sampler1DArrayShadow")
1975 check_extension(EXT_texture_array);
1976 call.name = "shadow1DArray";
1978 else if(type=="sampler2DArrayShadow")
1980 check_extension(EXT_texture_array);
1981 call.name = "shadow2DArray";
1984 for(; i!=call.arguments.end(); ++i)
1989 TraversingVisitor::visit(call);
1992 bool ProgramCompiler::LegacyConverter::supports_interface_layouts() const
1994 if(target_api==OPENGL_ES2)
1995 return check_version(Version(3, 0));
1996 else if(check_version(Version(3, 30)))
1999 return check_extension(ARB_explicit_attrib_location);
2002 bool ProgramCompiler::LegacyConverter::supports_centroid_sampling() const
2004 if(target_api==OPENGL_ES2)
2005 return check_version(Version(3, 0));
2006 else if(check_version(Version(1, 20)))
2009 return check_extension(EXT_gpu_shader4);
2012 bool ProgramCompiler::LegacyConverter::supports_sample_sampling() const
2014 if(target_api==OPENGL_ES2)
2015 return check_version(Version(3, 20));
2016 else if(check_version(Version(4, 0)))
2019 return check_extension(ARB_gpu_shader5);
2022 void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
2024 if(var.layout && !supports_interface_layouts())
2026 vector<Layout::Qualifier>::iterator i;
2027 for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
2028 if(i!=var.layout->qualifiers.end())
2030 unsigned location = lexical_cast<unsigned>(i->value);
2031 if(stage->type==VERTEX && var.interface=="in")
2033 stage->locations[var.name] = location;
2034 var.layout->qualifiers.erase(i);
2036 else if(stage->type==FRAGMENT && var.interface=="out")
2039 static Require _req(EXT_gpu_shader4);
2040 stage->locations[var.name] = location;
2041 var.layout->qualifiers.erase(i);
2044 if(var.layout->qualifiers.empty())
2049 if(var.sampling=="centroid")
2051 if(!supports_centroid_sampling())
2052 var.sampling = string();
2054 else if(var.sampling=="sample")
2056 if(!supports_sample_sampling())
2057 var.sampling = string();
2060 if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
2062 if(stage->type==FRAGMENT && var.interface=="out")
2069 TraversingVisitor::visit(var);
2072 bool ProgramCompiler::LegacyConverter::supports_interface_blocks(const string &iface) const
2074 if(target_api==OPENGL_ES2)
2076 if(iface=="uniform")
2077 return check_version(Version(3, 0));
2079 return check_version(Version(3, 20));
2081 else if(check_version(Version(1, 50)))
2083 else if(iface=="uniform")
2084 return check_extension(ARB_uniform_buffer_object);
2089 void ProgramCompiler::LegacyConverter::visit(InterfaceBlock &iface)
2091 if(!supports_interface_blocks(iface.interface))
2092 flatten_block(iface.members);