]> git.tdb.fi Git - libs/gl.git/commitdiff
Use type information in converting legacy texture sampling functions
authorMikko Rasa <tdb@tdb.fi>
Wed, 31 Mar 2021 20:21:47 +0000 (23:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 31 Mar 2021 20:21:47 +0000 (23:21 +0300)
source/glsl/finalize.cpp
source/glsl/finalize.h

index 8a525e378cc0c990b41b53071538602b9ccbb768..532e2ffcbb3914fd2d7e81dafab0f784e0b52daf 100644 (file)
@@ -166,12 +166,7 @@ void LegacyConverter::visit(VariableReference &var)
        {
                var.name = "gl_FragColor";
                var.declaration = 0;
-               r_type = "vec4";
        }
-       else if(var.declaration)
-               r_type = var.declaration->type;
-       else
-               r_type.clear();
 }
 
 void LegacyConverter::visit(Assignment &assign)
@@ -191,58 +186,39 @@ bool LegacyConverter::supports_unified_sampling_functions() const
 
 void LegacyConverter::visit(FunctionCall &call)
 {
-       if(call.name=="texture")
+       if(call.declaration && call.declaration->source==BUILTIN_SOURCE)
        {
-               string sampler_type;
-               r_type.clear();
-               NodeArray<Expression>::iterator i = call.arguments.begin();
-               if(i!=call.arguments.end())
+               if(!call.name.compare(0, 7, "texture") && call.arguments.size()>=1)
                {
-                       (*i)->visit(*this);
-                       sampler_type = r_type;
-
-                       for(; i!=call.arguments.end(); ++i)
-                               (*i)->visit(*this);
-               }
-
-               if(!supports_unified_sampling_functions())
-               {
-                       if(sampler_type=="sampler1D")
-                               call.name = "texture1D";
-                       else if(sampler_type=="sampler2D")
-                               call.name = "texture2D";
-                       else if(sampler_type=="sampler3D")
-                               call.name = "texture3D";
-                       else if(sampler_type=="samplerCube")
-                               call.name = "textureCube";
-                       else if(sampler_type=="sampler1DShadow")
-                               call.name = "shadow1D";
-                       else if(sampler_type=="sampler2DShadow")
-                               call.name = "shadow2D";
-                       else if(sampler_type=="sampler1DArray")
-                       {
-                               check_extension(&Features::ext_texture_array);
-                               call.name = "texture1DArray";
-                       }
-                       else if(sampler_type=="sampler2DArray")
+                       const ImageTypeDeclaration *arg_image = dynamic_cast<const ImageTypeDeclaration *>(call.arguments.front()->type);
+                       if(arg_image && !supports_unified_sampling_functions())
                        {
-                               check_extension(&Features::ext_texture_array);
-                               call.name = "texture2DArray";
-                       }
-                       else if(sampler_type=="sampler1DArrayShadow")
-                       {
-                               check_extension(&Features::ext_texture_array);
-                               call.name = "shadow1DArray";
-                       }
-                       else if(sampler_type=="sampler2DArrayShadow")
-                       {
-                               check_extension(&Features::ext_texture_array);
-                               call.name = "shadow2DArray";
+                               string suffix = call.name.substr(7);
+                               call.name = (arg_image->shadow ? "shadow" : "texture");
+
+                               switch(arg_image->dimensions)
+                               {
+                               case ImageTypeDeclaration::ONE: call.name += "1D"; break;
+                               case ImageTypeDeclaration::TWO: call.name += "2D"; break;
+                               case ImageTypeDeclaration::THREE: call.name += "3D"; break;
+                               case ImageTypeDeclaration::CUBE: call.name += "Cube"; break;
+                               }
+
+                               if(arg_image->array)
+                               {
+                                       /* Array textures and the unified sampling function name were
+                                       both introduced in GLSL 1.30. */
+                                       if(arg_image->dimensions==ImageTypeDeclaration::ONE || arg_image->dimensions==ImageTypeDeclaration::TWO)
+                                               check_extension(&Features::ext_texture_array);
+                                       call.name += "Array";
+                               }
+
+                               call.name += suffix;
                        }
                }
        }
-       else
-               TraversingVisitor::visit(call);
+
+       TraversingVisitor::visit(call);
 }
 
 bool LegacyConverter::supports_interface_layouts() const
index 21b4e316d0eec52ae34e5c5c050d15e024656bb7..e0904b2e40eab2272accfd1cf4c8e6d4972ac4fd 100644 (file)
@@ -36,7 +36,6 @@ class LegacyConverter: private TraversingVisitor
 private:
        Stage *stage;
        Features features;
-       std::string r_type;
        VariableDeclaration *frag_out;
        NodeList<Statement>::iterator uniform_insert_point;
        std::set<Node *> nodes_to_remove;