]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Use standard fixed-size integer 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 SpirVModule::EntryPoint::EntryPoint():
249         stage(VERTEX)
250 { }
251
252
253 SpirVModule::StructMember::StructMember():
254         type(VOID),
255         struct_type(0),
256         offset(0),
257         array_size(0),
258         array_size_spec(0),
259         array_stride(0),
260         matrix_stride(0)
261 { }
262
263
264 SpirVModule::Variable::Variable():
265         type(VOID),
266         struct_type(0),
267         location(-1),
268         descriptor_set(-1),
269         binding(-1)
270 { }
271
272 bool SpirVModule::Variable::operator==(const Variable &other) const
273 {
274         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
275                 return false;
276         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
277                 return false;
278         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
279                 return false;
280         return true;
281 }
282
283
284 SpirVModule::TypeInfo::TypeInfo():
285         type(VOID),
286         struct_type(0),
287         array_size_spec(0),
288         array_size(0),
289         array_stride(0),
290         storage(static_cast<StorageClass>(-1))
291 { }
292
293
294 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
295 {
296         return op&0xFFFF;
297 }
298
299 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
300 {
301         return op+(*op>>16);
302 }
303
304 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
305 {
306         string result;
307         for(; op!=op_end; ++op)
308         {
309                 unsigned word = *op;
310                 for(unsigned i=0; i<4; ++i)
311                 {
312                         char c = word&0xFF;
313                         if(!c)
314                         {
315                                 ++op;
316                                 return result;
317                         }
318                         result += c;
319                         word >>= 8;
320                 }
321         }
322
323         throw invalid_module("Unterminated SPIR-V string literal");
324 }
325
326 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
327 {
328         for(CodeIterator op=code.begin()+5; op!=code.end(); )
329         {
330                 unsigned word_count = *op>>16;
331                 if(word_count>static_cast<unsigned>(code.end()-op))
332                         throw invalid_module("Truncated SPIR-V instruction");
333
334                 switch(get_opcode(*op))
335                 {
336                 case OP_NAME: reflect_name(op); break;
337                 case OP_MEMBER_NAME: reflect_member_name(op); break;
338                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
339                 case OP_TYPE_VOID: reflect_void_type(op); break;
340                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
341                 case OP_TYPE_INT: reflect_int_type(op); break;
342                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
343                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
344                 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
345                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
346                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
347                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
348                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
349                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
350                 case OP_CONSTANT_TRUE:
351                 case OP_CONSTANT_FALSE:
352                 case OP_CONSTANT:
353                 case OP_SPEC_CONSTANT_TRUE:
354                 case OP_SPEC_CONSTANT_FALSE:
355                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
356                 case OP_VARIABLE: reflect_variable(op); break;
357                 case OP_DECORATE: reflect_decorate(op); break;
358                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
359                 }
360
361                 op += word_count;
362         }
363 }
364
365 void SpirVModule::Reflection::reflect_name(CodeIterator op)
366 {
367         CodeIterator op_end = get_op_end(op);
368         string &name = names[*(op+1)];
369         op += 2;
370         name = read_string(op, op_end);
371 }
372
373 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
374 {
375         CodeIterator op_end = get_op_end(op);
376         Structure &strct = structs[*(op+1)];
377         unsigned index = *(op+2);
378         if(index>=strct.members.size())
379                 strct.members.resize(index+1);
380         op += 3;
381         strct.members[index].name = read_string(op, op_end);
382 }
383
384 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
385 {
386         CodeIterator op_end = get_op_end(op);
387         EntryPoint &entry = entry_points[*(op+2)];
388         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
389         op += 3;
390         entry.name = read_string(op, op_end);
391
392         entry.globals.reserve(op_end-op);
393         for(; op!=op_end; ++op)
394                 entry.globals.push_back(&variables[*op]);
395 }
396
397 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
398 {
399         types[*(op+1)].type = VOID;
400 }
401
402 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
403 {
404         types[*(op+1)].type = BOOL;
405 }
406
407 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
408 {
409         TypeInfo &type = types[*(op+1)];
410         unsigned size = *(op+2);
411         bool sign = *(op+3);
412         type.type = static_cast<DataType>(size/8 | sign*0x100);
413 }
414
415 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
416 {
417         TypeInfo &type = types[*(op+1)];
418         unsigned size = *(op+2);
419         type.type = static_cast<DataType>(size/8 | 0x300);
420 }
421
422 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
423 {
424         TypeInfo &type = types[*(op+1)];
425         DataType component = types[*(op+2)].type;
426         unsigned count = *(op+3);
427         type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
428 }
429
430 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
431 {
432         TypeInfo &type = types[*(op+1)];
433         DataType column = types[*(op+2)].type;
434         unsigned count = *(op+3);
435         type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
436 }
437
438 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
439 {
440         TypeInfo &type = types[*(op+1)];
441         DataType sample = types[*(op+2)].type;
442         unsigned dimensions = *(op+3);
443         bool depth = *(op+4)==1;
444         bool array = *(op+5);
445         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
446 }
447
448 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
449 {
450         TypeInfo &type = types[*(op+1)];
451         DataType image = types[*(op+2)].type;
452         type.type = static_cast<DataType>(image | 0x100000);
453 }
454
455 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
456 {
457         TypeInfo &type = types[*(op+1)];
458         const TypeInfo &elem = types[*(op+2)];
459         type.type = elem.type;
460         type.struct_type = elem.struct_type;
461
462         const Constant &size = constants[*(op+3)];
463         if(size.constant_id>=0)
464                 type.array_size_spec = &size;
465         else if(size.type==INT || size.type==UNSIGNED_INT)
466                 type.array_size = size.i_value;
467 }
468
469 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
470 {
471         CodeIterator op_end = get_op_end(op);
472         unsigned id = *(op+1);
473         Structure &strct = structs[id];
474         strct.name = names[id];
475         types[id].struct_type = &strct;
476         op += 2;
477
478         strct.members.resize(op_end-op);
479         auto mem = strct.members.begin();
480         for(; op!=op_end; ++op, ++mem)
481         {
482                 TypeInfo &type = types[*op];
483                 mem->type = type.type;
484                 mem->struct_type = type.struct_type;
485                 mem->array_size = type.array_size;
486                 mem->array_size_spec = type.array_size_spec;
487                 mem->array_stride = type.array_stride;
488         }
489 }
490
491 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
492 {
493         TypeInfo &type = types[*(op+1)];
494         type = types[*(op+3)];
495         type.storage = static_cast<StorageClass>(*(op+2));
496 }
497
498 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
499 {
500         unsigned id = *(op+2);
501         Constant &cnst = constants[id];
502         cnst.name = names[id];
503         cnst.type = types[*(op+1)].type;
504         if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
505                 cnst.i_value = true;
506         else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
507                 cnst.i_value = false;
508         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
509                 cnst.i_value = *(op+3);
510         else if(cnst.type==FLOAT)
511                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
512 }
513
514 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
515 {
516         unsigned id = *(op+2);
517         Variable &var = variables[id];
518         var.name = names[id];
519         const TypeInfo &type = types[*(op+1)];
520         var.storage = type.storage;
521         var.type = type.type;
522         var.struct_type = type.struct_type;
523         var.array_size = type.array_size;
524         var.array_size_spec = type.array_size_spec;
525 }
526
527 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
528 {
529         unsigned id = *(op+1);
530         unsigned decoration = *(op+2);
531         op += 3;
532
533         switch(decoration)
534         {
535         case DECO_SPEC_ID:
536                 constants[id].constant_id = *op;
537                 break;
538         case DECO_ARRAY_STRIDE:
539                 types[id].array_stride = *op;
540                 break;
541         case DECO_LOCATION:
542                 variables[id].location = *op;
543                 break;
544         case DECO_BINDING:
545                 variables[id].binding = *op;
546                 break;
547         case DECO_DESCRIPTOR_SET:
548                 variables[id].descriptor_set = *op;
549                 break;
550         }
551 }
552
553 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
554 {
555         Structure &strct = structs[*(op+1)];
556         unsigned index = *(op+2);
557         if(index>=strct.members.size())
558                 strct.members.resize(index+1);
559         unsigned decoration = *(op+3);
560         op += 4;
561
562         StructMember &member = strct.members[index];
563         switch(decoration)
564         {
565         case DECO_MATRIX_STRIDE:
566                 member.matrix_stride = *op;
567                 break;
568         case DECO_OFFSET:
569                 member.offset = *op;
570                 break;
571         }
572 }
573
574 } // namespace GL
575 } // namespace Msp