]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/reflect.h
Add a SPIR-V backend to the GLSL compiler
[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 /** Compares two types for equality.  Struct types are compared recursively. */
11 class TypeComparer: private NodeVisitor
12 {
13 private:
14         Node *first;
15         Node *second;
16         unsigned first_tag;
17         bool r_result;
18
19         static unsigned next_tag;
20
21 public:
22         TypeComparer();
23
24         bool apply(TypeDeclaration &t1, TypeDeclaration &t2) { compare(t1, t2); return r_result; }
25
26 private:
27         void compare(Node &, Node &);
28         template<typename T>
29         T *multi_visit(T &);
30         virtual void visit(Literal &);
31         virtual void visit(VariableReference &);
32         virtual void visit(BasicTypeDeclaration &);
33         virtual void visit(ImageTypeDeclaration &);
34         virtual void visit(StructDeclaration &);
35         virtual void visit(VariableDeclaration &);
36 };
37
38 /** Determines the number of interface locations required by a variable. */
39 class LocationCounter: private NodeVisitor
40 {
41 private:
42         unsigned r_count;
43
44 public:
45         LocationCounter();
46
47         unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; }
48
49 private:
50         virtual void visit(BasicTypeDeclaration &);
51         virtual void visit(ImageTypeDeclaration &);
52         virtual void visit(StructDeclaration &);
53         virtual void visit(VariableDeclaration &);
54 };
55
56 /** Determines the size and alignment of a variable or a type, in bytes. */
57 class MemoryRequirementsCalculator: private NodeVisitor
58 {
59 public:
60         struct Result
61         {
62                 unsigned size;
63                 unsigned alignment;
64                 unsigned stride;
65
66                 Result(unsigned s, unsigned a): size(s), alignment(a), stride(s+a-1-(s+a-1)%a) { }
67         };
68 private:
69         unsigned r_size;
70         unsigned r_alignment;
71         int r_offset;
72
73 public:
74         Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); }
75         Result apply(TypeDeclaration &t) { t.visit(*this); return Result(r_size, r_alignment); }
76
77 private:
78         virtual void visit(BasicTypeDeclaration &);
79         virtual void visit(StructDeclaration &);
80         virtual void visit(VariableDeclaration &);
81 };
82
83 /** Collects dependencies of a function.  This includes global variables,
84 interface blocks, other functions and types. */
85 class DependencyCollector: private TraversingVisitor
86 {
87 private:
88         std::set<Node *> dependencies;
89         std::set<Node *> locals;
90         std::set<FunctionDeclaration *> visited_functions;
91
92 public:
93         std::set<Node *> apply(FunctionDeclaration &);
94
95 private:
96         virtual void visit(VariableReference &);
97         virtual void visit(InterfaceBlockReference &);
98         virtual void visit(FunctionCall &);
99         virtual void visit(VariableDeclaration &);
100         virtual void visit(FunctionDeclaration &);
101 };
102
103 } // namespace SL
104 } // namespace GL
105 } // namespace Msp
106
107 #endif