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