]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/reflect.h
Use forward references for entry point interfaces in SPIR-V
[libs/gl.git] / source / glsl / reflect.h
1 #ifndef MSP_GL_SL_REFLECT_H_
2 #define MSP_GL_SL_REFLECT_H_
3
4 #include "visitor.h"
5
6 namespace Msp {
7 namespace GL {
8 namespace SL {
9
10 bool is_scalar(const BasicTypeDeclaration &);
11 bool is_vector_or_matrix(const BasicTypeDeclaration &);
12 BasicTypeDeclaration *get_element_type(BasicTypeDeclaration &);
13 bool can_convert(const BasicTypeDeclaration &, const BasicTypeDeclaration &);
14
15 /** Compares two types for equality.  Struct types are compared recursively. */
16 class TypeComparer: private NodeVisitor
17 {
18 private:
19         Node *first = 0;
20         Node *second = 0;
21         unsigned first_tag = 0;
22         bool r_result = false;
23
24         static unsigned next_tag;
25
26 public:
27         bool apply(TypeDeclaration &t1, TypeDeclaration &t2) { compare(t1, t2); return r_result; }
28
29 private:
30         void compare(Node &, Node &);
31         template<typename T>
32         T *multi_visit(T &);
33         virtual void visit(Literal &);
34         virtual void visit(VariableReference &);
35         virtual void visit(UnaryExpression &);
36         virtual void visit(BinaryExpression &);
37         virtual void visit(TernaryExpression &);
38         virtual void visit(FunctionCall &);
39         virtual void visit(BasicTypeDeclaration &);
40         virtual void visit(ImageTypeDeclaration &);
41         virtual void visit(StructDeclaration &);
42         virtual void visit(VariableDeclaration &);
43 };
44
45 /** Determines the number of interface locations required by a variable. */
46 class LocationCounter: private NodeVisitor
47 {
48 private:
49         unsigned r_count = 0;
50
51 public:
52         unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; }
53
54 private:
55         virtual void visit(BasicTypeDeclaration &);
56         virtual void visit(ImageTypeDeclaration &);
57         virtual void visit(StructDeclaration &);
58         virtual void visit(VariableDeclaration &);
59 };
60
61 /** Determines the size and alignment of a variable or a type, in bytes. */
62 class MemoryRequirementsCalculator: private NodeVisitor
63 {
64 public:
65         struct Result
66         {
67                 unsigned size;
68                 unsigned alignment;
69                 unsigned stride;
70
71                 Result(unsigned s, unsigned a): size(s), alignment(a), stride(s+a-1-(s+a-1)%a) { }
72         };
73 private:
74         unsigned r_size = 0;
75         unsigned r_alignment = 1;
76         int r_offset = -1;
77
78 public:
79         Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); }
80         Result apply(TypeDeclaration &t) { t.visit(*this); return Result(r_size, r_alignment); }
81
82 private:
83         virtual void visit(BasicTypeDeclaration &);
84         virtual void visit(StructDeclaration &);
85         virtual void visit(VariableDeclaration &);
86 };
87
88 /** Collects dependencies of a function.  This includes global variables,
89 interface blocks, other functions and types. */
90 class DependencyCollector: private TraversingVisitor
91 {
92 private:
93         std::set<Node *> dependencies;
94         std::set<Node *> locals;
95         std::set<FunctionDeclaration *> visited_functions;
96
97 public:
98         std::set<Node *> apply(FunctionDeclaration &);
99
100 private:
101         virtual void visit(VariableReference &);
102         virtual void visit(FunctionCall &);
103         virtual void visit(VariableDeclaration &);
104         virtual void visit(FunctionDeclaration &);
105 };
106
107 class AssignmentCollector: private TraversingVisitor
108 {
109 private:
110         bool assignment_target = false;
111         std::set<Node *> assigned_variables;
112
113 public:
114         std::set<Node *> apply(Node &);
115
116 private:
117         virtual void visit(VariableReference &);
118         virtual void visit(UnaryExpression &);
119         virtual void visit(BinaryExpression &);
120         virtual void visit(Assignment &);
121 };
122
123 } // namespace SL
124 } // namespace GL
125 } // namespace Msp
126
127 #endif