]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Refactor interface management
[libs/gl.git] / source / glsl / generate.cpp
index ac16c31f889dce86ae02541121802d84b59a496e..48de1efc5992a4796411e5cadaf6618903c363e3 100644 (file)
@@ -8,22 +8,17 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-DeclarationCombiner::DeclarationCombiner():
-       toplevel(true)
-{ }
-
 void DeclarationCombiner::apply(Stage &stage)
 {
-       visit(stage.content);
+       stage.content.visit(*this);
        NodeRemover().apply(stage, nodes_to_remove);
 }
 
 void DeclarationCombiner::visit(Block &block)
 {
-       if(!toplevel)
+       if(current_block)
                return;
 
-       SetForScope<bool> set(toplevel, false);
        TraversingVisitor::visit(block);
 }
 
@@ -57,8 +52,9 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
                                {
                                        bool found = false;
                                        for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
-                                               if(j->identifier==i->identifier)
+                                               if(j->name==i->name)
                                                {
+                                                       j->has_value = i->value;
                                                        j->value = i->value;
                                                        found = true;
                                                }
@@ -77,8 +73,19 @@ void DeclarationCombiner::visit(VariableDeclaration &var)
 }
 
 
+void BlockResolver::enter(Block &block)
+{
+       block.parent = current_block;
+}
+
+void BlockResolver::visit(InterfaceBlock &iface)
+{
+       current_block->interfaces.insert(&iface);
+       TraversingVisitor::visit(iface);
+}
+
+
 VariableResolver::VariableResolver():
-       anonymous(false),
        record_target(false),
        assignment_target(0),
        self_referencing(false)
@@ -86,34 +93,44 @@ VariableResolver::VariableResolver():
 
 void VariableResolver::apply(Stage &stage)
 {
-       Stage *builtins = get_builtins(stage.type);
-       if(builtins)
-               blocks.push_back(&builtins->content);
-       visit(stage.content);
-       if(builtins)
-               blocks.pop_back();
+       Stage *builtin_stage = get_builtins(stage.type);
+       builtins = (builtin_stage ? &builtin_stage->content : 0);
+       stage.content.visit(*this);
 }
 
-void VariableResolver::visit(Block &block)
+Block *VariableResolver::next_block(Block &block)
+{
+       return block.parent ? block.parent : &block!=builtins ? builtins : 0;
+}
+
+void VariableResolver::enter(Block &block)
 {
-       blocks.push_back(&block);
        block.variables.clear();
-       TraversingVisitor::visit(block);
-       blocks.pop_back();
 }
 
 void VariableResolver::visit(VariableReference &var)
 {
        var.declaration = 0;
        type = 0;
-       for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+       for(Block *block=current_block; block; block=next_block(*block))
        {
-               --i;
-               map<string, VariableDeclaration *>::iterator j = (*i)->variables.find(var.name);
-               if(j!=(*i)->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;
                }
        }
@@ -186,16 +203,15 @@ void VariableResolver::visit(Assignment &assign)
 void VariableResolver::visit(StructDeclaration &strct)
 {
        TraversingVisitor::visit(strct);
-       blocks.back()->types[strct.name] = &strct;
+       current_block->types[strct.name] = &strct;
 }
 
 void VariableResolver::visit(VariableDeclaration &var)
 {
-       for(vector<Block *>::iterator i=blocks.end(); i!=blocks.begin(); )
+       for(Block *block=current_block; block; block=next_block(*block))
        {
-               --i;
-               map<string, StructDeclaration *>::iterator j = (*i)->types.find(var.type);
-               if(j!=(*i)->types.end())
+               map<string, StructDeclaration *>::iterator j = block->types.find(var.type);
+               if(j!=block->types.end())
                        var.type_declaration = j->second;
        }
 
@@ -203,15 +219,12 @@ void VariableResolver::visit(VariableDeclaration &var)
                var.interface = block_interface;
 
        TraversingVisitor::visit(var);
-       blocks.back()->variables[var.name] = &var;
-       if(anonymous && blocks.size()>1)
-               blocks[blocks.size()-2]->variables[var.name] = &var;
+       current_block->variables[var.name] = &var;
 }
 
 void VariableResolver::visit(InterfaceBlock &iface)
 {
-       SetFlag set(anonymous);
-       SetForScope<string> set2(block_interface, iface.interface);
+       SetForScope<string> set_iface(block_interface, iface.interface);
        TraversingVisitor::visit(iface);
 }
 
@@ -245,9 +258,7 @@ void FunctionResolver::visit(FunctionDeclaration &func)
 
 
 InterfaceGenerator::InterfaceGenerator():
-       stage(0),
-       scope_level(0),
-       current_block(0)
+       stage(0)
 { }
 
 string InterfaceGenerator::get_out_prefix(Stage::Type type)
@@ -266,18 +277,17 @@ void InterfaceGenerator::apply(Stage &s)
        if(stage->previous)
                in_prefix = get_out_prefix(stage->previous->type);
        out_prefix = get_out_prefix(stage->type);
-       visit(s.content);
+       s.content.visit(*this);
        NodeRemover().apply(s, nodes_to_remove);
 }
 
 void InterfaceGenerator::visit(Block &block)
 {
-       SetForScope<unsigned> set(scope_level, scope_level+1);
        SetForScope<Block *> set_block(current_block, &block);
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
                assignment_insert_point = i;
-               if(scope_level==1)
+               if(&block==&stage->content)
                        iface_insert_point = i;
 
                (*i)->visit(*this);
@@ -292,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;
@@ -309,13 +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<unsigned> set_level(scope_level, 1);
-               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;
 }
@@ -341,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;
@@ -357,9 +369,7 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
 {
        if(var.interface=="out")
        {
-               if(scope_level==1)
-                       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)
@@ -373,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;
@@ -391,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)
@@ -455,7 +469,6 @@ void InterfaceGenerator::visit(Passthrough &pass)
 
 
 DeclarationReorderer::DeclarationReorderer():
-       scope_level(0),
        kind(NO_DECLARATION)
 { }
 
@@ -470,8 +483,7 @@ void DeclarationReorderer::visit(FunctionCall &call)
 
 void DeclarationReorderer::visit(Block &block)
 {
-       SetForScope<unsigned> set(scope_level, scope_level+1);
-       if(scope_level>1)
+       if(block.parent)
                return TraversingVisitor::visit(block);
 
        NodeList<Statement>::iterator struct_insert_point = block.body.end();