]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Throw an exception if Texture*::allocate is called before storage
[libs/gl.git] / source / programcompiler.cpp
index 0d61c6853c737a75bac34212114077e1b691286c..5bb84269f30abe4fcd38198cef0b6a2ae416b2f8 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/raii.h>
+#include <msp/gl/extensions/ext_gpu_shader4.h>
 #include <msp/strings/format.h>
 #include <msp/strings/utils.h>
 #include "error.h"
@@ -12,12 +13,12 @@ using namespace std;
 namespace {
 
 const char builtins_src[] =
-       "////// vertex\n"
+       "#pragma MSP stage(vertex)\n"
        "out gl_PerVertex {\n"
        "  vec4 gl_Position;\n"
        "  float gl_ClipDistance[];\n"
-       "};"
-       "////// geometry\n"
+       "};\n"
+       "#pragma MSP stage(geometry)\n"
        "in gl_PerVertex {\n"
        "  vec4 gl_Position;\n"
        "  float gl_ClipDistance[];\n"
@@ -34,25 +35,45 @@ namespace GL {
 
 using namespace ProgramSyntax;
 
+// XXX For some reason global declarations are emitted for otherwise undeclared local variables
+
 ProgramCompiler::ProgramCompiler():
        resources(0),
        module(0)
 { }
 
-void ProgramCompiler::compile(const string &source)
+ProgramCompiler::~ProgramCompiler()
+{
+       delete module;
+}
+
+void ProgramCompiler::compile(const string &source, const string &src_name)
 {
        resources = 0;
-       module = &parser.parse(source);
+       delete module;
+       module = new Module();
+       ProgramParser parser;
+       imported_names.insert(src_name);
+       append_module(parser.parse(source, src_name));
        process();
 }
 
-void ProgramCompiler::compile(IO::Base &io, Resources *res)
+void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
 {
        resources = res;
-       module = &parser.parse(io);
+       delete module;
+       module = new Module();
+       ProgramParser parser;
+       imported_names.insert(src_name);
+       append_module(parser.parse(io, src_name));
        process();
 }
 
+void ProgramCompiler::compile(IO::Base &io, const string &src_name)
+{
+       compile(io, 0, src_name);
+}
+
 void ProgramCompiler::add_shaders(Program &program)
 {
        if(!module)
@@ -71,8 +92,11 @@ void ProgramCompiler::add_shaders(Program &program)
                else if(i->type==FRAGMENT)
                {
                        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);
+                       if(EXT_gpu_shader4)
+                       {
+                               for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
+                                       program.bind_fragment_data(j->second, j->first);
+                       }
                }
        }
 }
@@ -80,7 +104,7 @@ void ProgramCompiler::add_shaders(Program &program)
 Module *ProgramCompiler::create_builtins_module()
 {
        ProgramParser parser;
-       Module *module = new Module(parser.parse(builtins_src));
+       Module *module = new Module(parser.parse(builtins_src, "<builtin>"));
        for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
        {
                VariableResolver resolver;
@@ -106,13 +130,47 @@ Stage *ProgramCompiler::get_builtins(StageType type)
        return 0;
 }
 
-void ProgramCompiler::process()
+void ProgramCompiler::append_module(ProgramSyntax::Module &mod)
+{
+       vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
+       for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
+               import((*i)->module);
+       apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
+
+       append_stage(mod.shared);
+       for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
+               append_stage(*i);
+}
+
+void ProgramCompiler::append_stage(Stage &stage)
 {
-       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()));
+       Stage *target = 0;
+       if(stage.type==SHARED)
+               target = &module->shared;
+       else
+       {
+               list<Stage>::iterator i;
+               for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
+               if(i==module->stages.end() || i->type>stage.type)
+               {
+                       list<Stage>::iterator j = module->stages.insert(i, stage.type);
+                       if(i!=module->stages.end())
+                               i->previous = &*j;
+                       i = j;
+                       if(i!=module->stages.begin())
+                               i->previous = &*--j;
+               }
 
+               target = &*i;
+       }
+
+       for(NodeList<Node>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
+               target->content.body.push_back(*i);
+       apply<DeclarationCombiner>(*target);
+}
+
+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(); )
@@ -122,38 +180,22 @@ void ProgramCompiler::process()
                else
                        ++i;
        }
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               finalize(*i);
 }
 
 void ProgramCompiler::import(const string &name)
 {
        string fn = name+".glsl";
+       if(imported_names.count(fn))
+               return;
+       imported_names.insert(fn);
+
        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);
