]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Clear the stage map at the beginning if a resolving pass
[libs/gl.git] / source / glsl / generate.cpp
1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
4 #include "builtin.h"
5 #include "generate.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11 namespace SL {
12
13 void DeclarationCombiner::apply(Stage &stage)
14 {
15         stage.content.visit(*this);
16         NodeRemover().apply(stage, nodes_to_remove);
17 }
18
19 void DeclarationCombiner::visit(Block &block)
20 {
21         if(current_block)
22                 return;
23
24         TraversingVisitor::visit(block);
25 }
26
27 void DeclarationCombiner::visit(VariableDeclaration &var)
28 {
29         VariableDeclaration *&ptr = variables[var.name];
30         if(ptr)
31         {
32                 ptr->type = var.type;
33                 if(var.init_expression)
34                         ptr->init_expression = var.init_expression;
35                 if(var.layout)
36                 {
37                         if(ptr->layout)
38                         {
39                                 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
40                                 {
41                                         bool found = false;
42                                         for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
43                                                 if(j->name==i->name)
44                                                 {
45                                                         j->has_value = i->value;
46                                                         j->value = i->value;
47                                                         found = true;
48                                                 }
49
50                                         if(!found)
51                                                 ptr->layout->qualifiers.push_back(*i);
52                                 }
53                         }
54                         else
55                                 ptr->layout = var.layout;
56                 }
57                 nodes_to_remove.insert(&var);
58         }
59         else
60                 ptr = &var;
61 }
62
63
64 ConstantSpecializer::ConstantSpecializer():
65         values(0)
66 { }
67
68 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
69 {
70         values = v;
71         stage.content.visit(*this);
72 }
73
74 void ConstantSpecializer::visit(VariableDeclaration &var)
75 {
76         bool specializable = false;
77         if(var.layout)
78         {
79                 vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
80                 for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
81                         if(i->name=="constant_id")
82                         {
83                                 specializable = true;
84                                 if(values)
85                                         qualifiers.erase(i);
86                                 else if(i->value==-1)
87                                         i->value = hash32(var.name)&0x7FFFFFFF;
88                                 break;
89                         }
90
91                 if(qualifiers.empty())
92                         var.layout = 0;
93         }
94
95         if(specializable && values)
96         {
97                 map<string, int>::const_iterator i = values->find(var.name);
98                 if(i!=values->end())
99                 {
100                         RefPtr<Literal> literal = new Literal;
101                         if(var.type=="bool")
102                                 literal->token = (i->second ? "true" : "false");
103                         else if(var.type=="int")
104                                 literal->token = lexical_cast<string>(i->second);
105                         var.init_expression = literal;
106                 }
107         }
108 }
109
110
111 void BlockHierarchyResolver::enter(Block &block)
112 {
113         block.parent = current_block;
114 }
115
116
117 VariableResolver::VariableResolver():
118         stage(0),
119         builtins(0),
120         members(0),
121         record_target(false),
122         assignment_target(0),
123         self_referencing(false)
124 { }
125
126 void VariableResolver::apply(Stage &s)
127 {
128         stage = &s;
129         s.types.clear();
130         s.interface_blocks.clear();
131         Stage *builtin_stage = get_builtins(s.type);
132         builtins = (builtin_stage ? &builtin_stage->content : 0);
133         s.content.visit(*this);
134 }
135
136 Block *VariableResolver::next_block(Block &block)
137 {
138         return block.parent ? block.parent : &block!=builtins ? builtins : 0;
139 }
140
141 void VariableResolver::enter(Block &block)
142 {
143         block.variables.clear();
144 }
145
146 void VariableResolver::visit(VariableReference &var)
147 {
148         var.declaration = 0;
149         members = 0;
150         for(Block *block=current_block; (!var.declaration && block); block=next_block(*block))
151         {
152                 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
153                 if(i!=block->variables.end())
154                         var.declaration = i->second;
155         }
156
157         if(var.declaration)
158         {
159                 if(var.declaration->type_declaration)
160                         members = &var.declaration->type_declaration->members.variables;
161         }
162         else
163         {
164                 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
165                 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
166                 if(i!=blocks.end() && i->second->instance_name==var.name)
167                 {
168                         iface_ref = new InterfaceBlockReference;
169                         iface_ref->name = var.name;
170                         iface_ref->declaration = i->second;
171                         members = &i->second->members.variables;
172                 }
173                 else
174                 {
175                         for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
176                                 if(i->second->instance_name.empty())
177                                 {
178                                         map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
179                                         if(j!=i->second->members.variables.end())
180                                                 var.declaration = j->second;
181                                 }
182                 }
183         }
184
185         if(record_target)
186         {
187                 if(assignment_target)
188                 {
189                         record_target = false;
190                         assignment_target = 0;
191                 }
192                 else
193                         assignment_target = var.declaration;
194         }
195         else if(var.declaration && var.declaration==assignment_target)
196                 self_referencing = true;
197 }
198
199 void VariableResolver::visit(InterfaceBlockReference &iface)
200 {
201         iface.declaration = 0;
202         for(Block *block=current_block; block; block=next_block(*block))
203         {
204                 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
205                 if(i!=stage->interface_blocks.end())
206                 {
207                         iface.declaration = i->second;
208                         members = &i->second->members.variables;
209                         break;
210                 }
211         }
212 }
213
214 void VariableResolver::visit(MemberAccess &memacc)
215 {
216         members = 0;
217         iface_ref = 0;
218         memacc.left->visit(*this);
219
220         if(iface_ref)
221                 memacc.left = iface_ref;
222         iface_ref = 0;
223
224         memacc.declaration = 0;
225         if(members)
226         {
227                 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
228                 if(i!=members->end())
229                 {
230                         memacc.declaration = i->second;
231                         if(i->second->type_declaration)
232                                 members = &i->second->type_declaration->members.variables;
233                 }
234                 else
235                         members = 0;
236         }
237 }
238
239 void VariableResolver::visit(BinaryExpression &binary)
240 {
241         if(binary.oper=="[")
242         {
243                 {
244                         SetForScope<bool> set(record_target, false);
245                         binary.right->visit(*this);
246                 }
247                 members = 0;
248                 iface_ref = 0;
249                 binary.left->visit(*this);
250                 if(iface_ref)
251                         binary.left = iface_ref;
252                 iface_ref = 0;
253         }
254         else
255         {
256                 TraversingVisitor::visit(binary);
257                 members = 0;
258         }
259 }
260
261 void VariableResolver::visit(Assignment &assign)
262 {
263         {
264                 SetFlag set(record_target);
265                 assignment_target = 0;
266                 assign.left->visit(*this);
267         }
268
269         self_referencing = false;
270         assign.right->visit(*this);
271
272         assign.self_referencing = (self_referencing || assign.oper!="=");
273         assign.target_declaration = assignment_target;
274 }
275
276 void VariableResolver::visit(StructDeclaration &strct)
277 {
278         TraversingVisitor::visit(strct);
279         stage->types[strct.name] = &strct;
280 }
281
282 void VariableResolver::visit(VariableDeclaration &var)
283 {
284         map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
285         if(i!=stage->types.end())
286                 var.type_declaration = i->second;
287
288         if(!block_interface.empty() && var.interface.empty())
289                 var.interface = block_interface;
290
291         TraversingVisitor::visit(var);
292         current_block->variables[var.name] = &var;
293 }
294
295 void VariableResolver::visit(InterfaceBlock &iface)
296 {
297         /* Block names can't be used for any other identifiers so we can put them
298         in the same map with instance names. */
299         stage->interface_blocks[iface.name] = &iface;
300         if(!iface.instance_name.empty())
301                 stage->interface_blocks[iface.instance_name] = &iface;
302
303         SetForScope<string> set_iface(block_interface, iface.interface);
304         TraversingVisitor::visit(iface);
305 }
306
307
308 void FunctionResolver::apply(Stage &s)
309 {
310         stage = &s;
311         s.functions.clear();
312         s.content.visit(*this);
313 }
314
315 void FunctionResolver::visit(FunctionCall &call)
316 {
317         map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
318         if(i!=stage->functions.end())
319                 call.declaration = i->second;
320
321         TraversingVisitor::visit(call);
322 }
323
324 void FunctionResolver::visit(FunctionDeclaration &func)
325 {
326         FunctionDeclaration *&stage_decl = stage->functions[func.name];
327         vector<FunctionDeclaration *> &decls = declarations[func.name];
328         if(func.definition==&func)
329         {
330                 stage_decl = &func;
331
332                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
333                 {
334                         (*i)->definition = func.definition;
335                         (*i)->body.body.clear();
336                 }
337         }
338         else
339         {
340                 if(!stage_decl)
341                         stage_decl = &func;
342
343                 func.definition = stage_decl->definition;
344         }
345         decls.push_back(&func);
346
347         TraversingVisitor::visit(func);
348 }
349
350
351 InterfaceGenerator::InterfaceGenerator():
352         stage(0),
353         function_scope(false),
354         iface_block(0),
355         copy_block(false),
356         iface_target_block(0)
357 { }
358
359 string InterfaceGenerator::get_out_prefix(Stage::Type type)
360 {
361         if(type==Stage::VERTEX)
362                 return "_vs_out_";
363         else if(type==Stage::GEOMETRY)
364                 return "_gs_out_";
365         else
366                 return string();
367 }
368
369 void InterfaceGenerator::apply(Stage &s)
370 {
371         stage = &s;
372         iface_target_block = &stage->content;
373         if(stage->previous)
374                 in_prefix = get_out_prefix(stage->previous->type);
375         out_prefix = get_out_prefix(stage->type);
376         s.content.visit(*this);
377         NodeRemover().apply(s, nodes_to_remove);
378 }
379
380 void InterfaceGenerator::visit(Block &block)
381 {
382         SetForScope<Block *> set_block(current_block, &block);
383         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
384         {
385                 assignment_insert_point = i;
386                 if(&block==&stage->content)
387                         iface_insert_point = i;
388
389                 (*i)->visit(*this);
390         }
391 }
392
393 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
394 {
395         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
396         return prefix+name.substr(offset);
397 }
398
399 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
400 {
401         if(stage->content.variables.count(name))
402                 return false;
403
404         VariableDeclaration* iface_var = new VariableDeclaration;
405         iface_var->sampling = var.sampling;
406         iface_var->interface = iface;
407         iface_var->type = var.type;
408         iface_var->type_declaration = var.type_declaration;
409         iface_var->name = name;
410         if(stage->type==Stage::GEOMETRY && !copy_block)
411                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
412         else
413                 iface_var->array = var.array;
414         if(iface_var->array)
415                 iface_var->array_size = var.array_size;
416         if(iface=="in")
417         {
418                 iface_var->layout = var.layout;
419                 iface_var->linked_declaration = &var;
420                 var.linked_declaration = iface_var;
421         }
422
423         iface_target_block->body.insert(iface_insert_point, iface_var);
424         iface_target_block->variables[name] = iface_var;
425
426         return true;
427 }
428
429 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
430 {
431         if(stage->interface_blocks.count(out_block.name))
432                 return false;
433
434         InterfaceBlock *in_block = new InterfaceBlock;
435         in_block->interface = "in";
436         in_block->name = out_block.name;
437         in_block->instance_name = out_block.instance_name;
438         if(stage->type==Stage::GEOMETRY)
439                 in_block->array = true;
440         else
441                 in_block->array = out_block.array;
442         in_block->linked_block = &out_block;
443         out_block.linked_block = in_block;
444
445         {
446                 SetFlag set_copy(copy_block, true);
447                 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
448                 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
449                 out_block.members.visit(*this);
450         }
451
452         iface_target_block->body.insert(iface_insert_point, in_block);
453         stage->interface_blocks[in_block->name] = in_block;
454         if(!in_block->instance_name.empty())
455                 stage->interface_blocks[in_block->instance_name] = in_block;
456
457         SetFlag set_scope(function_scope, false);
458         SetForScope<Block *> set_block(current_block, &stage->content);
459         in_block->visit(*this);
460
461         return true;
462 }
463
464 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
465 {
466         Assignment *assign = new Assignment;
467         VariableReference *ref = new VariableReference;
468         ref->name = left;
469         assign->left = ref;
470         assign->oper = "=";
471         assign->right = right;
472
473         ExpressionStatement *stmt = new ExpressionStatement;
474         stmt->expression = assign;
475         current_block->body.insert(assignment_insert_point, stmt);
476         stmt->visit(*this);
477
478         return *stmt;
479 }
480
481 void InterfaceGenerator::visit(VariableReference &var)
482 {
483         if(var.declaration || !stage->previous)
484                 return;
485         /* Don't pull a variable from previous stage if we just generated an out
486         interface in this stage */
487         if(stage->content.variables.count(var.name))
488                 return;
489
490         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
491         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
492         if(i==prev_vars.end() || i->second->interface!="out")
493                 i = prev_vars.find(in_prefix+var.name);
494         if(i!=prev_vars.end() && i->second->interface=="out")
495         {
496                 generate_interface(*i->second, "in", i->second->name);
497                 var.name = i->second->name;
498                 return;
499         }
500
501         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
502         map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
503         if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
504         {
505                 generate_interface(*j->second);
506                 return;
507         }
508
509         for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
510                 if(j->second->instance_name.empty())
511                 {
512                         i = j->second->members.variables.find(var.name);
513                         if(i!=j->second->members.variables.end())
514                         {
515                                 generate_interface(*j->second);
516                                 return;
517                         }
518                 }
519 }
520
521 void InterfaceGenerator::visit(VariableDeclaration &var)
522 {
523         if(copy_block)
524         {
525                 generate_interface(var, "in", var.name);
526                 return;
527         }
528
529         if(iface_block)
530         {
531                 if(iface_block->linked_block)
532                 {
533                         const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
534                         map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
535                         if(i!=linked_vars.end())
536                                 var.linked_declaration = i->second;
537                 }
538                 return;
539         }
540
541         if(var.interface=="out")
542         {
543                 /* For out variables in function scope, generate a global interface and
544                 replace the local declaration with an assignment. */
545                 if(function_scope && generate_interface(var, "out", var.name))
546                 {
547                         nodes_to_remove.insert(&var);
548                         if(var.init_expression)
549                         {
550                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
551                                 stmt.source = var.source;
552                                 stmt.line = var.line;
553                                 return;
554                         }
555                 }
556         }
557         else if(var.interface=="in")
558         {
559                 /* Try to link in variables in global scope with out variables from
560                 previous stage */
561                 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
562                 {
563                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
564                         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
565                         if(i!=prev_vars.end() && i->second->interface=="out")
566                         {
567                                 var.linked_declaration = i->second;
568                                 i->second->linked_declaration = &var;
569                         }
570                 }
571         }
572
573         TraversingVisitor::visit(var);
574 }
575
576 void InterfaceGenerator::visit(InterfaceBlock &iface)
577 {
578         if(iface.interface=="in")
579         {
580                 if(!iface.linked_block && stage->previous)
581                 {
582                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
583                         map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
584                         if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
585                         {
586                                 iface.linked_block = i->second;
587                                 i->second->linked_block = &iface;
588                         }
589                 }
590         }
591
592         SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
593         TraversingVisitor::visit(iface);
594 }
595
596 void InterfaceGenerator::visit(FunctionDeclaration &func)
597 {
598         SetFlag set_scope(function_scope, true);
599         // Skip parameters because they're not useful here
600         func.body.visit(*this);
601 }
602
603 void InterfaceGenerator::visit(Passthrough &pass)
604 {
605         vector<VariableDeclaration *> pass_vars;
606
607         for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
608                 if(i->second->interface=="in")
609                         pass_vars.push_back(i->second);
610
611         if(stage->previous)
612         {
613                 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
614                 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
615                 {
616                         bool linked = false;
617                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
618                                 linked = ((*j)->linked_declaration==i->second);
619
620                         if(!linked && generate_interface(*i->second, "in", i->second->name))
621                                 pass_vars.push_back(i->second);
622                 }
623         }
624
625         if(stage->type==Stage::GEOMETRY)
626         {
627                 InterfaceBlockReference *ref = new InterfaceBlockReference;
628                 ref->name = "gl_in";
629
630                 BinaryExpression *subscript = new BinaryExpression;
631                 subscript->left = ref;
632                 subscript->oper = "[";
633                 subscript->right = pass.subscript;
634                 subscript->after = "]";
635
636                 MemberAccess *memacc = new MemberAccess;
637                 memacc->left = subscript;
638                 memacc->member = "gl_Position";
639
640                 insert_assignment("gl_Position", memacc);
641         }
642
643         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
644         {
645                 string out_name = change_prefix((*i)->name, out_prefix);
646                 generate_interface(**i, "out", out_name);
647
648                 VariableReference *ref = new VariableReference;
649                 ref->name = (*i)->name;
650                 if(pass.subscript)
651                 {
652                         BinaryExpression *subscript = new BinaryExpression;
653                         subscript->left = ref;
654                         subscript->oper = "[";
655                         subscript->right = pass.subscript;
656                         subscript->after = "]";
657                         insert_assignment(out_name, subscript);
658                 }
659                 else
660                         insert_assignment(out_name, ref);
661         }
662
663         nodes_to_remove.insert(&pass);
664 }
665
666
667 DeclarationReorderer::DeclarationReorderer():
668         kind(NO_DECLARATION)
669 { }
670
671 void DeclarationReorderer::visit(FunctionCall &call)
672 {
673         FunctionDeclaration *def = call.declaration;
674         if(def)
675                 def = def->definition;
676         if(def && !ordered_funcs.count(def))
677                 needed_funcs.insert(def);
678 }
679
680 void DeclarationReorderer::visit(Block &block)
681 {
682         if(block.parent)
683                 return TraversingVisitor::visit(block);
684
685         NodeList<Statement>::iterator struct_insert_point = block.body.end();
686         NodeList<Statement>::iterator variable_insert_point = block.body.end();
687         NodeList<Statement>::iterator function_insert_point = block.body.end();
688         unsigned unordered_func_count = 0;
689         bool ordered_any_funcs = false;
690
691         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
692         {
693                 kind = NO_DECLARATION;
694                 (*i)->visit(*this);
695
696                 bool moved = false;
697                 if(kind==STRUCT && struct_insert_point!=block.body.end())
698                 {
699                         block.body.insert(struct_insert_point, *i);
700                         moved = true;
701                 }
702                 else if(kind>STRUCT && struct_insert_point==block.body.end())
703                         struct_insert_point = i;
704
705                 if(kind==VARIABLE && variable_insert_point!=block.body.end())
706                 {
707                         block.body.insert(variable_insert_point, *i);
708                         moved = true;
709                 }
710                 else if(kind>VARIABLE && variable_insert_point==block.body.end())
711                         variable_insert_point = i;
712
713                 if(kind==FUNCTION)
714                 {
715                         if(function_insert_point==block.body.end())
716                                 function_insert_point = i;
717
718                         if(needed_funcs.empty())
719                         {
720                                 ordered_funcs.insert(i->get());
721                                 if(i!=function_insert_point)
722                                 {
723                                         block.body.insert(function_insert_point, *i);
724                                         moved = true;
725                                 }
726                                 else
727                                         ++function_insert_point;
728                                 ordered_any_funcs = true;
729                         }
730                         else
731                                 ++unordered_func_count;
732                 }
733
734                 if(moved)
735                 {
736                         if(function_insert_point==i)
737                                 ++function_insert_point;
738                         block.body.erase(i++);
739                 }
740                 else
741                         ++i;
742
743                 if(i==block.body.end() && unordered_func_count)
744                 {
745                         if(!ordered_any_funcs)
746                                 // A subset of the remaining functions forms a recursive loop
747                                 /* TODO pick a function and move it up, adding any necessary
748                                 declarations */
749                                 break;
750
751                         i = function_insert_point;
752                         unordered_func_count = 0;
753                 }
754         }
755 }
756
757 void DeclarationReorderer::visit(VariableDeclaration &var)
758 {
759         TraversingVisitor::visit(var);
760         kind = VARIABLE;
761 }
762
763 void DeclarationReorderer::visit(FunctionDeclaration &func)
764 {
765         needed_funcs.clear();
766         func.body.visit(*this);
767         needed_funcs.erase(&func);
768         kind = FUNCTION;
769 }
770
771 } // namespace SL
772 } // namespace GL
773 } // namespace Msp