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