]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/optimize.cpp
Fold type conversions of constants
[libs/gl.git] / source / glsl / optimize.cpp
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;
 }