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