]> git.tdb.fi Git - libs/gl.git/commitdiff
Move some type information functions to glsl/reflect.cpp
authorMikko Rasa <tdb@tdb.fi>
Tue, 20 Apr 2021 11:09:27 +0000 (14:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 20 Apr 2021 11:09:27 +0000 (14:09 +0300)
source/glsl/reflect.cpp
source/glsl/reflect.h
source/glsl/resolve.cpp
source/glsl/resolve.h
source/glsl/spirv.cpp
source/glsl/spirv.h

index c38e3aecbb3d487c6c7c2f10f510b6495092e872..c2a913f982acfcb61c6ca7a0289f3233f3d434e9 100644 (file)
@@ -6,6 +6,44 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+bool is_scalar(const BasicTypeDeclaration &type)
+{
+       return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT);
+}
+
+bool is_vector_or_matrix(const BasicTypeDeclaration &type)
+{
+       return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX);
+}
+
+BasicTypeDeclaration *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 can_convert(const BasicTypeDeclaration &from, const 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(is_vector_or_matrix(from) && 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;
+}
+
+
 unsigned TypeComparer::next_tag = 1;
 
 TypeComparer::TypeComparer():
index 8de915729ce50e8748c9e8011419d5bd4f7cf6b8..63dfe7bec53ed2e843d2f8857d8cb18b056a6fb2 100644 (file)
@@ -7,6 +7,11 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
+bool is_scalar(const BasicTypeDeclaration &);
+bool is_vector_or_matrix(const BasicTypeDeclaration &);
+BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
+bool can_convert(const BasicTypeDeclaration &, const BasicTypeDeclaration &);
+
 /** Compares two types for equality.  Struct types are compared recursively. */
 class TypeComparer: private NodeVisitor
 {
index fec3cf143130a583c38a961b38b70087309b555e..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)
index 9e1d4f51c8dd8a6f75f3bd601a45532bdf439917..2416502cf23f713e8f5f2f8a5de11391195250d2 100644 (file)
@@ -119,10 +119,6 @@ public:
        bool apply(Stage &);
 
 private:
-       static bool is_scalar(BasicTypeDeclaration &);
-       static bool is_vector_or_matrix(BasicTypeDeclaration &);
-       static BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
-       static bool can_convert(BasicTypeDeclaration &, BasicTypeDeclaration &);
        static Compatibility get_compatibility(BasicTypeDeclaration &, BasicTypeDeclaration &);
        BasicTypeDeclaration *find_type(BasicTypeDeclaration::Kind, unsigned);
        BasicTypeDeclaration *find_type(BasicTypeDeclaration &, BasicTypeDeclaration::Kind, unsigned);
index be226b3c556ff67650146cb8de9fbcaa2bb188cf..44c1eb87a4c382a4c4735df44361c4188a5a633b 100644 (file)
@@ -455,16 +455,6 @@ SpirVGenerator::Id SpirVGenerator::write_construct(Id type_id, const Id *elem_id
        return result_id;
 }
 
-BasicTypeDeclaration &SpirVGenerator::get_element_type(BasicTypeDeclaration &basic)
-{
-       if(basic.kind==BasicTypeDeclaration::BOOL || basic.kind==BasicTypeDeclaration::INT || basic.kind==BasicTypeDeclaration::FLOAT)
-               return basic;
-       else if((basic.kind==BasicTypeDeclaration::VECTOR || basic.kind==BasicTypeDeclaration::MATRIX) && basic.base_type)
-               return get_element_type(dynamic_cast<BasicTypeDeclaration &>(*basic.base_type));
-       else
-               throw invalid_argument("SpirVGenerator::get_element_type");
-}
-
 void SpirVGenerator::visit(Block &block)
 {
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
@@ -665,7 +655,7 @@ void SpirVGenerator::visit(UnaryExpression &unary)
                return;
 
        BasicTypeDeclaration &basic = dynamic_cast<BasicTypeDeclaration &>(*unary.expression->type);
-       BasicTypeDeclaration &elem = get_element_type(basic);
+       BasicTypeDeclaration &elem = *get_element_type(basic);
 
        if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
                /* SPIR-V allows constant operations on floating-point values only for
@@ -747,7 +737,7 @@ void SpirVGenerator::visit(BinaryExpression &binary)
        BasicTypeDeclaration &basic_left = dynamic_cast<BasicTypeDeclaration &>(*binary.left->type);
        BasicTypeDeclaration &basic_right = dynamic_cast<BasicTypeDeclaration &>(*binary.right->type);
        // Expression resolver ensures that element types are the same
-       BasicTypeDeclaration &elem = get_element_type(basic_left);
+       BasicTypeDeclaration &elem = *get_element_type(basic_left);
 
        if(constant_expression && elem.kind!=BasicTypeDeclaration::BOOL && elem.kind!=BasicTypeDeclaration::INT)
                /* SPIR-V allows constant operations on floating-point values only for
@@ -1024,7 +1014,7 @@ void SpirVGenerator::visit(FunctionCall &call)
                for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
                        if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*i)->type))
                        {
-                               BasicTypeDeclaration &elem_arg = get_element_type(*basic_arg);
+                               BasicTypeDeclaration &elem_arg = *get_element_type(*basic_arg);
                                switch(elem_arg.kind)
                                {
                                case BasicTypeDeclaration::BOOL: arg_types += 'b'; break;
@@ -1101,9 +1091,9 @@ void SpirVGenerator::visit_constructor(FunctionCall &call, const vector<Id> &arg
                return;
        }
 
-       BasicTypeDeclaration &elem = get_element_type(*basic);
+       BasicTypeDeclaration &elem = *get_element_type(*basic);
        BasicTypeDeclaration &basic_arg0 = dynamic_cast<BasicTypeDeclaration &>(*call.arguments[0]->type);
-       BasicTypeDeclaration &elem_arg0 = get_element_type(basic_arg0);
+       BasicTypeDeclaration &elem_arg0 = *get_element_type(basic_arg0);
 
        if(basic->kind==BasicTypeDeclaration::MATRIX)
        {
index 21f0ed5331c531c40455cdc5ae7d279f92ef1755..1e7ec370d92249b073b2b53d2299d6b96460147a 100644 (file)
@@ -127,7 +127,6 @@ private:
        Id write_expression(Opcode, Id, Id, Id);
        void write_deconstruct(Id, Id, Id *, unsigned);
        Id write_construct(Id, const Id *, unsigned);
-       static BasicTypeDeclaration &get_element_type(BasicTypeDeclaration &);
 
        virtual void visit(Block &);
        virtual void visit(Literal &);