]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Process existing inputs in passthrough
[libs/gl.git] / source / programcompiler.cpp
index 6bf5ba66a83ed851075e48fda66f33a3a17b5d6e..79031e41eeda0cc970162d3632c31eaadd253ae5 100644 (file)
@@ -35,12 +35,15 @@ void ProgramCompiler::add_shaders(Program &program)
                throw invalid_operation("ProgramCompiler::add_shaders");
 
        string head = "#version 150\n";
-       if(module->vertex_context.present)
-               program.attach_shader_owned(new VertexShader(head+format_context(module->vertex_context)));
-       if(module->geometry_context.present)
-               program.attach_shader_owned(new GeometryShader(head+format_context(module->geometry_context)));
-       if(module->fragment_context.present)
-               program.attach_shader_owned(new FragmentShader(head+format_context(module->fragment_context)));
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+       {
+               if(i->type==VERTEX)
+                       program.attach_shader_owned(new VertexShader(head+create_source(*i)));
+               else if(i->type==GEOMETRY)
+                       program.attach_shader_owned(new GeometryShader(head+create_source(*i)));
+               else if(i->type==FRAGMENT)
+                       program.attach_shader_owned(new FragmentShader(head+create_source(*i)));
+       }
 
        program.bind_attribute(VERTEX4, "vertex");
        program.bind_attribute(NORMAL3, "normal");
@@ -50,36 +53,32 @@ void ProgramCompiler::add_shaders(Program &program)
 
 void ProgramCompiler::process()
 {
-       if(module->vertex_context.present)
-               process(module->vertex_context);
-       if(module->geometry_context.present)
-               process(module->geometry_context);
-       if(module->fragment_context.present)
-               process(module->fragment_context);
+       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);
 }
 
-void ProgramCompiler::process(Context &context)
+void ProgramCompiler::generate(Stage &stage)
 {
-       inject_block(context.content, module->global_context.content);
-
-       resolve_variables(context);
-
-       InterfaceGenerator generator;
-       generator.visit(context);
+       inject_block(stage.content, module->shared.content);
 
-       resolve_variables(context);
-
-       VariableRenamer renamer;
-       context.content.visit(renamer);
+       apply<VariableResolver>(stage);
+       apply<InterfaceGenerator>(stage);
+       apply<VariableResolver>(stage);
+       apply<VariableRenamer>(stage);
+}
 
+void ProgramCompiler::optimize(Stage &stage)
+{
        while(1)
        {
                UnusedVariableLocator unused_locator;
-               context.content.visit(unused_locator);
+               unused_locator.apply(stage);
 
                NodeRemover remover;
-               remover.to_remove.insert(unused_locator.unused_variables.begin(), unused_locator.unused_variables.end());
-               context.content.visit(remover);
+               remover.to_remove = unused_locator.unused_nodes;
+               remover.apply(stage);
 
                if(!remover.n_removed)
                        break;
@@ -93,20 +92,32 @@ void ProgramCompiler::inject_block(Block &target, const Block &source)
                target.body.insert(insert_point, (*i)->clone());
 }
 
-void ProgramCompiler::resolve_variables(Context &context)
+template<typename T>
+void ProgramCompiler::apply(Stage &stage)
 {
-       VariableResolver resolver;
-       context.content.visit(resolver);
+       T visitor;
+       visitor.apply(stage);
 }
 
-string ProgramCompiler::format_context(Context &context)
+string ProgramCompiler::create_source(Stage &stage)
 {
        Formatter formatter;
-       context.content.visit(formatter);
+       formatter.apply(stage);
        return formatter.formatted;
 }
 
 
+ProgramCompiler::Visitor::Visitor():
+       stage(0)
+{ }
+
+void ProgramCompiler::Visitor::apply(Stage &s)
+{
+       SetForScope<Stage *> set(stage, &s);
+       stage->content.visit(*this);
+}
+
+
 ProgramCompiler::Formatter::Formatter():
        indent(0),
        parameter_list(false),
@@ -409,12 +420,11 @@ void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
 
 
 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
-       context(0),
        scope_level(0),
        remove_node(false)
 { }
 
-string ProgramCompiler::InterfaceGenerator::get_out_prefix(ContextType type)
+string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
 {
        if(type==VERTEX)
                return "_vs_out_";
@@ -424,13 +434,13 @@ string ProgramCompiler::InterfaceGenerator::get_out_prefix(ContextType type)
                return string();
 }
 
-void ProgramCompiler::InterfaceGenerator::visit(Context &ctx)
+void ProgramCompiler::InterfaceGenerator::apply(Stage &s)
 {
-       SetForScope<Context *> set(context, &ctx);
-       if(context->previous)
-               in_prefix = get_out_prefix(context->previous->type);
-       out_prefix = get_out_prefix(context->type);
-       ctx.content.visit(*this);
+       SetForScope<Stage *> set(stage, &s);
+       if(stage->previous)
+               in_prefix = get_out_prefix(stage->previous->type);
+       out_prefix = get_out_prefix(stage->type);
+       stage->content.visit(*this);
 }
 
 void ProgramCompiler::InterfaceGenerator::visit(Block &block)
@@ -442,7 +452,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block)
 
                if(scope_level==1)
                {
-                       for(map<string, NodePtr<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);
@@ -450,7 +460,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block)
                        iface_declarations.clear();
                }
 
