From: Mikko Rasa Date: Tue, 20 Apr 2021 11:09:27 +0000 (+0300) Subject: Move some type information functions to glsl/reflect.cpp X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=523491787b2b0321748a53f139c1a4355d2f9e85;p=libs%2Fgl.git Move some type information functions to glsl/reflect.cpp --- diff --git a/source/glsl/reflect.cpp b/source/glsl/reflect.cpp index c38e3aec..c2a913f9 100644 --- a/source/glsl/reflect.cpp +++ b/source/glsl/reflect.cpp @@ -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(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(from.base_type); + BasicTypeDeclaration *to_base = dynamic_cast(to.base_type); + return (from_base && to_base && can_convert(*from_base, *to_base)); + } + else + return false; +} + + unsigned TypeComparer::next_tag = 1; TypeComparer::TypeComparer(): diff --git a/source/glsl/reflect.h b/source/glsl/reflect.h index 8de91572..63dfe7be 100644 --- a/source/glsl/reflect.h +++ b/source/glsl/reflect.h @@ -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 { diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index fec3cf14..176b13c5 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1,6 +1,7 @@ #include #include #include +#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(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(from.base_type); - BasicTypeDeclaration *to_base = dynamic_cast(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) diff --git a/source/glsl/resolve.h b/source/glsl/resolve.h index 9e1d4f51..2416502c 100644 --- a/source/glsl/resolve.h +++ b/source/glsl/resolve.h @@ -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); diff --git a/source/glsl/spirv.cpp b/source/glsl/spirv.cpp index be226b3c..44c1eb87 100644 --- a/source/glsl/spirv.cpp +++ b/source/glsl/spirv.cpp @@ -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(*basic.base_type)); - else - throw invalid_argument("SpirVGenerator::get_element_type"); -} - void SpirVGenerator::visit(Block &block) { for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) @@ -665,7 +655,7 @@ void SpirVGenerator::visit(UnaryExpression &unary) return; BasicTypeDeclaration &basic = dynamic_cast(*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(*binary.left->type); BasicTypeDeclaration &basic_right = dynamic_cast(*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::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) if(BasicTypeDeclaration *basic_arg = dynamic_cast((*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 &arg return; } - BasicTypeDeclaration &elem = get_element_type(*basic); + BasicTypeDeclaration &elem = *get_element_type(*basic); BasicTypeDeclaration &basic_arg0 = dynamic_cast(*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) { diff --git a/source/glsl/spirv.h b/source/glsl/spirv.h index 21f0ed53..1e7ec370 100644 --- a/source/glsl/spirv.h +++ b/source/glsl/spirv.h @@ -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 &);