]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Support precision qualifiers in shaders
[libs/gl.git] / source / programcompiler.cpp
index 0d61c6853c737a75bac34212114077e1b691286c..c54a33d6b6fbf0147fe21dd069db9f391d42cc16 100644 (file)
@@ -122,6 +122,8 @@ 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)
@@ -185,6 +187,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();
@@ -228,9 +238,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 +347,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 +391,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)
        {
@@ -1483,15 +1518,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 +1599,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 +1621,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 +1663,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) ;
@@ -1567,15 +1696,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 +1708,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);
 }