]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Use layout declarations to set attribute and fragment data locations
[libs/gl.git] / source / programcompiler.cpp
index a730f74348f072fab4edd1398835b380b4295af4..fd756de0e00cbbca4c5aa18587b251afc4e94ccb 100644 (file)
@@ -4,6 +4,7 @@
 #include "error.h"
 #include "program.h"
 #include "programcompiler.h"
+#include "resources.h"
 #include "shader.h"
 
 using namespace std;
@@ -34,17 +35,20 @@ namespace GL {
 using namespace ProgramSyntax;
 
 ProgramCompiler::ProgramCompiler():
+       resources(0),
        module(0)
 { }
 
 void ProgramCompiler::compile(const string &source)
 {
+       resources = 0;
        module = &parser.parse(source);
        process();
 }
 
-void ProgramCompiler::compile(IO::Base &io)
+void ProgramCompiler::compile(IO::Base &io, Resources *res)
 {
+       resources = res;
        module = &parser.parse(io);
        process();
 }
@@ -54,21 +58,23 @@ void ProgramCompiler::add_shaders(Program &program)
        if(!module)
                throw invalid_operation("ProgramCompiler::add_shaders");
 
-       string head = "#version 150\n";
        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)));
+               {
+                       program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
+                       for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
+                               program.bind_attribute(j->second, j->first);
+               }
                else if(i->type==GEOMETRY)
-                       program.attach_shader_owned(new GeometryShader(head+create_source(*i)));
+                       program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
                else if(i->type==FRAGMENT)
-                       program.attach_shader_owned(new FragmentShader(head+create_source(*i)));
+               {
+                       program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
+                       for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
+                               program.bind_fragment_data(j->second, j->first);
+               }
        }
-
-       program.bind_attribute(VERTEX4, "vertex");
-       program.bind_attribute(NORMAL3, "normal");
-       program.bind_attribute(COLOR4_FLOAT, "color");
-       program.bind_attribute(TEXCOORD4, "texcoord");
 }
 
 Module *ProgramCompiler::create_builtins_module()
@@ -102,6 +108,11 @@ Stage *ProgramCompiler::get_builtins(StageType type)
 
 void ProgramCompiler::process()
 {
+       list<Import *> imports = apply<NodeGatherer<Import> >(module->shared);
+       for(list<Import *>::iterator i=imports.end(); i!=imports.begin(); )
+               import((*--i)->module);
+       apply<NodeRemover>(module->shared, set<Node *>(imports.begin(), imports.end()));
+
        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(); )
@@ -113,26 +124,62 @@ void ProgramCompiler::process()
        }
 }
 
+void ProgramCompiler::import(const string &name)
+{
+       string fn = name+".glsl";
+       RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
+       if(!io)
+               throw runtime_error(format("module %s not found", name));
+       ProgramParser import_parser;
+       Module &imported_module = import_parser.parse(*io);
+
+       inject_block(module->shared.content, imported_module.shared.content);
+       apply<DeclarationCombiner>(module->shared);
+       for(list<Stage>::iterator i=imported_module.stages.begin(); i!=imported_module.stages.end(); ++i)
+       {
+               list<Stage>::iterator j;
+               for(j=module->stages.begin(); (j!=module->stages.end() && j->type<i->type); ++j) ;
+               if(j==module->stages.end() || j->type>i->type)
+               {
+                       j = module->stages.insert(j, *i);
+                       list<Stage>::iterator k = j;
+                       if(++k!=module->stages.end())
+                               k->previous = &*j;
+                       if(j!=module->stages.begin())
+                               j->previous = &*--(k=j);
+               }
+               else
+               {
+                       inject_block(j->content, i->content);
+                       apply<DeclarationCombiner>(*j);
+               }
+       }
+}
+
 void ProgramCompiler::generate(Stage &stage)
 {
        inject_block(stage.content, module->shared.content);
 
+       apply<FunctionResolver>(stage);
        apply<VariableResolver>(stage);
        apply<InterfaceGenerator>(stage);
        apply<VariableResolver>(stage);
        apply<VariableRenamer>(stage);
+       apply<LegacyConverter>(stage);
+       apply<VariableResolver>(stage);
 }
 
 bool ProgramCompiler::optimize(Stage &stage)
 {
-       UnusedVariableLocator unused_locator;
-       unused_locator.apply(stage);
+       apply<ConstantConditionEliminator>(stage);
+       apply<VariableResolver>(stage);
 
-       NodeRemover remover;
-       remover.to_remove = unused_locator.unused_nodes;
-       remover.apply(stage);
+       set<Node *> unused = apply<UnusedVariableLocator>(stage);
+       set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
+       unused.insert(unused2.begin(), unused2.end());
+       apply<NodeRemover>(stage, unused);
 
-       return !unused_locator.unused_nodes.empty();
+       return !unused.empty();
 }
 
 void ProgramCompiler::inject_block(Block &target, const Block &source)
