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