]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.h
34553b44c0e239bfeccac39b873a61b4ea26333d
[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() { }
35 public:
36         virtual ~Module() { }
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;
90                 std::vector<const Variable *> globals;
91
92                 EntryPoint();
93         };
94
95         struct StructMember
96         {
97                 std::string name;
98                 DataType type;
99                 const Structure *struct_type;
100                 unsigned offset;
101                 unsigned array_size;
102                 const Constant *array_size_spec;
103                 unsigned array_stride;
104                 unsigned matrix_stride;
105
106                 StructMember();
107         };
108
109         struct Structure
110         {
111                 std::string name;
112                 std::vector<StructMember> members;
113                 unsigned size;
114         };
115
116         struct Variable
117         {
118                 std::string name;
119                 StorageClass storage;
120                 DataType type;
121                 const Structure *struct_type;
122                 const Constant *array_size_spec;
123                 unsigned array_size;
124                 int location;
125                 int descriptor_set;
126                 int binding;
127
128                 Variable();
129
130                 bool operator==(const Variable &) const;
131         };
132
133         struct Constant
134         {
135                 std::string name;
136                 int constant_id;
137                 DataType type;
138                 union
139                 {
140                         int i_value;
141                         float f_value;
142                 };
143         };
144
145 private:
146         struct TypeInfo
147         {
148                 DataType type;
149                 const Structure *struct_type;
150                 const Constant *array_size_spec;
151                 unsigned array_size;
152                 unsigned array_stride;
153                 StorageClass storage;
154
155                 TypeInfo();
156         };
157
158         struct Reflection
159         {
160                 typedef std::vector<std::uint32_t>::const_iterator CodeIterator;
161
162                 std::map<unsigned, std::string> names;
163                 std::map<unsigned, Constant> constants;
164                 std::map<unsigned, TypeInfo> types;
165                 std::map<unsigned, EntryPoint> entry_points;
166                 std::map<unsigned, Structure> structs;
167                 std::map<unsigned, Variable> variables;
168
169                 static std::uint32_t get_opcode(std::uint32_t);
170                 static CodeIterator get_op_end(const CodeIterator &);
171                 static std::string read_string(CodeIterator &, const CodeIterator &);
172
173                 void reflect_code(const std::vector<std::uint32_t> &);
174                 void reflect_name(CodeIterator);
175                 void reflect_member_name(CodeIterator);
176                 void reflect_entry_point(CodeIterator);
177                 void reflect_void_type(CodeIterator);
178                 void reflect_bool_type(CodeIterator);
179                 void reflect_int_type(CodeIterator);
180                 void reflect_float_type(CodeIterator);
181                 void reflect_vector_type(CodeIterator);
182                 void reflect_matrix_type(CodeIterator);
183                 void reflect_image_type(CodeIterator);
184                 void reflect_sampled_image_type(CodeIterator);
185                 void reflect_array_type(CodeIterator);
186                 void reflect_struct_type(CodeIterator);
187                 void reflect_pointer_type(CodeIterator);
188                 void reflect_constant(CodeIterator);
189                 void reflect_variable(CodeIterator);
190                 void reflect_decorate(CodeIterator);
191                 void reflect_member_decorate(CodeIterator);
192         };
193
194         std::vector<std::uint32_t> code;
195         std::vector<EntryPoint> entry_points;
196         std::vector<Structure> structs;
197         std::vector<Variable> variables;
198         std::vector<Constant> spec_constants;
199
200 public:
201         SpirVModule() { }
202         SpirVModule(const SpirVModule &);
203         SpirVModule &operator=(const SpirVModule &);
204 private:
205         void remap_pointers_from(const SpirVModule &);
206
207 public:
208         virtual Format get_format() const { return SPIR_V; }
209
210         void load_code(IO::Base &);
211 private:
212         virtual void compile(SL::Compiler &);
213         void reflect();
214
215 public:
216         const std::vector<std::uint32_t> &get_code() const { return code; }
217         const std::vector<EntryPoint> &get_entry_points() const { return entry_points; }
218         const std::vector<Variable> &get_variables() const { return variables; }
219         const std::vector<Constant> &get_spec_constants() const { return spec_constants; }
220 };
221
222 } // namespace GL
223 } // namespace Msp
224
225 #endif