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