+ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
+ context(0),
+ scope_level(0),
+ remove_node(false)
+{ }
+
+string ProgramCompiler::InterfaceGenerator::get_out_prefix(ContextType type)
+{
+ if(type==VERTEX)
+ return "_vs_out_";
+ else if(type==GEOMETRY)
+ return "_gs_out_";
+ else
+ return string();
+}
+
+void ProgramCompiler::InterfaceGenerator::visit(Context &ctx)
+{
+ SetForScope<Context *> set(context, &ctx);
+ if(context->previous)
+ in_prefix = get_out_prefix(context->previous->type);
+ out_prefix = get_out_prefix(context->type);
+ ctx.content.visit(*this);
+}
+
+void ProgramCompiler::InterfaceGenerator::visit(Block &block)
+{
+ SetForScope<unsigned> set(scope_level, scope_level+1);
+ for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+ {
+ (*i)->visit(*this);
+
+ if(scope_level==1)
+ {
+ for(map<string, NodePtr<Node> >::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
+ {
+ list<NodePtr<Node> >::iterator k = block.body.insert(i, j->second);
+ (*k)->visit(*this);
+ }
+ iface_declarations.clear();
+ }
+
+ for(list<NodePtr<Node> >::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
+ block.body.insert(i, *j);
+ insert_nodes.clear();
+
+ if(remove_node)
+ block.body.erase(i++);
+ else
+ ++i;
+ remove_node = false;
+ }
+}
+
+string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, const string &prefix) const
+{
+ unsigned offset = (name.compare(0, in_prefix.size(), in_prefix) ? 0 : in_prefix.size());
+ return prefix+name.substr(offset);
+}
+
+bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &out, const string &iface, const string &name)
+{
+ const map<string, VariableDeclaration *> &context_vars = (iface=="in" ? context->in_variables : context->out_variables);
+ if(context_vars.count(name) || iface_declarations.count(name))
+ return false;
+
+ VariableDeclaration* iface_var = new VariableDeclaration;
+ iface_var->sampling = out.sampling;
+ iface_var->interface = iface;
+ iface_var->type = out.type;
+ iface_var->type_declaration = out.type_declaration;
+ iface_var->name = name;
+ iface_var->array = (out.array || (context->type==GEOMETRY && iface=="in"));
+ iface_var->array_size = out.array_size;
+ if(iface=="in")
+ iface_var->linked_declaration = &out;
+ iface_declarations[iface_var->name] = iface_var;
+
+ return true;
+}
+
+void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left, ProgramSyntax::Expression *right)
+{
+ BinaryExpression *assign = new BinaryExpression;
+ VariableReference *ref = new VariableReference;
+ ref->name = left;
+ assign->left = ref;
+ assign->oper = "=";
+ assign->right = right;
+ assign->assignment = true;
+
+ ExpressionStatement *stmt = new ExpressionStatement;
+ stmt->expression = assign;
+ insert_nodes.push_back(stmt);
+}
+
+void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
+{
+ if(var.declaration || !context->previous)
+ return;
+ if(iface_declarations.count(var.name))
+ return;
+
+ const map<string, VariableDeclaration *> &prev_out = context->previous->out_variables;
+ map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
+ if(i==prev_out.end())
+ i = prev_out.find(in_prefix+var.name);
+ if(i!=prev_out.end())
+ generate_interface(*i->second, "in", var.name);
+}
+
+void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
+{
+ if(var.interface=="out")
+ {
+ if(scope_level==1)
+ context->out_variables[var.name] = &var;
+ else if(generate_interface(var, "out", change_prefix(var.name, string())))
+ {
+ remove_node = true;
+ if(var.init_expression)
+ insert_assignment(var.name, var.init_expression->clone());
+ }
+ }
+ else if(var.interface=="in")
+ {
+ context->in_variables[var.name] = &var;
+ if(context->previous)
+ {
+ const map<string, VariableDeclaration *> &prev_out = context->previous->out_variables;
+ map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
+ if(i!=prev_out.end())
+ {
+ var.linked_declaration = i->second;
+ i->second->linked_declaration = &var;
+ }
+ }
+ }
+
+ TraversingVisitor::visit(var);
+}
+
+void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
+{
+ if(context->previous)
+ {
+ const map<string, VariableDeclaration *> &prev_out = context->previous->out_variables;
+ for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
+ {
+ string out_name = change_prefix(i->second->name, out_prefix);
+ generate_interface(*i->second, "in", i->second->name);
+ generate_interface(*i->second, "out", out_name);
+
+ VariableReference *ref = new VariableReference;
+ ref->name = i->first;
+ if(pass.subscript)
+ {
+ BinaryExpression *subscript = new BinaryExpression;
+ subscript->left = ref;
+ subscript->oper = "[";
+ subscript->right = pass.subscript;
+ subscript->after = "]";
+ insert_assignment(out_name, subscript);
+ }
+ else
+ insert_assignment(out_name, ref);
+ }
+ }
+
+ remove_node = true;
+}
+
+
+void ProgramCompiler::VariableRenamer::visit(VariableReference &var)
+{
+ if(var.declaration)
+ var.name = var.declaration->name;
+}
+
+void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
+{
+ if(var.linked_declaration)
+ var.name = var.linked_declaration->name;
+ TraversingVisitor::visit(var);
+}
+
+