]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Have generate_interface return the generated statement
[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() && i->second->instance_name==var.name)
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[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[var.name] = &var;
312 }
313
314 void VariableResolver::visit(InterfaceBlock &iface)
315 {
316         /* Block names can't be used for any other identifiers so we can put them
317         in the same map with instance names. */
318         stage->interface_blocks[iface.name] = &iface;
319         if(!iface.instance_name.empty())
320                 stage->interface_blocks[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[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(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[in_block->name] = in_block;
476         if(!in_block->instance_name.empty())
477                 stage->interface_blocks[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" && j->second->instance_name==var.name)
526         {
527                 generate_interface(*j->second);
528                 return;
529         }
530
531         for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
532                 if(j->second->instance_name.empty())
533                 {
534                         i = j->second->members.variables.find(var.name);
535                         if(i!=j->second->members.variables.end())
536                         {
537                                 generate_interface(*j->second);
538                                 return;
539                         }
540                 }
541 }
542
543 void InterfaceGenerator::visit(VariableDeclaration &var)
544 {
545         if(copy_block)
546         {
547                 generate_interface(var, "in", var.name);
548                 return;
549         }
550
551         if(iface_block)
552         {
553                 if(iface_block->linked_block)
554                 {
555                         // Link all variables to their counterparts in the linked block.
556                         const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
557                         map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
558                         if(i!=linked_vars.end())
559                                 var.linked_declaration = i->second;
560                 }
561                 return;
562         }
563
564         if(var.interface=="out")
565         {
566                 /* For output variables in function scope, generate a global interface
567                 and replace the local declaration with an assignment. */
568                 if(function_scope && generate_interface(var, "out", var.name))
569                 {
570                         nodes_to_remove.insert(&var);
571                         if(var.init_expression)
572                         {
573                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
574                                 stmt.source = var.source;
575                                 stmt.line = var.line;
576                                 return;
577                         }
578                 }
579         }
580         else if(var.interface=="in")
581         {
582                 /* Try to link input variables in global scope with output variables from
583                 previous stage. */
584                 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
585                 {
586                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
587                         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
588                         if(i!=prev_vars.end() && i->second->interface=="out")
589                         {
590                                 var.linked_declaration = i->second;
591                                 i->second->linked_declaration = &var;
592                         }
593                 }
594         }
595
596         TraversingVisitor::visit(var);
597 }
598
599 void InterfaceGenerator::visit(InterfaceBlock &iface)
600 {
601         if(iface.interface=="in")
602         {
603                 /* Try to link input blocks with output blocks sharing the same block
604                 name from previous stage. */
605                 if(!iface.linked_block && stage->previous)
606                 {
607                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
608                         map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
609                         if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
610                         {
611                                 iface.linked_block = i->second;
612                                 i->second->linked_block = &iface;
613                         }
614                 }
615         }
616
617         SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
618         TraversingVisitor::visit(iface);
619 }
620
621 void InterfaceGenerator::visit(FunctionDeclaration &func)
622 {
623         SetFlag set_scope(function_scope, true);
624         // Skip parameters because they're not useful here
625         func.body.visit(*this);
626 }
627
628 void InterfaceGenerator::visit(Passthrough &pass)
629 {
630         vector<VariableDeclaration *> pass_vars;
631
632         // Pass through all input variables of this stage.
633         for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
634                 if(i->second->interface=="in")
635                         pass_vars.push_back(i->second);
636
637         if(stage->previous)
638         {
639                 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
640                 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
641                 {
642                         if(i->second->interface!="out")
643                                 continue;
644
645                         /* Pass through output variables from the previous stage, but only
646                         those which are not already linked to an input here. */
647                         if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
648                                 pass_vars.push_back(i->second);
649                 }
650         }
651
652         if(stage->type==Stage::GEOMETRY)
653         {
654                 /* Special case for geometry shader: copy gl_Position from input to
655                 output. */
656                 InterfaceBlockReference *ref = new InterfaceBlockReference;
657                 ref->name = "gl_in";
658
659                 BinaryExpression *subscript = new BinaryExpression;
660                 subscript->left = ref;
661                 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
662                 subscript->right = pass.subscript;
663
664                 MemberAccess *memacc = new MemberAccess;
665                 memacc->left = subscript;
666                 memacc->member = "gl_Position";
667
668                 insert_assignment("gl_Position", memacc);
669         }
670
671         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
672         {
673                 string out_name = change_prefix((*i)->name, out_prefix);
674                 generate_interface(**i, "out", out_name);
675
676                 VariableReference *ref = new VariableReference;
677                 ref->name = (*i)->name;
678                 if(pass.subscript)
679                 {
680                         BinaryExpression *subscript = new BinaryExpression;
681                         subscript->left = ref;
682                         subscript->oper = &Operator::get_operator("[", Operator::BINARY);
683                         subscript->right = pass.subscript;
684                         insert_assignment(out_name, subscript);
685                 }
686                 else
687                         insert_assignment(out_name, ref);
688         }
689
690         nodes_to_remove.insert(&pass);
691 }
692
693 } // namespace SL
694 } // namespace GL
695 } // namespace Msp