]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/reflect.h
Add visitors to calculate offsets of struct members
[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 /** Determines the number of interface locations required by a variable. */
11 class LocationCounter: private NodeVisitor
12 {
13 private:
14         unsigned r_count;
15
16 public:
17         LocationCounter();
18
19         unsigned apply(VariableDeclaration &v) { v.visit(*this); return r_count; }
20
21 private:
22         virtual void visit(BasicTypeDeclaration &);
23         virtual void visit(ImageTypeDeclaration &);
24         virtual void visit(StructDeclaration &);
25         virtual void visit(VariableDeclaration &);
26 };
27
28 /** Determines the size and alignment of a variable, in bytes. */
29 class MemoryRequirementsCalculator: private NodeVisitor
30 {
31 public:
32         struct Result
33         {
34                 unsigned size;
35                 unsigned alignment;
36
37                 Result(unsigned s, unsigned a): size(s), alignment(a) { }
38         };
39 private:
40         unsigned r_size;
41         unsigned r_alignment;
42         int r_offset;
43
44 public:
45         Result apply(VariableDeclaration &v) { v.visit(*this); return Result(r_size, r_alignment); }
46
47 private:
48         virtual void visit(BasicTypeDeclaration &);
49         virtual void visit(StructDeclaration &);
50         virtual void visit(VariableDeclaration &);
51 };
52
53 /** Collects dependencies of a function.  This includes global variables,
54 interface blocks, other functions and types. */
55 class DependencyCollector: private TraversingVisitor
56 {
57 private:
58         std::set<Node *> dependencies;
59         std::set<Node *> locals;
60
61 public:
62         std::set<Node *> apply(FunctionDeclaration &);
63
64 private:
65         virtual void visit(VariableReference &);
66         virtual void visit(InterfaceBlockReference &);
67         virtual void visit(FunctionCall &);
68         virtual void visit(VariableDeclaration &);
69 };
70
71 } // namespace SL
72 } // namespace GL
73 } // namespace Msp
74
75 #endif