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