]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.h
Completely hide OpenGL from the public headers
[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 "glsl/compiler.h"
8 #include "glsl/sourcemap.h"
9
10 namespace Msp {
11 namespace GL {
12
13 class invalid_module: public std::runtime_error
14 {
15 public:
16         invalid_module(const std::string &w): runtime_error(w) { }
17         virtual ~invalid_module() throw() { }
18 };
19
20 class Resources;
21
22 class Module
23 {
24 public:
25         enum Format
26         {
27                 GLSL,
28                 SPIR_V
29         };
30
31 protected:
32         Module() { }
33 public:
34         virtual ~Module() { }
35
36         virtual Format get_format() const = 0;
37
38         void set_source(const std::string &);
39         void load_source(IO::Base &, Resources *, const std::string &);
40         void load_source(IO::Base &, const std::string &);
41 private:
42         virtual void compile(SL::Compiler &) = 0;
43 };
44
45 class GlslModule: public Module
46 {
47 private:
48         std::string prepared_source;
49         SL::SourceMap source_map;
50
51 public:
52         virtual Format get_format() const { return GLSL; }
53
54 private:
55         virtual void compile(SL::Compiler &);
56
57 public:
58         const std::string &get_prepared_source() const { return prepared_source; }
59         const SL::SourceMap &get_source_map() const { return source_map; }
60 };
61
62 class SpirVModule: public Module
63 {
64 public:
65         enum Stage
66         {
67                 VERTEX = 0,
68                 GEOMETRY = 3,
69                 FRAGMENT = 4
70         };
71
72         enum StorageClass
73         {
74                 UNIFORM_CONSTANT = 0,
75                 INPUT = 1,
76                 UNIFORM = 2,
77                 OUTPUT = 3
78         };
79
80         struct Constant;
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                 const Constant *array_size_spec;
101                 unsigned array_stride;
102                 unsigned matrix_stride;
103
104                 StructMember();
105         };
106
107         struct Structure
108         {
109                 std::string name;
110                 std::vector<StructMember> members;
111                 unsigned size;
112         };
113
114         struct Variable
115         {
116                 std::string name;
117                 StorageClass storage;
118                 DataType type;
119                 const Structure *struct_type;
120                 const Constant *array_size_spec;
121                 unsigned array_size;
122                 int location;
123                 int descriptor_set;
124                 int binding;
125
126                 Variable();
127
128                 bool operator==(const Variable &) const;
129         };
130
131         struct Constant
132         {
133                 std::string name;
134                 int constant_id;
135                 DataType type;
136                 union
137                 {
138                         int i_value;
139                         float f_value;
140                 };
141         };
142
143 private:
144         struct TypeInfo
145         {
146                 DataType type;
147                 const Structure *struct_type;
148                 const Constant *array_size_spec;
149                 unsigned array_size;
150                 unsigned array_stride;
151                 StorageClass storage;
152
153                 TypeInfo();
154         };
155
156         struct Reflection
157         {
158                 typedef std::vector<std::uint32_t>::const_iterator CodeIterator;
159
160                 std::map<unsigned, std::string> names;
161                 std::map<unsigned, Constant> constants;
162                 std::map<unsigned, TypeInfo> types;
163                 std::map<unsigned, EntryPoint> entry_points;
164                 std::map<unsigned, Structure> structs;
165                 std::map<unsigned, Variable> variables;
166
167                 static std::uint32_t get_opcode(std::uint32_t);
168                 static CodeIterator get_op_end(const CodeIterator &);
169                 static std::string read_string(CodeIterator &, const CodeIterator &);
170
171                 void reflect_code(const std::vector<std::uint32_t> &);
172                 void reflect_name(CodeIterator);
173                 void reflect_member_name(CodeIterator);
174                 void reflect_entry_point(CodeIterator);
175                 void reflect_void_type(CodeIterator);
176                 void reflect_bool_type(CodeIterator);
177                 void reflect_int_type(CodeIterator);
178                 void reflect_float_type(CodeIterator);
179                 void reflect_vector_type(CodeIterator);
180                 void reflect_matrix_type(CodeIterator);
181                 void reflect_image_type(CodeIterator);
182                 void reflect_sampled_image_type(CodeIterator);
183                 void reflect_array_type(CodeIterator);
184                 void reflect_struct_type(CodeIterator);
185                 void reflect_pointer_type(CodeIterator);
186                 void reflect_constant(CodeIterator);
187                 void reflect_variable(CodeIterator);
188                 void reflect_decorate(CodeIterator);
189                 void reflect_member_decorate(CodeIterator);
190         };
191
192         std::vector<std::uint32_t> code;
193         std::vector<EntryPoint> entry_points;
194         std::vector<Structure> structs;
195         std::vector<Variable> variables;
196         std::vector<Constant> spec_constants;
197
198 public:
199         SpirVModule() { }
200         SpirVModule(const SpirVModule &);
201         SpirVModule &operator=(const SpirVModule &);
202 private:
203         void remap_pointers_from(const SpirVModule &);
204
205 public:
206         virtual Format get_format() const { return SPIR_V; }
207
208         void load_code(IO::Base &);
209 private:
210         virtual void compile(SL::Compiler &);
211         void reflect();
212
213 public:
214         const std::vector<std::uint32_t> &get_code() const { return code; }
215         const std::vector<EntryPoint> &get_entry_points() const { return entry_points; }
216         const std::vector<Variable> &get_variables() const { return variables; }
217         const std::vector<Constant> &get_spec_constants() const { return spec_constants; }
218 };
219
220 } // namespace GL
221 } // namespace Msp
222
223 #endif