]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/resolve.cpp
Move some type information functions to glsl/reflect.cpp
[libs/gl.git] / source / glsl / resolve.cpp
index 3201ea1bb2a70665507dd64469916fb0b09daa5c..176b13c5f1f55ad13e1613be145676150ef93db5 100644 (file)
@@ -1,6 +1,7 @@
 #include <algorithm>
 #include <msp/core/raii.h>
 #include <msp/strings/utils.h>
+#include "reflect.h"
 #include "resolve.h"
 
 using namespace std;
@@ -459,43 +460,6 @@ bool ExpressionResolver::apply(Stage &s)
        return r_any_resolved;
 }
 
-bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type)
-{
-       return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
-}
-
-bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type)
-{
-       return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
-}
-
-BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type)
-{
-       if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY)
-       {
-               BasicTypeDeclaration *basic_base = dynamic_cast<BasicTypeDeclaration *>(type.base_type);
-               return (basic_base ? get_element_type(*basic_base) : 0);
-       }
-       else
-               return &type;
-}
-
-bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to)
-{
-       if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT)
-               return from.size<=to.size;
-       else if(from.kind!=to.kind)
-               return false;
-       else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size)
-       {
-               BasicTypeDeclaration *from_base = dynamic_cast<BasicTypeDeclaration *>(from.base_type);
-               BasicTypeDeclaration *to_base = dynamic_cast<BasicTypeDeclaration *>(to.base_type);
-               return (from_base && to_base && can_convert(*from_base, *to_base));
-       }
-       else
-               return false;
-}
-
 ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right)
 {
        if(&left==&right)
@@ -745,6 +709,17 @@ void ExpressionResolver::visit(BinaryExpression &binary, bool assign)
                if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size))
                        return;
 
+               /* If the left operand is a vector and right is scalar, convert the right
+               operand to a vector too. */
+               if(left_size>1 && right_size==1)
+               {
+                       BasicTypeDeclaration *vec_right = find_type(*elem_right, basic_left->kind, basic_left->size);
+                       if(!vec_right)
+                               return;
+
+                       convert_to(binary.right, *vec_right);
+               }
+
                type = basic_left;
                // Don't perform conversion even if the operands are of different sizes.
                compat = SAME_TYPE;
@@ -883,6 +858,8 @@ void ExpressionResolver::visit_constructor(FunctionCall &call)
        map<string, TypeDeclaration *>::const_iterator i = stage->types.find(call.name);
        if(i==stage->types.end())
                return;
+       else if(call.arguments.size()==1 && i->second==call.arguments[0]->type)
+               ;
        else if(BasicTypeDeclaration *basic = dynamic_cast<BasicTypeDeclaration *>(i->second))
        {
                BasicTypeDeclaration *elem = get_element_type(*basic);