]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / glsl / generate.cpp
index 7c619830c1733f43160323172ff19605a69632e4..635d9f15dcec1b6720a13df6285d83a6e329c3aa 100644 (file)
@@ -1,8 +1,8 @@
+#include <msp/core/algorithm.h>
 #include <msp/core/hash.h>
 #include <msp/core/raii.h>
-#include <msp/strings/lexicalcast.h>
-#include "builtin.h"
 #include "generate.h"
+#include "reflect.h"
 
 using namespace std;
 
@@ -10,373 +10,52 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-void DeclarationCombiner::apply(Stage &stage)
+void ConstantIdAssigner::apply(Module &module, const Features &features)
 {
-       stage.content.visit(*this);
-       NodeRemover().apply(stage, nodes_to_remove);
-}
-
-void DeclarationCombiner::visit(Block &block)
-{
-       if(current_block)
-               return;
+       for(Stage &s: module.stages)
+               s.content.visit(*this);
 
-       TraversingVisitor::visit(block);
-}
-
-void DeclarationCombiner::visit(VariableDeclaration &var)
-{
-       VariableDeclaration *&ptr = variables[var.name];
-       if(ptr)
+       for(VariableDeclaration *v: auto_constants)
        {
-               ptr->type = var.type;
-               if(var.init_expression)
-                       ptr->init_expression = var.init_expression;
-               if(var.layout)
-               {
-                       if(ptr->layout)
-                       {
-                               for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
-                               {
-                                       bool found = false;
-                                       for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
-                                               if(j->name==i->name)
-                                               {
-                                                       j->has_value = i->value;
-                                                       j->value = i->value;
-                                                       found = true;
-                                               }
-
-                                       if(!found)
-                                               ptr->layout->qualifiers.push_back(*i);
-                               }
-                       }
-                       else
-                               ptr->layout = var.layout;
-               }
-               nodes_to_remove.insert(&var);
-       }
-       else
-               ptr = &var;
-}
-
-
-ConstantSpecializer::ConstantSpecializer():
-       values(0)
-{ }
-
-void ConstantSpecializer::apply(Stage &stage, const map<string, int> *v)
-{
-       values = v;
-       stage.content.visit(*this);
-}
-
-void ConstantSpecializer::visit(VariableDeclaration &var)
-{
-       bool specializable = false;
-       if(var.layout)
-       {
-               vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
-               for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
-                       if(i->name=="constant_id")
-                       {
-                               specializable = true;
-                               if(values)
-                                       qualifiers.erase(i);
-                               else if(i->value==-1)
-                                       i->value = hash32(var.name)&0x7FFFFFFF;
-                               break;
-                       }
-
-               if(qualifiers.empty())
-                       var.layout = 0;
-       }
-
-       if(specializable && values)
-       {
-               map<string, int>::const_iterator i = values->find(var.name);
-               if(i!=values->end())
-               {
-                       RefPtr<Literal> literal = new Literal;
-                       if(var.type=="bool")
-                               literal->token = (i->second ? "true" : "false");
-                       else if(var.type=="int")
-                               literal->token = lexical_cast<string>(i->second);
-                       var.init_expression = literal;
-               }
-       }
-}
-
-
-void BlockHierarchyResolver::enter(Block &block)
-{
-       block.parent = current_block;
-}
-
-
-VariableResolver::VariableResolver():
-       stage(0),
-       r_members(0),
-       record_target(false),
-       r_self_referencing(false),
-       r_assignment_target(0)
-{ }
-
-void VariableResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.types.clear();
-       s.interface_blocks.clear();
-       s.content.visit(*this);
-}
-
-void VariableResolver::enter(Block &block)
-{
-       block.variables.clear();
-}
-
-void VariableResolver::visit(VariableReference &var)
-{
-       var.declaration = 0;
-       r_members = 0;
-       /* Look for variable declarations in the block hierarchy first.  Interface
-       blocks are always defined in the top level so we can't accidentally skip
-       one. */
-       for(Block *block=current_block; (!var.declaration && block); block=block->parent)
-       {
-               map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
-               if(i!=block->variables.end())
-                       var.declaration = i->second;
-       }
-
-       if(var.declaration)
-       {
-               if(var.declaration->type_declaration)
-                       r_members = &var.declaration->type_declaration->members.variables;
-       }
-       else
-       {
-               const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
-               map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
-               if(i!=blocks.end() && i->second->instance_name==var.name)
-               {
-                       /* The name refers to an interface block with an instance name rather
-                       than a variable.  Prepare a new syntax tree node accordingly. */
-                       r_iface_ref = new InterfaceBlockReference;
-                       r_iface_ref->name = var.name;
-                       r_iface_ref->declaration = i->second;
-                       r_members = &i->second->members.variables;
-               }
+               unsigned id;
+               auto j = existing_constants.find(v->name);
+               if(j!=existing_constants.end())
+                       id = j->second;
                else
                {
-                       // Look for the variable in anonymous interface blocks.
-                       for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
-                               if(i->second->instance_name.empty())
-                               {
-                                       map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
-                                       if(j!=i->second->members.variables.end())
-                                               var.declaration = j->second;
-                               }
+                       id = hash<32>(v->name)%features.constant_id_range;
+                       while(used_ids.count(id))
+                               id = (id+1)%features.constant_id_range;
                }
-       }
 
-       if(record_target)
-       {
-               if(r_assignment_target)
-               {
-                       /* More than one variable reference found in assignment target.
-                       Unable to determine what the primary target is. */
-                       record_target = false;
-                       r_assignment_target = 0;
-               }
-               else
-                       r_assignment_target = var.declaration;
-       }
-       else if(var.declaration && var.declaration==r_assignment_target)
-               r_self_referencing = true;
-}
-
-void VariableResolver::visit(InterfaceBlockReference &iface)
-{
-       iface.declaration = 0;
-       for(Block *block=current_block; block; block=block->parent)
-       {
-               map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
-               if(i!=stage->interface_blocks.end())
-               {
-                       iface.declaration = i->second;
-                       r_members = &i->second->members.variables;
-                       break;
-               }
-       }
-}
-
-void VariableResolver::visit(MemberAccess &memacc)
-{
-       r_members = 0;
-       r_iface_ref = 0;
-       memacc.left->visit(*this);
-
-       if(r_iface_ref)
-               memacc.left = r_iface_ref;
-       r_iface_ref = 0;
-
-       memacc.declaration = 0;
-       if(r_members)
-       {
-               map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
-               if(i!=r_members->end())
-               {
-                       memacc.declaration = i->second;
-                       if(i->second->type_declaration)
-                               r_members = &i->second->type_declaration->members.variables;
-               }
-               else
-                       r_members = 0;
-       }
-}
-
-void VariableResolver::visit(UnaryExpression &unary)
-{
-       TraversingVisitor::visit(unary);
-       r_members = 0;
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(BinaryExpression &binary)
-{
-       if(binary.oper->token[0]=='[')
-       {
-               {
-                       /* The subscript expression is not a part of the primary assignment
-                       target. */
-                       SetFlag set(record_target, false);
-                       binary.right->visit(*this);
-               }
-               r_members = 0;
-               r_iface_ref = 0;
-               binary.left->visit(*this);
-               if(r_iface_ref)
-                       binary.left = r_iface_ref;
-       }
-       else
-       {
-               TraversingVisitor::visit(binary);
-               r_members = 0;
-       }
+               auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+               if(i!=v->layout->qualifiers.end())
+                       i->value = id;
 
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(Assignment &assign)
-{
-       {
-               SetFlag set(record_target);
-               r_assignment_target = 0;
-               assign.left->visit(*this);
-               assign.target_declaration = r_assignment_target;
+               used_ids.insert(id);
+               existing_constants[v->name] = id;
        }
-
-       r_self_referencing = false;
-       assign.right->visit(*this);
-       assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
-
-       r_members = 0;
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(FunctionCall &call)
-{
-       TraversingVisitor::visit(call);
-       r_members = 0;
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(StructDeclaration &strct)
-{
-       TraversingVisitor::visit(strct);
-       stage->types[strct.name] = &strct;
-}
-
-void VariableResolver::visit(VariableDeclaration &var)
-{
-       map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
-       if(i!=stage->types.end())
-               var.type_declaration = i->second;
-
-       if(!block_interface.empty() && var.interface.empty())
-               var.interface = block_interface;
-
-       TraversingVisitor::visit(var);
-       current_block->variables[var.name] = &var;
 }
 
-void VariableResolver::visit(InterfaceBlock &iface)
+void ConstantIdAssigner::visit(VariableDeclaration &var)
 {
-       /* Block names can't be used for any other identifiers so we can put them
-       in the same map with instance names. */
-       stage->interface_blocks[iface.name] = &iface;
-       if(!iface.instance_name.empty())
-               stage->interface_blocks[iface.instance_name] = &iface;
-
-       SetForScope<string> set_iface(block_interface, iface.interface);
-       TraversingVisitor::visit(iface);
-}
-
-
-void FunctionResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.functions.clear();
-       s.content.visit(*this);
-}
-
-void FunctionResolver::visit(FunctionCall &call)
-{
-       map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
-       if(i!=stage->functions.end())
-               call.declaration = i->second;
-
-       TraversingVisitor::visit(call);
-}
-
-void FunctionResolver::visit(FunctionDeclaration &func)
-{
-       FunctionDeclaration *&stage_decl = stage->functions[func.name];
-       vector<FunctionDeclaration *> &decls = declarations[func.name];
-       if(func.definition==&func)
+       if(var.layout)
        {
-               stage_decl = &func;
-
-               // Set all previous declarations to use this definition.
-               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+               auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+               if(i!=var.layout->qualifiers.end() && i->has_value)
                {
-                       (*i)->definition = func.definition;
-                       (*i)->body.body.clear();
+                       if(i->value==-1)
+                               auto_constants.push_back(&var);
+                       else
+                       {
+                               existing_constants[var.name] = i->value;
+                               used_ids.insert(i->value);
+                       }
                }
        }
-       else
-       {
-               func.definition = 0;
-               if(!stage_decl)
-                       stage_decl = &func;
-               else
-                       func.definition = stage_decl->definition;
-       }
-       decls.push_back(&func);
-
-       TraversingVisitor::visit(func);
 }
 
 
-InterfaceGenerator::InterfaceGenerator():
-       stage(0),
-       function_scope(false),
-       iface_block(0),
-       copy_block(false),
-       iface_target_block(0)
-{ }
-
 string InterfaceGenerator::get_out_prefix(Stage::Type type)
 {
        if(type==Stage::VERTEX)
@@ -401,7 +80,7 @@ void InterfaceGenerator::apply(Stage &s)
 void InterfaceGenerator::visit(Block &block)
 {
        SetForScope<Block *> set_block(current_block, &block);
-       for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+       for(auto i=block.body.begin(); i!=block.body.end(); ++i)
        {
                assignment_insert_point = i;
                if(&block==&stage->content)
@@ -422,14 +101,28 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration
        if(stage->content.variables.count(name))
                return 0;
 
+       if(stage->type==Stage::GEOMETRY && var.interface=="out" && var.array)
+               return 0;
+
        VariableDeclaration* iface_var = new VariableDeclaration;
        iface_var->sampling = var.sampling;
+       if(stage->type==Stage::FRAGMENT && iface=="in")
+               if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(var.type_declaration))
+                       if(BasicTypeDeclaration *elem = get_element_type(*basic))
+                               if(elem->kind==BasicTypeDeclaration::INT)
+                                       iface_var->interpolation = "flat";
        iface_var->interface = iface;
        iface_var->type = var.type;
        iface_var->name = name;
-       /* Geometry shader inputs are always arrays.  But if we're bringing in an
-       entire block, the array is on the block and not individual variables. */
-       if(stage->type==Stage::GEOMETRY && !copy_block)
+       // Tessellation and geometry inputs may be arrayed.
+       if(stage->type==Stage::TESS_CONTROL)
+               // VS out -> TCS in: add | TCS in -> TCS out: unchanged | VS out -> TCS out: add
+               iface_var->array = (var.array || var.interface!="in");
+       else if(stage->type==Stage::TESS_EVAL)
+               // TCS out -> TES in: unchanged | TES in -> TES out: remove | TCS out -> TES out: remove
+               iface_var->array = (var.array && iface=="in");
+       else if(stage->type==Stage::GEOMETRY)
+               // VS/TES out -> GS in: add | GS in -> GS out: remove | VS/TES out -> GS out: unchanged
                iface_var->array = ((var.array && var.interface!="in") || iface=="in");
        else
                iface_var->array = var.array;
@@ -442,53 +135,49 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration
                var.linked_declaration = iface_var;
        }
 
-       iface_target_block->body.insert(iface_insert_point, iface_var);
-       iface_target_block->variables[name] = iface_var;
-
-       return iface_var;
-}
-
-InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
-{
-       if(stage->interface_blocks.count(out_block.name))
-               return 0;
+       if(var.block_declaration)
+       {
+               StructDeclaration *iface_type = var.block_declaration->clone();
+               iface_type->name = format("_%s_%s", iface, var.block_declaration->block_name);
+               iface_target_block->body.insert(iface_insert_point, iface_type);
 
-       InterfaceBlock *in_block = new InterfaceBlock;
-       in_block->interface = "in";
-       in_block->name = out_block.name;
-       in_block->instance_name = out_block.instance_name;
-       if(stage->type==Stage::GEOMETRY)
-               in_block->array = true;
-       else
-               in_block->array = out_block.array;
-       in_block->linked_block = &out_block;
-       out_block.linked_block = in_block;
+               iface_var->type = iface_type->name;
+               if(name.empty())
+                       iface_var->name = format("%s %s", iface, var.block_declaration->block_name);
 
-       {
-               SetFlag set_copy(copy_block, true);
-               SetForScope<Block *> set_target(iface_target_block, &in_block->members);
-               SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
-               out_block.members.visit(*this);
+               stage->interface_blocks.insert(make_pair("in "+var.block_declaration->block_name, iface_var));
+               if(!name.empty())
+                       stage->interface_blocks.insert(make_pair(name, iface_var));
        }
 
-       iface_target_block->body.insert(iface_insert_point, in_block);
-       stage->interface_blocks[in_block->name] = in_block;
-       if(!in_block->instance_name.empty())
-               stage->interface_blocks[in_block->instance_name] = in_block;
-
-       SetFlag set_scope(function_scope, false);
-       SetForScope<Block *> set_block(current_block, &stage->content);
-       in_block->visit(*this);
+       iface_target_block->body.insert(iface_insert_point, iface_var);
+       iface_target_block->variables.insert(make_pair(name, iface_var));
+       if(iface_target_block==&stage->content && iface=="in")
+               declared_inputs.push_back(iface_var);
 
-       return in_block;
+       return iface_var;
 }
 
 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
 {
        Assignment *assign = new Assignment;
+
+       string::size_type dot = left.find('.');
        VariableReference *ref = new VariableReference;
-       ref->name = left;
+       ref->name = left.substr(0, dot);
        assign->left = ref;
+
+       while(dot!=string::npos)
+       {
+               string::size_type start = dot+1;
+               dot = left.find('.', start);
+
+               MemberAccess *memacc = new MemberAccess;
+               memacc->left = assign->left;
+               memacc->member = left.substr(start, dot-start);
+               assign->left = memacc;
+       }
+
        assign->oper = &Operator::get_operator("=", Operator::BINARY);
        assign->right = right;
 
@@ -510,31 +199,30 @@ void InterfaceGenerator::visit(VariableReference &var)
                return;
 
        const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
-       map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
+       auto i = prev_vars.find(var.name);
        if(i==prev_vars.end() || i->second->interface!="out")
                i = prev_vars.find(in_prefix+var.name);
        if(i!=prev_vars.end() && i->second->interface=="out")
        {
-               generate_interface(*i->second, "in", i->second->name);
-               var.name = i->second->name;
-               return;
-       }
-
-       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
-       map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
-       if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
-       {
-               generate_interface(*j->second);
+               if(stage->type==Stage::GEOMETRY && i->second->array)
+                       stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
+                               format("Can't access '%s' through automatic interface because it's an array", var.name)));
+               else
+               {
+                       generate_interface(*i->second, "in", i->second->name);
+                       var.name = i->second->name;
+               }
                return;
        }
 
-       for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
-               if(j->second->instance_name.empty())
+       for(const auto &kvp: stage->previous->interface_blocks)
+               if(kvp.second->name.find(' ')!=string::npos)
                {
-                       i = j->second->members.variables.find(var.name);
-                       if(i!=j->second->members.variables.end())
+                       const map<string, VariableDeclaration *> &iface_vars = kvp.second->block_declaration->members.variables;
+                       i = iface_vars.find(var.name);
+                       if(i!=iface_vars.end())
                        {
-                               generate_interface(*j->second);
+                               generate_interface(*kvp.second, "in", string());
                                return;
                        }
                }
@@ -542,31 +230,15 @@ void InterfaceGenerator::visit(VariableReference &var)
 
 void InterfaceGenerator::visit(VariableDeclaration &var)
 {
-       if(copy_block)
-       {
-               generate_interface(var, "in", var.name);
-               return;
-       }
-
-       if(iface_block)
-       {
-               if(iface_block->linked_block)
-               {
-                       // Link all variables to their counterparts in the linked block.
-                       const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
-                       map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
-                       if(i!=linked_vars.end())
-                               var.linked_declaration = i->second;
-               }
-               return;
-       }
-
        if(var.interface=="out")
        {
                /* For output variables in function scope, generate a global interface
                and replace the local declaration with an assignment. */
-               if(function_scope && generate_interface(var, "out", var.name))
+               VariableDeclaration *out_var = 0;
+               if(function_scope && (out_var=generate_interface(var, "out", var.name)))
                {
+                       out_var->source = var.source;
+                       out_var->line = var.line;
                        nodes_to_remove.insert(&var);
                        if(var.init_expression)
                        {
@@ -577,45 +249,39 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
                        }
                }
        }
-       else if(var.interface=="in")
+       else if(var.interface=="in" && current_block==&stage->content)
        {
+               if(var.name.compare(0, 3, "gl_"))
+                       declared_inputs.push_back(&var);
+
                /* Try to link input variables in global scope with output variables from
                previous stage. */
-               if(current_block==&stage->content && !var.linked_declaration && stage->previous)
+               if(!var.linked_declaration && stage->previous)
                {
-                       const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
-                       map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
-                       if(i!=prev_vars.end() && i->second->interface=="out")
+                       const map<string, VariableDeclaration *> *prev_vars;
+                       string name;
+                       // Blocks are linked by their block name, not instance name
+                       if(var.block_declaration)
                        {
-                               var.linked_declaration = i->second;
-                               i->second->linked_declaration = &var;
+                               prev_vars = &stage->previous->interface_blocks;
+                               name = "out "+var.block_declaration->block_name;
+                       }
+                       else
+                       {
+                               prev_vars = &stage->previous->content.variables;
+                               name = var.name;
                        }
-               }
-       }
-
-       TraversingVisitor::visit(var);
-}
 
-void InterfaceGenerator::visit(InterfaceBlock &iface)
-{
-       if(iface.interface=="in")
-       {
-               /* Try to link input blocks with output blocks sharing the same block
-               name from previous stage. */
-               if(!iface.linked_block && stage->previous)
-               {
-                       const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
-                       map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
-                       if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
+                       auto i = prev_vars->find(name);
+                       if(i!=prev_vars->end() && i->second->interface=="out")
                        {
-                               iface.linked_block = i->second;
-                               i->second->linked_block = &iface;
+                               var.linked_declaration = i->second;
+                               i->second->linked_declaration = &var;
                        }
                }
        }
 
-       SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
-       TraversingVisitor::visit(iface);
+       TraversingVisitor::visit(var);
 }
 
 void InterfaceGenerator::visit(FunctionDeclaration &func)
@@ -627,25 +293,20 @@ void InterfaceGenerator::visit(FunctionDeclaration &func)
 
 void InterfaceGenerator::visit(Passthrough &pass)
 {
-       vector<VariableDeclaration *> pass_vars;
-
-       // Pass through all input variables of this stage.
-       for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
-               if(i->second->interface=="in")
-                       pass_vars.push_back(i->second);
+       // Pass through all input variables declared so far.
+       vector<VariableDeclaration *> pass_vars = declared_inputs;
 
        if(stage->previous)
        {
-               const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
-               for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
+               for(const auto &kvp: stage->previous->content.variables)
                {
-                       if(i->second->interface!="out")
+                       if(kvp.second->interface!="out")
                                continue;
 
                        /* Pass through output variables from the previous stage, but only
                        those which are not already linked to an input here. */
-                       if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
-                               pass_vars.push_back(i->second);
+                       if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
+                               pass_vars.push_back(kvp.second);
                }
        }
 
@@ -653,7 +314,7 @@ void InterfaceGenerator::visit(Passthrough &pass)
        {
                /* Special case for geometry shader: copy gl_Position from input to
                output. */
-               InterfaceBlockReference *ref = new InterfaceBlockReference;
+               VariableReference *ref = new VariableReference;
                ref->name = "gl_in";
 
                BinaryExpression *subscript = new BinaryExpression;
@@ -665,16 +326,16 @@ void InterfaceGenerator::visit(Passthrough &pass)
                memacc->left = subscript;
                memacc->member = "gl_Position";
 
-               insert_assignment("gl_Position", memacc);
+               insert_assignment("out gl_PerVertex.gl_Position", memacc);
        }
 
-       for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
+       for(VariableDeclaration *v: pass_vars)
        {
-               string out_name = change_prefix((*i)->name, out_prefix);
-               generate_interface(**i, "out", out_name);
+               string out_name = change_prefix(v->name, out_prefix);
+               generate_interface(*v, "out", out_name);
 
                VariableReference *ref = new VariableReference;
-               ref->name = (*i)->name;
+               ref->name = v->name;
                if(pass.subscript)
                {
                        BinaryExpression *subscript = new BinaryExpression;
@@ -690,6 +351,151 @@ void InterfaceGenerator::visit(Passthrough &pass)
        nodes_to_remove.insert(&pass);
 }
 
+
+void LayoutDefaulter::apply(Stage &stage)
+{
+       if(stage.type==Stage::TESS_EVAL)
+       {
+               stage.content.visit(*this);
+               if((need_winding || need_spacing) && in_iface)
+               {
+                       if(need_winding)
+                               in_iface->layout.qualifiers.emplace_back("ccw");
+                       if(need_spacing)
+                               in_iface->layout.qualifiers.emplace_back("equal_spacing");
+               }
+       }
+}
+
+void LayoutDefaulter::visit(InterfaceLayout &iface)
+{
+       if(iface.interface=="in")
+       {
+               if(!in_iface)
+                       in_iface = &iface;
+               for(const Layout::Qualifier &q: iface.layout.qualifiers)
+               {
+                       if(q.name=="cw" || q.name=="ccw")
+                               need_winding = false;
+                       else if(q.name=="equal_spacing" || q.name=="fractional_even_spacing" || q.name=="fractional_odd_spacing")
+                               need_spacing = false;
+               }
+       }
+}
+
+
+void ArraySizer::apply(Stage &stage)
+{
+       stage.content.visit(*this);
+       for(const auto &kvp: max_indices)
+               if(kvp.first->array && !kvp.first->array_size)
+               {
+                       int size = 0;
+                       if(stage.type==Stage::GEOMETRY && kvp.first->interface=="in")
+                               size = input_size;
+                       else if(kvp.second>=0)
+                               size = kvp.second+1;
+                       if(!size && !kvp.first->name.compare(0, 3, "gl_"))
+                               size = 1;
+
+                       if(size>0)
+                       {
+                               Literal *literal_size = new Literal;
+                               literal_size->token = lexical_cast<string>(size);
+                               literal_size->value = size;
+                               kvp.first->array_size = literal_size;
+                       }
+               }
+}
+
+void ArraySizer::visit(VariableReference &var)
+{
+       r_declaration = var.declaration;
+}
+
+void ArraySizer::visit(MemberAccess &memacc)
+{
+       r_declaration = 0;
+       TraversingVisitor::visit(memacc);
+       VariableDeclaration *member_declaration = 0;
+       if(r_declaration)
+               if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(r_declaration->type_declaration))
+               {
+                       auto i = strct->members.variables.find(memacc.member);
+                       if(i!=strct->members.variables.end())
+                               member_declaration = i->second;
+               }
+       r_declaration = member_declaration;
+}
+
+void ArraySizer::visit(Swizzle &swizzle)
+{
+       TraversingVisitor::visit(swizzle);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(UnaryExpression &unary)
+{
+       TraversingVisitor::visit(unary);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(BinaryExpression &binary)
+{
+       if(binary.oper->token[0]=='[')
+               if(const Literal *literal_index = dynamic_cast<const Literal *>(binary.right.get()))
+                       if(literal_index->value.check_type<int>())
+                       {
+                               r_declaration = 0;
+                               binary.left->visit(*this);
+                               if(r_declaration)
+                               {
+                                       max_indices[r_declaration] = literal_index->value.value<int>();
+                                       return;
+                               }
+                       }
+
+       TraversingVisitor::visit(binary);
+}
+
+void ArraySizer::visit(TernaryExpression &ternary)
+{
+       TraversingVisitor::visit(ternary);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+       r_declaration = 0;
+}
+
+void ArraySizer::visit(InterfaceLayout &layout)
+{
+       if(layout.interface=="in")
+       {
+               for(const Layout::Qualifier &q: layout.layout.qualifiers)
+               {
+                       if(q.name=="points")
+                               input_size = 1;
+                       else if(q.name=="lines")
+                               input_size = 2;
+                       else if(q.name=="triangles")
+                               input_size = 3;
+                       else if(q.name=="lines_adjacency")
+                               input_size = 4;
+                       else if(q.name=="triangles_adjacency")
+                               input_size = 6;
+               }
+       }
+}
+
+void ArraySizer::visit(VariableDeclaration &var)
+{
+       if(var.array && !var.array_size)
+               max_indices[&var] = 0;
+}
+
 } // namespace SL
 } // namespace GL
 } // namespace Msp