]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/resolve.cpp
Fix opcode for matrix inverse
[libs/gl.git] / source / glsl / resolve.cpp
index 2b18c84b653bbbc1195f30a69c362137ec5754db..f2670b53e6805a78f7a2ce6627882299904ff254 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)
@@ -508,10 +472,10 @@ ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTyp
                return NOT_COMPATIBLE;
 }
 
-BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size)
+BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign)
 {
        for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
-               if((*i)->kind==kind && (*i)->size==size)
+               if((*i)->kind==kind && (*i)->size==size && (*i)->sign==sign)
                        return *i;
        return 0;
 }
@@ -597,7 +561,9 @@ void ExpressionResolver::visit(Literal &literal)
        if(literal.value.check_type<bool>())
                resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false);
        else if(literal.value.check_type<int>())
-               resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false);
+               resolve(literal, find_type(BasicTypeDeclaration::INT, 32, true), false);
+       else if(literal.value.check_type<unsigned>())
+               resolve(literal, find_type(BasicTypeDeclaration::INT, 32, false), false);
        else if(literal.value.check_type<float>())
                resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false);
 }
@@ -745,6 +711,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;
@@ -1127,6 +1104,29 @@ bool FunctionResolver::apply(Stage &s)
        return r_any_resolved;
 }
 
+bool FunctionResolver::can_convert_arguments(const FunctionCall &call, const FunctionDeclaration &decl)
+{
+       if(decl.parameters.size()!=call.arguments.size())
+               return false;
+
+       for(unsigned j=0; j<call.arguments.size(); ++j)
+       {
+               const TypeDeclaration *arg_type = call.arguments[j]->type;
+               const TypeDeclaration *param_type = decl.parameters[j]->type_declaration;
+               if(arg_type==param_type)
+                       continue;
+
+               const BasicTypeDeclaration *arg_basic = dynamic_cast<const BasicTypeDeclaration *>(arg_type);
+               const BasicTypeDeclaration *param_basic = dynamic_cast<const BasicTypeDeclaration *>(param_type);
+               if(arg_basic && param_basic && can_convert(*arg_basic, *param_basic))
+                       continue;
+
+               return false;
+       }
+
+       return true;
+}
+
 void FunctionResolver::visit(FunctionCall &call)
 {
        FunctionDeclaration *declaration = 0;
@@ -1148,6 +1148,21 @@ void FunctionResolver::visit(FunctionCall &call)
                {
                        map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
                        declaration = (i!=stage->functions.end() ? i->second : 0);
+
+                       if(!declaration)
+                       {
+                               for(i=stage->functions.lower_bound(call.name+"("); (i!=stage->functions.end() && i->second->name==call.name); ++i)
+                                       if(can_convert_arguments(call, *i->second))
+                                       {
+                                               if(declaration)
+                                               {
+                                                       declaration = 0;
+                                                       break;
+                                               }
+                                               else
+                                                       declaration = i->second;
+                                       }
+                       }
                }
        }