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