X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Freflect.h;h=d48fbec156506f31fda79fd86aa676b8257a128d;hp=a48ad53f8cfa9b960665af6a61cd66e2a475d686;hb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14;hpb=70d9d2d28e5fe723c6b46894276e4c935f578e2d diff --git a/source/glsl/reflect.h b/source/glsl/reflect.h index a48ad53f..d48fbec1 100644 --- a/source/glsl/reflect.h +++ b/source/glsl/reflect.h @@ -7,15 +7,47 @@ 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 +{ +private: + Node *first = 0; + Node *second = 0; + unsigned first_tag = 0; + bool r_result = false; + + static unsigned next_tag; + +public: + bool apply(TypeDeclaration &t1, TypeDeclaration &t2) { compare(t1, t2); return r_result; } + +private: + void compare(Node &, Node &); + template + T *multi_visit(T &); + virtual void visit(Literal &); + virtual void visit(VariableReference &); + virtual void visit(UnaryExpression &); + virtual void visit(BinaryExpression &); + virtual void visit(TernaryExpression &); + virtual void visit(BasicTypeDeclaration &); + virtual void visit(ImageTypeDeclaration &); + virtual void visit(StructDeclaration &); + virtual void visit(VariableDeclaration &); +}; + /** Determines the number of interface locations required by a variable. */ class LocationCounter: private NodeVisitor { private: - unsigned r_count; + unsigned r_count = 0; public: - LocationCounter(); - unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; } private: @@ -25,6 +57,53 @@ private: virtual void visit(VariableDeclaration &); }; +/** Determines the size and alignment of a variable or a type, in bytes. */ +class MemoryRequirementsCalculator: private NodeVisitor +{ +public: + struct Result + { + unsigned size; + unsigned alignment; + unsigned stride; + + Result(unsigned s, unsigned a): size(s), alignment(a), stride(s+a-1-(s+a-1)%a) { } + }; +private: + unsigned r_size = 0; + unsigned r_alignment = 1; + int r_offset = -1; + +public: + Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); } + Result apply(TypeDeclaration &t) { t.visit(*this); return Result(r_size, r_alignment); } + +private: + virtual void visit(BasicTypeDeclaration &); + virtual void visit(StructDeclaration &); + virtual void visit(VariableDeclaration &); +}; + +/** Collects dependencies of a function. This includes global variables, +interface blocks, other functions and types. */ +class DependencyCollector: private TraversingVisitor +{ +private: + std::set dependencies; + std::set locals; + std::set visited_functions; + +public: + std::set apply(FunctionDeclaration &); + +private: + virtual void visit(VariableReference &); + virtual void visit(InterfaceBlockReference &); + virtual void visit(FunctionCall &); + virtual void visit(VariableDeclaration &); + virtual void visit(FunctionDeclaration &); +}; + } // namespace SL } // namespace GL } // namespace Msp