]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/finalize.cpp
Fill in the required version if empty
[libs/gl.git] / source / glsl / finalize.cpp
index fb76ab6d57dc49c22997ad7a64b07e030ed45b34..8a525e378cc0c990b41b53071538602b9ccbb768 100644 (file)
@@ -10,17 +10,18 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-DefaultPrecisionGenerator::DefaultPrecisionGenerator():
+PrecisionConverter::PrecisionConverter():
        stage(0)
 { }
 
-void DefaultPrecisionGenerator::apply(Stage &s)
+void PrecisionConverter::apply(Stage &s)
 {
        stage = &s;
        s.content.visit(*this);
+       NodeRemover().apply(s, nodes_to_remove);
 }
 
-void DefaultPrecisionGenerator::visit(Block &block)
+void PrecisionConverter::visit(Block &block)
 {
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
@@ -30,56 +31,55 @@ void DefaultPrecisionGenerator::visit(Block &block)
        }
 }
 
-void DefaultPrecisionGenerator::visit(Precision &prec)
+void PrecisionConverter::visit(Precision &prec)
 {
-       have_default.insert(prec.type);
+       if(stage->required_features.gl_api==OPENGL_ES2)
+               have_default.insert(prec.type);
+       else
+               nodes_to_remove.insert(&prec);
 }
 
-void DefaultPrecisionGenerator::visit(VariableDeclaration &var)
+void PrecisionConverter::visit(VariableDeclaration &var)
 {
-       if(var.type_declaration)
+       if(stage->required_features.gl_api!=OPENGL_ES2)
+       {
+               var.precision.clear();
                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";
+       const char *default_prec = (stage->type==Stage::FRAGMENT ? "mediump" : "highp");
+       const TypeDeclaration *type = var.type_declaration;
+       while(type)
+       {
+               if(dynamic_cast<const ImageTypeDeclaration *>(type))
+               {
+                       default_prec = "lowp";
+                       break;
+               }
+               else if(const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(type))
+               {
+                       if(basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT)
+                               break;
+                       type = basic->base_type;
+               }
+               else
+                       return;
+       }
+       if(!type)
+               return;
 
-       if(!have_default.count(type))
+       if(!have_default.count(type->name))
        {
                Precision *prec = new Precision;
-               if(!type.compare(0, 7, "sampler"))
-                       prec->precision = "lowp";
-               else if(stage->type==Stage::FRAGMENT)
-                       prec->precision = "mediump";
-               else
-                       prec->precision = "highp";
-               prec->type = type;
+               prec->precision = default_prec;
+               prec->type = type->name;
                stage->content.body.insert(insert_point, prec);
 
-               have_default.insert(type);
+               have_default.insert(type->name);
        }
 }
 
 
-void PrecisionRemover::apply(Stage &stage)
-{
-       stage.content.visit(*this);
-       NodeRemover().apply(stage, nodes_to_remove);
-}
-
-void PrecisionRemover::visit(Precision &prec)
-{
-       nodes_to_remove.insert(&prec);
-}
-
-void PrecisionRemover::visit(VariableDeclaration &var)
-{
-       var.precision.clear();
-}
-
-
 LegacyConverter::LegacyConverter():
        frag_out(0)
 { }
@@ -89,7 +89,12 @@ void LegacyConverter::apply(Stage &s, const Features &feat)
        stage = &s;
        features = feat;
        if(supports_stage(s.type))
+       {
                s.content.visit(*this);
+
+               if(!stage->required_features.glsl_version)
+                       stage->required_features.glsl_version = Version(1, (stage->required_features.gl_api==OPENGL_ES2 ? 0 : 10));
+       }
        else
                unsupported(format("Stage %s is not supported", Stage::get_stage_name(s.type)));
 }