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;
103 literal->token = (i->second ? "true" : "false");
104 literal->value = static_cast<bool>(i->second);
106 else if(var.type=="int")
108 literal->token = lexical_cast<string>(i->second);
109 literal->value = i->second;
111 var.init_expression = literal;
117 void BlockHierarchyResolver::enter(Block &block)
119 block.parent = current_block;
123 TypeResolver::TypeResolver():
127 void TypeResolver::apply(Stage &s)
131 s.content.visit(*this);
134 TypeDeclaration *TypeResolver::resolve_type(const string &name)
136 map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
137 if(i!=stage->types.end())
139 map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
140 return (j!=alias_map.end() ? j->second : i->second);
146 void TypeResolver::visit(BasicTypeDeclaration &type)
148 type.base_type = resolve_type(type.base);
150 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
151 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
152 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
154 type.kind = BasicTypeDeclaration::MATRIX;
155 type.size |= basic_base->size<<16;
158 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
159 alias_map[&type] = type.base_type;
161 stage->types.insert(make_pair(type.name, &type));
164 void TypeResolver::visit(ImageTypeDeclaration &type)
166 type.base_type = resolve_type(type.base);
167 stage->types.insert(make_pair(type.name, &type));
170 void TypeResolver::visit(StructDeclaration &strct)
172 stage->types.insert(make_pair(strct.name, &strct));
173 TraversingVisitor::visit(strct);
176 void TypeResolver::visit(VariableDeclaration &var)
178 var.type_declaration = resolve_type(var.type);
181 void TypeResolver::visit(FunctionDeclaration &func)
183 func.return_type_declaration = resolve_type(func.return_type);
184 TraversingVisitor::visit(func);
188 VariableResolver::VariableResolver():
191 record_target(false),
192 r_self_referencing(false),
193 r_assignment_target(0)
196 void VariableResolver::apply(Stage &s)
199 s.interface_blocks.clear();
200 s.content.visit(*this);
203 void VariableResolver::enter(Block &block)
205 block.variables.clear();
208 void VariableResolver::visit(VariableReference &var)
212 /* Look for variable declarations in the block hierarchy first. Interface
213 blocks are always defined in the top level so we can't accidentally skip
215 for(Block *block=current_block; (!var.declaration && block); block=block->parent)
217 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
218 if(i!=block->variables.end())
219 var.declaration = i->second;
224 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
225 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
228 /* The name refers to an interface block with an instance name rather
229 than a variable. Prepare a new syntax tree node accordingly. */
230 r_iface_ref = new InterfaceBlockReference;
231 r_iface_ref->source = var.source;
232 r_iface_ref->line = var.line;
233 r_iface_ref->name = var.name;
234 r_iface_ref->declaration = i->second;
235 r_members = &i->second->members.variables;
239 // Look for the variable in anonymous interface blocks.
240 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
241 if(i->second->instance_name.empty())
243 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
244 if(j!=i->second->members.variables.end())
245 var.declaration = j->second;
251 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(var.declaration->type_declaration))
252 r_members = &strct->members.variables;
256 if(r_assignment_target)
258 /* More than one variable reference found in assignment target.
259 Unable to determine what the primary target is. */
260 record_target = false;
261 r_assignment_target = 0;
264 r_assignment_target = var.declaration;
266 else if(var.declaration && var.declaration==r_assignment_target)
267 r_self_referencing = true;
270 void VariableResolver::visit(InterfaceBlockReference &iface)
272 iface.declaration = 0;
273 for(Block *block=current_block; block; block=block->parent)
275 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
276 if(i!=stage->interface_blocks.end())
278 iface.declaration = i->second;
279 r_members = &i->second->members.variables;
285 void VariableResolver::visit(MemberAccess &memacc)
289 memacc.left->visit(*this);
292 memacc.left = r_iface_ref;
295 memacc.declaration = 0;
298 map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
299 if(i!=r_members->end())
301 memacc.declaration = i->second;
302 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second->type_declaration))
303 r_members = &strct->members.variables;
310 void VariableResolver::visit(UnaryExpression &unary)
312 TraversingVisitor::visit(unary);
317 void VariableResolver::visit(BinaryExpression &binary)
319 if(binary.oper->token[0]=='[')
322 /* The subscript expression is not a part of the primary assignment
324 SetFlag set(record_target, false);
325 binary.right->visit(*this);
329 binary.left->visit(*this);
331 binary.left = r_iface_ref;
335 TraversingVisitor::visit(binary);
342 void VariableResolver::visit(Assignment &assign)
345 SetFlag set(record_target);
346 r_assignment_target = 0;
347 assign.left->visit(*this);
348 assign.target_declaration = r_assignment_target;
351 r_self_referencing = false;
352 assign.right->visit(*this);
353 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
359 void VariableResolver::visit(FunctionCall &call)
361 TraversingVisitor::visit(call);
366 void VariableResolver::visit(VariableDeclaration &var)
368 if(!block_interface.empty() && var.interface.empty())
369 var.interface = block_interface;
371 TraversingVisitor::visit(var);
372 current_block->variables.insert(make_pair(var.name, &var));
375 void VariableResolver::visit(InterfaceBlock &iface)
377 /* Block names can be reused in different interfaces. Prefix the name with
378 the first character of the interface to avoid conflicts. */
379 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
380 if(!iface.instance_name.empty())
381 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
383 SetForScope<string> set_iface(block_interface, iface.interface);
384 TraversingVisitor::visit(iface);
388 void FunctionResolver::apply(Stage &s)
392 s.content.visit(*this);
395 void FunctionResolver::visit(FunctionCall &call)
397 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
398 if(i!=stage->functions.end())
399 call.declaration = i->second;
401 TraversingVisitor::visit(call);
404 void FunctionResolver::visit(FunctionDeclaration &func)
406 FunctionDeclaration *&stage_decl = stage->functions[func.name];
407 vector<FunctionDeclaration *> &decls = declarations[func.name];
408 if(func.definition==&func)
412 // Set all previous declarations to use this definition.
413 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
415 (*i)->definition = func.definition;
416 (*i)->body.body.clear();
425 func.definition = stage_decl->definition;
427 decls.push_back(&func);
429 TraversingVisitor::visit(func);
433 InterfaceGenerator::InterfaceGenerator():
435 function_scope(false),
438 iface_target_block(0)
441 string InterfaceGenerator::get_out_prefix(Stage::Type type)
443 if(type==Stage::VERTEX)
445 else if(type==Stage::GEOMETRY)
451 void InterfaceGenerator::apply(Stage &s)
454 iface_target_block = &stage->content;
456 in_prefix = get_out_prefix(stage->previous->type);
457 out_prefix = get_out_prefix(stage->type);
458 s.content.visit(*this);
459 NodeRemover().apply(s, nodes_to_remove);
462 void InterfaceGenerator::visit(Block &block)
464 SetForScope<Block *> set_block(current_block, &block);
465 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
467 assignment_insert_point = i;
468 if(&block==&stage->content)
469 iface_insert_point = i;
475 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
477 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
478 return prefix+name.substr(offset);
481 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
483 if(stage->content.variables.count(name))
486 VariableDeclaration* iface_var = new VariableDeclaration;
487 iface_var->sampling = var.sampling;
488 iface_var->interface = iface;
489 iface_var->type = var.type;
490 iface_var->name = name;
491 /* Geometry shader inputs are always arrays. But if we're bringing in an
492 entire block, the array is on the block and not individual variables. */
493 if(stage->type==Stage::GEOMETRY && !copy_block)
494 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
496 iface_var->array = var.array;
498 iface_var->array_size = var.array_size;
501 iface_var->layout = var.layout;
502 iface_var->linked_declaration = &var;
503 var.linked_declaration = iface_var;
506 iface_target_block->body.insert(iface_insert_point, iface_var);
507 iface_target_block->variables.insert(make_pair(name, iface_var));
512 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
514 if(stage->interface_blocks.count("in"+out_block.name))
517 InterfaceBlock *in_block = new InterfaceBlock;
518 in_block->interface = "in";
519 in_block->name = out_block.name;
520 in_block->instance_name = out_block.instance_name;
521 if(stage->type==Stage::GEOMETRY)
522 in_block->array = true;
524 in_block->array = out_block.array;
525 in_block->linked_block = &out_block;
526 out_block.linked_block = in_block;
529 SetFlag set_copy(copy_block, true);
530 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
531 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
532 out_block.members.visit(*this);
535 iface_target_block->body.insert(iface_insert_point, in_block);
536 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
537 if(!in_block->instance_name.empty())
538 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
540 SetFlag set_scope(function_scope, false);
541 SetForScope<Block *> set_block(current_block, &stage->content);
542 in_block->visit(*this);
547 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
549 Assignment *assign = new Assignment;
550 VariableReference *ref = new VariableReference;
553 assign->oper = &Operator::get_operator("=", Operator::BINARY);
554 assign->right = right;
556 ExpressionStatement *stmt = new ExpressionStatement;
557 stmt->expression = assign;
558 current_block->body.insert(assignment_insert_point, stmt);
564 void InterfaceGenerator::visit(VariableReference &var)
566 if(var.declaration || !stage->previous)
568 /* Don't pull a variable from previous stage if we just generated an output
569 interface in this stage */
570 if(stage->content.variables.count(var.name))
573 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
574 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
575 if(i==prev_vars.end() || i->second->interface!="out")
576 i = prev_vars.find(in_prefix+var.name);
577 if(i!=prev_vars.end() && i->second->interface=="out")
579 generate_interface(*i->second, "in", i->second->name);
580 var.name = i->second->name;
584 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
585 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
586 if(j!=prev_blocks.end() && j->second->interface=="out")
588 generate_interface(*j->second);
589 /* Let VariableResolver convert the variable reference into an interface
594 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
595 if(j->second->instance_name.empty())
597 i = j->second->members.variables.find(var.name);
598 if(i!=j->second->members.variables.end())
600 generate_interface(*j->second);
606 void InterfaceGenerator::visit(VariableDeclaration &var)
610 generate_interface(var, "in", var.name);
616 if(iface_block->linked_block)
618 // Link all variables to their counterparts in the linked block.
619 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
620 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
621 if(i!=linked_vars.end())
623 var.linked_declaration = i->second;
624 var.linked_declaration->linked_declaration = &var;
630 if(var.interface=="out")
632 /* For output variables in function scope, generate a global interface
633 and replace the local declaration with an assignment. */
634 VariableDeclaration *out_var = 0;
635 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
637 out_var->source = var.source;
638 out_var->line = var.line;
639 nodes_to_remove.insert(&var);
640 if(var.init_expression)
642 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
643 stmt.source = var.source;
644 stmt.line = var.line;
649 else if(var.interface=="in")
651 /* Try to link input variables in global scope with output variables from
653 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
655 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
656 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
657 if(i!=prev_vars.end() && i->second->interface=="out")
659 var.linked_declaration = i->second;
660 i->second->linked_declaration = &var;
665 TraversingVisitor::visit(var);
668 void InterfaceGenerator::visit(InterfaceBlock &iface)
670 if(iface.interface=="in")
672 /* Try to link input blocks with output blocks sharing the same block
673 name from previous stage. */
674 if(!iface.linked_block && stage->previous)
676 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
677 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
678 if(i!=prev_blocks.end())
680 iface.linked_block = i->second;
681 i->second->linked_block = &iface;
686 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
687 TraversingVisitor::visit(iface);
690 void InterfaceGenerator::visit(FunctionDeclaration &func)
692 SetFlag set_scope(function_scope, true);
693 // Skip parameters because they're not useful here
694 func.body.visit(*this);
697 void InterfaceGenerator::visit(Passthrough &pass)
699 vector<VariableDeclaration *> pass_vars;
701 // Pass through all input variables of this stage.
702 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
703 if(i->second->interface=="in")
704 pass_vars.push_back(i->second);
708 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
709 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
711 if(i->second->interface!="out")
714 /* Pass through output variables from the previous stage, but only
715 those which are not already linked to an input here. */
716 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
717 pass_vars.push_back(i->second);
721 if(stage->type==Stage::GEOMETRY)
723 /* Special case for geometry shader: copy gl_Position from input to
725 InterfaceBlockReference *ref = new InterfaceBlockReference;
728 BinaryExpression *subscript = new BinaryExpression;
729 subscript->left = ref;
730 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
731 subscript->right = pass.subscript;
733 MemberAccess *memacc = new MemberAccess;
734 memacc->left = subscript;
735 memacc->member = "gl_Position";
737 insert_assignment("gl_Position", memacc);
740 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
742 string out_name = change_prefix((*i)->name, out_prefix);
743 generate_interface(**i, "out", out_name);
745 VariableReference *ref = new VariableReference;
746 ref->name = (*i)->name;
749 BinaryExpression *subscript = new BinaryExpression;
750 subscript->left = ref;
751 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
752 subscript->right = pass.subscript;
753 insert_assignment(out_name, subscript);
756 insert_assignment(out_name, ref);
759 nodes_to_remove.insert(&pass);