-               for(list<NodePtr<Node> >::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
+               for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
                        block.body.insert(i, *j);
                insert_nodes.clear();
 
@@ -468,23 +478,23 @@ 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 *> &context_vars = (iface=="in" ? context->in_variables : context->out_variables);
-       if(context_vars.count(name) || iface_declarations.count(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 || (context->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_declarations[iface_var->name] = iface_var;
+               iface_var->linked_declaration = &var;
+       iface_declarations[name] = iface_var;
 
        return true;
 }
@@ -506,12 +516,12 @@ void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left,
 
 void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
 {
-       if(var.declaration || !context->previous)
+       if(var.declaration || !stage->previous)
                return;
        if(iface_declarations.count(var.name))
                return;
 
-       const map<string, VariableDeclaration *> &prev_out = context->previous->out_variables;
+       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);
@@ -524,7 +534,7 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
        if(var.interface=="out")
        {
                if(scope_level==1)
-                       context->out_variables[var.name] = &var;
+                       stage->out_variables[var.name] = &var;
                else if(generate_interface(var, "out", change_prefix(var.name, string())))
                {
                        remove_node = true;
@@ -534,10 +544,12 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
        }
        else if(var.interface=="in")
        {
-               context->in_variables[var.name] = &var;
-               if(context->previous)
+               stage->in_variables[var.name] = &var;
+               if(var.linked_declaration)
+                       var.linked_declaration->linked_declaration = &var;
+               else if(stage->previous)
                {
-                       const map<string, VariableDeclaration *> &prev_out = context->previous->out_variables;
+                       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())
                        {
@@ -552,31 +564,48 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
 
 void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
 {
-       if(context->previous)
+       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 = context->previous->out_variables;
+               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;
 }
 
@@ -595,20 +624,67 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
 }
 
 
+ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
+       assignment(false),
+       assignment_target(0)
+{ }
+
 void ProgramCompiler::UnusedVariableLocator::visit(VariableReference &var)
 {
-       unused_variables.erase(var.declaration);
+       if(assignment)
+               assignment_target = var.declaration;
+       else
+       {
+               unused_nodes.erase(var.declaration);
+               map<VariableDeclaration *, Node *>::iterator i = assignments.find(var.declaration);
+               if(i!=assignments.end())
+               {
+                       unused_nodes.erase(i->second);
+                       assignments.erase(i);
+               }
+       }
 }
 
 void ProgramCompiler::UnusedVariableLocator::visit(MemberAccess &memacc)
 {
        TraversingVisitor::visit(memacc);
-       unused_variables.erase(memacc.declaration);
+       unused_nodes.erase(memacc.declaration);
+}
+
+void ProgramCompiler::UnusedVariableLocator::visit(BinaryExpression &binary)
+{
+       if(binary.assignment)
+       {
+               binary.right->visit(*this);
+               assignment = true;
+               binary.left->visit(*this);
+       }
+       else
+               TraversingVisitor::visit(binary);
+}
+
+void ProgramCompiler::UnusedVariableLocator::visit(ExpressionStatement &expr)
+{
+       assignment = false;
+       assignment_target = 0;
+       TraversingVisitor::visit(expr);
+       if(assignment && assignment_target)
+       {
+               Node *&assign = assignments[assignment_target];
+               if(assign)
+                       unused_nodes.insert(assign);
+               assign = &expr;
+               if(assignment_target->interface!="out" || (stage->type!=FRAGMENT && !assignment_target->linked_declaration))
+                       unused_nodes.insert(&expr);
+               else
+                       unused_nodes.erase(assignment_target);
+       }
+       assignment = false;
 }
 
 void ProgramCompiler::UnusedVariableLocator::visit(VariableDeclaration &var)
 {
-       unused_variables.insert(&var);
+       unused_nodes.insert(&var);
        TraversingVisitor::visit(var);
 }
 
@@ -624,22 +700,18 @@ void ProgramCompiler::NodeRemover::visit(Block &block)
        remove_block = immutable_block;
        for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
-               bool remove = false;
-               if(to_remove.count(&**i))
-                       remove = !immutable_block;
-               else
-               {
+               bool remove = to_remove.count(&**i);
+               if(!remove)
                        remove_block = false;
-                       (*i)->visit(*this);
-                       remove = remove_block;
-               }
+               (*i)->visit(*this);
 
-               if(remove)
+               if(remove ? !immutable_block : remove_block)
+               {
                        block.body.erase(i++);
+                       ++n_removed;
+               }
                else
                        ++i;
-
-               n_removed += remove;
        }
 }
 
@@ -649,6 +721,17 @@ void ProgramCompiler::NodeRemover::visit(StructDeclaration &strct)
        TraversingVisitor::visit(strct);
 }
 
+void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
+{
+       if(to_remove.count(&var))
+       {
+               stage->in_variables.erase(var.name);
+               stage->out_variables.erase(var.name);
+               if(var.linked_declaration)
+                       var.linked_declaration->linked_declaration = 0;
+       }
+}
+
 void ProgramCompiler::NodeRemover::visit(InterfaceBlock &iface)
 {
        SetFlag set(immutable_block);