]> git.tdb.fi Git - libs/gl.git/blob - source/glsl/finalize.h
Clear load ID when assigning to a component
[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 /**
60 Converts the output depth range to match expectations of the target API.
61 */
62 class DepthRangeConverter: private TraversingVisitor
63 {
64 private:
65         bool assignment_target = false;
66         bool r_gl_pervertex = false;
67         bool r_gl_position = false;
68         bool r_position_z_assigned = false;
69
70 public:
71         void apply(Stage &, const Features &);
72
73 private:
74         virtual void visit(VariableReference &);
75         virtual void visit(MemberAccess &);
76         virtual void visit(Swizzle &);
77         virtual void visit(Assignment &);
78         virtual void visit(FunctionDeclaration &);
79 };
80
81 /** Generates default precision declarations or removes precision declarations
82 according to the requirements of the target API. */
83 class PrecisionConverter: private TraversingVisitor
84 {
85 private:
86         Stage *stage = 0;
87         std::set<std::string> have_default;
88         NodeList<Statement>::iterator insert_point;
89         std::set<Node *> nodes_to_remove;
90
91 public:
92         void apply(Stage &);
93
94 private:
95         virtual void visit(Block &);
96         virtual void visit(Precision &);
97         virtual void visit(VariableDeclaration &);
98 };
99
100 /** Base class for feature converters. */
101 class FeatureConverter: protected TraversingVisitor
102 {
103 protected:
104         Stage *stage = 0;
105         Features features;
106
107         FeatureConverter() = default;
108
109 public:
110         void apply(Stage &, const Features &);
111 protected:
112         virtual void apply() = 0;
113
114         void unsupported(const std::string &);
115
116         bool check_version(const Version &) const;
117         bool check_extension(bool Features::*) const;
118 };
119
120 /** Converts structures of the syntax tree to match a particular set of
121 features. */
122 class StructuralFeatureConverter: public FeatureConverter
123 {
124 private:
125         VariableDeclaration *frag_out = 0;
126         NodeList<Statement>::iterator uniform_insert_point;
127         std::set<Node *> nodes_to_remove;
128         RefPtr<Expression> r_replaced_reference;
129         bool r_flattened_interface = false;
130
131 public:
132         void apply(Stage &s, const Features &f) { FeatureConverter::apply(s, f); }
133 private:
134         virtual void apply();
135
136         virtual void visit(Block &);
137         virtual void visit(RefPtr<Expression> &);
138         bool supports_stage(Stage::Type) const;
139         bool supports_unified_interface_syntax() const;
140         virtual void visit(VariableReference &);
141         virtual void visit(MemberAccess &);
142         virtual void visit(Assignment &);
143         bool supports_unified_sampling_functions() const;
144         virtual void visit(FunctionCall &);
145         bool supports_interface_blocks(const std::string &) const;
146         virtual void visit(VariableDeclaration &);
147 };
148
149 /** Converts qualifiers on variables and blocksto match a particular set of
150 features. */
151 class QualifierConverter: private FeatureConverter
152 {
153 public:
154         void apply(Stage &s, const Features &f) { FeatureConverter::apply(s, f); }
155 private:
156         virtual void apply();
157
158         bool supports_interface_layouts() const;
159         bool supports_stage_interface_layouts() const;
160         bool supports_centroid_sampling() const;
161         bool supports_sample_sampling() const;
162         bool supports_uniform_location() const;
163         bool supports_binding() const;
164         bool supports_interface_block_location() const;
165         virtual void visit(VariableDeclaration &);
166 };
167
168 } // namespace SL
169 } // namespace GL
170 } // namespace Msp
171
172 #endif