]> git.tdb.fi Git - libs/gl.git/commitdiff
Fold type conversions of constants
authorMikko Rasa <tdb@tdb.fi>
Tue, 20 Apr 2021 15:21:35 +0000 (18:21 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 20 Apr 2021 15:25:37 +0000 (18:25 +0300)
source/glsl/optimize.cpp
source/glsl/optimize.h
tests/glsl/binary_operators.glsl
tests/glsl/constructors.glsl
tests/glsl/inexact_function_argument_type.glsl

index 06ea0f49c98a03b4c8186449eaac823d922a4719..d21512f6c82e4e6bdef3ccbdf38f70cd6e018e66 100644 (file)
@@ -604,6 +604,17 @@ T ConstantFolder::evaluate_int_special_op(char oper, T left, T right)
        }
 }
 
+template<typename T>
+void ConstantFolder::convert_to_result(const Variant &value)
+{
+       if(value.check_type<bool>())
+               set_result(static_cast<T>(value.value<bool>()));
+       else if(value.check_type<int>())
+               set_result(static_cast<T>(value.value<int>()));
+       else if(value.check_type<float>())
+               set_result(static_cast<T>(value.value<float>()));
+}
+
 void ConstantFolder::set_result(const Variant &value, bool literal)
 {
        r_constant_value = value;
@@ -776,6 +787,28 @@ void ConstantFolder::visit(TernaryExpression &ternary)
 
 void ConstantFolder::visit(FunctionCall &call)
 {
+       if(call.constructor && call.type && call.arguments.size()==1)
+       {
+               const BasicTypeDeclaration *basic = dynamic_cast<const BasicTypeDeclaration *>(call.type);
+               if(basic)
+               {
+                       call.arguments[0]->visit(*this);
+                       bool can_fold = r_constant;
+                       r_constant = false;
+                       if(!can_fold)
+                               return;
+
+                       if(basic->kind==BasicTypeDeclaration::BOOL)
+                               convert_to_result<bool>(r_constant_value);
+                       else if(basic->kind==BasicTypeDeclaration::INT && basic->size==32)
+                               convert_to_result<int>(r_constant_value);
+                       else if(basic->kind==BasicTypeDeclaration::FLOAT && basic->size==32)
+                               convert_to_result<float>(r_constant_value);
+
+                       return;
+               }
+       }
+
        TraversingVisitor::visit(call);
        r_constant = false;
 }
index ed9f91345f71b405209e3b6a32f7adec0d5ebeb3..7d064056e48ceb6d72bb7d9a4b2e9d5099f2e401 100644 (file)
@@ -183,6 +183,8 @@ private:
        static T evaluate_arithmetic(char, T, T);
        template<typename T>
        static T evaluate_int_special_op(char, T, T);
+       template<typename T>
+       void convert_to_result(const Variant &);
        void set_result(const Variant &, bool = false);
 
        virtual void visit(RefPtr<Expression> &);
index d84962861e689408111836121181a1453c08e3e2..edf36ebbee46ec833fd96ccc68a56c4fd5c34adc 100644 (file)
@@ -52,7 +52,7 @@ void main()
        mat2x4 m2;
        vec4 v1 = vec4(1.0);
        vec2 v3;
-       v3 = v1*m2+(m2*m1*float(5)*v1).xy+vec2(iv);
+       v3 = v1*m2+(m2*m1*5.0*v1).xy+vec2(iv);
        if(b)
                ++v3;
 }
index ddb1dceae6f1c811bf8fde17cf3a0a9e1db8f314..9b878e49727c3dbf9ccc7e3d65668292ec35adfc 100644 (file)
@@ -41,7 +41,7 @@ void main()
 {
   mat3 normal_matrix = mat3(model[0].xyz, model[1].xyz, model[2].xyz);
   tbn_light_dir = mat3(normal_matrix*tangent, normal_matrix*binormal, normal_matrix*normal)*light_dir;
-  gl_Position = view_projection*model*vec4(position, float(1));
+  gl_Position = view_projection*model*vec4(position, 1.0);
   _vs_out_texcoord = texcoord;
 }
 */
@@ -53,6 +53,6 @@ layout(location=1) in vec2 _vs_out_texcoord;
 layout(location=0) in vec3 tbn_light_dir;
 void main()
 {
-  frag_color = vec4(vec3(dot(vec3(texture(normalmap, _vs_out_texcoord).xyz)*2.0-1.0, normalize(tbn_light_dir))), float(1));
+  frag_color = vec4(vec3(dot(vec3(texture(normalmap, _vs_out_texcoord).xyz)*2.0-1.0, normalize(tbn_light_dir))), 1.0);
 }
 */
index 2bbece37f5b70039600d259138f8b2f54043314a..627fa5d849235e60c0de9a682758c160b2c0577c 100644 (file)
@@ -13,6 +13,6 @@ void main()
 layout(location=0) in vec4 position;
 void main()
 {
-       gl_Position = vec4(position.xy*float(2), 0.0, 1.0);
+       gl_Position = vec4(position.xy*2.0, 0.0, 1.0);
 }
 */