-               }
-       }
+       append_module(import_parser.parse(*io, fn));
 }
 
 void ProgramCompiler::generate(Stage &stage)
@@ -165,8 +207,8 @@ void ProgramCompiler::generate(Stage &stage)
        apply<VariableResolver>(stage);
        apply<InterfaceGenerator>(stage);
        apply<VariableResolver>(stage);
-       apply<VariableRenamer>(stage);
        apply<DeclarationReorderer>(stage);
+       apply<FunctionResolver>(stage);
        apply<LegacyConverter>(stage);
 }
 
@@ -185,6 +227,14 @@ bool ProgramCompiler::optimize(Stage &stage)
        return !unused.empty();
 }
 
+void ProgramCompiler::finalize(Stage &stage)
+{
+       if(get_gl_api()==OPENGL_ES2)
+               apply<DefaultPrecisionGenerator>(stage);
+       else
+               apply<PrecisionRemover>(stage);
+}
+
 void ProgramCompiler::inject_block(Block &target, const Block &source)
 {
        list<RefPtr<Node> >::iterator insert_point = target.body.begin();
@@ -220,6 +270,38 @@ void ProgramCompiler::Visitor::apply(Stage &s)
 }
 
 
+ProgramCompiler::BlockModifier::BlockModifier():
+       remove_node(false)
+{ }
+
+void ProgramCompiler::BlockModifier::flatten_block(Block &block)
+{
+       insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
+       remove_node = true;
+}
+
+void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
+{
+       block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
+       insert_nodes.clear();
+
+       if(remove_node)
+               block.body.erase(i++);
+       else
+               ++i;
+       remove_node = false;
+}
+
+void ProgramCompiler::BlockModifier::visit(Block &block)
+{
+       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+       {
+               (*i)->visit(*this);
+               apply_and_increment(block, i);
+       }
+}
+
+
 ProgramCompiler::Formatter::Formatter():
        indent(0),
        parameter_list(false),
@@ -228,9 +310,17 @@ ProgramCompiler::Formatter::Formatter():
 
 void ProgramCompiler::Formatter::apply(ProgramSyntax::Stage &s)
 {
+       GLApi api = get_gl_api();
        const Version &ver = s.required_version;
-       if(ver.major)
-               formatted += format("#version %d%d\n", ver.major, ver.minor);
+
+       if(ver)
+       {
+               formatted += format("#version %d%02d", ver.major, ver.minor);
+               if(api==OPENGL_ES2 && ver>=Version(3, 0))
+                       formatted += " es";
+               formatted += '\n';
+       }
+
        Visitor::apply(s);
 }
 
@@ -329,6 +419,11 @@ void ProgramCompiler::Formatter::visit(Import &import)
        formatted += format("import %s;", import.module);
 }
 
