]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Use default member initializers for simple types
[libs/gl.git] / source / core / module.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
3 #include "module.h"
4 #include "resources.h"
5
6 using namespace std;
7
8 enum SpirVConstants
9 {
10         SPIRV_MAGIC = 0x07230203,
11         SPIRV_MAGIC_REVERSED = 0x03022307,
12
13         OP_NAME = 5,
14         OP_MEMBER_NAME = 6,
15         OP_ENTRY_POINT = 15,
16         OP_TYPE_VOID = 19,
17         OP_TYPE_BOOL = 20,
18         OP_TYPE_INT = 21,
19         OP_TYPE_FLOAT = 22,
20         OP_TYPE_VECTOR = 23,
21         OP_TYPE_MATRIX = 24,
22         OP_TYPE_IMAGE = 25,
23         OP_TYPE_SAMPLED_IMAGE = 27,
24         OP_TYPE_ARRAY = 28,
25         OP_TYPE_STRUCT = 30,
26         OP_TYPE_POINTER = 32,
27         OP_CONSTANT_TRUE = 41,
28         OP_CONSTANT_FALSE = 42,
29         OP_CONSTANT = 43,
30         OP_SPEC_CONSTANT_TRUE = 48,
31         OP_SPEC_CONSTANT_FALSE = 49,
32         OP_SPEC_CONSTANT = 50,
33         OP_VARIABLE = 59,
34         OP_DECORATE = 71,
35         OP_MEMBER_DECORATE = 72,
36
37         DECO_SPEC_ID = 1,
38         DECO_ARRAY_STRIDE = 6,
39         DECO_MATRIX_STRIDE = 7,
40         DECO_LOCATION = 30,
41         DECO_BINDING = 33,
42         DECO_DESCRIPTOR_SET = 34,
43         DECO_OFFSET = 35
44 };
45
46 namespace Msp {
47 namespace GL {
48
49 void Module::set_source(const string &src)
50 {
51         SL::Compiler compiler;
52         compiler.set_source(src);
53         compile(compiler);
54 }
55
56 void Module::load_source(IO::Base &io, Resources *res, const string &name)
57 {
58         SL::Compiler compiler;
59         compiler.load_source(io, res, name);
60         compile(compiler);
61 }
62
63 void Module::load_source(IO::Base &io, const string &name)
64 {
65         load_source(io, 0, name);
66 }
67
68
69 void GlslModule::compile(SL::Compiler &compiler)
70 {
71         compiler.compile(SL::Compiler::MODULE);
72         prepared_source = compiler.get_combined_glsl();
73         source_map = compiler.get_source_map();
74
75 #ifdef DEBUG
76         string diagnostics = compiler.get_diagnostics();
77         if(!diagnostics.empty())
78                 IO::print("Module diagnostics:\n%s\n", diagnostics);
79 #endif
80 }
81
82
83 SpirVModule::SpirVModule(const SpirVModule &other):
84         code(other.code),
85         entry_points(other.entry_points),
86         structs(other.structs),
87         variables(other.variables)
88 {
89         remap_pointers_from(other);
90 }
91
92 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
93 {
94         code = other.code;
95         entry_points = other.entry_points;
96         structs = other.structs;
97         variables = other.variables;
98         remap_pointers_from(other);
99         return *this;
100 }
101
102 void SpirVModule::remap_pointers_from(const SpirVModule &other)
103 {
104         for(EntryPoint &e: entry_points)
105                 for(const Variable *&v: e.globals)
106                         v = &variables[v-&other.variables.front()];
107
108         for(Variable &v: variables)
109                 if(v.struct_type)
110                         v.struct_type = &structs[v.struct_type-&other.structs.front()];
111
112         for(Structure &s: structs)
113                 for(StructMember &m: s.members)
114                         if(m.struct_type)
115                                 m.struct_type = &structs[m.struct_type-&other.structs.front()];
116 }
117
118 void SpirVModule::load_code(IO::Base &io)
119 {
120         uint32_t buffer[1024];
121         while(1)
122         {
123                 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
124                 if(!len)
125                         break;
126                 len /= 4;
127                 code.reserve(code.size()+len);
128                 code.insert(code.end(), buffer, buffer+len);
129         }
130
131         reflect();
132 }
133
134 void SpirVModule::compile(SL::Compiler &compiler)
135 {
136         compiler.compile(SL::Compiler::SPIRV);
137         code = compiler.get_combined_spirv();
138         reflect();
139 }
140
141 void SpirVModule::reflect()
142 {
143         if(code.empty())
144                 throw invalid_module("Empty SPIR-V code");
145
146         if(code[0]==SPIRV_MAGIC_REVERSED)
147         {
148                 for(uint32_t &c: code)
149                         c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
150         }
151         else if(code[0]!=SPIRV_MAGIC)
152                 throw invalid_module("SPIR-V magic number not found");
153
154         Reflection reflection;
155         reflection.reflect_code(code);
156
157         map<const Constant *, unsigned> spec_indices;
158         for(const auto &kvp: reflection.constants)
159                 if(kvp.second.constant_id>=0)
160                 {
161                         spec_indices[&kvp.second] = spec_constants.size();
162                         spec_constants.push_back(kvp.second);
163                 }
164
165         map<const Structure *, unsigned> struct_indices;
166         structs.reserve(reflection.structs.size());
167         for(const auto &kvp: reflection.structs)
168         {
169                 struct_indices[&kvp.second] = structs.size();
170                 structs.push_back(kvp.second);
171         }
172
173         for(Structure &s: structs)
174         {
175                 for(StructMember &m: s.members)
176                 {
177                         if(m.struct_type)
178                         {
179                                 auto i = struct_indices.find(m.struct_type);
180                                 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
181                         }
182                         if(m.array_size_spec)
183                         {
184                                 auto i = spec_indices.find(m.array_size_spec);
185                                 m.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
186                         }
187                 }
188
189                 const StructMember *last_member = &s.members.back();
190                 unsigned last_offset = last_member->offset;
191                 while(last_member->struct_type)
192                 {
193                         const StructMember *lm = &last_member->struct_type->members.back();
194                         if(last_member->array_size)
195                                 last_offset += last_member->array_stride*(last_member->array_size-1);
196                         else if(last_member->array_size_spec)
197                                 last_offset += last_member->array_stride*(last_member->array_size_spec->i_value-1);
198                         last_offset += lm->offset;
199                         last_member = lm;
200                 }
201                 
202                 s.size = last_offset+get_type_size(last_member->type);
203                 s.size = (s.size+15)&~15;
204         }
205
206         map<const Variable *, unsigned> var_indices;
207         variables.reserve(reflection.variables.size());
208         for(const auto &kvp: reflection.variables)
209         {
210                 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
211                 if(i!=variables.end())
212                         var_indices[&kvp.second] = i-variables.begin();
213                 else
214                 {
215                         var_indices[&kvp.second] = variables.size();
216                         variables.push_back(kvp.second);
217                 }
218         }
219
220         for(Variable &v: variables)
221         {
222                 if(v.struct_type)
223                 {
224                         auto i = struct_indices.find(v.struct_type);
225                         v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
226                 }
227                 if(v.array_size_spec)
228                 {
229                         auto i = spec_indices.find(v.array_size_spec);
230                         v.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
231                 }
232         }
233
234         entry_points.reserve(reflection.entry_points.size());
235         for(const auto &kvp: reflection.entry_points)
236         {
237                 entry_points.push_back(kvp.second);
238                 EntryPoint &entry = entry_points.back();
239                 for(const Variable *&v: entry.globals)
240                 {
241                         auto i = var_indices.find(v);
242                         v = (i!=var_indices.end() ? &variables[i->second] : 0);
243                 }
244         }
245 }
246
247
248 bool SpirVModule::Variable::operator==(const Variable &other) const
249 {
250         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
251                 return false;
252         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
253                 return false;
254         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
255                 return false;
256         return true;
257 }
258
259
260 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
261 {
262         return op&0xFFFF;
263 }
264
265 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
266 {
267         return op+(*op>>16);
268 }
269
270 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
271 {
272         string result;
273         for(; op!=op_end; ++op)
274         {
275                 unsigned word = *op;
276                 for(unsigned i=0; i<4; ++i)
277                 {
278                         char c = word&0xFF;
279                         if(!c)
280                         {
281                                 ++op;
282                                 return result;
283                         }
284                         result += c;
285                         word >>= 8;
286                 }
287         }
288
289         throw invalid_module("Unterminated SPIR-V string literal");
290 }
291
292 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
293 {
294         for(CodeIterator op=code.begin()+5; op!=code.end(); )
295         {
296                 unsigned word_count = *op>>16;
297                 if(word_count>static_cast<unsigned>(code.end()-op))
298                         throw invalid_module("Truncated SPIR-V instruction");
299
300                 switch(get_opcode(*op))
301                 {
302                 case OP_NAME: reflect_name(op); break;
303                 case OP_MEMBER_NAME: reflect_member_name(op); break;
304                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
305                 case OP_TYPE_VOID: reflect_void_type(op); break;
306                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
307                 case OP_TYPE_INT: reflect_int_type(op); break;
308                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
309                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
310                 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
311                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
312                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
313                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
314                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
315                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
316                 case OP_CONSTANT_TRUE:
317                 case OP_CONSTANT_FALSE:
318                 case OP_CONSTANT:
319                 case OP_SPEC_CONSTANT_TRUE:
320                 case OP_SPEC_CONSTANT_FALSE:
321                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
322                 case OP_VARIABLE: reflect_variable(op); break;
323                 case OP_DECORATE: reflect_decorate(op); break;
324                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
325                 }
326
327                 op += word_count;
328         }
329 }
330
331 void SpirVModule::Reflection::reflect_name(CodeIterator op)
332 {
333         CodeIterator op_end = get_op_end(op);
334         string &name = names[*(op+1)];
335         op += 2;
336         name = read_string(op, op_end);
337 }
338
339 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
340 {
341         CodeIterator op_end = get_op_end(op);
342         Structure &strct = structs[*(op+1)];
343         unsigned index = *(op+2);
344         if(index>=strct.members.size())
345                 strct.members.resize(index+1);
346         op += 3;
347         strct.members[index].name = read_string(op, op_end);
348 }
349
350 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
351 {
352         CodeIterator op_end = get_op_end(op);
353         EntryPoint &entry = entry_points[*(op+2)];
354         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
355         op += 3;
356         entry.name = read_string(op, op_end);
357
358         entry.globals.reserve(op_end-op);
359         for(; op!=op_end; ++op)
360                 entry.globals.push_back(&variables[*op]);
361 }
362
363 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
364 {
365         types[*(op+1)].type = VOID;
366 }
367
368 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
369 {
370         types[*(op+1)].type = BOOL;
371 }
372
373 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
374 {
375         TypeInfo &type = types[*(op+1)];
376         unsigned size = *(op+2);
377         bool sign = *(op+3);
378         type.type = static_cast<DataType>(size/8 | sign*0x100);
379 }
380
381 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
382 {
383         TypeInfo &type = types[*(op+1)];
384         unsigned size = *(op+2);
385         type.type = static_cast<DataType>(size/8 | 0x300);
386 }
387
388 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
389 {
390         TypeInfo &type = types[*(op+1)];
391         DataType component = types[*(op+2)].type;
392         unsigned count = *(op+3);
393         type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
394 }
395
396 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
397 {
398         TypeInfo &type = types[*(op+1)];
399         DataType column = types[*(op+2)].type;
400         unsigned count = *(op+3);
401         type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
402 }
403
404 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
405 {
406         TypeInfo &type = types[*(op+1)];
407         DataType sample = types[*(op+2)].type;
408         unsigned dimensions = *(op+3);
409         bool depth = *(op+4)==1;
410         bool array = *(op+5);
411         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
412 }
413
414 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
415 {
416         TypeInfo &type = types[*(op+1)];
417         DataType image = types[*(op+2)].type;
418         type.type = static_cast<DataType>(image | 0x100000);
419 }
420
421 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
422 {
423         TypeInfo &type = types[*(op+1)];
424         const TypeInfo &elem = types[*(op+2)];
425         type.type = elem.type;
426         type.struct_type = elem.struct_type;
427
428         const Constant &size = constants[*(op+3)];
429         if(size.constant_id>=0)
430                 type.array_size_spec = &size;
431         else if(size.type==INT || size.type==UNSIGNED_INT)
432                 type.array_size = size.i_value;
433 }
434
435 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
436 {
437         CodeIterator op_end = get_op_end(op);
438         unsigned id = *(op+1);
439         Structure &strct = structs[id];
440         strct.name = names[id];
441         types[id].struct_type = &strct;
442         op += 2;
443
444         strct.members.resize(op_end-op);
445         auto mem = strct.members.begin();
446         for(; op!=op_end; ++op, ++mem)
447         {
448                 TypeInfo &type = types[*op];
449                 mem->type = type.type;
450                 mem->struct_type = type.struct_type;
451                 mem->array_size = type.array_size;
452                 mem->array_size_spec = type.array_size_spec;
453                 mem->array_stride = type.array_stride;
454         }
455 }
456
457 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
458 {
459         TypeInfo &type = types[*(op+1)];
460         type = types[*(op+3)];
461         type.storage = static_cast<StorageClass>(*(op+2));
462 }
463
464 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
465 {
466         unsigned id = *(op+2);
467         Constant &cnst = constants[id];
468         cnst.name = names[id];
469         cnst.type = types[*(op+1)].type;
470         if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
471                 cnst.i_value = true;
472         else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
473                 cnst.i_value = false;
474         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
475                 cnst.i_value = *(op+3);
476         else if(cnst.type==FLOAT)
477                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
478 }
479
480 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
481 {
482         unsigned id = *(op+2);
483         Variable &var = variables[id];
484         var.name = names[id];
485         const TypeInfo &type = types[*(op+1)];
486         var.storage = type.storage;
487         var.type = type.type;
488         var.struct_type = type.struct_type;
489         var.array_size = type.array_size;
490         var.array_size_spec = type.array_size_spec;
491 }
492
493 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
494 {
495         unsigned id = *(op+1);
496         unsigned decoration = *(op+2);
497         op += 3;
498
499         switch(decoration)
500         {
501         case DECO_SPEC_ID:
502                 constants[id].constant_id = *op;
503                 break;
504         case DECO_ARRAY_STRIDE:
505                 types[id].array_stride = *op;
506                 break;
507         case DECO_LOCATION:
508                 variables[id].location = *op;
509                 break;
510         case DECO_BINDING:
511                 variables[id].binding = *op;
512                 break;
513         case DECO_DESCRIPTOR_SET:
514                 variables[id].descriptor_set = *op;
515                 break;
516         }
517 }
518
519 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
520 {
521         Structure &strct = structs[*(op+1)];
522         unsigned index = *(op+2);
523         if(index>=strct.members.size())
524                 strct.members.resize(index+1);
525         unsigned decoration = *(op+3);
526         op += 4;
527
528         StructMember &member = strct.members[index];
529         switch(decoration)
530         {
531         case DECO_MATRIX_STRIDE:
532                 member.matrix_stride = *op;
533                 break;
534         case DECO_OFFSET:
535                 member.offset = *op;
536                 break;
537         }
538 }
539
540 } // namespace GL
541 } // namespace Msp