]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Add a missing symmetry to linking variables
[libs/gl.git] / source / glsl / generate.cpp
index 90128a18e1a7bbdfcaa023579c9665cac27c9a78..b91c2e2614ef2d577ce1e501893222eed678507d 100644 (file)
@@ -139,6 +139,9 @@ 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);
@@ -154,9 +157,11 @@ void VariableResolver::visit(VariableReference &var)
        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)
+               map<string, InterfaceBlock *>::const_iterator i = blocks.find("_"+var.name);
+               if(i!=blocks.end())
                {
+                       /* 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;
@@ -164,6 +169,7 @@ void VariableResolver::visit(VariableReference &var)
                }
                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())
                                {
@@ -178,6 +184,8 @@ void VariableResolver::visit(VariableReference &var)
        {
                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;
                }
@@ -193,7 +201,7 @@ 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);
+               map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find("_"+iface.name);
                if(i!=stage->interface_blocks.end())
                {
                        iface.declaration = i->second;
@@ -240,6 +248,8 @@ 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);
                }
@@ -285,7 +295,7 @@ void VariableResolver::visit(FunctionCall &call)
 void VariableResolver::visit(StructDeclaration &strct)
 {
        TraversingVisitor::visit(strct);
-       stage->types[strct.name] = &strct;
+       stage->types.insert(make_pair(strct.name, &strct));
 }
 
 void VariableResolver::visit(VariableDeclaration &var)
@@ -298,16 +308,16 @@ void VariableResolver::visit(VariableDeclaration &var)
                var.interface = block_interface;
 
        TraversingVisitor::visit(var);
-       current_block->variables[var.name] = &var;
+       current_block->variables.insert(make_pair(var.name, &var));
 }
 
 void VariableResolver::visit(InterfaceBlock &iface)
 {
-       /* 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;
+       /* Block names can be reused in different interfaces.  Prefix the name with
+       the first character of the interface to avoid conflicts. */
+       stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface));
        if(!iface.instance_name.empty())
-               stage->interface_blocks[iface.instance_name] = &iface;
+               stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
 
        SetForScope<string> set_iface(block_interface, iface.interface);
        TraversingVisitor::visit(iface);
@@ -338,6 +348,7 @@ void FunctionResolver::visit(FunctionDeclaration &func)
        {
                stage_decl = &func;
 
+               // Set all previous declarations to use this definition.
                for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
                {
                        (*i)->definition = func.definition;
@@ -406,16 +417,18 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi
        return prefix+name.substr(offset);
 }
 
-bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
+VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
 {
        if(stage->content.variables.count(name))
-               return false;
+               return 0;
 
        VariableDeclaration* iface_var = new VariableDeclaration;
        iface_var->sampling = var.sampling;
        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)
                iface_var->array = ((var.array && var.interface!="in") || iface=="in");
        else
@@ -430,15 +443,15 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri
        }
 
        iface_target_block->body.insert(iface_insert_point, iface_var);
-       iface_target_block->variables[name] = iface_var;
+       iface_target_block->variables.insert(make_pair(name, iface_var));
 
-       return true;
+       return iface_var;
 }
 
-bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
+InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
 {
-       if(stage->interface_blocks.count(out_block.name))
-               return false;
+       if(stage->interface_blocks.count("in"+out_block.name))
+               return 0;
 
        InterfaceBlock *in_block = new InterfaceBlock;
        in_block->interface = "in";
@@ -459,15 +472,15 @@ bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
        }
 
        iface_target_block->body.insert(iface_insert_point, in_block);
-       stage->interface_blocks[in_block->name] = in_block;
+       stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block));
        if(!in_block->instance_name.empty())
-               stage->interface_blocks[in_block->instance_name] = in_block;
+               stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
 
        SetFlag set_scope(function_scope, false);
        SetForScope<Block *> set_block(current_block, &stage->content);
        in_block->visit(*this);
 
-       return true;
+       return in_block;
 }
 
 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
@@ -491,7 +504,7 @@ void InterfaceGenerator::visit(VariableReference &var)
 {
        if(var.declaration || !stage->previous)
                return;
-       /* Don't pull a variable from previous stage if we just generated an out
+       /* Don't pull a variable from previous stage if we just generated an output
        interface in this stage */
        if(stage->content.variables.count(var.name))
                return;
@@ -508,10 +521,12 @@ void InterfaceGenerator::visit(VariableReference &var)
        }
 
        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)
+       map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
+       if(j!=prev_blocks.end() && j->second->interface=="out")
        {
                generate_interface(*j->second);
+               /* Let VariableResolver convert the variable reference into an interface
+               block reference. */
                return;
        }
 
@@ -539,20 +554,27 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        {
                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;
+                               var.linked_declaration->linked_declaration = &var;
+                       }
                }
                return;
        }
 
        if(var.interface=="out")
        {
-               /* For out 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))
+               /* For output variables in function scope, generate a global interface
+               and replace the local declaration with an assignment. */
+               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)
                        {
@@ -565,8 +587,8 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        }
        else if(var.interface=="in")
        {
-               /* Try to link in variables in global scope with out variables from
-               previous stage */
+               /* Try to link input variables in global scope with output variables from
+               previous stage. */
                if(current_block==&stage->content && !var.linked_declaration && stage->previous)
                {
                        const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
@@ -586,11 +608,13 @@ 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)
+                       map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.name);
+                       if(i!=prev_blocks.end())
                        {
                                iface.linked_block = i->second;
                                i->second->linked_block = &iface;
@@ -613,6 +637,7 @@ 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);
@@ -625,6 +650,8 @@ void InterfaceGenerator::visit(Passthrough &pass)
                        if(i->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);
                }
@@ -632,6 +659,8 @@ void InterfaceGenerator::visit(Passthrough &pass)
 
        if(stage->type==Stage::GEOMETRY)
        {
+               /* Special case for geometry shader: copy gl_Position from input to
+               output. */
                InterfaceBlockReference *ref = new InterfaceBlockReference;
                ref->name = "gl_in";