@@ -143,17 +190,19 @@ void ProgramCompiler::inject_block(Block &target, const Block &source)
 }
 
 template<typename T>
-void ProgramCompiler::apply(Stage &stage)
+typename T::ResultType ProgramCompiler::apply(Stage &stage)
 {
        T visitor;
        visitor.apply(stage);
+       return visitor.get_result();
 }
 
-string ProgramCompiler::create_source(Stage &stage)
+template<typename T, typename A>
+typename T::ResultType ProgramCompiler::apply(Stage &stage, const A &arg)
 {
-       Formatter formatter;
-       formatter.apply(stage);
-       return formatter.formatted;
+       T visitor(arg);
+       visitor.apply(stage);
+       return visitor.get_result();
 }
 
 
@@ -174,6 +223,14 @@ ProgramCompiler::Formatter::Formatter():
        else_if(0)
 { }
 
+void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s)
+{
+       const Version &ver = s.required_version;
+       if(ver.major)
+               formatted += format("#version %d%d\n", ver.major, ver.minor);
+       Visitor::apply(s);
+}
+
 void ProgramCompiler::Formatter::visit(Literal &literal)
 {
        formatted += literal.token;
@@ -249,7 +306,7 @@ void ProgramCompiler::Formatter::visit(Block &block)
        if(use_braces)
                formatted += format("%s{\n", string(brace_indent*2, ' '));
 
-       SetForScope<unsigned> set(indent, indent+!formatted.empty());
+       SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
        string spaces(indent*2, ' ');
        for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
@@ -264,6 +321,11 @@ void ProgramCompiler::Formatter::visit(Block &block)
                formatted += format("\n%s}", string(brace_indent*2, ' '));
 }
 
+void ProgramCompiler::Formatter::visit(Import &import)
+{
+       formatted += format("import %s;", import.module);
+}
+
 void ProgramCompiler::Formatter::visit(Layout &layout)
 {
        formatted += "layout(";
@@ -275,7 +337,13 @@ void ProgramCompiler::Formatter::visit(Layout &layout)
                if(!i->value.empty())
                        formatted += format("=%s", i->value);
        }
-       formatted += format(") %s;", layout.interface);
+       formatted += ')';
+}
+
+void ProgramCompiler::Formatter::visit(InterfaceLayout &layout)
+{
+       layout.layout.visit(*this);
+       formatted += format(" %s;", layout.interface);
 }
 
 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
@@ -287,6 +355,11 @@ void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
 
 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
 {
+       if(var.layout)
+       {
+               var.layout->visit(*this);
+               formatted += ' ';
+       }
        if(var.constant)
                formatted += "const ";
        if(!var.sampling.empty())
@@ -329,7 +402,7 @@ void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
                (*i)->visit(*this);
        }
        formatted += ')';
-       if(func.definition)
+       if(func.definition==&func)
        {
                formatted += '\n';
                func.body.visit(*this);
@@ -378,6 +451,57 @@ void ProgramCompiler::Formatter::visit(Return &ret)
 }
 
 
+ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
+       toplevel(true),
+       remove_node(false)
+{ }
+
+void ProgramCompiler::DeclarationCombiner::visit(Block &block)
+{
+       if(!toplevel)
+               return;
+
+       SetForScope<bool> set(toplevel, false);
+       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               remove_node = false;
+               (*i)->visit(*this);
+               if(remove_node)
+                       block.body.erase(i++);
+               else
+                       ++i;
+       }
+}
+
+void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
+{
+       vector<FunctionDeclaration *> &decls = functions[func.name];
+       if(func.definition)
+       {
+               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+               {
+                       (*i)->definition = func.definition;
+                       (*i)->body.body.clear();
+               }
+       }
+       decls.push_back(&func);
+}
+
+void ProgramCompiler::DeclarationCombiner::visit(VariableDeclaration &var)
+{
+       VariableDeclaration *&ptr = variables[var.name];
+       if(ptr)
+       {
+               ptr->type = var.type;
+               if(var.init_expression)
+                       ptr->init_expression = var.init_expression;
+               remove_node = true;
+       }
+       else
+               ptr = &var;
+}
+
+
 ProgramCompiler::VariableResolver::VariableResolver():
        anonymous(false),
        record_target(false),
@@ -518,11 +642,72 @@ void ProgramCompiler::VariableResolver::visit(InterfaceBlock &iface)
 }
 
 
-ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
-       scope_level(0),
+void ProgramCompiler::FunctionResolver::visit(FunctionCall &call)
+{
+       map<string, vector<FunctionDeclaration *> >::iterator i = functions.find(call.name);
+       if(i!=functions.end())
+               call.declaration = i->second.back();
+
+       TraversingVisitor::visit(call);
+}
+
+void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
+{
+       vector<FunctionDeclaration *> &decls = functions[func.name];
+       if(func.definition)
+       {
+               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+                       (*i)->definition = func.definition;
+               decls.clear();
+               decls.push_back(&func);
+       }
+       else if(!decls.empty() && decls.back()->definition)
+               func.definition = decls.back()->definition;
+       else
+               decls.push_back(&func);
+
+       TraversingVisitor::visit(func);
+}
+
+
+ProgramCompiler::BlockModifier::BlockModifier():
        remove_node(false)
 { }
 
+void ProgramCompiler::BlockModifier::flatten_block(Block &block)
+{
+       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+               insert_nodes.push_back((*i)->clone());
+       remove_node = true;
+}
+
+void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<NodePtr<Node> >::iterator &i)
+{
+       for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
+               block.body.insert(i, *j);
+       insert_nodes.clear();
+
+       if(remove_node)
+               block.body.erase(i++);
+       else
+               ++i;
+       remove_node = false;
+}
+
+void ProgramCompiler::BlockModifier::visit(Block &block)
+{
+       for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               (*i)->visit(*this);
+               apply_and_increment(block, i);
+       }
+}
+
+
+ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
+       scope_level(0)
+{ }
+
 string ProgramCompiler::InterfaceGenerator::get_out_prefix(StageType type)
 {
        if(type==VERTEX)
@@ -559,15 +744,7 @@ void ProgramCompiler::InterfaceGenerator::visit(Block &block)
                        iface_declarations.clear();
                }
 
-               for(list<Node *>::iterator j=insert_nodes.begin(); j!=insert_nodes.end(); ++j)
-                       block.body.insert(i, *j);
-               insert_nodes.clear();
-
-               if(remove_node)
-                       block.body.erase(i++);
-               else
-                       ++i;
-               remove_node = false;
+               apply_and_increment(block, i);
        }
 }
 
@@ -744,6 +921,142 @@ void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
 }
 
 
+ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator():
+       variable_values(0),
+       result(0.0f),
+       result_valid(false)
+{ }
+
+ProgramCompiler::ExpressionEvaluator::ExpressionEvaluator(const ValueMap &v):
+       variable_values(&v),
+       result(0.0f),
+       result_valid(false)
+{ }
+
+void ProgramCompiler::ExpressionEvaluator::visit(Literal &literal)
+{
+       if(literal.token=="true")
+               result = 1.0f;
+       else if(literal.token=="false")
+               result = 0.0f;
+       else
+               result = lexical_cast<float>(literal.token);
+       result_valid = true;
+}
+
+void ProgramCompiler::ExpressionEvaluator::visit(VariableReference &var)
+{
+       if(!var.declaration)
+               return;
+
+       if(variable_values)
+       {
+               ValueMap::const_iterator i = variable_values->find(var.declaration);
+               if(i!=variable_values->end())
+                       i->second->visit(*this);
+       }
+       else if(var.declaration->init_expression)
+               var.declaration->init_expression->visit(*this);
+}
+
+void ProgramCompiler::ExpressionEvaluator::visit(UnaryExpression &unary)
+{
+       result_valid = false;
+       unary.expression->visit(*this);
+       if(!result_valid)
+               return;
+
+       if(unary.oper=="!")
+               result = !result;
+       else
+               result_valid = false;
+}
+
+void ProgramCompiler::ExpressionEvaluator::visit(BinaryExpression &binary)
+{
+       result_valid = false;
+       binary.left->visit(*this);
+       if(!result_valid)
+               return;
+
+       float left_result = result;
+       result_valid = false;
+       binary.right->visit(*this);
+       if(!result_valid)
+               return;
+
+       if(binary.oper=="<")
+               result = (left_result<result);
+       else if(binary.oper=="<=")
+               result = (left_result<=result);
+       else if(binary.oper==">")
+               result = (left_result>result);
+       else if(binary.oper==">=")
+               result = (left_result>=result);
+       else if(binary.oper=="==")
+               result = (left_result==result);
+       else if(binary.oper=="!=")
+               result = (left_result!=result);
+       else if(binary.oper=="&&")
+               result = (left_result && result);
+       else if(binary.oper=="||")
+               result = (left_result || result);
+       else
+               result_valid = false;
+}
+
+
+ProgramCompiler::ConstantConditionEliminator::ConstantConditionEliminator():
+       scope_level(0)
+{ }
+
+void ProgramCompiler::ConstantConditionEliminator::visit(Block &block)
+{
+       SetForScope<unsigned> set(scope_level, scope_level+1);
+       BlockModifier::visit(block);
+
+       for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
+               variable_values.erase(i->second);
+}
+
+void ProgramCompiler::ConstantConditionEliminator::visit(Assignment &assign)
+{
+       variable_values.erase(assign.target_declaration);
+}
+
+void ProgramCompiler::ConstantConditionEliminator::visit(VariableDeclaration &var)
+{
+       if(var.constant || scope_level>1)
+               variable_values[&var] = &*var.init_expression;
+}
+
+void ProgramCompiler::ConstantConditionEliminator::visit(Conditional &cond)
+{
+       ExpressionEvaluator eval(variable_values);
+       cond.condition->visit(eval);
+       if(eval.result_valid)
+               flatten_block(eval.result ? cond.body : cond.else_body);
+       else
+               TraversingVisitor::visit(cond);
+}
+
+void ProgramCompiler::ConstantConditionEliminator::visit(Iteration &iter)
+{
+       if(iter.condition)
+       {
+               ExpressionEvaluator eval;
+               iter.condition->visit(eval);
+               if(eval.result_valid && !eval.result)
+               {
+                       remove_node = true;
+                       return;
+               }
+       }
+
+       TraversingVisitor::visit(iter);
+}
+
+
 ProgramCompiler::UnusedVariableLocator::UnusedVariableLocator():
        aggregate(0),
        assignment(0),
@@ -944,6 +1257,28 @@ void ProgramCompiler::UnusedVariableLocator::visit(Iteration &iter)
 }
 
 
