]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Create specialized versions of SPIR-V modules with default spec values
[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                 if(b.condition)
241                         specializable = true;
242
243                 for(const Variable *&v: b.accessed_variables)
244                 {
245                         auto j = var_indices.find(v);
246                         v = (j!=var_indices.end() ? &variables[j->second] : 0);
247                 }
248
249                 for(const InstructionBlock *&s: b.successors)
250                 {
251                         auto j = block_indices.find(s);
252                         s = (j!=block_indices.end() ? &blocks[j->second] : 0);
253                 }
254         }
255 }
256
257 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
258 {
259         vector<uint8_t> flags(code[3], 1);
260
261         std::map<unsigned, int> spec_values_by_id;
262         for(const Constant &c: spec_constants)
263         {
264                 auto i = spec_values.find(c.name);
265                 if(i!=spec_values.end())
266                 {
267                         flags[c.id] = (i->second ? 5 : 3);
268                         spec_values_by_id[c.constant_id] = i->second;
269                 }
270         }
271
272         for(const Variable &v: variables)
273                 flags[v.id] = 0;
274         for(const InstructionBlock &b: blocks)
275                 flags[b.id] = 0;
276         for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
277         {
278                 flags[b->id] = 1;
279                 for(const Variable *v: b->accessed_variables)
280                         flags[v->id] = 1;
281         }
282
283         std::vector<uint32_t> new_code;
284         new_code.reserve(code.size());
285
286         auto op = code.begin()+5;
287         new_code.insert(new_code.begin(), code.begin(), op);
288
289         bool skip_block = false;
290         while(op!=code.end())
291         {
292                 unsigned word_count = *op>>16;
293                 unsigned opcode = *op&0xFFFF;
294
295                 bool copy = !skip_block;
296                 if(skip_block)
297                 {
298                         skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
299                                 opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
300                 }
301                 else
302                 {
303                         if(opcode==OP_NAME)
304                                 copy = flags[*(op+1)];
305                         else if(opcode==OP_ENTRY_POINT)
306                         {
307                                 unsigned start = new_code.size();
308                                 new_code.push_back(opcode);
309                                 new_code.push_back(*(op+1));
310                                 new_code.push_back(*(op+2));
311
312                                 unsigned i=3;
313                                 while(i<word_count)
314                                 {
315                                         unsigned word = *(op+i++);
316                                         new_code.push_back(word);
317                                         if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
318                                                 break;
319                                 }
320
321                                 for(; i<word_count; ++i)
322                                 {
323                                         unsigned id = *(op+i);
324                                         if(flags[id])
325                                                 new_code.push_back(id);
326                                 }
327
328                                 new_code[start] |= (new_code.size()-start)<<16;
329
330                                 copy = false;
331                         }
332                         else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
333                         {
334                                 unsigned id = *(op+2);
335                                 if(flags[id]&2)
336                                 {
337                                         new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
338                                         new_code.push_back(*(op+1));
339                                         new_code.push_back(id);
340
341                                         copy = false;
342                                 }
343                         }
344                         else if(opcode==OP_VARIABLE)
345                                 copy = flags[*(op+2)];
346                         else if(opcode==OP_DECORATE)
347                         {
348                                 unsigned id = *(op+1);
349                                 copy = flags[id];
350                                 if(copy && *(op+2)==DECO_SPEC_ID)
351                                         copy = !(flags[id]&2);
352                         }
353                         else if(opcode==OP_LABEL)
354                         {
355                                 copy = flags[*(op+1)];
356                                 skip_block = !copy;
357                         }
358                         else if(opcode==OP_SELECTION_MERGE)
359                         {
360                                 unsigned next_opcode = *(op+word_count)&0xFFFF;
361                                 if(next_opcode==OP_BRANCH_CONDITIONAL)
362                                 {
363                                         unsigned true_id = *(op+word_count+2);
364                                         unsigned false_id = *(op+word_count+3);
365                                         if(!flags[true_id] || !flags[false_id])
366                                         {
367                                                 new_code.push_back(0x20000 | OP_BRANCH);
368                                                 new_code.push_back(flags[true_id] ? true_id : false_id);
369                                                 copy = false;
370
371                                                 /* Skip the branch instruction when it's encountered on the
372                                                 next iteration */
373                                                 skip_block = true;
374                                         }
375                                 }
376                         }
377                 }
378
379                 if(copy)
380                 {
381                         for(unsigned i=0; i<word_count; ++i)
382                                 new_code.push_back(*(op+i));
383                 }
384
385                 op += word_count;
386         }
387
388         SpirVModule *spec_mod = new SpirVModule;
389         spec_mod->code = move(new_code);
390         spec_mod->reflect();
391         spec_mod->create();
392
393         return spec_mod;
394 }
395
396 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
397 {
398         vector<uint8_t> visited(blocks.size(), 4);
399         for(unsigned i=0; i<blocks.size(); ++i)
400         {
401                 const InstructionBlock &b = blocks[i];
402
403                 bool cond = true;
404                 if(b.condition)
405                 {
406                         cond = b.condition->i_value;
407                         auto j = spec_values.find(b.condition->constant_id);
408                         if(j!=spec_values.end())
409                                 cond = j->second;
410                         if(b.negate_condition)
411                                 cond = !cond;
412                 }
413
414                 visited[i] |= cond*2;
415                 for(const InstructionBlock *s: b.successors)
416                         visited[s-blocks.data()] &= 3;
417         }
418
419         for(unsigned i=0; i<blocks.size(); ++i)
420                 if(visited[i]&4)
421                         collect_visited_blocks(i, visited);
422
423         vector<const SpirVModule::InstructionBlock *> result;
424         for(unsigned i=0; i<blocks.size(); ++i)
425                 if(visited[i]&1)
426                         result.push_back(&blocks[i]);
427
428         return result;
429 }
430
431 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
432 {
433         visited[i] |= 1;
434         for(const InstructionBlock *s: blocks[i].successors)
435         {
436                 unsigned j = s-blocks.data();
437                 if((visited[j]&3)==2)
438                         collect_visited_blocks(j, visited);
439         }
440 }
441
442
443 bool SpirVModule::Variable::operator==(const Variable &other) const
444 {
445         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
446                 return false;
447         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
448                 return false;
449         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
450                 return false;
451         return true;
452 }
453
454
455 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
456 {
457         return op&0xFFFF;
458 }
459
460 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
461 {
462         return op+(*op>>16);
463 }
464
465 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
466 {
467         string result;
468         for(; op!=op_end; ++op)
469         {
470                 unsigned word = *op;
471                 for(unsigned i=0; i<4; ++i)
472                 {
473                         char c = word&0xFF;
474                         if(!c)
475                         {
476                                 ++op;
477                                 return result;
478                         }
479                         result += c;
480                         word >>= 8;
481                 }
482         }
483
484         throw invalid_module("Unterminated SPIR-V string literal");
485 }
486
487 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
488 {
489         for(CodeIterator op=code.begin()+5; op!=code.end(); )
490         {
491                 unsigned word_count = *op>>16;
492                 if(word_count>static_cast<unsigned>(code.end()-op))
493                         throw invalid_module("Truncated SPIR-V instruction");
494
495                 switch(get_opcode(*op))
496                 {
497                 case OP_NAME: reflect_name(op); break;
498                 case OP_MEMBER_NAME: reflect_member_name(op); break;
499                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
500                 case OP_TYPE_VOID: reflect_void_type(op); break;
501                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
502                 case OP_TYPE_INT: reflect_int_type(op); break;
503                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
504                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
505                 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
506                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
507                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
508                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
509                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
510                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
511                 case OP_CONSTANT_TRUE:
512                 case OP_CONSTANT_FALSE:
513                 case OP_CONSTANT:
514                 case OP_SPEC_CONSTANT_TRUE:
515                 case OP_SPEC_CONSTANT_FALSE:
516                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
517                 case OP_VARIABLE: reflect_variable(op); break;
518                 case OP_LOAD:
519                 case OP_STORE: reflect_access(op); break;
520                 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
521                 case OP_DECORATE: reflect_decorate(op); break;
522                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
523                 case OP_LABEL: reflect_label(op); break;
524                 case OP_BRANCH: reflect_branch(op); break;
525                 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
526                 }
527
528                 op += word_count;
529         }
530 }
531
532 void SpirVModule::Reflection::reflect_name(CodeIterator op)
533 {
534         CodeIterator op_end = get_op_end(op);
535         string &name = names[*(op+1)];
536         op += 2;
537         name = read_string(op, op_end);
538 }
539
540 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
541 {
542         CodeIterator op_end = get_op_end(op);
543         Structure &strct = structs[*(op+1)];
544         unsigned index = *(op+2);
545         if(index>=strct.members.size())
546                 strct.members.resize(index+1);
547         op += 3;
548         strct.members[index].name = read_string(op, op_end);
549 }
550
551 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
552 {
553         CodeIterator op_end = get_op_end(op);
554         unsigned id = *(op+2);
555         EntryPoint &entry = entry_points[id];
556         entry.id = id;
557         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
558         op += 3;
559         entry.name = read_string(op, op_end);
560
561         entry.globals.reserve(op_end-op);
562         for(; op!=op_end; ++op)
563                 entry.globals.push_back(&variables[*op]);
564 }
565
566 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
567 {
568         types[*(op+1)].type = VOID;
569 }
570
571 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
572 {
573         types[*(op+1)].type = BOOL;
574 }
575
576 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
577 {
578         TypeInfo &type = types[*(op+1)];
579         unsigned size = *(op+2);
580         bool sign = *(op+3);
581         type.type = static_cast<DataType>(size/8 | sign*0x100);
582 }
583
584 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
585 {
586         TypeInfo &type = types[*(op+1)];
587         unsigned size = *(op+2);
588         type.type = static_cast<DataType>(size/8 | 0x300);
589 }
590
591 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
592 {
593         TypeInfo &type = types[*(op+1)];
594         DataType component = types[*(op+2)].type;
595         unsigned count = *(op+3);
596         type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
597 }
598
599 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
600 {
601         TypeInfo &type = types[*(op+1)];
602         DataType column = types[*(op+2)].type;
603         unsigned count = *(op+3);
604         type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
605 }
606
607 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
608 {
609         TypeInfo &type = types[*(op+1)];
610         DataType sample = types[*(op+2)].type;
611         unsigned dimensions = *(op+3);
612         bool depth = *(op+4)==1;
613         bool array = *(op+5);
614         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
615 }
616
617 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
618 {
619         TypeInfo &type = types[*(op+1)];
620         DataType image = types[*(op+2)].type;
621         type.type = static_cast<DataType>(image | 0x100000);
622 }
623
624 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
625 {
626         TypeInfo &type = types[*(op+1)];
627         const TypeInfo &elem = types[*(op+2)];
628         type.type = elem.type;
629         type.struct_type = elem.struct_type;
630
631         const Constant &size = constants[*(op+3)];
632         if(size.type==INT || size.type==UNSIGNED_INT)
633                 type.array_size = size.i_value;
634 }
635
636 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
637 {
638         CodeIterator op_end = get_op_end(op);
639         unsigned id = *(op+1);
640         Structure &strct = structs[id];
641         strct.name = names[id];
642         strct.id = id;
643         types[id].struct_type = &strct;
644         op += 2;
645
646         strct.members.resize(op_end-op);
647         auto mem = strct.members.begin();
648         for(; op!=op_end; ++op, ++mem)
649         {
650                 TypeInfo &type = types[*op];
651                 mem->type = type.type;
652                 mem->struct_type = type.struct_type;
653                 mem->array_size = type.array_size;
654                 mem->array_stride = type.array_stride;
655         }
656 }
657
658 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
659 {
660         TypeInfo &type = types[*(op+1)];
661         type = types[*(op+3)];
662         type.storage = static_cast<StorageClass>(*(op+2));
663 }
664
665 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
666 {
667         unsigned opcode = get_opcode(*op);
668         unsigned id = *(op+2);
669         Constant &cnst = constants[id];
670         cnst.name = names[id];
671         cnst.id = id;
672         cnst.type = types[*(op+1)].type;
673         if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
674                 cnst.i_value = true;
675         else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
676                 cnst.i_value = false;
677         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
678                 cnst.i_value = *(op+3);
679         else if(cnst.type==FLOAT)
680                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
681 }
682
683 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
684 {
685         unsigned id = *(op+2);
686         Variable &var = variables[id];
687         var.name = names[id];
688         var.id = id;
689         const TypeInfo &type = types[*(op+1)];
690         var.storage = type.storage;
691         var.type = type.type;
692         var.struct_type = type.struct_type;
693         var.array_size = type.array_size;
694 }
695
696 void SpirVModule::Reflection::reflect_access(CodeIterator op)
697 {
698         if(current_block)
699         {
700                 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
701                 auto i = access_chain_bases.find(id);
702                 if(i!=access_chain_bases.end())
703                         id = i->second;
704                 Variable &var = variables[id];
705                 auto j = find(current_block->accessed_variables, &var);
706                 if(j==current_block->accessed_variables.end())
707                         current_block->accessed_variables.push_back(&var);
708         }
709 }
710
711 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
712 {
713         access_chain_bases[*(op+2)] = *(op+3);
714 }
715
716 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
717 {
718         unsigned id = *(op+1);
719         unsigned decoration = *(op+2);
720         op += 3;
721
722         switch(decoration)
723         {
724         case DECO_SPEC_ID:
725                 constants[id].constant_id = *op;
726                 break;
727         case DECO_ARRAY_STRIDE:
728                 types[id].array_stride = *op;
729                 break;
730         case DECO_BUILTIN:
731                 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
732                 break;
733         case DECO_LOCATION:
734                 variables[id].location = *op;
735                 break;
736         case DECO_BINDING:
737                 variables[id].binding = *op;
738                 break;
739         case DECO_DESCRIPTOR_SET:
740                 variables[id].descriptor_set = *op;
741                 break;
742         }
743 }
744
745 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
746 {
747         Structure &strct = structs[*(op+1)];
748         unsigned index = *(op+2);
749         if(index>=strct.members.size())
750                 strct.members.resize(index+1);
751         unsigned decoration = *(op+3);
752         op += 4;
753
754         StructMember &member = strct.members[index];
755         switch(decoration)
756         {
757         case DECO_MATRIX_STRIDE:
758                 member.matrix_stride = *op;
759                 break;
760         case DECO_BUILTIN:
761                 member.builtin = static_cast<BuiltinSemantic>(*op);
762                 break;
763         case DECO_OFFSET:
764                 member.offset = *op;
765                 break;
766         }
767 }
768
769 void SpirVModule::Reflection::reflect_label(CodeIterator op)
770 {
771         unsigned id = *(op+1);
772         current_block = &blocks[id];
773         current_block->id = id;
774 }
775
776 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
777 {
778         InstructionBlock &block = blocks[*(op+1)];
779         block.condition = &true_condition;
780         current_block->successors.push_back(&block);
781 }
782
783 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
784 {
785         InstructionBlock &true_block = blocks[*(op+2)];
786         InstructionBlock &false_block = blocks[*(op+3)];
787
788         auto i = constants.find(*(op+1));
789         if(i!=constants.end() && i->second.constant_id)
790         {
791                 if(!true_block.condition)
792                         true_block.condition = &i->second;
793                 if(!false_block.condition)
794                 {
795                         false_block.condition = &i->second;
796                         false_block.negate_condition = true;
797                 }
798         }
799
800         current_block->successors.push_back(&true_block);
801         current_block->successors.push_back(&false_block);
802 }
803
804 } // namespace GL
805 } // namespace Msp