]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.h
Fix incorrect lookup of GLSL interface variables
[libs/gl.git] / source / glsl / finalize.h
1 #ifndef MSP_GL_SL_FINALIZE_H_
2 #define MSP_GL_SL_FINALIZE_H_
3
4 #include <string>
5 #include "visitor.h"
6
7 namespace Msp {
8 namespace GL {
9 namespace SL {
10
11 /** Assigns offset layout qualifiers to struct members. */
12 class StructOrganizer: private TraversingVisitor
13 {
14 private:
15         int offset = -1;
16
17 public:
18         void apply(Stage &s) { s.content.visit(*this); }
19
20 private:
21         virtual void visit(StructDeclaration &);
22         virtual void visit(VariableDeclaration &);
23 };
24
25 /** Assigns location and binding layout qualifiers to interface variables and
26 blocks. */
27 class LocationAllocator: private TraversingVisitor
28 {
29 private:
30         struct Uniform
31         {
32                 int location = -1;
33                 int desc_set = 0;
34                 int bind_point = -1;
35         };
36
37         Features features;
38         bool alloc_new = true;
39         std::map<std::string, std::set<unsigned> > used_locations;
40         std::map<std::string, Uniform> uniforms;
41         std::map<unsigned, std::set<unsigned> > used_bindings;
42         std::vector<VariableDeclaration *> unplaced_variables;
43         std::vector<VariableDeclaration *> unbound_textures;
44         std::vector<VariableDeclaration *> unbound_blocks;
45
46 public:
47         void apply(Module &, const Features &, bool = true);
48 private:
49         void apply(Stage &);
50
51         void allocate_locations(const std::string &);
52         void bind_uniform(RefPtr<Layout> &, const std::string &, unsigned);
53
54         bool visit_uniform(const std::string &, RefPtr<Layout> &);
55         virtual void visit(VariableDeclaration &);
56         virtual void visit(FunctionDeclaration &) { }
57 };
58
59 /** Generates default precision declarations or removes precision declarations
60 according to the requirements of the target API. */
61 class PrecisionConverter: private TraversingVisitor
62 {
63 private:
64         Stage *stage = 0;
65         std::set<std::string> have_default;
66         NodeList<Statement>::iterator insert_point;
67         std::set<Node *> nodes_to_remove;
68
69 public:
70         void apply(Stage &);
71
72 private:
73         virtual void visit(Block &);
74         virtual void visit(Precision &);
75         virtual void visit(VariableDeclaration &);
76 };
77
78 /** Base class for feature converters. */
79 class FeatureConverter: protected TraversingVisitor
80 {
81 protected:
82         Stage *stage = 0;
83         Features features;
84
85         FeatureConverter() = default;
86
87 public:
88         void apply(Stage &, const Features &);
89 protected:
90         virtual void apply() = 0;
91
92         void unsupported(const std::string &);
93
94         bool check_version(const Version &) const;
95         bool check_extension(bool Features::*) const;
96 };
97
98 /** Converts structures of the syntax tree to match a particular set of
99 features. */
100 class StructuralFeatureConverter: public FeatureConverter
101 {
102 private:
103         VariableDeclaration *frag_out = 0;
104         NodeList<Statement>::iterator uniform_insert_point;
105         std::set<Node *> nodes_to_remove;
106         RefPtr<Expression> r_replaced_reference;
107         bool r_flattened_interface = false;
108
109 public:
110         void apply(Stage &s, const Features &f) { FeatureConverter::apply(s, f); }
111 private:
112         virtual void apply();
113
114         virtual void visit(Block &);
115         virtual void visit(RefPtr<Expression> &);
116         bool supports_stage(Stage::Type) const;
117         bool supports_unified_interface_syntax() const;
118         virtual void visit(VariableReference &);
119         virtual void visit(MemberAccess &);
120         virtual void visit(Assignment &);
121         bool supports_unified_sampling_functions() const;
122         virtual void visit(FunctionCall &);
123         bool supports_interface_blocks(const std::string &) const;
124         virtual void visit(VariableDeclaration &);
125 };
126
127 /** Converts qualifiers on variables and blocksto match a particular set of
128 features. */
129 class QualifierConverter: private FeatureConverter
130 {
131 public:
132         void apply(Stage &s, const Features &f) { FeatureConverter::apply(s, f); }
133 private:
134         virtual void apply();
135
136         bool supports_interface_layouts() const;
137         bool supports_stage_interface_layouts() const;
138         bool supports_centroid_sampling() const;
139         bool supports_sample_sampling() const;
140         bool supports_uniform_location() const;
141         bool supports_binding() const;
142         bool supports_interface_block_location() const;
143         virtual void visit(VariableDeclaration &);
144 };
145
146 } // namespace SL
147 } // namespace GL
148 } // namespace Msp
149
150 #endif