]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.h
Unify handling of constants in SpirVModule
[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 Constant
130         {
131                 std::string name;
132                 int constant_id;
133                 DataType type;
134                 union
135                 {
136                         int i_value;
137                         float f_value;
138                 };
139         };
140
141 private:
142         struct TypeInfo
143         {
144                 DataType type;
145                 const Structure *struct_type;
146                 unsigned array_size;
147                 unsigned array_stride;
148                 StorageClass storage;
149
150                 TypeInfo();
151         };
152
153         struct Reflection
154         {
155                 typedef std::vector<UInt32>::const_iterator CodeIterator;
156
157                 std::map<unsigned, std::string> names;
158                 std::map<unsigned, Constant> constants;
159                 std::map<unsigned, TypeInfo> types;
160                 std::map<unsigned, EntryPoint> entry_points;
161                 std::map<unsigned, Structure> structs;
162                 std::map<unsigned, Variable> variables;
163
164                 static UInt32 get_opcode(UInt32);
165                 static CodeIterator get_op_end(const CodeIterator &);
166                 static std::string read_string(CodeIterator &, const CodeIterator &);
167
168                 void reflect_code(const std::vector<UInt32> &);
169                 void reflect_name(CodeIterator);
170                 void reflect_member_name(CodeIterator);
171                 void reflect_entry_point(CodeIterator);
172                 void reflect_void_type(CodeIterator);
173                 void reflect_bool_type(CodeIterator);
174                 void reflect_int_type(CodeIterator);
175                 void reflect_float_type(CodeIterator);
176                 void reflect_vector_type(CodeIterator);
177                 void reflect_matrix_type(CodeIterator);
178                 void reflect_image_type(CodeIterator);
179                 void reflect_sampled_image_type(CodeIterator);
180                 void reflect_array_type(CodeIterator);
181                 void reflect_struct_type(CodeIterator);
182                 void reflect_pointer_type(CodeIterator);
183                 void reflect_constant(CodeIterator);
184                 void reflect_variable(CodeIterator);
185                 void reflect_decorate(CodeIterator);
186                 void reflect_member_decorate(CodeIterator);
187         };
188
189         std::vector<UInt32> code;
190         std::vector<EntryPoint> entry_points;
191         std::vector<Structure> structs;
192         std::vector<Variable> variables;
193         std::vector<Constant> spec_constants;
194
195 public:
196         SpirVModule() { }
197         SpirVModule(const SpirVModule &);
198         SpirVModule &operator=(const SpirVModule &);
199 private:
200         void remap_pointers_from(const SpirVModule &);
201
202 public:
203         virtual Format get_format() const { return SPIR_V; }
204
205         void load_code(IO::Base &);
206 private:
207         virtual void compile(SL::Compiler &);
208         void reflect();
209
210 public:
211         const std::vector<UInt32> &get_code() const { return code; }
212         const std::vector<EntryPoint> &get_entry_points() const { return entry_points; }
213         const std::vector<Variable> &get_variables() const { return variables; }
214         const std::vector<Constant> &get_spec_constants() const { return spec_constants; }
215 };
216
217 } // namespace GL
218 } // namespace Msp
219
220 #endif