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