+void ProgramCompiler::UnusedFunctionLocator::visit(FunctionCall &call)
+{
+       TraversingVisitor::visit(call);
+
+       unused_nodes.erase(call.declaration);
+       if(call.declaration && call.declaration->definition!=call.declaration)
+               used_definitions.insert(call.declaration->definition);
+}
+
+void ProgramCompiler::UnusedFunctionLocator::visit(FunctionDeclaration &func)
+{
+       TraversingVisitor::visit(func);
+
+       if(func.name!="main" && !used_definitions.count(&func))
+               unused_nodes.insert(&func);
+}
+
+
+ProgramCompiler::NodeRemover::NodeRemover(const set<Node *> &r):
+       to_remove(r)
+{ }
+
 void ProgramCompiler::NodeRemover::visit(Block &block)
 {
        for(list<NodePtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
@@ -962,6 +1297,7 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
        {
                stage->in_variables.erase(var.name);
                stage->out_variables.erase(var.name);
+               stage->locations.erase(var.name);
                if(var.linked_declaration)
                        var.linked_declaration->linked_declaration = 0;
        }
@@ -969,5 +1305,112 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
                var.init_expression = 0;
 }
 
+
+ProgramCompiler::LegacyConverter::LegacyConverter():
+       target_version(get_glsl_version())
+{ }
+
+ProgramCompiler::LegacyConverter::LegacyConverter(const Version &v):
+       target_version(v)
+{ }
+
+bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_version)
+{
+       if(target_version<feature_version)
+               return false;
+       else if(stage->required_version<feature_version)
+               stage->required_version = feature_version;
+
+       return true;
+}
+
+void ProgramCompiler::LegacyConverter::visit(VariableReference &var)
+{
+       if(var.name==frag_out_name && !check_version(Version(1, 30)))
+       {
+               var.name = "gl_FragColor";
+               var.declaration = 0;
+               type = "vec4";
+       }
+       else if(var.declaration)
+               type = var.declaration->type;
+       else
+               type = string();
+}
+
+void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
+{
+       if(call.name=="texture" && !call.declaration && !check_version(Version(1, 30)))
+       {
+               vector<NodePtr<Expression> >::iterator i = call.arguments.begin();
+               if(i!=call.arguments.end())
+               {
+                       (*i)->visit(*this);
+                       if(type=="sampler1D")
+                               call.name = "texture1D";
+                       else if(type=="sampler2D")
+                               call.name = "texture2D";
+                       else if(type=="sampler3D")
+                               call.name = "texture3D";
+                       else if(type=="sampler1DShadow")
+                               call.name = "shadow1D";
+                       else if(type=="sampler2DShadow")
+                               call.name = "shadow2D";
+
+                       for(; i!=call.arguments.end(); ++i)
+                               (*i)->visit(*this);
+               }
+       }
+       else
+               TraversingVisitor::visit(call);
+}
+
+void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
+{
+       if(var.layout && !check_version(Version(3, 30)))
+       {
+               vector<Layout::Qualifier>::iterator i;
+               for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
+               if(i!=var.layout->qualifiers.end())
+               {
+                       unsigned location = lexical_cast<unsigned>(i->value);
+                       if(stage->type==VERTEX && var.interface=="in")
+                       {
+                               stage->locations[var.name] = location;
+                               var.layout->qualifiers.erase(i);
+                       }
+                       else if(stage->type==FRAGMENT && var.interface=="out")
+                       {
+                               stage->locations[var.name] = location;
+                               var.layout->qualifiers.erase(i);
+                       }
+
+                       if(var.layout->qualifiers.empty())
+                               var.layout = 0;
+               }
+       }
+
+       if((var.interface=="in" || var.interface=="out") && !check_version(Version(1, 30)))
+       {
+               if(stage->type==VERTEX && var.interface=="in")
+                       var.interface = "attribute";
+               else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in"))
+                       var.interface = "varying";
+               else if(stage->type==FRAGMENT && var.interface=="out")
+               {
+                       frag_out_name = var.name;
+                       remove_node = true;
+               }
+       }
+
+       TraversingVisitor::visit(var);
+}
+
+void ProgramCompiler::LegacyConverter::visit(InterfaceBlock &iface)
+{
+       if(!check_version(Version(1, 50)))
+               flatten_block(iface.members);
+}
+
 } // namespace GL
 } // namespace Msp