]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Comment fixes
[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 }
120
121 void SpirVModule::compile(SL::Compiler &compiler)
122 {
123         compiler.compile(SL::Compiler::SPIRV);
124         code = compiler.get_combined_spirv();
125         reflect();
126 }
127
128 void SpirVModule::reflect()
129 {
130         if(code.empty())
131                 throw invalid_module("Empty SPIR-V code");
132
133         if(code[0]==SPIRV_MAGIC_REVERSED)
134         {
135                 for(uint32_t &c: code)
136                         c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>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 Constant *, unsigned> spec_indices;
145         for(const auto &kvp: reflection.constants)
146                 if(kvp.second.constant_id>=0)
147                 {
148                         spec_indices[&kvp.second] = spec_constants.size();
149                         spec_constants.push_back(kvp.second);
150                 }
151
152         map<const Structure *, unsigned> struct_indices;
153         structs.reserve(reflection.structs.size());
154         for(const auto &kvp: reflection.structs)
155         {
156                 struct_indices[&kvp.second] = structs.size();
157                 structs.push_back(kvp.second);
158         }
159
160         for(Structure &s: structs)
161         {
162                 for(StructMember &m: s.members)
163                         if(m.struct_type)
164                         {
165                                 auto i = struct_indices.find(m.struct_type);
166                                 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
167                         }
168
169                 const StructMember *last_member = &s.members.back();
170                 unsigned last_offset = last_member->offset;
171                 while(last_member->struct_type)
172                 {
173                         const StructMember *lm = &last_member->struct_type->members.back();
174                         if(last_member->array_size)
175                                 last_offset += last_member->array_stride*(last_member->array_size-1);
176                         last_offset += lm->offset;
177                         last_member = lm;
178                 }
179                 
180                 unsigned last_size = get_type_size(last_member->type); 
181                 if(last_member->array_size)
182                         last_size += last_member->array_stride*(last_member->array_size-1);
183                 s.size = last_offset+last_size;
184                 s.size = (s.size+15)&~15;
185         }
186
187         map<const Variable *, unsigned> var_indices;
188         variables.reserve(reflection.variables.size());
189         for(const auto &kvp: reflection.variables)
190         {
191                 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
192                 if(i!=variables.end())
193                         var_indices[&kvp.second] = i-variables.begin();
194                 else
195                 {
196                         var_indices[&kvp.second] = variables.size();
197                         variables.push_back(kvp.second);
198                 }
199         }
200
201         for(Variable &v: variables)
202                 if(v.struct_type)
203                 {
204                         auto i = struct_indices.find(v.struct_type);
205                         v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
206                 }
207
208         entry_points.reserve(reflection.entry_points.size());
209         for(const auto &kvp: reflection.entry_points)
210         {
211                 entry_points.push_back(kvp.second);
212                 EntryPoint &entry = entry_points.back();
213                 for(const Variable *&v: entry.globals)
214                 {
215                         auto i = var_indices.find(v);
216                         v = (i!=var_indices.end() ? &variables[i->second] : 0);
217                 }
218         }
219
220         map<const InstructionBlock *, unsigned> block_indices;
221         blocks.reserve(reflection.blocks.size());
222         for(const auto &kvp: reflection.blocks)
223         {
224                 block_indices[&kvp.second] = blocks.size();
225                 blocks.push_back(kvp.second);
226         }
227
228         for(InstructionBlock &b: blocks)
229         {
230                 auto i = spec_indices.find(b.condition);
231                 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
232
233                 for(const Variable *&v: b.accessed_variables)
234                 {
235                         auto j = var_indices.find(v);
236                         v = (j!=var_indices.end() ? &variables[j->second] : 0);
237                 }
238
239                 for(const InstructionBlock *&s: b.successors)
240                 {
241                         auto j = block_indices.find(s);
242                         s = (j!=block_indices.end() ? &blocks[j->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_matrix_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_LOAD:
324                 case OP_STORE: reflect_access(op); break;
325                 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
326                 case OP_DECORATE: reflect_decorate(op); break;
327                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
328                 case OP_LABEL: reflect_label(op); break;
329                 case OP_BRANCH: reflect_branch(op); break;
330                 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
331                 }
332
333                 op += word_count;
334         }
335 }
336
337 void SpirVModule::Reflection::reflect_name(CodeIterator op)
338 {
339         CodeIterator op_end = get_op_end(op);
340         string &name = names[*(op+1)];
341         op += 2;
342         name = read_string(op, op_end);
343 }
344
345 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
346 {
347         CodeIterator op_end = get_op_end(op);
348         Structure &strct = structs[*(op+1)];
349         unsigned index = *(op+2);
350         if(index>=strct.members.size())
351                 strct.members.resize(index+1);
352         op += 3;
353         strct.members[index].name = read_string(op, op_end);
354 }
355
356 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
357 {
358         CodeIterator op_end = get_op_end(op);
359         EntryPoint &entry = entry_points[*(op+2)];
360         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
361         op += 3;
362         entry.name = read_string(op, op_end);
363
364         entry.globals.reserve(op_end-op);
365         for(; op!=op_end; ++op)
366                 entry.globals.push_back(&variables[*op]);
367 }
368
369 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
370 {
371         types[*(op+1)].type = VOID;
372 }
373
374 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
375 {
376         types[*(op+1)].type = BOOL;
377 }
378
379 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
380 {
381         TypeInfo &type = types[*(op+1)];
382         unsigned size = *(op+2);
383         bool sign = *(op+3);
384         type.type = static_cast<DataType>(size/8 | sign*0x100);
385 }
386
387 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
388 {
389         TypeInfo &type = types[*(op+1)];
390         unsigned size = *(op+2);
391         type.type = static_cast<DataType>(size/8 | 0x300);
392 }
393
394 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
395 {
396         TypeInfo &type = types[*(op+1)];
397         DataType component = types[*(op+2)].type;
398         unsigned count = *(op+3);
399         type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
400 }
401
402 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
403 {
404         TypeInfo &type = types[*(op+1)];
405         DataType column = types[*(op+2)].type;
406         unsigned count = *(op+3);
407         type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
408 }
409
410 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
411 {
412         TypeInfo &type = types[*(op+1)];
413         DataType sample = types[*(op+2)].type;
414         unsigned dimensions = *(op+3);
415         bool depth = *(op+4)==1;
416         bool array = *(op+5);
417         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
418 }
419
420 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
421 {
422         TypeInfo &type = types[*(op+1)];
423         DataType image = types[*(op+2)].type;
424         type.type = static_cast<DataType>(image | 0x100000);
425 }
426
427 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
428 {
429         TypeInfo &type = types[*(op+1)];
430         const TypeInfo &elem = types[*(op+2)];
431         type.type = elem.type;
432         type.struct_type = elem.struct_type;
433
434         const Constant &size = constants[*(op+3)];
435         if(size.type==INT || size.type==UNSIGNED_INT)
436                 type.array_size = size.i_value;
437 }
438
439 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
440 {
441         CodeIterator op_end = get_op_end(op);
442         unsigned id = *(op+1);
443         Structure &strct = structs[id];
444         strct.name = names[id];
445         types[id].struct_type = &strct;
446         op += 2;
447
448         strct.members.resize(op_end-op);
449         auto mem = strct.members.begin();
450         for(; op!=op_end; ++op, ++mem)
451         {
452                 TypeInfo &type = types[*op];
453                 mem->type = type.type;
454                 mem->struct_type = type.struct_type;
455                 mem->array_size = type.array_size;
456                 mem->array_stride = type.array_stride;
457         }
458 }
459
460 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
461 {
462         TypeInfo &type = types[*(op+1)];
463         type = types[*(op+3)];
464         type.storage = static_cast<StorageClass>(*(op+2));
465 }
466
467 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
468 {
469         unsigned opcode = get_opcode(*op);
470         unsigned id = *(op+2);
471         Constant &cnst = constants[id];
472         cnst.name = names[id];
473         cnst.type = types[*(op+1)].type;
474         if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
475                 cnst.i_value = true;
476         else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
477                 cnst.i_value = false;
478         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
479                 cnst.i_value = *(op+3);
480         else if(cnst.type==FLOAT)
481                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
482 }
483
484 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
485 {
486         unsigned id = *(op+2);
487         Variable &var = variables[id];
488         var.name = names[id];
489         const TypeInfo &type = types[*(op+1)];
490         var.storage = type.storage;
491         var.type = type.type;
492         var.struct_type = type.struct_type;
493         var.array_size = type.array_size;
494 }
495
496 void SpirVModule::Reflection::reflect_access(CodeIterator op)
497 {
498         if(current_block)
499         {
500                 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
501                 auto i = access_chain_bases.find(id);
502                 if(i!=access_chain_bases.end())
503                         id = i->second;
504                 Variable &var = variables[id];
505                 auto j = find(current_block->accessed_variables, &var);
506                 if(j==current_block->accessed_variables.end())
507                         current_block->accessed_variables.push_back(&var);
508         }
509 }
510
511 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
512 {
513         access_chain_bases[*(op+2)] = *(op+3);
514 }
515
516 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
517 {
518         unsigned id = *(op+1);
519         unsigned decoration = *(op+2);
520         op += 3;
521
522         switch(decoration)
523         {
524         case DECO_SPEC_ID:
525                 constants[id].constant_id = *op;
526                 break;
527         case DECO_ARRAY_STRIDE:
528                 types[id].array_stride = *op;
529                 break;
530         case DECO_BUILTIN:
531                 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
532                 break;
533         case DECO_LOCATION:
534                 variables[id].location = *op;
535                 break;
536         case DECO_BINDING:
537                 variables[id].binding = *op;
538                 break;
539         case DECO_DESCRIPTOR_SET:
540                 variables[id].descriptor_set = *op;
541                 break;
542         }
543 }
544
545 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
546 {
547         Structure &strct = structs[*(op+1)];
548         unsigned index = *(op+2);
549         if(index>=strct.members.size())
550                 strct.members.resize(index+1);
551         unsigned decoration = *(op+3);
552         op += 4;
553
554         StructMember &member = strct.members[index];
555         switch(decoration)
556         {
557         case DECO_MATRIX_STRIDE:
558                 member.matrix_stride = *op;
559                 break;
560         case DECO_BUILTIN:
561                 member.builtin = static_cast<BuiltinSemantic>(*op);
562                 break;
563         case DECO_OFFSET:
564                 member.offset = *op;
565                 break;
566         }
567 }
568
569 void SpirVModule::Reflection::reflect_label(CodeIterator op)
570 {
571         current_block = &blocks[*(op+1)];
572 }
573
574 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
575 {
576         InstructionBlock &block = blocks[*(op+1)];
577         block.condition = &true_condition;
578         current_block->successors.push_back(&block);
579 }
580
581 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
582 {
583         InstructionBlock &true_block = blocks[*(op+2)];
584         InstructionBlock &false_block = blocks[*(op+3)];
585
586         auto i = constants.find(*(op+1));
587         if(i!=constants.end() && i->second.constant_id)
588         {
589                 if(!true_block.condition)
590                         true_block.condition = &i->second;
591                 if(!false_block.condition)
592                 {
593                         false_block.condition = &i->second;
594                         false_block.negate_condition = true;
595                 }
596         }
597
598         current_block->successors.push_back(&true_block);
599         current_block->successors.push_back(&false_block);
600 }
601
602 } // namespace GL
603 } // namespace Msp