]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Minor tweaks to MemoryAllocator
[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_SELECTION_MERGE = 247,
41         OP_LABEL = 248,
42         OP_BRANCH = 249,
43         OP_BRANCH_CONDITIONAL = 250,
44         OP_SWITCH = 251,
45         OP_KILL = 252,
46         OP_RETURN = 253,
47         OP_RETURN_VALUE = 254,
48         OP_UNREACHABLE = 255,
49
50         DECO_SPEC_ID = 1,
51         DECO_ARRAY_STRIDE = 6,
52         DECO_MATRIX_STRIDE = 7,
53         DECO_BUILTIN = 11,
54         DECO_LOCATION = 30,
55         DECO_BINDING = 33,
56         DECO_DESCRIPTOR_SET = 34,
57         DECO_OFFSET = 35
58 };
59
60 namespace Msp {
61 namespace GL {
62
63 void Module::set_source(const string &src)
64 {
65         SL::Compiler compiler(create_features());
66         compiler.set_source(src);
67         compile(compiler);
68 }
69
70 void Module::load_source(IO::Base &io, Resources *res, const string &name)
71 {
72         SL::Compiler compiler(create_features());
73         compiler.load_source(io, res, name);
74         compile(compiler);
75 }
76
77 void Module::load_source(IO::Base &io, const string &name)
78 {
79         load_source(io, 0, name);
80 }
81
82 SL::Features Module::create_features() const
83 {
84         const DeviceInfo &dev_info = Device::get_current().get_info();
85         const SL::Features &device_features = dev_info.glsl_features;
86         SL::Features latest_features = SL::Features::latest(dev_info.api);
87         SL::Features features;
88         features.target_api = latest_features.target_api;
89         features.glsl_version = latest_features.glsl_version;
90         features.constant_id_range = device_features.constant_id_range;
91         features.uniform_binding_range = device_features.uniform_binding_range;
92         features.texture_binding_range = device_features.texture_binding_range;
93         return features;
94 }
95
96
97 void GlslModule::compile(SL::Compiler &compiler)
98 {
99         compiler.compile(SL::Compiler::MODULE);
100         prepared_source = compiler.get_combined_glsl();
101         source_map = compiler.get_source_map();
102
103 #ifdef DEBUG
104         string diagnostics = compiler.get_diagnostics();
105         if(!diagnostics.empty())
106                 IO::print("Module diagnostics:\n%s\n", diagnostics);
107 #endif
108 }
109
110
111 void SpirVModule::load_code(IO::Base &io)
112 {
113         uint32_t buffer[1024];
114         while(1)
115         {
116                 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
117                 if(!len)
118                         break;
119                 len /= 4;
120                 code.reserve(code.size()+len);
121                 code.insert(code.end(), buffer, buffer+len);
122         }
123
124         reflect();
125         create();
126 }
127
128 void SpirVModule::compile(SL::Compiler &compiler)
129 {
130         compiler.compile(SL::Compiler::SPIRV);
131         code = compiler.get_combined_spirv();
132         reflect();
133         create();
134 }
135
136 void SpirVModule::reflect()
137 {
138         if(code.empty())
139                 throw invalid_module("Empty SPIR-V code");
140
141         if(code[0]==SPIRV_MAGIC_REVERSED)
142         {
143                 for(uint32_t &c: code)
144                         c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
145         }
146         else if(code[0]!=SPIRV_MAGIC)
147                 throw invalid_module("SPIR-V magic number not found");
148
149         Reflection reflection;
150         reflection.reflect_code(code);
151
152         map<const Constant *, unsigned> spec_indices;
153         for(const auto &kvp: reflection.constants)
154                 if(kvp.second.constant_id>=0)
155                 {
156                         spec_indices[&kvp.second] = spec_constants.size();
157                         spec_constants.push_back(kvp.second);
158                 }
159
160         map<const Structure *, unsigned> struct_indices;
161         structs.reserve(reflection.structs.size());
162         for(const auto &kvp: reflection.structs)
163         {
164                 struct_indices[&kvp.second] = structs.size();
165                 structs.push_back(kvp.second);
166         }
167
168         for(Structure &s: structs)
169         {
170                 for(StructMember &m: s.members)
171                         if(m.struct_type)
172                         {
173                                 auto i = struct_indices.find(m.struct_type);
174                                 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
175                         }
176
177                 const StructMember *last_member = &s.members.back();
178                 unsigned last_offset = last_member->offset;
179                 while(last_member->struct_type)
180                 {
181                         const StructMember *lm = &last_member->struct_type->members.back();
182                         if(last_member->array_size)
183                                 last_offset += last_member->array_stride*(last_member->array_size-1);
184                         last_offset += lm->offset;
185                         last_member = lm;
186                 }
187                 
188                 unsigned last_size = get_type_size(last_member->type); 
189                 if(last_member->array_size)
190                         last_size += last_member->array_stride*(last_member->array_size-1);
191                 s.size = last_offset+last_size;
192                 s.size = (s.size+15)&~15;
193         }
194
195         map<const Variable *, unsigned> var_indices;
196         variables.reserve(reflection.variables.size());
197         for(const auto &kvp: reflection.variables)
198         {
199                 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
200                 if(i!=variables.end())
201                         var_indices[&kvp.second] = i-variables.begin();
202                 else
203                 {
204                         var_indices[&kvp.second] = variables.size();
205                         variables.push_back(kvp.second);
206                 }
207         }
208
209         for(Variable &v: variables)
210                 if(v.struct_type)
211                 {
212                         auto i = struct_indices.find(v.struct_type);
213                         v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
214                 }
215
216         entry_points.reserve(reflection.entry_points.size());
217         for(const auto &kvp: reflection.entry_points)
218         {
219                 entry_points.push_back(kvp.second);
220                 EntryPoint &entry = entry_points.back();
221                 for(const Variable *&v: entry.globals)
222                 {
223                         auto i = var_indices.find(v);
224                         v = (i!=var_indices.end() ? &variables[i->second] : 0);
225                 }
226         }
227
228         map<const InstructionBlock *, unsigned> block_indices;
229         blocks.reserve(reflection.blocks.size());
230         for(const auto &kvp: reflection.blocks)
231         {
232                 block_indices[&kvp.second] = blocks.size();
233                 blocks.push_back(kvp.second);
234         }
235
236         for(InstructionBlock &b: blocks)
237         {
238                 auto i = spec_indices.find(b.condition);
239                 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
240
241                 for(const Variable *&v: b.accessed_variables)
242                 {
243                         auto j = var_indices.find(v);
244                         v = (j!=var_indices.end() ? &variables[j->second] : 0);
245                 }
246
247                 for(const InstructionBlock *&s: b.successors)
248                 {
249                         auto j = block_indices.find(s);
250                         s = (j!=block_indices.end() ? &blocks[j->second] : 0);
251                 }
252         }
253 }
254
255 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
256 {
257         vector<uint8_t> flags(code[3], 1);
258
259         std::map<unsigned, int> spec_values_by_id;
260         for(const Constant &c: spec_constants)
261         {
262                 auto i = spec_values.find(c.name);
263                 if(i!=spec_values.end())
264                 {
265                         flags[c.id] = (i->second ? 5 : 3);
266                         spec_values_by_id[c.constant_id] = i->second;
267                 }
268         }
269
270         for(const Variable &v: variables)
271                 flags[v.id] = 0;
272         for(const InstructionBlock &b: blocks)
273                 flags[b.id] = 0;
274         for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
275         {
276                 flags[b->id] = 1;
277                 for(const Variable *v: b->accessed_variables)
278                         flags[v->id] = 1;
279         }
280
281         std::vector<uint32_t> new_code;
282         new_code.reserve(code.size());
283
284         auto op = code.begin()+5;
285         new_code.insert(new_code.begin(), code.begin(), op);
286
287         bool skip_block = false;
288         while(op!=code.end())
289         {
290                 unsigned word_count = *op>>16;
291                 unsigned opcode = *op&0xFFFF;
292
293                 bool copy = !skip_block;
294                 if(skip_block)
295                 {
296                         skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
297                                 opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
298                 }
299                 else
300                 {
301                         if(opcode==OP_NAME)
302                                 copy = flags[*(op+1)];
303                         else if(opcode==OP_ENTRY_POINT)
304                         {
305                                 unsigned start = new_code.size();
306                                 new_code.push_back(opcode);
307                                 new_code.push_back(*(op+1));
308                                 new_code.push_back(*(op+2));
309
310                                 unsigned i=3;
311                                 while(i<word_count)
312                                 {
313                                         unsigned word = *(op+i++);
314                                         new_code.push_back(word);
315                                         if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
316                                                 break;
317                                 }
318
319                                 for(; i<word_count; ++i)
320                                 {
321                                         unsigned id = *(op+i);
322                                         if(flags[id])
323                                                 new_code.push_back(id);
324                                 }
325
326                                 new_code[start] |= (new_code.size()-start)<<16;
327
328                                 copy = false;
329                         }
330                         else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
331                         {
332                                 unsigned id = *(op+2);
333                                 if(flags[id]&2)
334                                 {
335                                         new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
336                                         new_code.push_back(*(op+1));
337                                         new_code.push_back(id);
338
339                                         copy = false;
340                                 }
341                         }
342                         else if(opcode==OP_VARIABLE)
343                                 copy = flags[*(op+2)];
344                         else if(opcode==OP_DECORATE)
345                         {
346                                 unsigned id = *(op+1);
347                                 copy = flags[id];
348                                 if(copy && *(op+2)==DECO_SPEC_ID)
349                                         copy = !(flags[id]&2);
350                         }
351                         else if(opcode==OP_LABEL)
352                         {
353                                 copy = flags[*(op+1)];
354                                 skip_block = !copy;
355                         }
356                         else if(opcode==OP_SELECTION_MERGE)
357                         {
358                                 unsigned next_opcode = *(op+word_count)&0xFFFF;
359                                 if(next_opcode==OP_BRANCH_CONDITIONAL)
360                                 {
361                                         unsigned true_id = *(op+word_count+2);
362                                         unsigned false_id = *(op+word_count+3);
363                                         if(!flags[true_id] || !flags[false_id])
364                                         {
365                                                 new_code.push_back(0x20000 | OP_BRANCH);
366                                                 new_code.push_back(flags[true_id] ? true_id : false_id);
367                                                 copy = false;
368
369                                                 /* Skip the branch instruction when it's encountered on the
370                                                 next iteration */
371                                                 skip_block = true;
372                                         }
373                                 }
374                         }
375                 }
376
377                 if(copy)
378                 {
379                         for(unsigned i=0; i<word_count; ++i)
380                                 new_code.push_back(*(op+i));
381                 }
382
383                 op += word_count;
384         }
385
386         SpirVModule *spec_mod = new SpirVModule;
387         spec_mod->code = move(new_code);
388         spec_mod->reflect();
389         spec_mod->create();
390
391         return spec_mod;
392 }
393
394 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
395 {
396         vector<uint8_t> visited(blocks.size(), 4);
397         for(unsigned i=0; i<blocks.size(); ++i)
398         {
399                 const InstructionBlock &b = blocks[i];
400
401                 bool cond = true;
402                 if(b.condition)
403                 {
404                         cond = b.condition->i_value;
405                         auto j = spec_values.find(b.condition->constant_id);
406                         if(j!=spec_values.end())
407                                 cond = j->second;
408                         if(b.negate_condition)
409                                 cond = !cond;
410                 }
411
412                 visited[i] |= cond*2;
413                 for(const InstructionBlock *s: b.successors)
414                         visited[s-blocks.data()] &= 3;
415         }
416
417         for(unsigned i=0; i<blocks.size(); ++i)
418                 if(visited[i]&4)
419                         collect_visited_blocks(i, visited);
420
421         vector<const SpirVModule::InstructionBlock *> result;
422         for(unsigned i=0; i<blocks.size(); ++i)
423                 if(visited[i]&1)
424                         result.push_back(&blocks[i]);
425
426         return result;
427 }
428
429 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
430 {
431         visited[i] |= 1;
432         for(const InstructionBlock *s: blocks[i].successors)
433         {
434                 unsigned j = s-blocks.data();
435                 if((visited[j]&3)==2)
436                         collect_visited_blocks(j, visited);
437         }
438 }
439
440
441 bool SpirVModule::Variable::operator==(const Variable &other) const
442 {
443         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
444                 return false;
445         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
446                 return false;
447         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
448                 return false;
449         return true;
450 }
451
452
453 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
454 {
455         return op&0xFFFF;
456 }
457
458 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
459 {
460         return op+(*op>>16);
461 }
462
463 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
464 {
465         string result;
466         for(; op!=op_end; ++op)
467         {
468                 unsigned word = *op;
469                 for(unsigned i=0; i<4; ++i)
470                 {
471                         char c = word&0xFF;
472                         if(!c)
473                         {
474                                 ++op;
475                                 return result;
476                         }
477                         result += c;
478                         word >>= 8;
479                 }
480         }
481
482         throw invalid_module("Unterminated SPIR-V string literal");
483 }
484
485 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
486 {
487         for(CodeIterator op=code.begin()+5; op!=code.end(); )
488         {
489                 unsigned word_count = *op>>16;
490                 if(word_count>static_cast<unsigned>(code.end()-op))
491                         throw invalid_module("Truncated SPIR-V instruction");
492
493                 switch(get_opcode(*op))
494                 {
495                 case OP_NAME: reflect_name(op); break;
496                 case OP_MEMBER_NAME: reflect_member_name(op); break;
497                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
498                 case OP_TYPE_VOID: reflect_void_type(op); break;
499                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
500                 case OP_TYPE_INT: reflect_int_type(op); break;
501                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
502                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
503                 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
504                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
505                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
506                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
507                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
508                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
509                 case OP_CONSTANT_TRUE:
510                 case OP_CONSTANT_FALSE:
511                 case OP_CONSTANT:
512                 case OP_SPEC_CONSTANT_TRUE:
513                 case OP_SPEC_CONSTANT_FALSE:
514                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
515                 case OP_VARIABLE: reflect_variable(op); break;
516                 case OP_LOAD:
517                 case OP_STORE: reflect_access(op); break;
518                 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
519                 case OP_DECORATE: reflect_decorate(op); break;
520                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
521                 case OP_LABEL: reflect_label(op); break;
522                 case OP_BRANCH: reflect_branch(op); break;
523                 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
524                 }
525
526                 op += word_count;
527         }
528 }
529
530 void SpirVModule::Reflection::reflect_name(CodeIterator op)
531 {
532         CodeIterator op_end = get_op_end(op);
533         string &name = names[*(op+1)];
534         op += 2;
535         name = read_string(op, op_end);
536 }
537
538 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
539 {
540         CodeIterator op_end = get_op_end(op);
541         Structure &strct = structs[*(op+1)];
542         unsigned index = *(op+2);
543         if(index>=strct.members.size())
544                 strct.members.resize(index+1);
545         op += 3;
546         strct.members[index].name = read_string(op, op_end);
547 }
548
549 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
550 {
551         CodeIterator op_end = get_op_end(op);
552         unsigned id = *(op+2);
553         EntryPoint &entry = entry_points[id];
554         entry.id = id;
555         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
556         op += 3;
557         entry.name = read_string(op, op_end);
558
559         entry.globals.reserve(op_end-op);
560         for(; op!=op_end; ++op)
561                 entry.globals.push_back(&variables[*op]);
562 }
563
564 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
565 {
566         types[*(op+1)].type = VOID;
567 }
568
569 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
570 {
571         types[*(op+1)].type = BOOL;
572 }
573
574 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
575 {
576         TypeInfo &type = types[*(op+1)];
577         unsigned size = *(op+2);
578         bool sign = *(op+3);
579         type.type = static_cast<DataType>(size/8 | sign*0x100);
580 }
581
582 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
583 {
584         TypeInfo &type = types[*(op+1)];
585         unsigned size = *(op+2);
586         type.type = static_cast<DataType>(size/8 | 0x300);
587 }
588
589 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
590 {
591         TypeInfo &type = types[*(op+1)];
592         DataType component = types[*(op+2)].type;
593         unsigned count = *(op+3);
594         type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
595 }
596
597 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
598 {
599         TypeInfo &type = types[*(op+1)];
600         DataType column = types[*(op+2)].type;
601         unsigned count = *(op+3);
602         type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
603 }
604
605 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
606 {
607         TypeInfo &type = types[*(op+1)];
608         DataType sample = types[*(op+2)].type;
609         unsigned dimensions = *(op+3);
610         bool depth = *(op+4)==1;
611         bool array = *(op+5);
612         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
613 }
614
615 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
616 {
617         TypeInfo &type = types[*(op+1)];
618         DataType image = types[*(op+2)].type;
619         type.type = static_cast<DataType>(image | 0x100000);
620 }
621
622 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
623 {
624         TypeInfo &type = types[*(op+1)];
625         const TypeInfo &elem = types[*(op+2)];
626         type.type = elem.type;
627         type.struct_type = elem.struct_type;
628
629         const Constant &size = constants[*(op+3)];
630         if(size.type==INT || size.type==UNSIGNED_INT)
631                 type.array_size = size.i_value;
632 }
633
634 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
635 {
636         CodeIterator op_end = get_op_end(op);
637         unsigned id = *(op+1);
638         Structure &strct = structs[id];
639         strct.name = names[id];
640         strct.id = id;
641         types[id].struct_type = &strct;
642         op += 2;
643
644         strct.members.resize(op_end-op);
645         auto mem = strct.members.begin();
646         for(; op!=op_end; ++op, ++mem)
647         {
648                 TypeInfo &type = types[*op];
649                 mem->type = type.type;
650                 mem->struct_type = type.struct_type;
651                 mem->array_size = type.array_size;
652                 mem->array_stride = type.array_stride;
653         }
654 }
655
656 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
657 {
658         TypeInfo &type = types[*(op+1)];
659         type = types[*(op+3)];
660         type.storage = static_cast<StorageClass>(*(op+2));
661 }
662
663 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
664 {
665         unsigned opcode = get_opcode(*op);
666         unsigned id = *(op+2);
667         Constant &cnst = constants[id];
668         cnst.name = names[id];
669         cnst.id = id;
670         cnst.type = types[*(op+1)].type;
671         if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
672                 cnst.i_value = true;
673         else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
674                 cnst.i_value = false;
675         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
676                 cnst.i_value = *(op+3);
677         else if(cnst.type==FLOAT)
678                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
679 }
680
681 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
682 {
683         unsigned id = *(op+2);
684         Variable &var = variables[id];
685         var.name = names[id];
686         var.id = id;
687         const TypeInfo &type = types[*(op+1)];
688         var.storage = type.storage;
689         var.type = type.type;
690         var.struct_type = type.struct_type;
691         var.array_size = type.array_size;
692 }
693
694 void SpirVModule::Reflection::reflect_access(CodeIterator op)
695 {
696         if(current_block)
697         {
698                 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
699                 auto i = access_chain_bases.find(id);
700                 if(i!=access_chain_bases.end())
701                         id = i->second;
702                 Variable &var = variables[id];
703                 auto j = find(current_block->accessed_variables, &var);
704                 if(j==current_block->accessed_variables.end())
705                         current_block->accessed_variables.push_back(&var);
706         }
707 }
708
709 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
710 {
711         access_chain_bases[*(op+2)] = *(op+3);
712 }
713
714 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
715 {
716         unsigned id = *(op+1);
717         unsigned decoration = *(op+2);
718         op += 3;
719
720         switch(decoration)
721         {
722         case DECO_SPEC_ID:
723                 constants[id].constant_id = *op;
724                 break;
725         case DECO_ARRAY_STRIDE:
726                 types[id].array_stride = *op;
727                 break;
728         case DECO_BUILTIN:
729                 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
730                 break;
731         case DECO_LOCATION:
732                 variables[id].location = *op;
733                 break;
734         case DECO_BINDING:
735                 variables[id].binding = *op;
736                 break;
737         case DECO_DESCRIPTOR_SET:
738                 variables[id].descriptor_set = *op;
739                 break;
740         }
741 }
742
743 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
744 {
745         Structure &strct = structs[*(op+1)];
746         unsigned index = *(op+2);
747         if(index>=strct.members.size())
748                 strct.members.resize(index+1);
749         unsigned decoration = *(op+3);
750         op += 4;
751
752         StructMember &member = strct.members[index];
753         switch(decoration)
754         {
755         case DECO_MATRIX_STRIDE:
756                 member.matrix_stride = *op;
757                 break;
758         case DECO_BUILTIN:
759                 member.builtin = static_cast<BuiltinSemantic>(*op);
760                 break;
761         case DECO_OFFSET:
762                 member.offset = *op;
763                 break;
764         }
765 }
766
767 void SpirVModule::Reflection::reflect_label(CodeIterator op)
768 {
769         unsigned id = *(op+1);
770         current_block = &blocks[id];
771         current_block->id = id;
772 }
773
774 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
775 {
776         InstructionBlock &block = blocks[*(op+1)];
777         block.condition = &true_condition;
778         current_block->successors.push_back(&block);
779 }
780
781 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
782 {
783         InstructionBlock &true_block = blocks[*(op+2)];
784         InstructionBlock &false_block = blocks[*(op+3)];
785
786         auto i = constants.find(*(op+1));
787         if(i!=constants.end() && i->second.constant_id)
788         {
789                 if(!true_block.condition)
790                         true_block.condition = &i->second;
791                 if(!false_block.condition)
792                 {
793                         false_block.condition = &i->second;
794                         false_block.negate_condition = true;
795                 }
796         }
797
798         current_block->successors.push_back(&true_block);
799         current_block->successors.push_back(&false_block);
800 }
801
802 } // namespace GL
803 } // namespace Msp