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