]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Refactor interface management
[libs/gl.git] / source / glsl / generate.cpp
index ca4d41e64c7a8c63bf51fd8d446e5ccb4dc8bd5a..48de1efc5992a4796411e5cadaf6618903c363e3 100644 (file)
@@ -73,15 +73,14 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
 }
 
 
-void BlockResolver::visit(Block &block)
+void BlockResolver::enter(Block &block)
 {
        block.parent = current_block;
-       TraversingVisitor::visit(block);
 }
 
 void BlockResolver::visit(InterfaceBlock &iface)
 {
-       iface.members.anonymous = true;
+       current_block->interfaces.insert(&iface);
        TraversingVisitor::visit(iface);
 }
 
@@ -104,12 +103,9 @@ Block *VariableResolver::next_block(Block &block)
        return block.parent ? block.parent : &block!=builtins ? builtins : 0;
 }
 
-void VariableResolver::visit(Block &block)
+void VariableResolver::enter(Block &block)
 {
-       if(current_block!=&block)
-               block.variables.clear();
-
-       TraversingVisitor::visit(block);
+       block.variables.clear();
 }
 
 void VariableResolver::visit(VariableReference &var)
@@ -118,11 +114,23 @@ void VariableResolver::visit(VariableReference &var)
        type = 0;
        for(Block *block=current_block; block; block=next_block(*block))
        {
-               map<string, VariableDeclaration *>::iterator j = block->variables.find(var.name);
-               if(j!=block->variables.end())
+               map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
+               if(i!=block->variables.end())
+                       var.declaration = i->second;
+               else
                {
-                       var.declaration = j->second;
-                       type = j->second->type_declaration;
+                       const set<InterfaceBlock *> &ifaces = block->interfaces;
+                       for(set<InterfaceBlock *>::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j)
+                       {
+                               i = (*j)->members.variables.find(var.name);
+                               if(i!=(*j)->members.variables.end())
+                                       var.declaration = i->second;
+                       }
+               }
+
+               if(var.declaration)
+               {
+                       type = var.declaration->type_declaration;
                        break;
                }
        }
@@ -212,8 +220,6 @@ void VariableResolver::visit(VariableDeclaration &var)
 
        TraversingVisitor::visit(var);
        current_block->variables[var.name] = &var;
-       if(current_block->anonymous && current_block->parent)
-               current_block->parent->variables[var.name] = &var;
 }
 
 void VariableResolver::visit(InterfaceBlock &iface)
@@ -222,20 +228,6 @@ void VariableResolver::visit(InterfaceBlock &iface)
        TraversingVisitor::visit(iface);
 }
 
-void VariableResolver::visit(FunctionDeclaration &func)
-{
-       SetForScope<Block *> set_block(current_block, &func.body);
-       func.body.variables.clear();
-       TraversingVisitor::visit(func);
-}
-
-void VariableResolver::visit(Iteration &iter)
-{
-       SetForScope<Block *> set_block(current_block, &iter.body);
-       iter.body.variables.clear();
-       TraversingVisitor::visit(iter);
-}
-
 
 void FunctionResolver::visit(FunctionCall &call)
 {
@@ -310,8 +302,7 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi
 
 bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
 {
-       const map<string, VariableDeclaration *> &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables);
-       if(stage_vars.count(name))
+       if(stage->content.variables.count(name))
                return false;
 
        VariableDeclaration* iface_var = new VariableDeclaration;
@@ -327,12 +318,12 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri
        if(iface_var->array)
                iface_var->array_size = var.array_size;
        if(iface=="in")
-               iface_var->linked_declaration = &var;
-       stage->content.body.insert(iface_insert_point, iface_var);
        {
-               SetForScope<Block *> set_block(current_block, &stage->content);
-               iface_var->visit(*this);
+               iface_var->linked_declaration = &var;
+               var.linked_declaration = iface_var;
        }
+       stage->content.body.insert(iface_insert_point, iface_var);
+       stage->content.variables[name] = iface_var;
 
        return true;
 }
@@ -358,12 +349,16 @@ 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
+       interface in this stage */
+       if(stage->content.variables.count(var.name))
+               return;
 
-       const map<string, VariableDeclaration *> &prev_out = stage->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())
+       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")
+               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;
@@ -374,9 +369,7 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
 {
        if(var.interface=="out")
        {
-               if(current_block==&stage->content)
-                       stage->out_variables[var.name] = &var;
-               else if(generate_interface(var, "out", change_prefix(var.name, string())))
+               if(current_block!=&stage->content && generate_interface(var, "out", change_prefix(var.name, string())))
                {
                        nodes_to_remove.insert(&var);
                        if(var.init_expression)
@@ -390,14 +383,11 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        }
        else if(var.interface=="in")
        {
-               stage->in_variables[var.name] = &var;
-               if(var.linked_declaration)
-                       var.linked_declaration->linked_declaration = &var;
-               else if(stage->previous)
+               if(!var.linked_declaration && stage->previous)
                {
-                       const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
-                       map<string, VariableDeclaration *>::const_iterator i = prev_out.find(var.name);
-                       if(i!=prev_out.end())
+                       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")
                        {
                                var.linked_declaration = i->second;
                                i->second->linked_declaration = &var;
@@ -408,17 +398,24 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
+void InterfaceGenerator::visit(FunctionDeclaration &func)
+{
+       // Skip parameters because they're not useful here
+       func.body.visit(*this);
+}
+
 void InterfaceGenerator::visit(Passthrough &pass)
 {
        vector<VariableDeclaration *> pass_vars;
 
-       for(map<string, VariableDeclaration *>::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i)
-               pass_vars.push_back(i->second);
+       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);
 
        if(stage->previous)
        {
-               const map<string, VariableDeclaration *> &prev_out = stage->previous->out_variables;
-               for(map<string, VariableDeclaration *>::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i)
+               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)
                {
                        bool linked = false;
                        for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)