]> git.tdb.fi Git - libs/gl.git/blob - source/programcompiler.cpp
Add another constructor to RenderTarget to resolve an ambiguity
[libs/gl.git] / source / programcompiler.cpp
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>
11 #include "error.h"
12 #include "program.h"
13 #include "programcompiler.h"
14 #include "resources.h"
15 #include "shader.h"
16
17 #undef interface
18
19 using namespace std;
20
21 namespace {
22
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"
28         "};\n"
29         "#pragma MSP stage(geometry)\n"
30         "in gl_PerVertex {\n"
31         "  vec4 gl_Position;\n"
32         "  float gl_ClipDistance[];\n"
33         "} gl_in[];\n"
34         "out gl_PerVertex {\n"
35         "  vec4 gl_Position;\n"
36         "  float gl_ClipDistance[];\n"
37         "};\n";
38
39 }
40
41 namespace Msp {
42 namespace GL {
43
44 using namespace ProgramSyntax;
45
46 ProgramCompiler::ProgramCompiler():
47         resources(0),
48         module(0)
49 { }
50
51 ProgramCompiler::~ProgramCompiler()
52 {
53         delete module;
54 }
55
56 void ProgramCompiler::compile(const string &source, const string &src_name)
57 {
58         resources = 0;
59         delete module;
60         module = new Module();
61         ProgramParser parser;
62         imported_names.push_back(src_name);
63         append_module(parser.parse(source, src_name, 1));
64         process();
65 }
66
67 void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
68 {
69         resources = res;
70         delete module;
71         module = new Module();
72         ProgramParser parser;
73         imported_names.push_back(src_name);
74         append_module(parser.parse(io, src_name, 1));
75         process();
76 }
77
78 void ProgramCompiler::compile(IO::Base &io, const string &src_name)
79 {
80         compile(io, 0, src_name);
81 }
82
83 void ProgramCompiler::add_shaders(Program &program)
84 {
85         if(!module)
86                 throw invalid_operation("ProgramCompiler::add_shaders");
87
88         try
89         {
90                 for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
91                 {
92                         if(i->type==VERTEX)
93                         {
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);
97                         }
98                         else if(i->type==GEOMETRY)
99                                 program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
100                         else if(i->type==FRAGMENT)
101                         {
102                                 program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
103                                 if(EXT_gpu_shader4)
104                                 {
105                                         for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
106                                                 program.bind_fragment_data(j->second, j->first);
107                                 }
108                         }
109                 }
110         }
111         catch(const compile_error &e)
112         {
113                 static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
114                 vector<string> lines = split(e.what(), '\n');
115                 string translated;
116                 for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
117                 {
118                         RegMatch m = r_message.match(*i);
119                         if(m)
120                         {
121                                 unsigned index = 0;
122                                 unsigned line = 0;
123                                 if(m[2])
124                                 {
125                                         index = lexical_cast<unsigned>(m[2].str);
126                                         line = lexical_cast<unsigned>(m[3].str);
127                                 }
128                                 else if(m[4])
129                                 {
130                                         index = lexical_cast<unsigned>(m[4].str);
131                                         line = lexical_cast<unsigned>(m[5].str);
132                                 }
133                                 const char *src = "<unknown>";
134                                 if(index==0)
135                                         src = "<generated>";
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);
139                         }
140                         else
141                                 translated += *i;
142                         translated += '\n';
143                 }
144
145                 throw compile_error(translated);
146         }
147 }
148
149 Module *ProgramCompiler::create_builtins_module()
150 {
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)
154         {
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;
159         }
160         return module;
161 }
162
163 Module &ProgramCompiler::get_builtins_module()
164 {
165         static RefPtr<Module> builtins_module = create_builtins_module();
166         return *builtins_module;
167 }
168
169 Stage *ProgramCompiler::get_builtins(StageType type)
170 {
171         Module &module = get_builtins_module();
172         for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
173                 if(i->type==type)
174                         return &*i;
175         return 0;
176 }
177
178 void ProgramCompiler::append_module(ProgramSyntax::Module &mod)
179 {
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()));
184
185         append_stage(mod.shared);
186         for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
187                 append_stage(*i);
188 }
189
190 void ProgramCompiler::append_stage(Stage &stage)
191 {
192         Stage *target = 0;
193         if(stage.type==SHARED)
194                 target = &module->shared;
195         else
196         {
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)
200                 {
201                         list<Stage>::iterator j = module->stages.insert(i, stage.type);
202                         if(i!=module->stages.end())
203                                 i->previous = &*j;
204                         i = j;
205                         if(i!=module->stages.begin())
206                                 i->previous = &*--j;
207                 }
208
209                 target = &*i;
210         }
211
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);
217 }
218
219 void ProgramCompiler::process()
220 {
221         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
222                 generate(*i);
223         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
224         {
225                 if(optimize(*i))
226                         i = module->stages.begin();
227                 else
228                         ++i;
229         }
230         for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
231                 finalize(*i);
232 }
233
234 void ProgramCompiler::import(const string &name)
235 {
236         string fn = name+".glsl";
237         if(find(imported_names, fn)!=imported_names.end())
238                 return;
239         imported_names.push_back(fn);
240
241         RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
242         if(!io)
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()));
246 }
247
248 void ProgramCompiler::generate(Stage &stage)
249 {
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);
253
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);
262 }
263
264 bool ProgramCompiler::optimize(Stage &stage)
265 {
266         apply<ConstantConditionEliminator>(stage);
267
268         set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
269         apply<FunctionInliner>(stage, inlineable);
270
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);
275
276         return !unused.empty();
277 }
278
279 void ProgramCompiler::finalize(Stage &stage)
280 {
281         if(get_gl_api()==OPENGL_ES2)
282                 apply<DefaultPrecisionGenerator>(stage);
283         else
284                 apply<PrecisionRemover>(stage);
285 }
286
287 void ProgramCompiler::inject_block(Block &target, const Block &source)
288 {
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());
292 }
293
294 template<typename T>
295 typename T::ResultType ProgramCompiler::apply(Stage &stage)
296 {
297         T visitor;
298         visitor.apply(stage);
299         return visitor.get_result();
300 }
301
302 template<typename T, typename A>
303 typename T::ResultType ProgramCompiler::apply(Stage &stage, const A &arg)
304 {
305         T visitor(arg);
306         visitor.apply(stage);
307         return visitor.get_result();
308 }
309
310
311 ProgramCompiler::Visitor::Visitor():
312         stage(0)
313 { }
314
315 void ProgramCompiler::Visitor::apply(Stage &s)
316 {
317         SetForScope<Stage *> set(stage, &s);
318         stage->content.visit(*this);
319 }
320
321
322 ProgramCompiler::BlockModifier::BlockModifier():
323         remove_node(false)
324 { }
325
326 void ProgramCompiler::BlockModifier::flatten_block(Block &block)
327 {
328         insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
329         remove_node = true;
330 }
331
332 void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, NodeList<Statement>::iterator &i)
333 {
334         block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
335         insert_nodes.clear();
336
337         if(remove_node)
338                 block.body.erase(i++);
339         else
340                 ++i;
341         remove_node = false;
342 }
343
344 void ProgramCompiler::BlockModifier::visit(Block &block)
345 {
346         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
347         {
348                 (*i)->visit(*this);
349                 apply_and_increment(block, i);
350         }
351 }
352
353
354 ProgramCompiler::Formatter::Formatter():
355         source_index(0),
356         source_line(1),
357         indent(0),
358         parameter_list(false)
359 { }
360
361 void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s)
362 {
363         GLApi api = get_gl_api();
364         const Version &ver = s.required_version;
365
366         if(ver)
367         {
368                 append(format("#version %d%02d", ver.major, ver.minor));
369                 if(api==OPENGL_ES2 && ver>=Version(3, 0))
370                         append(" es");
371                 formatted += '\n';
372         }
373
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())
377                 formatted += '\n';
378
379         Visitor::apply(s);
380 }
381
382 void ProgramCompiler::Formatter::append(const string &text)
383 {
384         formatted += text;
385         for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
386                 if(*i=='\n')
387                         ++source_line;
388 }
389
390 void ProgramCompiler::Formatter::append(char c)
391 {
392         formatted += c;
393         if(c=='\n')
394                 ++source_line;
395 }
396
397 void ProgramCompiler::Formatter::set_source(unsigned index, unsigned line)
398 {
399         if(index!=source_index || (index && line!=source_line))
400         {
401                 if(index==source_index && line==source_line+1)
402                         formatted += '\n';
403                 else
404                 {
405                         unsigned l = line;
406                         if(stage->required_version<Version(3, 30))
407                                 --l;
408                         formatted += format("#line %d %d\n", l, index);
409                 }
410         }
411         source_index = index;
412         source_line = line;
413 }
414
415 void ProgramCompiler::Formatter::visit(Literal &literal)
416 {
417         append(literal.token);
418 }
419
420 void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr)
421 {
422         append('(');
423         parexpr.expression->visit(*this);
424         append(')');
425 }
426
427 void ProgramCompiler::Formatter::visit(VariableReference &var)
428 {
429         append(var.name);
430 }
431
432 void ProgramCompiler::Formatter::visit(MemberAccess &memacc)
433 {
434         memacc.left->visit(*this);
435         append(format(".%s", memacc.member));
436 }
437
438 void ProgramCompiler::Formatter::visit(UnaryExpression &unary)
439 {
440         if(unary.prefix)
441                 append(unary.oper);
442         unary.expression->visit(*this);
443         if(!unary.prefix)
444                 append(unary.oper);
445 }
446
447 void ProgramCompiler::Formatter::visit(BinaryExpression &binary)
448 {
449         binary.left->visit(*this);
450         append(binary.oper);
451         binary.right->visit(*this);
452         append(binary.after);
453 }
454
455 void ProgramCompiler::Formatter::visit(Assignment &assign)
456 {
457         assign.left->visit(*this);
458         append(format(" %s ", assign.oper));
459         assign.right->visit(*this);
460 }
461
462 void ProgramCompiler::Formatter::visit(FunctionCall &call)
463 {
464         append(format("%s(", call.name));
465         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
466         {
467                 if(i!=call.arguments.begin())
468                         append(", ");
469                 (*i)->visit(*this);
470         }
471         append(')');
472 }
473
474 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
475 {
476         expr.expression->visit(*this);
477         append(';');
478 }
479
480 void ProgramCompiler::Formatter::visit(Block &block)
481 {
482         unsigned brace_indent = indent;
483         bool use_braces = (block.use_braces || (indent && block.body.size()!=1));
484         if(use_braces)
485                 append(format("%s{\n", string(brace_indent*2, ' ')));
486
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)
490         {
491                 if(i!=block.body.begin())
492                         append('\n');
493                 set_source((*i)->source, (*i)->line);
494                 append(spaces);
495                 (*i)->visit(*this);
496         }
497
498         if(use_braces)
499                 append(format("\n%s}", string(brace_indent*2, ' ')));
500 }
501
502 void ProgramCompiler::Formatter::visit(Import &import)
503 {
504         append(format("import %s;", import.module));
505 }
506
507 void ProgramCompiler::Formatter::visit(Precision &prec)
508 {
509         append(format("precision %s %s;", prec.precision, prec.type));
510 }
511
512 void ProgramCompiler::Formatter::visit(Layout &layout)
513 {
514         append("layout(");
515         for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
516         {
517                 if(i!=layout.qualifiers.begin())
518                         append(", ");
519                 append(i->identifier);
520                 if(!i->value.empty())
521                         append(format("=%s", i->value));
522         }
523         append(')');
524 }
525
526 void ProgramCompiler::Formatter::visit(InterfaceLayout &layout)
527 {
528         layout.layout.visit(*this);
529         append(format(" %s;", layout.interface));
530 }
531
532 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
533 {
534         append(format("struct %s\n", strct.name));
535         strct.members.visit(*this);
536         append(';');
537 }
538
539 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
540 {
541         if(var.layout)
542         {
543                 var.layout->visit(*this);
544                 append(' ');
545         }
546         if(var.constant)
547                 append("const ");
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)
553         {
554                 string interface = var.interface;
555                 if(stage->required_version<Version(1, 30))
556                 {
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";
561                 }
562                 append(format("%s ", interface));
563         }
564         if(!var.precision.empty())
565                 append(format("%s ", var.precision));
566         append(format("%s %s", var.type, var.name));
567         if(var.array)
568         {
569                 append('[');
570                 if(var.array_size)
571                         var.array_size->visit(*this);
572                 append(']');
573         }
574         if(var.init_expression)
575         {
576                 append(" = ");
577                 var.init_expression->visit(*this);
578         }
579         if(!parameter_list)
580                 append(';');
581 }
582
583 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
584 {
585         SetForScope<string> set(block_interface, iface.interface);
586         append(format("%s %s\n", iface.interface, iface.name));
587         iface.members.visit(*this);
588         append(';');
589 }
590
591 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
592 {
593         append(format("%s %s(", func.return_type, func.name));
594         for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
595         {
596                 if(i!=func.parameters.begin())
597                         append(", ");
598                 SetFlag set(parameter_list);
599                 (*i)->visit(*this);
600         }
601         append(')');
602         if(func.definition==&func)
603         {
604                 append('\n');
605                 func.body.visit(*this);
606         }
607         else
608                 append(';');
609 }
610
611 void ProgramCompiler::Formatter::visit(Conditional &cond)
612 {
613         append("if(");
614         cond.condition->visit(*this);
615         append(")\n");
616
617         cond.body.visit(*this);
618         if(!cond.else_body.body.empty())
619         {
620                 Conditional *else_cond = dynamic_cast<Conditional *>(cond.else_body.body.front().get());
621                 if(cond.else_body.body.size()==1 && else_cond)
622                 {
623                         append('\n');
624                         set_source(else_cond->source, else_cond->line);
625                         append(format("%selse ", string(indent*2, ' ')));
626                         else_cond->visit(*this);
627                 }
628                 else
629                 {
630                         append(format("\n%selse\n", string(indent*2, ' ')));
631                         cond.else_body.visit(*this);
632                 }
633         }
634 }
635
636 void ProgramCompiler::Formatter::visit(Iteration &iter)
637 {
638         if(!iter.init_statement && iter.condition && !iter.loop_expression)
639         {
640                 append("while(");
641                 iter.condition->visit(*this);
642                 append(')');
643         }
644         else
645         {
646                 append("for(");
647                 if(iter.init_statement)
648                         iter.init_statement->visit(*this);
649                 else
650                         append(';');
651                 if(iter.condition)
652                 {
653                         append(' ');
654                         iter.condition->visit(*this);
655                 }
656                 append(';');
657                 if(iter.loop_expression)
658                 {
659                         append(' ');
660                         iter.loop_expression->visit(*this);
661                 }
662                 append(')');
663         }
664
665         if(iter.body.body.empty())
666                 append(" { }");
667         else
668         {
669                 append('\n');
670                 iter.body.visit(*this);
671         }
672 }
673
674 void ProgramCompiler::Formatter::visit(Return &ret)
675 {
676         append("return");
677         if(ret.expression)
678         {
679                 append(' ');
680                 ret.expression->visit(*this);
681         }
682         append(';');
683 }
684
685 void ProgramCompiler::Formatter::visit(Jump &jump)
686 {
687         append(jump.keyword);
688         append(';');
689 }
690
691
692 ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
693         toplevel(true)
694 { }
695
696 void ProgramCompiler::DeclarationCombiner::visit(Block &block)
697 {
698         if(!toplevel)
699                 return;
700
701         SetForScope<bool> set(toplevel, false);
702         BlockModifier::visit(block);
703 }
704
705 void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
706 {
707         vector<FunctionDeclaration *> &decls = functions[func.name];
708         if(func.definition)
709         {
710                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
711                 {
712                         (*i)->definition = func.definition;
713                         (*i)->body.body.clear();
714                 }
715         }
716         decls.push_back(&func);
717 }
718
719 void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
720 {
721         VariableDeclaration *&ptr = variables[var.name];
722         if(ptr)
723         {
724                 ptr->type = var.type;
725                 if(var.init_expression)
726                         ptr->init_expression = var.init_expression;
727                 if(var.layout)
728                 {
729                         if(ptr->layout)
730                         {
731                                 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
732                                 {
733                                         bool found = false;
734                                         for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
735                                                 if(j->identifier==i->identifier)
736                                                 {
737                                                         j->value = i->value;
738                                                         found = true;
739                                                 }
740
741                                         if(!found)
742                                                 ptr->layout->qualifiers.push_back(*i);
743                                 }
744                         }
745                         else
746                                 ptr->layout = var.layout;
747                 }
748                 remove_node = true;
749         }
750         else
751                 ptr = &var;
752 }
753
754
755 ProgramCompiler::VariableResolver::VariableResolver():
756         anonymous(false),
757         record_target(false),
758         assignment_target(0),
759         self_referencing(false)
760 { }
761
762 void ProgramCompiler::VariableResolver::apply(Stage &s)
763 {
764         SetForScope<Stage *> set(stage, &s);
765         Stage *builtins = get_builtins(stage->type);
766         if(builtins)
767                 blocks.push_back(&builtins->content);
768         stage->content.visit(*this);
769         if(builtins)
770                 blocks.pop_back();
771 }
772
773 void ProgramCompiler::VariableResolver::visit(Block &block)
774 {
775         blocks.push_back(&block);
776         block.variables.clear();
777         TraversingVisitor::visit(block);
778         blocks.pop_back();
779 }
780
781 void ProgramCompiler::VariableResolver::visit(VariableReference &var)
782 {
783         var.declaration = 0;
784         type = 0;
785         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
786         {
787                 --i;
788                 map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
789                 if(j!=(*i)->variables.end())
790                 {
791                         var.declaration = j->second;
792                         type = j->second->type_declaration;
793                         break;
794                 }
795         }
796
797         if(record_target)
798         {
799                 if(assignment_target)
800                 {
801                         record_target = false;
802                         assignment_target = 0;
803                 }
804                 else
805                         assignment_target = var.declaration;
806         }
807         else if(var.declaration && var.declaration==assignment_target)
808                 self_referencing = true;
809 }
810
811 void ProgramCompiler::VariableResolver::visit(MemberAccess &memacc)
812 {
813         type = 0;
814         TraversingVisitor::visit(memacc);
815         memacc.declaration = 0;
816         if(type)
817         {
818                 map<string, VariableDeclaration *>::iterator i = type->members.variables.find(memacc.member);
819                 if(i!=type->members.variables.end())
820                 {
821                         memacc.declaration = i->second;
822                         type = i->second->type_declaration;
823                 }
824                 else
825                         type = 0;
826         }
827 }
828
829 void ProgramCompiler::VariableResolver::visit(BinaryExpression &binary)
830 {
831         if(binary.oper=="[")
832         {
833                 {
834                         SetForScope<bool> set(record_target, false);
835                         binary.right->visit(*this);
836                 }
837                 type = 0;
838                 binary.left->visit(*this);
839         }
840         else
841         {
842                 TraversingVisitor::visit(binary);
843                 type = 0;
844         }
845 }
846
847 void ProgramCompiler::VariableResolver::visit(Assignment &assign)
848 {
849         {
850                 SetFlag set(record_target);
851                 assignment_target = 0;
852                 assign.left->visit(*this);
853         }
854
855         self_referencing = false;
856         assign.right->visit(*this);
857
858         assign.self_referencing = (self_referencing || assign.oper!="=");
859         assign.target_declaration = assignment_target;
860 }
861
862 void ProgramCompiler::VariableResolver::visit(StructDeclaration &strct)
863 {
864         TraversingVisitor::visit(strct);
865         blocks.back()->types[strct.name] = &strct;
866 }
867
868 void ProgramCompiler::VariableResolver::visit(VariableDeclaration &var)
869 {
870         for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
871         {
872                 --i;
873                 map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
874                 if(j!=(*i)->types.end())
875                         var.type_declaration = j->second;
876         }
877
878         if(!block_interface.empty() && var.interface.empty())
879                 var.interface = block_interface;
880
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;
885 }
886
887 void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
888 {
889         SetFlag set(anonymous);
890         SetForScope<string> set2(block_interface, iface.interface);
891         TraversingVisitor::visit(iface);
892 }
893
894
895 void ProgramCompiler::FunctionResolver::visit(FunctionCall &call)
896 {
897         map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
898         if(i!=functions.end())
899                 call.declaration = i->second.back();
900
901         TraversingVisitor::visit(call);
902 }
903
904 void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
905 {
906         vector<FunctionDeclaration *> &decls = functions[func.name];
907         if(func.definition)
908         {
909                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
910                         (*i)->definition = func.definition;
911                 decls.clear();
912                 decls.push_back(&func);
913         }
914         else if(!decls.empty() && decls.back()->definition)
915                 func.definition = decls.back()->definition;
916         else
917                 decls.push_back(&func);
918
919         TraversingVisitor::visit(func);
920 }
921
922
923 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
924         scope_level(0)
925 { }
926
927 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
928 {
929         if(type==VERTEX)
930                 return "_vs_out_";
931         else if(type==GEOMETRY)
932                 return "_gs_out_";
933         else
934                 return string();
935 }
936
937 void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
938 {
939         SetForScope<Stage *> set(stage, &s);
940         if(stage->previous)
941                 in_prefix = get_out_prefix(stage->previous->type);
942         out_prefix = get_out_prefix(stage->type);
943         stage->content.visit(*this);
944 }
945
946 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
947 {
948         SetForScope<unsigned> set(scope_level, scope_level+1);
949         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
950         {
951                 (*i)->visit(*this);
952
953                 if(scope_level==1)
954                 {
955                         for(map<string, RefPtr<VariableDeclaration> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
956                         {
957                                 NodeList<Statement>::iterator k = block.body.insert(i, j->second);
958                                 (*k)->visit(*this);
959                         }
960                         iface_declarations.clear();
961                 }
962
963                 apply_and_increment(block, i);
964         }
965 }
966
967 string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
968 {
969         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
970         return prefix+name.substr(offset);
971 }
972
973 bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
974 {
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))
977                 return false;
978
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");
987         else
988                 iface_var->array = var.array;
989         if(iface_var->array)
990                 iface_var->array_size = var.array_size;
991         if(iface=="in")
992                 iface_var->linked_declaration = &var;
993         iface_declarations[name] = iface_var;
994
995         return true;
996 }
997
998 ExpressionStatement &ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
999 {
1000         Assignment *assign = new Assignment;
1001         VariableReference *ref = new VariableReference;
1002         ref->name = left;
1003         assign->left = ref;
1004         assign->oper = "=";
1005         assign->right = right;
1006
1007         ExpressionStatement *stmt = new ExpressionStatement;
1008         stmt->expression = assign;
1009         stmt->visit(*this);
1010         insert_nodes.push_back(stmt);
1011
1012         return *stmt;
1013 }
1014
1015 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
1016 {
1017         if(var.declaration || !stage->previous)
1018                 return;
1019         if(iface_declarations.count(var.name))
1020                 return;
1021
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())
1027         {
1028                 generate_interface(*i->second, "in", i->second->name);
1029                 var.name = i->second->name;
1030         }
1031 }
1032
1033 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
1034 {
1035         if(var.interface=="out")
1036         {
1037                 if(scope_level==1)
1038                         stage->out_variables[var.name] = &var;
1039                 else if(generate_interface(var, "out", change_prefix(var.name, string())))
1040                 {
1041                         remove_node = true;
1042                         if(var.init_expression)
1043                         {
1044                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1045                                 stmt.source = var.source;
1046                                 stmt.line = var.line;
1047                                 return;
1048                         }
1049                 }
1050         }
1051         else if(var.interface=="in")
1052         {
1053                 stage->in_variables[var.name] = &var;
1054                 if(var.linked_declaration)
1055                         var.linked_declaration->linked_declaration = &var;
1056                 else if(stage->previous)
1057                 {
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())
1061                         {
1062                                 var.linked_declaration = i->second;
1063                                 i->second->linked_declaration = &var;
1064                         }
1065                 }
1066         }
1067
1068         TraversingVisitor::visit(var);
1069 }
1070
1071 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
1072 {
1073         vector<VariableDeclaration *> pass_vars;
1074
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());
1080
1081         if(stage->previous)
1082         {
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)
1085                 {
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);
1089
1090                         if(!linked && generate_interface(*i->second, "in", i->second->name))
1091                                 pass_vars.push_back(i->second);
1092                 }
1093         }
1094
1095         if(stage->type==GEOMETRY)
1096         {
1097                 VariableReference *ref = new VariableReference;
1098                 ref->name = "gl_in";
1099
1100                 BinaryExpression *subscript = new BinaryExpression;
1101                 subscript->left = ref;
1102                 subscript->oper = "[";
1103                 subscript->right = pass.subscript;
1104                 subscript->after = "]";
1105
1106                 MemberAccess *memacc = new MemberAccess;
1107                 memacc->left = subscript;
1108                 memacc->member = "gl_Position";
1109
1110                 insert_assignment("gl_Position", memacc);
1111         }
1112
1113         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1114         {
1115                 string out_name = change_prefix((*i)->name, out_prefix);
1116                 generate_interface(**i, "out", out_name);
1117
1118                 VariableReference *ref = new VariableReference;
1119                 ref->name = (*i)->name;
1120                 if(pass.subscript)
1121                 {
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);
1128                 }
1129                 else
1130                         insert_assignment(out_name, ref);
1131         }
1132
1133         remove_node = true;
1134 }
1135
1136
1137 ProgramCompiler::DeclarationReorderer::DeclarationReorderer():
1138         scope_level(0),
1139         kind(NO_DECLARATION)
1140 { }
1141
1142 void ProgramCompiler::DeclarationReorderer::visit(FunctionCall &call)
1143 {
1144         FunctionDeclaration *def = call.declaration;
1145         if(def)
1146                 def = def->definition;
1147         if(def && !ordered_funcs.count(def))
1148                 needed_funcs.insert(def);
1149 }
1150
1151 void ProgramCompiler::DeclarationReorderer::visit(Block &block)
1152 {
1153         SetForScope<unsigned> set(scope_level, scope_level+1);
1154         if(scope_level>1)
1155                 return Visitor::visit(block);
1156
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;
1162
1163         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
1164         {
1165                 kind = NO_DECLARATION;
1166                 (*i)->visit(*this);
1167
1168                 bool moved = false;
1169                 if(kind==STRUCT && struct_insert_point!=block.body.end())
1170                 {
1171                         block.body.insert(struct_insert_point, *i);
1172                         moved = true;
1173                 }
1174                 else if(kind>STRUCT && struct_insert_point==block.body.end())
1175                         struct_insert_point = i;
1176
1177                 if(kind==VARIABLE && variable_insert_point!=block.body.end())
1178                 {
1179                         block.body.insert(variable_insert_point, *i);
1180                         moved = true;
1181                 }
1182                 else if(kind>VARIABLE && variable_insert_point==block.body.end())
1183                         variable_insert_point = i;
1184
1185                 if(kind==FUNCTION)
1186                 {
1187                         if(function_insert_point==block.body.end())
1188                                 function_insert_point = i;
1189
1190                         if(needed_funcs.empty())
1191                         {
1192                                 ordered_funcs.insert(i->get());
1193                                 if(i!=function_insert_point)
1194                                 {
1195                                         block.body.insert(function_insert_point, *i);
1196                                         moved = true;
1197                                 }
1198                                 else
1199                                         ++function_insert_point;
1200                                 ordered_any_funcs = true;
1201                         }
1202                         else
1203                                 ++unordered_func_count;
1204                 }
1205
1206                 if(moved)
1207                 {
1208                         if(function_insert_point==i)
1209                                 ++function_insert_point;
1210                         block.body.erase(i++);
1211                 }
1212                 else
1213                         ++i;
1214
1215                 if(i==block.body.end() && unordered_func_count)
1216                 {
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
1220                                 declarations */
1221                                 break;
1222
1223                         i = function_insert_point;
1224                         unordered_func_count = 0;
1225                 }
1226         }
1227 }
1228
1229 void ProgramCompiler::DeclarationReorderer::visit(ProgramSyntax::VariableDeclaration &var)
1230 {
1231         Visitor::visit(var);
1232         kind = VARIABLE;
1233 }
1234
1235 void ProgramCompiler::DeclarationReorderer::visit(FunctionDeclaration &func)
1236 {
1237         needed_funcs.clear();
1238         func.body.visit(*this);
1239         needed_funcs.erase(&func);
1240         kind = FUNCTION;
1241 }
1242
1243
1244 ProgramCompiler::InlineableFunctionLocator::InlineableFunctionLocator():
1245         in_function(0)
1246 { }
1247
1248 void ProgramCompiler::InlineableFunctionLocator::visit(FunctionCall &call)
1249 {
1250         FunctionDeclaration *def = call.declaration;
1251         if(def && def->definition!=def)
1252                 def = def->definition;
1253
1254         if(def)
1255         {
1256                 unsigned &count = refcounts[def];
1257                 ++count;
1258                 if(count>1 || def==in_function)
1259                         inlineable.erase(def);
1260         }
1261
1262         TraversingVisitor::visit(call);
1263 }
1264
1265 void ProgramCompiler::InlineableFunctionLocator::visit(FunctionDeclaration &func)
1266 {
1267         unsigned &count = refcounts[func.definition];
1268         if(!count && func.parameters.empty())
1269                 inlineable.insert(func.definition);
1270
1271         SetForScope<FunctionDeclaration *> set(in_function, &func);
1272         TraversingVisitor::visit(func);
1273 }
1274
1275
1276 ProgramCompiler::FunctionInliner::FunctionInliner():
1277         extract_result(0)
1278 { }
1279
1280 ProgramCompiler::FunctionInliner::FunctionInliner(const set<FunctionDeclaration *> &in):
1281         inlineable(in),
1282         extract_result(0)
1283 { }
1284
1285 void ProgramCompiler::FunctionInliner::visit_and_inline(RefPtr<Expression> &ptr)
1286 {
1287         inline_result = 0;
1288         ptr->visit(*this);
1289         if(inline_result)
1290                 ptr = inline_result;
1291 }
1292
1293 void ProgramCompiler::FunctionInliner::visit(Block &block)
1294 {
1295         if(extract_result)
1296                 --extract_result;
1297
1298         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
1299         {
1300                 (*i)->visit(*this);
1301                 if(extract_result)
1302                         --extract_result;
1303         }
1304 }
1305
1306 void ProgramCompiler::FunctionInliner::visit(UnaryExpression &unary)
1307 {
1308         visit_and_inline(unary.expression);
1309         inline_result = 0;
1310 }
1311
1312 void ProgramCompiler::FunctionInliner::visit(BinaryExpression &binary)
1313 {
1314         visit_and_inline(binary.left);
1315         visit_and_inline(binary.right);
1316         inline_result = 0;
1317 }
1318
1319 void ProgramCompiler::FunctionInliner::visit(MemberAccess &memacc)
1320 {
1321         visit_and_inline(memacc.left);
1322         inline_result = 0;
1323 }
1324
1325 void ProgramCompiler::FunctionInliner::visit(FunctionCall &call)
1326 {
1327         for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
1328                 visit_and_inline(*i);
1329
1330         FunctionDeclaration *def = call.declaration;
1331         if(def && def->definition!=def)
1332                 def = def->definition;
1333
1334         if(def && inlineable.count(def))
1335         {
1336                 extract_result = 2;
1337                 def->visit(*this);
1338         }
1339         else
1340                 inline_result = 0;
1341 }
1342
1343 void ProgramCompiler::FunctionInliner::visit(VariableDeclaration &var)
1344 {
1345         if(var.init_expression)
1346                 visit_and_inline(var.init_expression);
1347         inline_result = 0;
1348 }
1349
1350 void ProgramCompiler::FunctionInliner::visit(Return &ret)
1351 {
1352         TraversingVisitor::visit(ret);
1353
1354         if(extract_result)
1355                 inline_result = ret.expression->clone();
1356 }
1357
1358
1359 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator():
1360         variable_values(0),
1361         result(0.0f),
1362         result_valid(false)
1363 { }
1364
1365 ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator(const ValueMap &v):
1366         variable_values(&v),
1367         result(0.0f),
1368         result_valid(false)
1369 { }
1370
1371 void ProgramCompiler::ExpressionEvaluator::visit(Literal &literal)
1372 {
1373         if(literal.token=="true")
1374                 result = 1.0f;
1375         else if(literal.token=="false")
1376                 result = 0.0f;
1377         else
1378                 result = lexical_cast<float>(literal.token);
1379         result_valid = true;
1380 }
1381
1382 void ProgramCompiler::ExpressionEvaluator::visit(ParenthesizedExpression &parexp)
1383 {
1384         parexp.expression->visit(*this);
1385 }
1386
1387 void ProgramCompiler::ExpressionEvaluator::visit(VariableReference &var)
1388 {
1389         if(!var.declaration)
1390                 return;
1391
1392         if(variable_values)
1393         {
1394                 ValueMap::const_iterator i = variable_values->find(var.declaration);
1395                 if(i!=variable_values->end())
1396                         i->second->visit(*this);
1397         }
1398         else if(var.declaration->init_expression)
1399                 var.declaration->init_expression->visit(*this);
1400 }
1401
1402 void ProgramCompiler::ExpressionEvaluator::visit(UnaryExpression &unary)
1403 {
1404         result_valid = false;
1405         unary.expression->visit(*this);
1406         if(!result_valid)
1407                 return;
1408
1409         if(unary.oper=="!")
1410                 result = !result;
1411         else
1412                 result_valid = false;
1413 }
1414
1415 void ProgramCompiler::ExpressionEvaluator::visit(BinaryExpression &binary)
1416 {
1417         result_valid = false;
1418         binary.left->visit(*this);
1419         if(!result_valid)
1420                 return;
1421
1422         float left_result = result;
1423         result_valid = false;
1424         binary.right->visit(*this);
1425         if(!result_valid)
1426                 return;
1427
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);
1444         else
1445                 result_valid = false;
1446 }
1447
1448
1449 ProgramCompiler::ConstantConditionEliminator::ConstantConditionEliminator():
1450         scope_level(0),
1451         record_only(false)
1452 { }
1453
1454 void ProgramCompiler::ConstantConditionEliminator::visit(Block &block)
1455 {
1456         SetForScope<unsigned> set(scope_level, scope_level+1);
1457         BlockModifier::visit(block);
1458
1459         for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
1460                 variable_values.erase(i->second);
1461 }
1462
1463 void ProgramCompiler::ConstantConditionEliminator::visit(UnaryExpression &unary)
1464 {
1465         if(VariableReference *var = dynamic_cast<VariableReference *>(unary.expression.get()))
1466                 if(unary.oper=="++" || unary.oper=="--")
1467                         variable_values.erase(var->declaration);
1468 }
1469
1470 void ProgramCompiler::ConstantConditionEliminator::visit(Assignment &assign)
1471 {
1472         variable_values.erase(assign.target_declaration);
1473 }
1474
1475 void ProgramCompiler::ConstantConditionEliminator::visit(VariableDeclaration &var)
1476 {
1477         if(var.constant || scope_level>1)
1478                 variable_values[&var] = var.init_expression.get();
1479 }
1480
1481 void ProgramCompiler::ConstantConditionEliminator::visit(Conditional &cond)
1482 {
1483         if(!record_only)
1484         {
1485                 ExpressionEvaluator eval(variable_values);
1486                 cond.condition->visit(eval);
1487                 if(eval.result_valid)
1488                 {
1489                         flatten_block(eval.result ? cond.body : cond.else_body);
1490                         return;
1491                 }
1492         }
1493
1494         TraversingVisitor::visit(cond);
1495 }
1496
1497 void ProgramCompiler::ConstantConditionEliminator::visit(Iteration &iter)
1498 {
1499         if(!record_only)
1500         {
1501                 if(iter.condition)
1502                 {
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)
1510                         {
1511                                 remove_node = true;
1512                                 return;
1513                         }
1514                 }
1515
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);
1520         }
1521
1522         TraversingVisitor::visit(iter);
1523
1524         if(VariableDeclaration *init_decl = dynamic_cast<VariableDeclaration *>(iter.init_statement.get()))
1525                 variable_values.erase(init_decl);
1526 }
1527
1528
1529 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
1530         aggregate(0),
1531         assignment(0),
1532         assignment_target(false),
1533         assign_to_subscript(false),
1534         global_scope(true)
1535 { }
1536
1537 void ProgramCompiler::UnusedVariableLocator::apply(Stage &s)
1538 {
1539         variables.push_back(BlockVariableMap());
1540         Visitor::apply(s);
1541         BlockVariableMap &global_variables = variables.back();
1542         for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
1543         {
1544                 if(i->first->interface=="out" && (s.type==FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
1545                         continue;
1546                 if(!i->second.referenced)
1547                 {
1548                         unused_nodes.insert(i->first);
1549                         clear_assignments(i->second, true);
1550                 }
1551         }
1552         variables.pop_back();
1553 }
1554
1555 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
1556 {
1557         map<VariableDeclaration *, Node *>::iterator i = aggregates.find(var.declaration);
1558         if(i!=aggregates.end())
1559                 unused_nodes.erase(i->second);
1560
1561         if(var.declaration && !assignment_target)
1562         {
1563                 VariableInfo &var_info = variables.back()[var.declaration];
1564                 var_info.assignments.clear();
1565                 var_info.referenced = true;
1566         }
1567 }
1568
1569 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
1570 {
1571         TraversingVisitor::visit(memacc);
1572         unused_nodes.erase(memacc.declaration);
1573 }
1574
1575 void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
1576 {
1577         if(binary.oper=="[")
1578         {
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);
1584         }
1585         else
1586                 TraversingVisitor::visit(binary);
1587 }
1588
1589 void ProgramCompiler::UnusedVariableLocator::visit(Assignment &assign)
1590 {
1591         {
1592                 assign_to_subscript = false;
1593                 SetForScope<bool> set(assignment_target, !assign.self_referencing);
1594                 assign.left->visit(*this);
1595         }
1596         assign.right->visit(*this);
1597         assignment = &assign;
1598 }
1599
1600 void ProgramCompiler::UnusedVariableLocator::record_assignment(VariableDeclaration &var, Node &node, bool chained)
1601 {
1602         VariableInfo &var_info = variables.back()[&var];
1603         if(!chained)
1604                 clear_assignments(var_info, true);
1605         var_info.assignments.push_back(&node);
1606         var_info.conditionally_assigned = false;
1607 }
1608
1609 void ProgramCompiler::UnusedVariableLocator::clear_assignments(VariableInfo &var_info, bool mark_unused)
1610 {
1611         if(mark_unused)
1612         {
1613                 for(vector<Node *>::iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
1614                         unused_nodes.insert(*i);
1615         }
1616         var_info.assignments.clear();
1617 }
1618
1619 void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
1620 {
1621         assignment = 0;
1622         TraversingVisitor::visit(expr);
1623         if(assignment && assignment->target_declaration)
1624                 record_assignment(*assignment->target_declaration, expr, (assignment->self_referencing || assign_to_subscript));
1625 }
1626
1627 void ProgramCompiler::UnusedVariableLocator::visit(StructDeclaration &strct)
1628 {
1629         SetForScope<Node *> set(aggregate, &strct);
1630         unused_nodes.insert(&strct);
1631         TraversingVisitor::visit(strct);
1632 }
1633
1634 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
1635 {
1636         if(aggregate)
1637                 aggregates[&var] = aggregate;
1638         else
1639         {
1640                 variables.back()[&var].local = true;
1641                 if(var.init_expression)
1642                         record_assignment(var, *var.init_expression, false);
1643         }
1644         unused_nodes.erase(var.type_declaration);
1645         TraversingVisitor::visit(var);
1646 }
1647
1648 void ProgramCompiler::UnusedVariableLocator::visit(InterfaceBlock &iface)
1649 {
1650         SetForScope<Node *> set(aggregate, &iface);
1651         unused_nodes.insert(&iface);
1652         TraversingVisitor::visit(iface);
1653 }
1654
1655 void ProgramCompiler::UnusedVariableLocator::visit(FunctionDeclaration &func)
1656 {
1657         variables.push_back(BlockVariableMap());
1658
1659         {
1660                 SetForScope<bool> set(global_scope, false);
1661                 for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
1662                         (*i)->visit(*this);
1663                 func.body.visit(*this);
1664         }
1665
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();
1672 }
1673
1674 void ProgramCompiler::UnusedVariableLocator::merge_down_variables()
1675 {
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)
1679         {
1680                 if(i->second.local)
1681                 {
1682                         if(!i->second.referenced)
1683                                 unused_nodes.insert(i->first);
1684                         clear_assignments(i->second, i->first->interface!="out");
1685                         continue;
1686                 }
1687
1688                 BlockVariableMap::iterator j = parent_variables.find(i->first);
1689                 if(j==parent_variables.end())
1690                         parent_variables.insert(*i);
1691                 else
1692                 {
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());
1698                 }
1699         }
1700         variables.pop_back();
1701 }
1702
1703 void ProgramCompiler::UnusedVariableLocator::visit(Conditional &cond)
1704 {
1705         cond.condition->visit(*this);
1706         variables.push_back(BlockVariableMap());
1707         cond.body.visit(*this);
1708
1709         BlockVariableMap if_variables;
1710         swap(variables.back(), if_variables);
1711         cond.else_body.visit(*this);
1712
1713         BlockVariableMap &else_variables = variables.back();
1714         for(BlockVariableMap::iterator i=else_variables.begin(); i!=else_variables.end(); ++i)
1715         {
1716                 BlockVariableMap::iterator j = if_variables.find(i->first);
1717                 if(j!=if_variables.end())
1718                 {
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);
1722                 }
1723                 else
1724                         i->second.conditionally_assigned = true;
1725         }
1726
1727         for(BlockVariableMap::iterator i=if_variables.begin(); i!=if_variables.end(); ++i)
1728         {
1729                 i->second.conditionally_assigned = true;
1730                 else_variables.insert(*i);
1731         }
1732
1733         merge_down_variables();
1734 }
1735
1736 void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter)
1737 {
1738         variables.push_back(BlockVariableMap());
1739         TraversingVisitor::visit(iter);
1740
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();
1745
1746         merge_down_variables();
1747 }
1748
1749
1750 ProgramCompiler::UnusedVariableLocator::VariableInfo::VariableInfo():
1751         local(false),
1752         conditionally_assigned(false),
1753         referenced(false)
1754 { }
1755
1756
1757 void ProgramCompiler::UnusedFunctionLocator::visit(FunctionCall &call)
1758 {
1759         TraversingVisitor::visit(call);
1760
1761         unused_nodes.erase(call.declaration);
1762         if(call.declaration && call.declaration->definition!=call.declaration)
1763                 used_definitions.insert(call.declaration->definition);
1764 }
1765
1766 void ProgramCompiler::UnusedFunctionLocator::visit(FunctionDeclaration &func)
1767 {
1768         TraversingVisitor::visit(func);
1769
1770         if((func.name!="main" || func.body.body.empty()) && !used_definitions.count(&func))
1771                 unused_nodes.insert(&func);
1772 }
1773
1774
1775 ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
1776         to_remove(r)
1777 { }
1778
1779 void ProgramCompiler::NodeRemover::visit(Block &block)
1780 {
1781         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
1782         {
1783                 (*i)->visit(*this);
1784                 if(to_remove.count(i->get()))
1785                         block.body.erase(i++);
1786                 else
1787                         ++i;
1788         }
1789 }
1790
1791 void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
1792 {
1793         if(to_remove.count(&var))
1794         {
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;
1800         }
1801         else if(var.init_expression && to_remove.count(var.init_expression.get()))
1802                 var.init_expression = 0;
1803 }
1804
1805 void ProgramCompiler::NodeRemover::visit(Iteration &iter)
1806 {
1807         if(to_remove.count(iter.init_statement.get()))
1808                 iter.init_statement = 0;
1809         TraversingVisitor::visit(iter);
1810 }
1811
1812
1813 void ProgramCompiler::PrecisionRemover::visit(Precision &)
1814 {
1815         remove_node = true;
1816 }
1817
1818 void ProgramCompiler::PrecisionRemover::visit(VariableDeclaration &var)
1819 {
1820         var.precision.clear();
1821 }
1822
1823
1824 ProgramCompiler::DefaultPrecisionGenerator::DefaultPrecisionGenerator():
1825         toplevel(true)
1826 { }
1827
1828 void ProgramCompiler::DefaultPrecisionGenerator::visit(Block &block)
1829 {
1830         if(toplevel)
1831         {
1832                 SetForScope<bool> set(toplevel, false);
1833                 BlockModifier::visit(block);
1834         }
1835         else
1836                 Visitor::visit(block);
1837 }
1838
1839 void ProgramCompiler::DefaultPrecisionGenerator::visit(Precision &prec)
1840 {
1841         have_default.insert(prec.type);
1842 }
1843
1844 void ProgramCompiler::DefaultPrecisionGenerator::visit(VariableDeclaration &var)
1845 {
1846         if(var.type_declaration)
1847                 return;
1848
1849         string type = var.type;
1850         if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
1851                 type = "float";
1852         else if(!type.compare(0, 3, "ivec") || type=="uint")
1853                 type = "int";
1854
1855         if(!have_default.count(type))
1856         {
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";
1862                 else
1863                         prec->precision = "highp";
1864                 prec->type = type;
1865                 insert_nodes.push_back(prec);
1866
1867                 have_default.insert(type);
1868         }
1869 }
1870
1871
1872 ProgramCompiler::LegacyConverter::LegacyConverter():
1873         target_api(get_gl_api()),
1874         target_version(get_glsl_version()),
1875         frag_out(0)
1876 { }
1877
1878 ProgramCompiler::LegacyConverter::LegacyConverter(const Version &v):
1879         target_api(get_gl_api()),
1880         target_version(v),
1881         frag_out(0)
1882 { }
1883
1884 bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_version) const
1885 {
1886         if(target_version<feature_version)
1887                 return false;
1888         else if(stage->required_version<feature_version)
1889                 stage->required_version = feature_version;
1890
1891         return true;
1892 }
1893
1894 bool ProgramCompiler::LegacyConverter::check_extension(const Extension &extension) const
1895 {
1896         if(!extension)
1897                 return false;
1898
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);
1902
1903         return true;
1904 }
1905
1906 bool ProgramCompiler::LegacyConverter::supports_unified_interface_syntax() const
1907 {
1908         if(target_api==OPENGL_ES2)
1909                 return check_version(Version(3, 0));
1910         else
1911                 return check_version(Version(1, 30));
1912 }
1913
1914 void ProgramCompiler::LegacyConverter::visit(VariableReference &var)
1915 {
1916         if(var.declaration==frag_out && !supports_unified_interface_syntax())
1917         {
1918                 var.name = "gl_FragColor";
1919                 var.declaration = 0;
1920                 type = "vec4";
1921         }
1922         else if(var.declaration)
1923                 type = var.declaration->type;
1924         else
1925                 type = string();
1926 }
1927
1928 void ProgramCompiler::LegacyConverter::visit(Assignment &assign)
1929 {
1930         TraversingVisitor::visit(assign);
1931         if(assign.target_declaration==frag_out && !supports_unified_interface_syntax())
1932                 assign.target_declaration = 0;
1933 }
1934
1935 bool ProgramCompiler::LegacyConverter::supports_unified_sampling_functions() const
1936 {
1937         if(target_api==OPENGL_ES2)
1938                 return check_version(Version(3, 0));
1939         else
1940                 return check_version(Version(1, 30));
1941 }
1942
1943 void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
1944 {
1945         if(call.name=="texture" && !call.declaration && !supports_unified_sampling_functions())
1946         {
1947                 NodeArray<Expression>::iterator i = call.arguments.begin();
1948                 if(i!=call.arguments.end())
1949                 {
1950                         (*i)->visit(*this);
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")
1964                         {
1965                                 check_extension(EXT_texture_array);
1966                                 call.name = "texture1DArray";
1967                         }
1968                         else if(type=="sampler2DArray")
1969                         {
1970                                 check_extension(EXT_texture_array);
1971                                 call.name = "texture2DArray";
1972                         }
1973                         else if(type=="sampler1DArrayShadow")
1974                         {
1975                                 check_extension(EXT_texture_array);
1976                                 call.name = "shadow1DArray";
1977                         }
1978                         else if(type=="sampler2DArrayShadow")
1979                         {
1980                                 check_extension(EXT_texture_array);
1981                                 call.name = "shadow2DArray";
1982                         }
1983
1984                         for(; i!=call.arguments.end(); ++i)
1985                                 (*i)->visit(*this);
1986                 }
1987         }
1988         else
1989                 TraversingVisitor::visit(call);
1990 }
1991
1992 bool ProgramCompiler::LegacyConverter::supports_interface_layouts() const
1993 {
1994         if(target_api==OPENGL_ES2)
1995                 return check_version(Version(3, 0));
1996         else if(check_version(Version(3, 30)))
1997                 return true;
1998         else
1999                 return check_extension(ARB_explicit_attrib_location);
2000 }
2001
2002 bool ProgramCompiler::LegacyConverter::supports_centroid_sampling() const
2003 {
2004         if(target_api==OPENGL_ES2)
2005                 return check_version(Version(3, 0));
2006         else if(check_version(Version(1, 20)))
2007                 return true;
2008         else
2009                 return check_extension(EXT_gpu_shader4);
2010 }
2011
2012 bool ProgramCompiler::LegacyConverter::supports_sample_sampling() const
2013 {
2014         if(target_api==OPENGL_ES2)
2015                 return check_version(Version(3, 20));
2016         else if(check_version(Version(4, 0)))
2017                 return true;
2018         else
2019                 return check_extension(ARB_gpu_shader5);
2020 }
2021
2022 void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
2023 {
2024         if(var.layout && !supports_interface_layouts())
2025         {
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())
2029                 {
2030                         unsigned location = lexical_cast<unsigned>(i->value);
2031                         if(stage->type==VERTEX && var.interface=="in")
2032                         {
2033                                 stage->locations[var.name] = location;
2034                                 var.layout->qualifiers.erase(i);
2035                         }
2036                         else if(stage->type==FRAGMENT && var.interface=="out")
2037                         {
2038                                 if(location!=0)
2039                                         static Require _req(EXT_gpu_shader4);
2040                                 stage->locations[var.name] = location;
2041                                 var.layout->qualifiers.erase(i);
2042                         }
2043
2044                         if(var.layout->qualifiers.empty())
2045                                 var.layout = 0;
2046                 }
2047         }
2048
2049         if(var.sampling=="centroid")
2050         {
2051                 if(!supports_centroid_sampling())
2052                         var.sampling = string();
2053         }
2054         else if(var.sampling=="sample")
2055         {
2056                 if(!supports_sample_sampling())
2057                         var.sampling = string();
2058         }
2059
2060         if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
2061         {
2062                 if(stage->type==FRAGMENT && var.interface=="out")
2063                 {
2064                         frag_out = &var;
2065                         remove_node = true;
2066                 }
2067         }
2068
2069         TraversingVisitor::visit(var);
2070 }
2071
2072 bool ProgramCompiler::LegacyConverter::supports_interface_blocks(const string &iface) const
2073 {
2074         if(target_api==OPENGL_ES2)
2075         {
2076                 if(iface=="uniform")
2077                         return check_version(Version(3, 0));
2078                 else
2079                         return check_version(Version(3, 20));
2080         }
2081         else if(check_version(Version(1, 50)))
2082                 return true;
2083         else if(iface=="uniform")
2084                 return check_extension(ARB_uniform_buffer_object);
2085         else
2086                 return false;
2087 }
2088
2089 void ProgramCompiler::LegacyConverter::visit(InterfaceBlock &iface)
2090 {
2091         if(!supports_interface_blocks(iface.interface))
2092                 flatten_block(iface.members);
2093 }
2094
2095 } // namespace GL
2096 } // namespace Msp