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