1 #ifndef MSP_GL_MODULE_H_
2 #define MSP_GL_MODULE_H_
7 #include <msp/io/base.h>
9 #include "glsl/compiler.h"
10 #include "glsl/sourcemap.h"
15 class invalid_module: public std::runtime_error
18 invalid_module(const std::string &w): runtime_error(w) { }
19 virtual ~invalid_module() throw() { }
36 virtual ~Module() = default;
38 virtual Format get_format() const = 0;
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 &);
44 virtual void compile(SL::Compiler &) = 0;
47 class GlslModule: public Module
50 std::string prepared_source;
51 SL::SourceMap source_map;
54 virtual Format get_format() const { return GLSL; }
57 virtual void compile(SL::Compiler &);
60 const std::string &get_prepared_source() const { return prepared_source; }
61 const SL::SourceMap &get_source_map() const { return source_map; }
64 class SpirVModule: public Module
99 std::vector<const Variable *> globals;
105 DataType type = VOID;
106 const Structure *struct_type = 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;
118 std::vector<StructMember> members;
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;
131 int descriptor_set = -1;
133 BuiltinSemantic builtin = NOT_BUILTIN;
135 bool operator==(const Variable &) const;
141 int constant_id = -1;
142 DataType type = VOID;
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);
163 typedef std::vector<std::uint32_t>::const_iterator CodeIterator;
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;
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 &);
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);
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;
204 SpirVModule() = default;
205 SpirVModule(const SpirVModule &);
206 SpirVModule &operator=(const SpirVModule &);
208 void remap_pointers_from(const SpirVModule &);
211 virtual Format get_format() const { return SPIR_V; }
213 void load_code(IO::Base &);
215 virtual void compile(SL::Compiler &);
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; }