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