+void ProgramCompiler::Formatter::visit(Precision &prec)
+{
+       formatted += format("precision %s %s;", prec.precision, prec.type);
+}
+
 void ProgramCompiler::Formatter::visit(Layout &layout)
 {
        formatted += "layout(";
@@ -368,7 +463,19 @@ void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
        if(!var.sampling.empty())
                formatted += format("%s ", var.sampling);
        if(!var.interface.empty() && var.interface!=block_interface)
-               formatted += format("%s ", var.interface);
+       {
+               string interface = var.interface;
+               if(stage->required_version<Version(1, 30))
+               {
+                       if(stage->type==VERTEX && var.interface=="in")
+                               interface = "attribute";
+                       else if((stage->type==VERTEX && var.interface=="out") || (stage->type==FRAGMENT && var.interface=="in"))
+                               interface = "varying";
+               }
+               formatted += format("%s ", interface);
+       }
+       if(!var.precision.empty())
+               formatted += format("%s ", var.precision);
        formatted += format("%s %s", var.type, var.name);
        if(var.array)
        {
@@ -461,8 +568,7 @@ void ProgramCompiler::Formatter::visit(Jump &jump)
 
 
 ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
-       toplevel(true),
-       remove_node(false)
+       toplevel(true)
 { }
 
 void ProgramCompiler::DeclarationCombiner::visit(Block &block)
@@ -471,15 +577,7 @@ void ProgramCompiler::DeclarationCombiner::visit(Block &block)
                return;
 
        SetForScope<bool> set(toplevel, false);
-       for(list<RefPtr<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;
-       }
+       BlockModifier::visit(block);
 }
 
 void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
@@ -679,38 +777,6 @@ void ProgramCompiler::FunctionResolver::visit(FunctionDeclaration &func)
 }
 
 
-ProgramCompiler::BlockModifier::BlockModifier():
-       remove_node(false)
-{ }
-
-void ProgramCompiler::BlockModifier::flatten_block(Block &block)
-{
-       insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
-       remove_node = true;
-}
-
-void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
-{
-       block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
-       insert_nodes.clear();
-
-       if(remove_node)
-               block.body.erase(i++);
-       else
-               ++i;
-       remove_node = false;
-}
-
-void ProgramCompiler::BlockModifier::visit(Block &block)
-{
-       for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
-       {
-               (*i)->visit(*this);
-               apply_and_increment(block, i);
-       }
-}
-
-
 ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
        scope_level(0)
 { }
@@ -797,6 +863,7 @@ void ProgramCompiler::InterfaceGenerator::insert_assignment(const string &left,
 
        ExpressionStatement *stmt = new ExpressionStatement;
        stmt->expression = assign;
+       stmt->visit(*this);
        insert_nodes.push_back(stmt);
 }
 
@@ -812,7 +879,10 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableReference &var)
        if(i==prev_out.end())
                i = prev_out.find(in_prefix+var.name);
        if(i!=prev_out.end())
-               generate_interface(*i->second, "in", var.name);
+       {
+               generate_interface(*i->second, "in", i->second->name);
+               var.name = i->second->name;
+       }
 }
 
 void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
@@ -825,7 +895,10 @@ void ProgramCompiler::InterfaceGenerator::visit(VariableDeclaration &var)
                {
                        remove_node = true;
                        if(var.init_expression)
+                       {
                                insert_assignment(var.name, var.init_expression->clone());
+                               return;
+                       }
                }
        }
        else if(var.interface=="in")
@@ -914,28 +987,31 @@ void ProgramCompiler::InterfaceGenerator::visit(Passthrough &pass)
 }
 
 
-void ProgramCompiler::VariableRenamer::visit(VariableReference &var)
-{
-       if(var.declaration)
-               var.name = var.declaration->name;
-}
-
-void ProgramCompiler::VariableRenamer::visit(VariableDeclaration &var)
-{
-       if(var.linked_declaration)
-               var.name = var.linked_declaration->name;
-       TraversingVisitor::visit(var);
-}
-
-
 ProgramCompiler::DeclarationReorderer::DeclarationReorderer():
+       scope_level(0),
        kind(NO_DECLARATION)
 { }
 
