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(Block &block)
148 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
151 type_insert_point = i;
156 void TypeResolver::visit(BasicTypeDeclaration &type)
158 type.base_type = resolve_type(type.base);
160 if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type)
161 if(BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type))
162 if(basic_base->kind==BasicTypeDeclaration::VECTOR)
164 type.kind = BasicTypeDeclaration::MATRIX;
165 type.size |= basic_base->size<<16;
168 if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type)
169 alias_map[&type] = type.base_type;
170 else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type)
171 array_types[type.base_type] = &type;
173 stage->types.insert(make_pair(type.name, &type));
176 void TypeResolver::visit(ImageTypeDeclaration &type)
178 type.base_type = resolve_type(type.base);
179 stage->types.insert(make_pair(type.name, &type));
182 void TypeResolver::visit(StructDeclaration &strct)
184 stage->types.insert(make_pair(strct.name, &strct));
185 TraversingVisitor::visit(strct);
188 void TypeResolver::visit(VariableDeclaration &var)
190 TypeDeclaration *type = resolve_type(var.type);
191 if(var.array && type)
193 map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(type);
194 if(i==array_types.end())
196 BasicTypeDeclaration *array = new BasicTypeDeclaration;
197 array->source = BUILTIN_SOURCE;
198 array->name = type->name+"[]";
199 array->kind = BasicTypeDeclaration::ARRAY;
200 array->base = type->name;
201 array->base_type = type;
202 stage->content.body.insert(type_insert_point, array);
209 var.type_declaration = type;
212 void TypeResolver::visit(FunctionDeclaration &func)
214 func.return_type_declaration = resolve_type(func.return_type);
215 TraversingVisitor::visit(func);
219 VariableResolver::VariableResolver():
222 record_target(false),
223 r_self_referencing(false),
224 r_assignment_target(0)
227 void VariableResolver::apply(Stage &s)
230 s.interface_blocks.clear();
231 s.content.visit(*this);
234 void VariableResolver::enter(Block &block)
236 block.variables.clear();
239 void VariableResolver::visit(VariableReference &var)
243 /* Look for variable declarations in the block hierarchy first. Interface
244 blocks are always defined in the top level so we can't accidentally skip
246 for(Block *block=current_block; (!var.declaration && block); block=block->parent)
248 map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
249 if(i!=block->variables.end())
250 var.declaration = i->second;
255 const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
256 map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
259 /* The name refers to an interface block with an instance name rather
260 than a variable. Prepare a new syntax tree node accordingly. */
261 r_iface_ref = new InterfaceBlockReference;
262 r_iface_ref->source = var.source;
263 r_iface_ref->line = var.line;
264 r_iface_ref->name = var.name;
265 r_iface_ref->declaration = i->second;
266 r_members = &i->second->members.variables;
270 // Look for the variable in anonymous interface blocks.
271 for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
272 if(i->second->instance_name.empty())
274 map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
275 if(j!=i->second->members.variables.end())
276 var.declaration = j->second;
282 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(var.declaration->type_declaration))
283 r_members = &strct->members.variables;
287 if(r_assignment_target)
289 /* More than one variable reference found in assignment target.
290 Unable to determine what the primary target is. */
291 record_target = false;
292 r_assignment_target = 0;
295 r_assignment_target = var.declaration;
297 else if(var.declaration && var.declaration==r_assignment_target)
298 r_self_referencing = true;
301 void VariableResolver::visit(InterfaceBlockReference &iface)
303 iface.declaration = 0;
304 map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
305 if(i!=stage->interface_blocks.end())
307 iface.declaration = i->second;
308 r_members = &i->second->members.variables;
312 void VariableResolver::visit(MemberAccess &memacc)
316 memacc.left->visit(*this);
319 memacc.left = r_iface_ref;
322 memacc.declaration = 0;
325 map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
326 if(i!=r_members->end())
328 memacc.declaration = i->second;
329 if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(i->second->type_declaration))
330 r_members = &strct->members.variables;
337 void VariableResolver::visit(UnaryExpression &unary)
339 TraversingVisitor::visit(unary);
344 void VariableResolver::visit(BinaryExpression &binary)
346 if(binary.oper->token[0]=='[')
349 /* The subscript expression is not a part of the primary assignment
351 SetFlag set(record_target, false);
352 binary.right->visit(*this);
356 binary.left->visit(*this);
358 binary.left = r_iface_ref;
362 TraversingVisitor::visit(binary);
369 void VariableResolver::visit(Assignment &assign)
372 SetFlag set(record_target);
373 r_assignment_target = 0;
374 assign.left->visit(*this);
375 assign.target_declaration = r_assignment_target;
378 r_self_referencing = false;
379 assign.right->visit(*this);
380 assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
386 void VariableResolver::visit(FunctionCall &call)
388 TraversingVisitor::visit(call);
393 void VariableResolver::visit(VariableDeclaration &var)
395 if(!block_interface.empty() && var.interface.empty())
396 var.interface = block_interface;
398 TraversingVisitor::visit(var);
399 current_block->variables.insert(make_pair(var.name, &var));
402 void VariableResolver::visit(InterfaceBlock &iface)
404 /* Block names can be reused in different interfaces. Prefix the name with
405 the first character of the interface to avoid conflicts. */
406 stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
407 if(!iface.instance_name.empty())
408 stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
410 SetForScope<string> set_iface(block_interface, iface.interface);
411 TraversingVisitor::visit(iface);
415 bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
417 return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
420 bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
422 return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
425 BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
427 if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
429 BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
430 return (basic_base ? get_element_type(*basic_base) : 0);
436 bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
438 if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
439 return from.size<=to.size;
440 else if(from.kind!=to.kind)
442 else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
444 BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
445 BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
446 return (from_base && to_base && can_convert(*from_base, *to_base));
452 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
456 else if(can_convert(left, right))
457 return LEFT_CONVERTIBLE;
458 else if(can_convert(right, left))
459 return RIGHT_CONVERTIBLE;
461 return NOT_COMPATIBLE;
464 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
466 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
467 if((*i)->kind==kind && (*i)->size==size)
472 BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
474 for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
475 if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
480 void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
482 RefPtr<FunctionCall> call = new FunctionCall;
483 call->name = type.name;
484 call->constructor = true;
485 call->arguments.push_back(0);
486 call->arguments.back() = expr;
491 bool ExpressionResolver::convert_to_element(RefPtr<Expression> &expr, BasicTypeDeclaration &elem_type)
493 if(BasicTypeDeclaration *expr_type = dynamic_cast<BasicTypeDeclaration *>(expr->type))
495 BasicTypeDeclaration *to_type = &elem_type;
496 if(is_vector_or_matrix(*expr_type))
497 to_type = find_type(elem_type, expr_type->kind, expr_type->size);
500 convert_to(expr, *to_type);
508 void ExpressionResolver::visit(Literal &literal)
510 if(literal.value.check_type<bool>())
511 literal.type = find_type(BasicTypeDeclaration::BOOL, 1);
512 else if(literal.value.check_type<int>())
513 literal.type = find_type(BasicTypeDeclaration::INT, 32);
514 else if(literal.value.check_type<float>())
515 literal.type = find_type(BasicTypeDeclaration::FLOAT, 32);
518 void ExpressionResolver::visit(ParenthesizedExpression &parexpr)
520 TraversingVisitor::visit(parexpr);
522 parexpr.type = parexpr.expression->type;
523 parexpr.lvalue = parexpr.expression->lvalue;
526 void ExpressionResolver::visit(VariableReference &var)
529 var.type = var.declaration->type_declaration;
533 void ExpressionResolver::visit(InterfaceBlockReference &iface)
538 void ExpressionResolver::visit(MemberAccess &memacc)
540 TraversingVisitor::visit(memacc);
542 if(memacc.declaration)
543 memacc.type = memacc.declaration->type_declaration;
544 memacc.lvalue = memacc.left->lvalue;
547 void ExpressionResolver::visit(UnaryExpression &unary)
549 TraversingVisitor::visit(unary);
551 BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(unary.expression->type);
555 char oper = unary.oper->token[0];
558 if(basic->kind==BasicTypeDeclaration::BOOL)
563 if(basic->kind==BasicTypeDeclaration::INT)
566 else if(oper=='+' || oper=='-')
568 BasicTypeDeclaration *elem = get_element_type(*basic);
569 if(elem && is_scalar(*elem))
572 unary.lvalue = unary.expression->lvalue;
575 void ExpressionResolver::visit(BinaryExpression &binary)
577 TraversingVisitor::visit(binary);
579 /* Binary operators are only defined for basic types (not for image or
581 BasicTypeDeclaration *basic_left = dynamic_cast<BasicTypeDeclaration *>(binary.left->type);
582 BasicTypeDeclaration *basic_right = dynamic_cast<BasicTypeDeclaration *>(binary.right->type);
583 if(!basic_left || !basic_right)
586 binary.lvalue = false;
588 char oper = binary.oper->token[0];
591 /* Subscripting operates on vectors, matrices and arrays, and the right
592 operand must be an integer. */
593 if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT)
596 binary.type = basic_left->base_type;
597 binary.lvalue = binary.left->lvalue;
600 else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY)
601 // No other binary operator can be used with arrays.
604 BasicTypeDeclaration *elem_left = get_element_type(*basic_left);
605 BasicTypeDeclaration *elem_right = get_element_type(*basic_right);
606 if(!elem_left || !elem_right)
609 Compatibility compat = get_compatibility(*basic_left, *basic_right);
610 Compatibility elem_compat = get_compatibility(*elem_left, *elem_right);
611 if(elem_compat==NOT_COMPATIBLE)
614 char oper2 = binary.oper->token[1];
615 if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>'))
617 /* Relational operators compare two scalar integer or floating-point
619 if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE)
622 binary.type = find_type(BasicTypeDeclaration::BOOL, 1);
624 else if((oper=='=' || oper=='!') && oper2=='=')
626 // Equality comparison can be done on any compatible types.
627 if(compat==NOT_COMPATIBLE)
630 binary.type = find_type(BasicTypeDeclaration::BOOL, 1);
632 else if(oper2=='&' || oper2=='|' || oper2=='^')
634 // Logical operators can only be applied to booleans.
635 if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL)
638 binary.type = basic_left;
640 else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2)
642 // Bitwise operators and modulo can only be applied to integers.
643 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
646 binary.type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left);
648 else if((oper=='<' || oper=='>') && oper2==oper)
650 // Shifts only apply to integers.
651 if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT)
654 binary.type = basic_left;
656 else if(oper=='+' || oper=='-' || oper=='*' || oper=='/')
658 // Arithmetic operators require scalar elements.
659 if(!is_scalar(*elem_left) || !is_scalar(*elem_right))
662 if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) &&
663 (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX))
665 /* Multiplication has special rules when at least one operand is a
666 matrix and the other is a vector or a matrix. */
667 unsigned left_columns = basic_left->size&0xFFFF;
668 unsigned right_rows = basic_right->size;
669 if(basic_right->kind==BasicTypeDeclaration::MATRIX)
671 if(left_columns!=right_rows)
674 BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left);
676 if(basic_left->kind==BasicTypeDeclaration::VECTOR)
677 binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF);
678 else if(basic_right->kind==BasicTypeDeclaration::VECTOR)
679 binary.type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16);
681 binary.type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF));
683 else if(compat==NOT_COMPATIBLE)
685 // Arithmetic between scalars and matrices or vectors is supported.
686 if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right))
687 binary.type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right);
688 else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right))
689 binary.type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left);
693 else if(compat==LEFT_CONVERTIBLE)
694 binary.type = basic_right;
696 binary.type = basic_left;
701 bool converted = true;
702 if(compat==LEFT_CONVERTIBLE)
703 convert_to(binary.left, *basic_right);
704 else if(compat==RIGHT_CONVERTIBLE)
705 convert_to(binary.right, *basic_left);
706 else if(elem_compat==LEFT_CONVERTIBLE)
707 converted = convert_to_element(binary.left, *elem_right);
708 else if(elem_compat==RIGHT_CONVERTIBLE)
709 converted = convert_to_element(binary.right, *elem_left);
715 void ExpressionResolver::visit(Assignment &assign)
717 TraversingVisitor::visit(assign);
718 assign.type = assign.left->type;
719 assign.lvalue = true;
722 void ExpressionResolver::visit(FunctionCall &call)
724 TraversingVisitor::visit(call);
727 call.type = call.declaration->return_type_declaration;
728 else if(call.constructor)
730 map<string, TypeDeclaration *>::const_iterator i=stage->types.find(call.name);
731 call.type = (i!=stage->types.end() ? i->second : 0);
736 void ExpressionResolver::visit(BasicTypeDeclaration &type)
738 basic_types.push_back(&type);
741 void ExpressionResolver::visit(VariableDeclaration &var)
743 TraversingVisitor::visit(var);
744 if(!var.init_expression)
747 BasicTypeDeclaration *var_basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration);
748 BasicTypeDeclaration *init_basic = dynamic_cast<BasicTypeDeclaration *>(var.init_expression->type);
749 if(!var_basic || !init_basic)
752 Compatibility compat = get_compatibility(*var_basic, *init_basic);
753 if(compat==RIGHT_CONVERTIBLE)
754 convert_to(var.init_expression, *var_basic);
758 void FunctionResolver::apply(Stage &s)
762 s.content.visit(*this);
765 void FunctionResolver::visit(FunctionCall &call)
767 map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
768 if(i!=stage->functions.end())
769 call.declaration = i->second;
771 TraversingVisitor::visit(call);
774 void FunctionResolver::visit(FunctionDeclaration &func)
776 FunctionDeclaration *&stage_decl = stage->functions[func.name];
777 vector<FunctionDeclaration *> &decls = declarations[func.name];
778 if(func.definition==&func)
782 // Set all previous declarations to use this definition.
783 for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
785 (*i)->definition = func.definition;
786 (*i)->body.body.clear();
795 func.definition = stage_decl->definition;
797 decls.push_back(&func);
799 TraversingVisitor::visit(func);
803 InterfaceGenerator::InterfaceGenerator():
805 function_scope(false),
808 iface_target_block(0)
811 string InterfaceGenerator::get_out_prefix(Stage::Type type)
813 if(type==Stage::VERTEX)
815 else if(type==Stage::GEOMETRY)
821 void InterfaceGenerator::apply(Stage &s)
824 iface_target_block = &stage->content;
826 in_prefix = get_out_prefix(stage->previous->type);
827 out_prefix = get_out_prefix(stage->type);
828 s.content.visit(*this);
829 NodeRemover().apply(s, nodes_to_remove);
832 void InterfaceGenerator::visit(Block &block)
834 SetForScope<Block *> set_block(current_block, &block);
835 for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
837 assignment_insert_point = i;
838 if(&block==&stage->content)
839 iface_insert_point = i;
845 string InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
847 unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
848 return prefix+name.substr(offset);
851 VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
853 if(stage->content.variables.count(name))
856 VariableDeclaration* iface_var = new VariableDeclaration;
857 iface_var->sampling = var.sampling;
858 iface_var->interface = iface;
859 iface_var->type = var.type;
860 iface_var->name = name;
861 /* Geometry shader inputs are always arrays. But if we're bringing in an
862 entire block, the array is on the block and not individual variables. */
863 if(stage->type==Stage::GEOMETRY && !copy_block)
864 iface_var->array = ((var.array && var.interface!="in") || iface=="in");
866 iface_var->array = var.array;
868 iface_var->array_size = var.array_size;
871 iface_var->layout = var.layout;
872 iface_var->linked_declaration = &var;
873 var.linked_declaration = iface_var;
876 iface_target_block->body.insert(iface_insert_point, iface_var);
877 iface_target_block->variables.insert(make_pair(name, iface_var));
882 InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
884 if(stage->interface_blocks.count("in"+out_block.name))
887 InterfaceBlock *in_block = new InterfaceBlock;
888 in_block->interface = "in";
889 in_block->name = out_block.name;
890 in_block->instance_name = out_block.instance_name;
891 if(stage->type==Stage::GEOMETRY)
892 in_block->array = true;
894 in_block->array = out_block.array;
895 in_block->linked_block = &out_block;
896 out_block.linked_block = in_block;
899 SetFlag set_copy(copy_block, true);
900 SetForScope<Block *> set_target(iface_target_block, &in_block->members);
901 SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
902 out_block.members.visit(*this);
905 iface_target_block->body.insert(iface_insert_point, in_block);
906 stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
907 if(!in_block->instance_name.empty())
908 stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
910 SetFlag set_scope(function_scope, false);
911 SetForScope<Block *> set_block(current_block, &stage->content);
912 in_block->visit(*this);
917 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
919 Assignment *assign = new Assignment;
920 VariableReference *ref = new VariableReference;
923 assign->oper = &Operator::get_operator("=", Operator::BINARY);
924 assign->right = right;
926 ExpressionStatement *stmt = new ExpressionStatement;
927 stmt->expression = assign;
928 current_block->body.insert(assignment_insert_point, stmt);
934 void InterfaceGenerator::visit(VariableReference &var)
936 if(var.declaration || !stage->previous)
938 /* Don't pull a variable from previous stage if we just generated an output
939 interface in this stage */
940 if(stage->content.variables.count(var.name))
943 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
944 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
945 if(i==prev_vars.end() || i->second->interface!="out")
946 i = prev_vars.find(in_prefix+var.name);
947 if(i!=prev_vars.end() && i->second->interface=="out")
949 generate_interface(*i->second, "in", i->second->name);
950 var.name = i->second->name;
954 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
955 map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
956 if(j!=prev_blocks.end() && j->second->interface=="out")
958 generate_interface(*j->second);
959 /* Let VariableResolver convert the variable reference into an interface
964 for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
965 if(j->second->instance_name.empty())
967 i = j->second->members.variables.find(var.name);
968 if(i!=j->second->members.variables.end())
970 generate_interface(*j->second);
976 void InterfaceGenerator::visit(VariableDeclaration &var)
980 generate_interface(var, "in", var.name);
986 if(iface_block->linked_block)
988 // Link all variables to their counterparts in the linked block.
989 const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
990 map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
991 if(i!=linked_vars.end())
993 var.linked_declaration = i->second;
994 var.linked_declaration->linked_declaration = &var;
1000 if(var.interface=="out")
1002 /* For output variables in function scope, generate a global interface
1003 and replace the local declaration with an assignment. */
1004 VariableDeclaration *out_var = 0;
1005 if(function_scope && (out_var=generate_interface(var, "out", var.name)))
1007 out_var->source = var.source;
1008 out_var->line = var.line;
1009 nodes_to_remove.insert(&var);
1010 if(var.init_expression)
1012 ExpressionStatement &stmt = insert_assignment(var.name, var.init_expression->clone());
1013 stmt.source = var.source;
1014 stmt.line = var.line;
1019 else if(var.interface=="in")
1021 /* Try to link input variables in global scope with output variables from
1023 if(current_block==&stage->content && !var.linked_declaration && stage->previous)
1025 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1026 map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
1027 if(i!=prev_vars.end() && i->second->interface=="out")
1029 var.linked_declaration = i->second;
1030 i->second->linked_declaration = &var;
1035 TraversingVisitor::visit(var);
1038 void InterfaceGenerator::visit(InterfaceBlock &iface)
1040 if(iface.interface=="in")
1042 /* Try to link input blocks with output blocks sharing the same block
1043 name from previous stage. */
1044 if(!iface.linked_block && stage->previous)
1046 const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
1047 map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
1048 if(i!=prev_blocks.end())
1050 iface.linked_block = i->second;
1051 i->second->linked_block = &iface;
1056 SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
1057 TraversingVisitor::visit(iface);
1060 void InterfaceGenerator::visit(FunctionDeclaration &func)
1062 SetFlag set_scope(function_scope, true);
1063 // Skip parameters because they're not useful here
1064 func.body.visit(*this);
1067 void InterfaceGenerator::visit(Passthrough &pass)
1069 vector<VariableDeclaration *> pass_vars;
1071 // Pass through all input variables of this stage.
1072 for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
1073 if(i->second->interface=="in")
1074 pass_vars.push_back(i->second);
1078 const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
1079 for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
1081 if(i->second->interface!="out")
1084 /* Pass through output variables from the previous stage, but only
1085 those which are not already linked to an input here. */
1086 if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
1087 pass_vars.push_back(i->second);
1091 if(stage->type==Stage::GEOMETRY)
1093 /* Special case for geometry shader: copy gl_Position from input to
1095 InterfaceBlockReference *ref = new InterfaceBlockReference;
1096 ref->name = "gl_in";
1098 BinaryExpression *subscript = new BinaryExpression;
1099 subscript->left = ref;
1100 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1101 subscript->right = pass.subscript;
1103 MemberAccess *memacc = new MemberAccess;
1104 memacc->left = subscript;
1105 memacc->member = "gl_Position";
1107 insert_assignment("gl_Position", memacc);
1110 for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
1112 string out_name = change_prefix((*i)->name, out_prefix);
1113 generate_interface(**i, "out", out_name);
1115 VariableReference *ref = new VariableReference;
1116 ref->name = (*i)->name;
1119 BinaryExpression *subscript = new BinaryExpression;
1120 subscript->left = ref;
1121 subscript->oper = &Operator::get_operator("[", Operator::BINARY);
1122 subscript->right = pass.subscript;
1123 insert_assignment(out_name, subscript);
1126 insert_assignment(out_name, ref);
1129 nodes_to_remove.insert(&pass);