]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.h
Guard against hitting the end of the array in get_gl_type
[libs/gl.git] / source / core / module.h
1 #ifndef MSP_GL_MODULE_H_
2 #define MSP_GL_MODULE_H_
3
4 #include <string>
5 #include <msp/io/base.h>
6 #include "datatype.h"
7 #include "gl.h"
8 #include "glsl/compiler.h"
9 #include "glsl/sourcemap.h"
10
11 namespace Msp {
12 namespace GL {
13
14 class invalid_module: public std::runtime_error
15 {
16 public:
17         invalid_module(const std::string &w): runtime_error(w) { }
18         virtual ~invalid_module() throw() { }
19 };
20
21 class Resources;
22
23 class Module
24 {
25 public:
26         enum Format
27         {
28                 GLSL,
29                 SPIR_V
30         };
31
32 protected:
33         Module() { }
34 public:
35         virtual ~Module() { }
36
37         virtual Format get_format() const = 0;
38
39         void set_source(const std::string &);
40         void load_source(IO::Base &, Resources *, const std::string &);
41         void load_source(IO::Base &, const std::string &);
42 private:
43         virtual void compile(SL::Compiler &) = 0;
44 };
45
46 class GlslModule: public Module
47 {
48 private:
49         std::string prepared_source;
50         SL::SourceMap source_map;
51
52 public:
53         virtual Format get_format() const { return GLSL; }
54
55 private:
56         virtual void compile(SL::Compiler &);
57
58 public:
59         const std::string &get_prepared_source() const { return prepared_source; }
60         const SL::SourceMap &get_source_map() const { return source_map; }
61 };
62
63 class SpirVModule: public Module
64 {
65 public:
66         enum Stage
67         {
68                 VERTEX = 0,
69                 GEOMETRY = 3,
70                 FRAGMENT = 4
71         };
72
73         enum StorageClass
74         {
75                 UNIFORM_CONSTANT = 0,
76                 INPUT = 1,
77                 UNIFORM = 2,
78                 OUTPUT = 3
79         };
80
81         struct Structure;
82         struct Variable;
83
84         struct EntryPoint
85         {
86                 std::string name;
87                 Stage stage;
88                 std::vector<const Variable *> globals;
89
90                 EntryPoint();
91         };
92
93         struct StructMember
94         {
95                 std::string name;
96                 DataType type;
97                 const Structure *struct_type;
98                 unsigned offset;
99                 unsigned array_size;
100                 unsigned array_stride;
101                 unsigned matrix_stride;
102
103                 StructMember();
104         };
105
106         struct Structure
107         {
108                 std::string name;
109                 std::vector<StructMember> members;
110                 unsigned size;
111         };
112
113         struct Variable
114         {
115                 std::string name;
116                 StorageClass storage;
117                 DataType type;
118                 const Structure *struct_type;
119                 unsigned array_size;
120                 int location;
121                 int descriptor_set;
122                 int binding;
123
124                 Variable();
125
126                 bool operator==(const Variable &) const;
127         };
128
129         struct SpecConstant
130         {
131                 std::string name;
132                 unsigned constant_id;
133                 DataType type;
134         };
135
136 private:
137         struct TypeInfo
138         {
139                 DataType type;
140                 const Structure *struct_type;
141                 unsigned array_size;
142                 unsigned array_stride;
143                 StorageClass storage;
144
145                 TypeInfo();
146         };
147
148         struct Reflection
149         {
150                 typedef std::vector<UInt32>::const_iterator CodeIterator;
151
152                 std::map<unsigned, std::string> names;
153                 std::map<unsigned, Variant> constants;
154                 std::map<unsigned, TypeInfo> types;
155                 std::map<unsigned, EntryPoint> entry_points;
156                 std::map<unsigned, Structure> structs;
157                 std::map<unsigned, Variable> variables;
158                 std::map<unsigned, SpecConstant> spec_constants;
159
160                 static UInt32 get_opcode(UInt32);
161                 static CodeIterator get_op_end(const CodeIterator &);
162                 static std::string read_string(CodeIterator &, const CodeIterator &);
163
164                 void reflect_code(const std::vector<UInt32> &);
165                 void reflect_name(CodeIterator);
166                 void reflect_member_name(CodeIterator);
167                 void reflect_entry_point(CodeIterator);
168                 void reflect_void_type(CodeIterator);
169                 void reflect_bool_type(CodeIterator);
170                 void reflect_int_type(CodeIterator);
171                 void reflect_float_type(CodeIterator);
172                 void reflect_vector_type(CodeIterator);
173                 void reflect_matrix_type(CodeIterator);
174                 void reflect_image_type(CodeIterator);
175                 void reflect_sampled_image_type(CodeIterator);
176                 void reflect_array_type(CodeIterator);
177                 void reflect_struct_type(CodeIterator);
178                 void reflect_pointer_type(CodeIterator);
179                 void reflect_constant(CodeIterator);
180                 void reflect_spec_constant_bool(CodeIterator);
181                 void reflect_spec_constant(CodeIterator);
182                 void reflect_variable(CodeIterator);
183                 void reflect_decorate(CodeIterator);
184                 void reflect_member_decorate(CodeIterator);
185         };
186
187         std::vector<UInt32> code;
188         std::vector<EntryPoint> entry_points;
189         std::vector<Structure> structs;
190         std::vector<Variable> variables;
191         std::vector<SpecConstant> spec_constants;
192
193 public:
194         SpirVModule() { }
195         SpirVModule(const SpirVModule &);
196         SpirVModule &operator=(const SpirVModule &);
197 private:
198         void remap_pointers_from(const SpirVModule &);
199
200 public:
201         virtual Format get_format() const { return SPIR_V; }
202
203         void load_code(IO::Base &);
204 private:
205         virtual void compile(SL::Compiler &);
206
207 public:
208         const std::vector<UInt32> &get_code() const { return code; }
209         const std::vector<EntryPoint> &get_entry_points() const { return entry_points; }
210         const std::vector<Variable> &get_variables() const { return variables; }
211         const std::vector<SpecConstant> &get_spec_constants() const { return spec_constants; }
212 };
213
214 } // namespace GL
215 } // namespace Msp
216
217 #endif