+void ProgramCompiler::DeclarationReorderer::visit(FunctionCall &call)
+{
+       FunctionDeclaration *def = call.declaration;
+       if(def)
+               def = def->definition;
+       if(def && !ordered_funcs.count(def))
+               needed_funcs.insert(def);
+}
+
 void ProgramCompiler::DeclarationReorderer::visit(Block &block)
 {
+       SetForScope<unsigned> set(scope_level, scope_level+1);
+       if(scope_level>1)
+               return Visitor::visit(block);
+
        list<RefPtr<Node> >::iterator struct_insert_point = block.body.end();
        list<RefPtr<Node> >::iterator variable_insert_point = block.body.end();
+       list<RefPtr<Node> >::iterator function_insert_point = block.body.end();
+       unsigned unordered_func_count = 0;
+       bool ordered_any_funcs = false;
 
        for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
        {
@@ -959,13 +1035,64 @@ void ProgramCompiler::DeclarationReorderer::visit(Block &block)
                else if(kind>VARIABLE && variable_insert_point==block.body.end())
                        variable_insert_point = i;
 
+               if(kind==FUNCTION)
+               {
+                       if(function_insert_point==block.body.end())
+                               function_insert_point = i;
+
+                       if(needed_funcs.empty())
+                       {
+                               ordered_funcs.insert(i->get());
+                               if(i!=function_insert_point)
+                               {
+                                       block.body.insert(function_insert_point, *i);
+                                       moved = true;
+                               }
+                               else
+                                       ++function_insert_point;
+                               ordered_any_funcs = true;
+                       }
+                       else
+                               ++unordered_func_count;
+               }
+
                if(moved)
+               {
+                       if(function_insert_point==i)
+                               ++function_insert_point;
                        block.body.erase(i++);
+               }
                else
                        ++i;
+
+               if(i==block.body.end() && unordered_func_count)
+               {
+                       if(!ordered_any_funcs)
+                               // A subset of the remaining functions forms a recursive loop
+                               /* TODO pick a function and move it up, adding any necessary
+                               declarations */
+                               break;
+
+                       i = function_insert_point;
+                       unordered_func_count = 0;
+               }
        }
 }
 
+void ProgramCompiler::DeclarationReorderer::visit(ProgramSyntax::VariableDeclaration &var)
+{
+       Visitor::visit(var);
+       kind = VARIABLE;
+}
+
+void ProgramCompiler::DeclarationReorderer::visit(FunctionDeclaration &func)
+{
+       needed_funcs.clear();
+       func.body.visit(*this);
+       needed_funcs.erase(&func);
+       kind = FUNCTION;
+}
+
 
 ProgramCompiler::InlineableFunctionLocator::InlineableFunctionLocator():
        in_function(0)
@@ -1483,15 +1610,78 @@ void ProgramCompiler::NodeRemover::visit(VariableDeclaration &var)
 }
 
 
+void ProgramCompiler::PrecisionRemover::visit(Precision &)
+{
+       remove_node = true;
+}
+
+void ProgramCompiler::PrecisionRemover::visit(VariableDeclaration &var)
+{
+       var.precision.clear();
+}
+
+
+ProgramCompiler::DefaultPrecisionGenerator::DefaultPrecisionGenerator():
+       toplevel(true)
+{ }
+
+void ProgramCompiler::DefaultPrecisionGenerator::visit(Block &block)
+{
+       if(toplevel)
+       {
+               SetForScope<bool> set(toplevel, false);
+               BlockModifier::visit(block);
+       }
+       else
+               Visitor::visit(block);
+}
+
+void ProgramCompiler::DefaultPrecisionGenerator::visit(Precision &prec)
+{
+       have_default.insert(prec.type);
+}
+
+void ProgramCompiler::DefaultPrecisionGenerator::visit(VariableDeclaration &var)
+{
+       if(var.type_declaration)
+               return;
+
+       string type = var.type;
+       if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
+               type = "float";
+       else if(!type.compare(0, 3, "ivec") || type=="uint")
+               type = "int";
+
+       if(!have_default.count(type))
+       {
+               Precision *prec = new Precision;
+               if(!type.compare(0, 7, "sampler"))
+                       prec->precision = "lowp";
+               else if(stage->type==FRAGMENT)
+                       prec->precision = "mediump";
+               else
+                       prec->precision = "highp";
+               prec->type = type;
+               insert_nodes.push_back(prec);
+
+               have_default.insert(type);
+       }
+}
+
+
 ProgramCompiler::LegacyConverter::LegacyConverter():
-       target_version(get_glsl_version())
+       target_api(get_gl_api()),
+       target_version(get_glsl_version()),
+       frag_out(0)
 { }
 
 ProgramCompiler::LegacyConverter::LegacyConverter(const Version &v):
