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