1 #include <msp/core/hash.h>
2 #include <msp/core/raii.h>
3 #include <msp/strings/lexicalcast.h>
13 void DeclarationCombiner::apply(Stage &stage)
15 stage.content.visit(*this);
16 NodeRemover().apply(stage, nodes_to_remove);
19 void DeclarationCombiner::visit(Block &block)
24 TraversingVisitor::visit(block);
27 void DeclarationCombiner::visit(VariableDeclaration &var)
29 VariableDeclaration *&ptr = variables[var.name];
33 if(var.init_expression)
34 ptr->init_expression = var.init_expression;
39 for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
42 for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
45 j->has_value = i->value;
51 ptr->layout->qualifiers.push_back(*i);
55 ptr->layout = var.layout;
57 nodes_to_remove.insert(&var);
64 ConstantSpecializer::ConstantSpecializer():
68 void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
71 stage.content.visit(*this);
74 void ConstantSpecializer::visit(VariableDeclaration &var)
76 bool specializable = false;
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")
87 i->value = hash32(var.name)&0x7FFFFFFF;
91 if(qualifiers.empty())
95 if(specializable && values)
97 map<string, int>::const_iterator i = values->find(var.name);
100 RefPtr<Literal> literal = new Literal;
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;
111 void BlockHierarchyResolver::enter(Block &block)
113 block.parent = current_block;
117 VariableResolver::VariableResolver():
120 record_target(false),
121 assignment_target(0),
122 self_referencing(false)
125 void VariableResolver::apply(Stage &s)
129 s.interface_blocks.clear();
130 s.content.visit(*this);
133 void VariableResolver::enter(Block &block)
135 block.variables.clear();
138 void VariableResolver::visit(VariableReference &var)
142 for(Block *block=current_block; (!var.declaration && block); block=block->parent)
144 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
145 if(i!=block->variables.end())
146 var.declaration = i->second;
151 if(var.declaration->type_declaration)
152 members = &var.declaration->type_declaration->members.variables;
156 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
157 map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
158 if(i!=blocks.end() && i->second->instance_name==var.name)
160 iface_ref = new InterfaceBlockReference;
161 iface_ref->name = var.name;
162 iface_ref->declaration = i->second;
163 members = &i->second->members.variables;
167 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
168 if(i->second->instance_name.empty())
170 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
171 if(j!=i->second->members.variables.end())
172 var.declaration = j->second;
179 if(assignment_target)
181 record_target = false;
182 assignment_target = 0;
185 assignment_target = var.declaration;
187 else if(var.declaration && var.declaration==assignment_target)
188 self_referencing = true;
191 void VariableResolver::visit(InterfaceBlockReference &iface)
193 iface.declaration = 0;
194 for(Block *block=current_block; block; block=block->parent)
196 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
197 if(i!=stage->interface_blocks.end())
199 iface.declaration = i->second;
200 members = &i->second->members.variables;
206 void VariableResolver::visit(MemberAccess &memacc)
210 memacc.left->visit(*this);
213 memacc.left = iface_ref;
216 memacc.declaration = 0;
219 map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
220 if(i!=members->end())
222 memacc.declaration = i->second;
223 if(i->second->type_declaration)
224 members = &i->second->type_declaration->members.variables;
231 void VariableResolver::visit(BinaryExpression &binary)
233 if(binary.oper->token[0]=='[')
236 SetForScope<bool> set(record_target, false);
237 binary.right->visit(*this);
241 binary.left->visit(*this);
243 binary.left = iface_ref;
248 TraversingVisitor::visit(binary);
253 void VariableResolver::visit(Assignment &assign)
256 SetFlag set(record_target);
257 assignment_target = 0;
258 assign.left->visit(*this);
261 self_referencing = false;
262 assign.right->visit(*this);
264 assign.self_referencing = (self_referencing || assign.oper->token[0]!='=');
265 assign.target_declaration = assignment_target;
268 void VariableResolver::visit(StructDeclaration &strct)
270 TraversingVisitor::visit(strct);
271 stage->types[strct.name] = &strct;
274 void VariableResolver::visit(VariableDeclaration &var)
276 map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
277 if(i!=stage->types.end())
278 var.type_declaration = i->second;
280 if(!block_interface.empty() && var.interface.empty())
281 var.interface = block_interface;
283 TraversingVisitor::visit(var);
284 current_block->variables[var.name] = &var;
287 void VariableResolver::visit(InterfaceBlock &iface)
289 /* Block names can't be used for any other identifiers so we can put them
290 in the same map with instance names. */
291 stage->interface_blocks[iface.name] = &iface;
292 if(!iface.instance_name.empty())
293 stage->interface_blocks[iface.instance_name] = &iface;
295 SetForScope<string> set_iface(block_interface, iface.interface);
296 TraversingVisitor::visit(iface);
300 void FunctionResolver::apply(Stage &s)
304 s.content.visit(*this);
307 void FunctionResolver::visit(FunctionCall &call)
309 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
310 if(i!=stage->functions.end())
311 call.declaration = i->second;
313 TraversingVisitor::visit(call);
316 void FunctionResolver::visit(FunctionDeclaration &func)
318 FunctionDeclaration *&stage_decl = stage->functions[func.name];
319 vector<FunctionDeclaration *> &decls = declarations[func.name];
320 if(func.definition==&func)
324 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
326 (*i)->definition = func.definition;
327 (*i)->body.body.clear();
336 func.definition = stage_decl->definition;
338 decls.push_back(&func);
340 TraversingVisitor::visit(func);
344 InterfaceGenerator::InterfaceGenerator():
346 function_scope(false),
349 iface_target_block(0)
352 string InterfaceGenerator::get_out_prefix(Stage::Type type)
354 if(type==Stage::VERTEX)
356 else if(type==Stage::GEOMETRY)
362 void InterfaceGenerator::apply(Stage &s)
365 iface_target_block = &stage->content;
367 in_prefix = get_out_prefix(stage->previous->type);
368 out_prefix = get_out_prefix(stage->type);
369 s.content.visit(*this);
370 NodeRemover().apply(s, nodes_to_remove);
373 void InterfaceGenerator::visit(Block &block)
375 SetForScope<Block *> set_block(current_block, &block);
376 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
378 assignment_insert_point = i;
379 if(&block==&stage->content)
380 iface_insert_point = i;
386 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
388 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
389 return prefix+name.substr(offset);
392 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
394 if(stage->content.variables.count(name))
397 VariableDeclaration* iface_var = new VariableDeclaration;
398 iface_var->sampling = var.sampling;
399 iface_var->interface = iface;
400 iface_var->type = var.type;
401 iface_var->type_declaration = var.type_declaration;
402 iface_var->name = name;
403 if(stage->type==Stage::GEOMETRY && !copy_block)
404 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
406 iface_var->array = var.array;
408 iface_var->array_size = var.array_size;
411 iface_var->layout = var.layout;
412 iface_var->linked_declaration = &var;
413 var.linked_declaration = iface_var;
416 iface_target_block->body.insert(iface_insert_point, iface_var);
417 iface_target_block->variables[name] = iface_var;
422 bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
424 if(stage->interface_blocks.count(out_block.name))
427 InterfaceBlock *in_block = new InterfaceBlock;
428 in_block->interface = "in";
429 in_block->name = out_block.name;
430 in_block->instance_name = out_block.instance_name;
431 if(stage->type==Stage::GEOMETRY)
432 in_block->array = true;
434 in_block->array = out_block.array;
435 in_block->linked_block = &out_block;
436 out_block.linked_block = in_block;
439 SetFlag set_copy(copy_block, true);
440 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
441 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
442 out_block.members.visit(*this);
445 iface_target_block->body.insert(iface_insert_point, in_block);
446 stage->interface_blocks[in_block->name] = in_block;
447 if(!in_block->instance_name.empty())
448 stage->interface_blocks[in_block->instance_name] = in_block;
450 SetFlag set_scope(function_scope, false);
451 SetForScope<Block *> set_block(current_block, &stage->content);
452 in_block->visit(*this);
457 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
459 Assignment *assign = new Assignment;
460 VariableReference *ref = new VariableReference;
463 assign->oper = &Operator::get_operator("=", Operator::BINARY);
464 assign->right = right;
466 ExpressionStatement *stmt = new ExpressionStatement;
467 stmt->expression = assign;
468 current_block->body.insert(assignment_insert_point, stmt);
474 void InterfaceGenerator::visit(VariableReference &var)
476 if(var.declaration || !stage->previous)
478 /* Don't pull a variable from previous stage if we just generated an out
479 interface in this stage */
480 if(stage->content.variables.count(var.name))
483 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
484 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
485 if(i==prev_vars.end() || i->second->interface!="out")
486 i = prev_vars.find(in_prefix+var.name);
487 if(i!=prev_vars.end() && i->second->interface=="out")
489 generate_interface(*i->second, "in", i->second->name);
490 var.name = i->second->name;
494 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
495 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
496 if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
498 generate_interface(*j->second);
502 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
503 if(j->second->instance_name.empty())
505 i = j->second->members.variables.find(var.name);
506 if(i!=j->second->members.variables.end())
508 generate_interface(*j->second);
514 void InterfaceGenerator::visit(VariableDeclaration &var)
518 generate_interface(var, "in", var.name);
524 if(iface_block->linked_block)
526 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
527 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
528 if(i!=linked_vars.end())
529 var.linked_declaration = i->second;
534 if(var.interface=="out")
536 /* For out variables in function scope, generate a global interface and
537 replace the local declaration with an assignment. */
538 if(function_scope && generate_interface(var, "out", var.name))
540 nodes_to_remove.insert(&var);
541 if(var.init_expression)
543 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
544 stmt.source = var.source;
545 stmt.line = var.line;
550 else if(var.interface=="in")
552 /* Try to link in variables in global scope with out variables from
554 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
556 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
557 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
558 if(i!=prev_vars.end() && i->second->interface=="out")
560 var.linked_declaration = i->second;
561 i->second->linked_declaration = &var;
566 TraversingVisitor::visit(var);
569 void InterfaceGenerator::visit(InterfaceBlock &iface)
571 if(iface.interface=="in")
573 if(!iface.linked_block && stage->previous)
575 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
576 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
577 if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
579 iface.linked_block = i->second;
580 i->second->linked_block = &iface;
585 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
586 TraversingVisitor::visit(iface);
589 void InterfaceGenerator::visit(FunctionDeclaration &func)
591 SetFlag set_scope(function_scope, true);
592 // Skip parameters because they're not useful here
593 func.body.visit(*this);
596 void InterfaceGenerator::visit(Passthrough &pass)
598 vector<VariableDeclaration *> pass_vars;
600 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
601 if(i->second->interface=="in")
602 pass_vars.push_back(i->second);
606 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
607 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
610 for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
611 linked = ((*j)->linked_declaration==i->second);
613 if(!linked && generate_interface(*i->second, "in", i->second->name))
614 pass_vars.push_back(i->second);
618 if(stage->type==Stage::GEOMETRY)
620 InterfaceBlockReference *ref = new InterfaceBlockReference;
623 BinaryExpression *subscript = new BinaryExpression;
624 subscript->left = ref;
625 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
626 subscript->right = pass.subscript;
628 MemberAccess *memacc = new MemberAccess;
629 memacc->left = subscript;
630 memacc->member = "gl_Position";
632 insert_assignment("gl_Position", memacc);
635 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
637 string out_name = change_prefix((*i)->name, out_prefix);
638 generate_interface(**i, "out", out_name);
640 VariableReference *ref = new VariableReference;
641 ref->name = (*i)->name;
644 BinaryExpression *subscript = new BinaryExpression;
645 subscript->left = ref;
646 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
647 subscript->right = pass.subscript;
648 insert_assignment(out_name, subscript);
651 insert_assignment(out_name, ref);
654 nodes_to_remove.insert(&pass);