-       target_version(v)
+       target_api(get_gl_api()),
+       target_version(v),
+       frag_out(0)
 { }
 
-bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_version)
+bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_version) const
 {
        if(target_version<feature_version)
                return false;
@@ -1501,9 +1691,17 @@ bool ProgramCompiler::LegacyConverter::check_version(const Version &feature_vers
        return true;
 }
 
+bool ProgramCompiler::LegacyConverter::supports_unified_interface_syntax() const
+{
+       if(target_api==OPENGL_ES2)
+               return check_version(Version(3, 0));
+       else
+               return check_version(Version(1, 30));
+}
+
 void ProgramCompiler::LegacyConverter::visit(VariableReference &var)
 {
-       if(var.name==frag_out_name && !check_version(Version(1, 30)))
+       if(var.declaration==frag_out && !supports_unified_interface_syntax())
        {
                var.name = "gl_FragColor";
                var.declaration = 0;
@@ -1515,9 +1713,24 @@ void ProgramCompiler::LegacyConverter::visit(VariableReference &var)
                type = string();
 }
 
+void ProgramCompiler::LegacyConverter::visit(Assignment &assign)
+{
+       TraversingVisitor::visit(assign);
+       if(assign.target_declaration==frag_out && !supports_unified_interface_syntax())
+               assign.target_declaration = 0;
+}
+
+bool ProgramCompiler::LegacyConverter::supports_unified_sampling_functions() const
+{
+       if(target_api==OPENGL_ES2)
+               return check_version(Version(3, 0));
+       else
+               return check_version(Version(1, 30));
+}
+
 void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
 {
-       if(call.name=="texture" && !call.declaration && !check_version(Version(1, 30)))
+       if(call.name=="texture" && !call.declaration && !supports_unified_sampling_functions())
        {
                vector<RefPtr<Expression> >::iterator i = call.arguments.begin();
                if(i!=call.arguments.end())
@@ -1542,9 +1755,17 @@ void ProgramCompiler::LegacyConverter::visit(FunctionCall &call)
                TraversingVisitor::visit(call);
 }
 
+bool ProgramCompiler::LegacyConverter::supports_interface_layouts() const
+{
+       if(target_api==OPENGL_ES2)
+               return check_version(Version(3, 0));
+       else
+               return check_version(Version(3, 30));
+}
+
 void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
 {
-       if(var.layout && !check_version(Version(3, 30)))
+       if(var.layout && !supports_interface_layouts())
        {
                vector<Layout::Qualifier>::iterator i;
                for(i=var.layout->qualifiers.begin(); (i!=var.layout->qualifiers.end() && i->identifier!="location"); ++i) ;
@@ -1558,6 +1779,8 @@ void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
                        }
                        else if(stage->type==FRAGMENT && var.interface=="out")
                        {
+                               if(location!=0)
+                                       static Require _req(EXT_gpu_shader4);
                                stage->locations[var.name] = location;
                                var.layout->qualifiers.erase(i);
                        }
@@ -1567,15 +1790,11 @@ void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
                }
        }
 
-       if((var.interface=="in" || var.interface=="out") && !check_version(Version(1, 30)))
+       if((var.interface=="in" || var.interface=="out") && !supports_unified_interface_syntax())
        {
-               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")
+               if(stage->type==FRAGMENT && var.interface=="out")
                {
-                       frag_out_name = var.name;
+                       frag_out = &var;
                        remove_node = true;
                }
        }
@@ -1583,9 +1802,22 @@ void ProgramCompiler::LegacyConverter::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
+bool ProgramCompiler::LegacyConverter::supports_interface_blocks(const string &iface) const
+{
+       if(target_api==OPENGL_ES2)
+       {
+               if(iface=="uniform")
+                       return check_version(Version(3, 0));
+               else
+                       return check_version(Version(3, 20));
+       }
+       else
+               return check_version(Version(1, 50));
+}
+
 void ProgramCompiler::LegacyConverter::visit(InterfaceBlock &iface)
 {
-       if(!check_version(Version(1, 50)))
+       if(!supports_interface_blocks(iface.interface))
                flatten_block(iface.members);
 }