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