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