]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Return back to first stage after some optimization happens
[libs/gl.git] / source / programcompiler.cpp
index d518ef3aa5fb6d8fd93c8cace2cc71502f771eba..934416c28684f250fa8c37b9d842f5f05c39b7a9 100644 (file)
@@ -55,8 +55,13 @@ void ProgramCompiler::process()
 {
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
                generate(*i);
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-               optimize(*i);
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
+       {
+               if(optimize(*i))
+                       i = module->stages.begin();
+               else
+                       ++i;
+       }
 }
 
 void ProgramCompiler::generate(Stage &stage)
@@ -69,20 +74,16 @@ void ProgramCompiler::generate(Stage &stage)
        apply<VariableRenamer>(stage);
 }
 
-void ProgramCompiler::optimize(Stage &stage)
+bool ProgramCompiler::optimize(Stage &stage)
 {
-       while(1)
-       {
-               UnusedVariableLocator unused_locator;
-               unused_locator.apply(stage);
+       UnusedVariableLocator unused_locator;
+       unused_locator.apply(stage);
 
-               NodeRemover remover;
-               remover.to_remove = unused_locator.unused_nodes;
-               remover.apply(stage);
+       NodeRemover remover;
+       remover.to_remove = unused_locator.unused_nodes;
+       remover.apply(stage);
 
-               if(!remover.n_removed)
-                       break;
-       }
+       return remover.n_removed;
 }
 
 void ProgramCompiler::inject_block(Block &target, const Block &source)
@@ -452,7 +453,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block)
 
                if(scope_level==1)
                {
-                       for(map<string, Node *>::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
+                       for(map<string, VariableDeclaration *>::iterator j=iface_declarations.begin(); j!=iface_declarations.end(); ++j)
                        {
                                list<NodePtr<Node> >::iterator k = block.body.insert(i, j->second);
                                (*k)->visit(*this);
@@ -478,22 +479,22 @@ string ProgramCompiler::InterfaceGenerator::change_prefix(const string &name, co
        return prefix+name.substr(offset);
 }
 
-bool ProgramCompiler::InterfaceGenerator::generate_interface(VariableDeclaration &out, const string &iface, const string &name)
+bool ProgramCompiler::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) || iface_declarations.count(name))
                return false;
 
        VariableDeclaration* iface_var = new VariableDeclaration;
-       iface_var->sampling = out.sampling;
+       iface_var->sampling = var.sampling;
        iface_var->interface = iface;
-       iface_var->type = out.type;
-       iface_var->type_declaration = out.type_declaration;
+       iface_var->type = var.type;
+       iface_var->type_declaration = var.type_declaration;
        iface_var->name = name;
-       iface_var->array = (out.array || (stage->type==GEOMETRY && iface=="in"));
-       iface_var->array_size = out.array_size;
+       iface_var->array = (var.array || (stage->type==GEOMETRY && iface=="in"));
+       iface_var->array_size = var.array_size;
        if(iface=="in")
-               iface_var->linked_declaration = &out;
+               iface_var->linked_declaration = &var;
        iface_declarations[name] = iface_var;
 
        return true;
@@ -564,31 +565,48 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
 
 void ProgramCompiler::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=iface_declarations.begin(); i!=iface_declarations.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)
                {
-                       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);
+                       bool linked = false;
+                       for(vector<VariableDeclaration *>::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j)
+                               linked = ((*j)->linked_declaration==i->second);
 
-                       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);
+                       if(!linked && generate_interface(*i->second, "in", i->second->name))
+                               pass_vars.push_back(i->second);
                }
        }
 
+       for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
+       {
+               string out_name = change_prefix((*i)->name, out_prefix);
+               generate_interface(**i, "out", out_name);
+
+               VariableReference *ref = new VariableReference;
+               ref->name = (*i)->name;
+               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;
 }