]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/generate.cpp
Improve support for interface blocks
[libs/gl.git] / source / glsl / generate.cpp
1 #include <msp/core/raii.h>
2 #include "builtin.h"
3 #include "generate.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 void DeclarationCombiner::apply(Stage &stage)
12 {
13         stage.content.visit(*this);
14         NodeRemover().apply(stage, nodes_to_remove);
15 }
16
17 void DeclarationCombiner::visit(Block &block)
18 {
19         if(current_block)
20                 return;
21
22         TraversingVisitor::visit(block);
23 }
24
25 void DeclarationCombiner::visit(FunctionDeclaration &func)
26 {
27         vector<FunctionDeclaration *> &decls = functions[func.name];
28         if(func.definition)
29         {
30                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
31                 {
32                         (*i)->definition = func.definition;
33                         (*i)->body.body.clear();
34                 }
35         }
36         decls.push_back(&func);
37 }
38
39 void DeclarationCombiner::visit(VariableDeclaration &var)
40 {
41         VariableDeclaration *&ptr = variables[var.name];
42         if(ptr)
43         {
44                 ptr->type = var.type;
45                 if(var.init_expression)
46                         ptr->init_expression = var.init_expression;
47                 if(var.layout)
48                 {
49                         if(ptr->layout)
50                         {
51                                 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
52                                 {
53                                         bool found = false;
54                                         for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
55                                                 if(j->name==i->name)
56                                                 {
57                                                         j->has_value = i->value;
58                                                         j->value = i->value;
59                                                         found = true;
60                                                 }
61
62                                         if(!found)
63                                                 ptr->layout->qualifiers.push_back(*i);
64                                 }
65                         }
66                         else
67                                 ptr->layout = var.layout;
68                 }
69                 nodes_to_remove.insert(&var);
70         }
71         else
72                 ptr = &var;
73 }
74
75
76 void BlockResolver::enter(Block &block)
77 {
78         block.parent = current_block;
79 }
80
81 void BlockResolver::visit(InterfaceBlock &iface)
82 {
83         /* Block names can't be used for any other identifiers so we can put them
84         in the same map with instance names. */
85         current_block->interfaces[iface.name] = &iface;
86         if(!iface.instance_name.empty())
87                 current_block->interfaces[iface.instance_name] = &iface;
88         TraversingVisitor::visit(iface);
89 }
90
91
92 VariableResolver::VariableResolver():
93         builtins(0),
94         members(0),
95         record_target(false),
96         assignment_target(0),
97         self_referencing(false)
98 { }
99
100 void VariableResolver::apply(Stage &stage)
101 {
102         Stage *builtin_stage = get_builtins(stage.type);
103         builtins = (builtin_stage ? &builtin_stage->content : 0);
104         stage.content.visit(*this);
105 }
106
107 Block *VariableResolver::next_block(Block &block)
108 {
109         return block.parent ? block.parent : &block!=builtins ? builtins : 0;
110 }
111
112 void VariableResolver::enter(Block &block)
113 {
114         block.variables.clear();
115 }
116
117 void VariableResolver::visit(VariableReference &var)
118 {
119         var.declaration = 0;
120         members = 0;
121         for(Block *block=current_block; block; block=next_block(*block))
122         {
123                 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
124                 if(i!=block->variables.end())
125                         var.declaration = i->second;
126                 else
127                 {
128                         const map<string, InterfaceBlock *> &ifaces = block->interfaces;
129                         for(map<string, InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
130                                 if(j->second->instance_name.empty())
131                                 {
132                                         i = j->second->members.variables.find(var.name);
133                                         if(i!=j->second->members.variables.end())
134                                                 var.declaration = i->second;
135                                 }
136                 }
137
138                 if(var.declaration)
139                 {
140                         if(var.declaration->type_declaration)
141                                 members = &var.declaration->type_declaration->members.variables;
142                         break;
143                 }
144
145                 map<string, InterfaceBlock *>::iterator j = block->interfaces.find(var.name);
146                 if(j!=block->interfaces.end() && j->second->instance_name==var.name)
147                 {
148                         iface_ref = new InterfaceBlockReference;
149                         iface_ref->name = var.name;
150                         iface_ref->declaration = j->second;
151                         members = &j->second->members.variables;
152                         break;
153                 }
154         }
155
156         if(record_target)
157         {
158                 if(assignment_target)
159                 {
160                         record_target = false;
161                         assignment_target = 0;
162                 }
163                 else
164                         assignment_target = var.declaration;
165         }
166         else if(var.declaration && var.declaration==assignment_target)
167                 self_referencing = true;
168 }
169
170 void VariableResolver::visit(InterfaceBlockReference &iface)
171 {
172         iface.declaration = 0;
173         for(Block *block=current_block; block; block=next_block(*block))
174         {
175                 map<string, InterfaceBlock *>::iterator j = block->interfaces.find(iface.name);
176                 if(j!=block->interfaces.end())
177                 {
178                         iface.declaration = j->second;
179                         members = &j->second->members.variables;
180                         break;
181                 }
182         }
183 }
184
185 void VariableResolver::visit(MemberAccess &memacc)
186 {
187         members = 0;
188         iface_ref = 0;
189         memacc.left->visit(*this);
190
191         if(iface_ref)
192                 memacc.left = iface_ref;
193         iface_ref = 0;
194
195         memacc.declaration = 0;
196         if(members)
197         {
198                 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
199                 if(i!=members->end())
200                 {
201                         memacc.declaration = i->second;
202                         if(i->second->type_declaration)
203                                 members = &i->second->type_declaration->members.variables;
204                 }
205                 else
206                         members = 0;
207         }
208 }
209
210 void VariableResolver::visit(BinaryExpression &binary)
211 {
212         if(binary.oper=="[")
213         {
214                 {
215                         SetForScope<bool> set(record_target, false);
216                         binary.right->visit(*this);
217                 }
218                 members = 0;
219                 iface_ref = 0;
220                 binary.left->visit(*this);
221                 if(iface_ref)
222                         binary.left = iface_ref;
223                 iface_ref = 0;
224         }
225         else
226         {
227                 TraversingVisitor::visit(binary);
228                 members = 0;
229         }
230 }
231
232 void VariableResolver::visit(Assignment &assign)
233 {
234         {
235                 SetFlag set(record_target);
236                 assignment_target = 0;
237                 assign.left->visit(*this);
238         }
239
240         self_referencing = false;
241         assign.right->visit(*this);
242
243         assign.self_referencing = (self_referencing || assign.oper!="=");
244         assign.target_declaration = assignment_target;
245 }
246
247 void VariableResolver::visit(StructDeclaration &strct)
248 {
249         TraversingVisitor::visit(strct);
250         current_block->types[strct.name] = &strct;
251 }
252
253 void VariableResolver::visit(VariableDeclaration &var)
254 {
255         for(Block *block=current_block; block; block=next_block(*block))
256         {
257                 map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
258                 if(j!=block->types.end())
259                         var.type_declaration = j->second;
260         }
261
262         if(!block_interface.empty() && var.interface.empty())
263                 var.interface = block_interface;
264
265         TraversingVisitor::visit(var);
266         current_block->variables[var.name] = &var;
267 }
268
269 void VariableResolver::visit(InterfaceBlock &iface)
270 {
271         SetForScope<string> set_iface(block_interface, iface.interface);
272         TraversingVisitor::visit(iface);
273 }
274
275
276 void FunctionResolver::visit(FunctionCall &call)
277 {
278         map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
279         if(i!=functions.end())
280                 call.declaration = i->second.back();
281
282         TraversingVisitor::visit(call);
283 }
284
285 void FunctionResolver::visit(FunctionDeclaration &func)
286 {
287         vector<FunctionDeclaration *> &decls = functions[func.name];
288         if(func.definition)
289         {
290                 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
291                         (*i)->definition = func.definition;
292                 decls.clear();
293                 decls.push_back(&func);
294         }
295         else if(!decls.empty() && decls.back()->definition)
296                 func.definition = decls.back()->definition;
297         else
298                 decls.push_back(&func);
299
300         TraversingVisitor::visit(func);
301 }
302
303
304 InterfaceGenerator::InterfaceGenerator():
305         stage(0),
306         function_scope(false),
307         iface_block(0)
308 { }
309
310 string InterfaceGenerator::get_out_prefix(Stage::Type type)
311 {
312         if(type==Stage::VERTEX)
313                 return "_vs_out_";
314         else if(type==Stage::GEOMETRY)
315                 return "_gs_out_";
316         else
317                 return string();
318 }
319
320 void InterfaceGenerator::apply(Stage &s)
321 {
322         stage = &s;
323         if(stage->previous)
324                 in_prefix = get_out_prefix(stage->previous->type);
325         out_prefix = get_out_prefix(stage->type);
326         s.content.visit(*this);
327         NodeRemover().apply(s, nodes_to_remove);
328 }
329
330 void InterfaceGenerator::visit(Block &block)
331 {
332         SetForScope<Block *> set_block(current_block, &block);
333         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
334         {
335                 assignment_insert_point = i;
336                 if(&block==&stage->content)
337                         iface_insert_point = i;
338
339                 (*i)->visit(*this);
340         }
341 }
342
343 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
344 {
345         unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
346         return prefix+name.substr(offset);
347 }
348
349 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
350 {
351         if(stage->content.variables.count(name))
352                 return false;
353
354         VariableDeclaration* iface_var = new VariableDeclaration;
355         iface_var->sampling = var.sampling;
356         iface_var->interface = iface;
357         iface_var->type = var.type;
358         iface_var->type_declaration = var.type_declaration;
359         iface_var->name = name;
360         if(stage->type==Stage::GEOMETRY)
361                 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
362         else
363                 iface_var->array = var.array;
364         if(iface_var->array)
365                 iface_var->array_size = var.array_size;
366         if(iface=="in")
367         {
368                 iface_var->linked_declaration = &var;
369                 var.linked_declaration = iface_var;
370         }
371         stage->content.body.insert(iface_insert_point, iface_var);
372         stage->content.variables[name] = iface_var;
373
374         return true;
375 }
376
377 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
378 {
379         Assignment *assign = new Assignment;
380         VariableReference *ref = new VariableReference;
381         ref->name = left;
382         assign->left = ref;
383         assign->oper = "=";
384         assign->right = right;
385
386         ExpressionStatement *stmt = new ExpressionStatement;
387         stmt->expression = assign;
388         current_block->body.insert(assignment_insert_point, stmt);
389         stmt->visit(*this);
390
391         return *stmt;
392 }
393
394 void InterfaceGenerator::visit(VariableReference &var)
395 {
396         if(var.declaration || !stage->previous)
397                 return;
398         /* Don't pull a variable from previous stage if we just generated an out
399         interface in this stage */
400         if(stage->content.variables.count(var.name))
401                 return;
402
403         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
404         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
405         if(i==prev_vars.end() || i->second->interface!="out")
406                 i = prev_vars.find(in_prefix+var.name);
407         if(i!=prev_vars.end() && i->second->interface=="out")
408         {
409                 generate_interface(*i->second, "in", i->second->name);
410                 var.name = i->second->name;
411         }
412 }
413
414 void InterfaceGenerator::visit(VariableDeclaration &var)
415 {
416         if(iface_block)
417         {
418                 if(iface_block->linked_block)
419                 {
420                         const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
421                         map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
422                         if(i!=linked_vars.end())
423                                 var.linked_declaration = i->second;
424                 }
425                 return;
426         }
427
428         if(var.interface=="out")
429         {
430                 /* For out variables in function scope, generate a global interface and
431                 replace the local declaration with an assignment. */
432                 if(function_scope && generate_interface(var, "out", var.name))
433                 {
434                         nodes_to_remove.insert(&var);
435                         if(var.init_expression)
436                         {
437                                 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
438                                 stmt.source = var.source;
439                                 stmt.line = var.line;
440                                 return;
441                         }
442                 }
443         }
444         else if(var.interface=="in")
445         {
446                 /* Try to link in variables in global scope with out variables from
447                 previous stage */
448                 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
449                 {
450                         const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
451                         map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
452                         if(i!=prev_vars.end() && i->second->interface=="out")
453                         {
454                                 var.linked_declaration = i->second;
455                                 i->second->linked_declaration = &var;
456                         }
457                 }
458         }
459
460         TraversingVisitor::visit(var);
461 }
462
463 void InterfaceGenerator::visit(InterfaceBlock &iface)
464 {
465         if(iface.interface=="in")
466         {
467                 if(!iface.linked_block && stage->previous)
468                 {
469                         const map<string, InterfaceBlock *> &prev_blocks = stage->previous->content.interfaces;
470                         map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
471                         if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
472                         {
473                                 iface.linked_block = i->second;
474                                 i->second->linked_block = &iface;
475                         }
476                 }
477         }
478
479         SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
480         TraversingVisitor::visit(iface);
481 }
482
483 void InterfaceGenerator::visit(FunctionDeclaration &func)
484 {
485         SetFlag set_scope(function_scope, true);
486         // Skip parameters because they're not useful here
487         func.body.visit(*this);
488 }
489
490 void InterfaceGenerator::visit(Passthrough &pass)
491 {
492         vector<VariableDeclaration *> pass_vars;
493
494         for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
495                 if(i->second->interface=="in")
496                         pass_vars.push_back(i->second);
497
498         if(stage->previous)
499         {
500                 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
501                 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
502                 {
503                         bool linked = false;
504                         for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
505                                 linked = ((*j)->linked_declaration==i->second);
506
507                         if(!linked && generate_interface(*i->second, "in", i->second->name))
508                                 pass_vars.push_back(i->second);
509                 }
510         }
511
512         if(stage->type==Stage::GEOMETRY)
513         {
514                 InterfaceBlockReference *ref = new InterfaceBlockReference;
515                 ref->name = "gl_in";
516
517                 BinaryExpression *subscript = new BinaryExpression;
518                 subscript->left = ref;
519                 subscript->oper = "[";
520                 subscript->right = pass.subscript;
521                 subscript->after = "]";
522
523                 MemberAccess *memacc = new MemberAccess;
524                 memacc->left = subscript;
525                 memacc->member = "gl_Position";
526
527                 insert_assignment("gl_Position", memacc);
528         }
529
530         for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
531         {
532                 string out_name = change_prefix((*i)->name, out_prefix);
533                 generate_interface(**i, "out", out_name);
534
535                 VariableReference *ref = new VariableReference;
536                 ref->name = (*i)->name;
537                 if(pass.subscript)
538                 {
539                         BinaryExpression *subscript = new BinaryExpression;
540                         subscript->left = ref;
541                         subscript->oper = "[";
542                         subscript->right = pass.subscript;
543                         subscript->after = "]";
544                         insert_assignment(out_name, subscript);
545                 }
546                 else
547                         insert_assignment(out_name, ref);
548         }
549
550         nodes_to_remove.insert(&pass);
551 }
552
553
554 DeclarationReorderer::DeclarationReorderer():
555         kind(NO_DECLARATION)
556 { }
557
558 void DeclarationReorderer::visit(FunctionCall &call)
559 {
560         FunctionDeclaration *def = call.declaration;
561         if(def)
562                 def = def->definition;
563         if(def && !ordered_funcs.count(def))
564                 needed_funcs.insert(def);
565 }
566
567 void DeclarationReorderer::visit(Block &block)
568 {
569         if(block.parent)
570                 return TraversingVisitor::visit(block);
571
572         NodeList<Statement>::iterator struct_insert_point = block.body.end();
573         NodeList<Statement>::iterator variable_insert_point = block.body.end();
574         NodeList<Statement>::iterator function_insert_point = block.body.end();
575         unsigned unordered_func_count = 0;
576         bool ordered_any_funcs = false;
577
578         for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
579         {
580                 kind = NO_DECLARATION;
581                 (*i)->visit(*this);
582
583                 bool moved = false;
584                 if(kind==STRUCT && struct_insert_point!=block.body.end())
585                 {
586                         block.body.insert(struct_insert_point, *i);
587                         moved = true;
588                 }
589                 else if(kind>STRUCT && struct_insert_point==block.body.end())
590                         struct_insert_point = i;
591
592                 if(kind==VARIABLE && variable_insert_point!=block.body.end())
593                 {
594                         block.body.insert(variable_insert_point, *i);
595                         moved = true;
596                 }
597                 else if(kind>VARIABLE && variable_insert_point==block.body.end())
598                         variable_insert_point = i;
599
600                 if(kind==FUNCTION)
601                 {
602                         if(function_insert_point==block.body.end())
603                                 function_insert_point = i;
604
605                         if(needed_funcs.empty())
606                         {
607                                 ordered_funcs.insert(i->get());
608                                 if(i!=function_insert_point)
609                                 {
610                                         block.body.insert(function_insert_point, *i);
611                                         moved = true;
612                                 }
613                                 else
614                                         ++function_insert_point;
615                                 ordered_any_funcs = true;
616                         }
617                         else
618                                 ++unordered_func_count;
619                 }
620
621                 if(moved)
622                 {
623                         if(function_insert_point==i)
624                                 ++function_insert_point;
625                         block.body.erase(i++);
626                 }
627                 else
628                         ++i;
629
630                 if(i==block.body.end() && unordered_func_count)
631                 {
632                         if(!ordered_any_funcs)
633                                 // A subset of the remaining functions forms a recursive loop
634                                 /* TODO pick a function and move it up, adding any necessary
635                                 declarations */
636                                 break;
637
638                         i = function_insert_point;
639                         unordered_func_count = 0;
640                 }
641         }
642 }
643
644 void DeclarationReorderer::visit(VariableDeclaration &var)
645 {
646         TraversingVisitor::visit(var);
647         kind = VARIABLE;
648 }
649
650 void DeclarationReorderer::visit(FunctionDeclaration &func)
651 {
652         needed_funcs.clear();
653         func.body.visit(*this);
654         needed_funcs.erase(&func);
655         kind = FUNCTION;
656 }
657
658 } // namespace SL
659 } // namespace GL
660 } // namespace Msp