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