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