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