1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
11 SPIRV_MAGIC = 0x07230203,
12 SPIRV_MAGIC_REVERSED = 0x03022307,
24 OP_TYPE_SAMPLED_IMAGE = 27,
28 OP_CONSTANT_TRUE = 41,
29 OP_CONSTANT_FALSE = 42,
31 OP_SPEC_CONSTANT_TRUE = 48,
32 OP_SPEC_CONSTANT_FALSE = 49,
33 OP_SPEC_CONSTANT = 50,
39 OP_MEMBER_DECORATE = 72,
40 OP_SELECTION_MERGE = 247,
43 OP_BRANCH_CONDITIONAL = 250,
47 OP_RETURN_VALUE = 254,
51 DECO_ARRAY_STRIDE = 6,
52 DECO_MATRIX_STRIDE = 7,
56 DECO_DESCRIPTOR_SET = 34,
63 void Module::set_source(const string &src)
65 SL::Compiler compiler(create_features());
66 compiler.set_source(src);
70 void Module::load_source(IO::Base &io, Resources *res, const string &name)
72 SL::Compiler compiler(create_features());
73 compiler.load_source(io, res, name);
77 void Module::load_source(IO::Base &io, const string &name)
79 load_source(io, 0, name);
82 SL::Features Module::create_features() const
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;
97 void GlslModule::compile(SL::Compiler &compiler)
99 compiler.compile(SL::Compiler::MODULE);
100 prepared_source = compiler.get_combined_glsl();
101 source_map = compiler.get_source_map();
104 string diagnostics = compiler.get_diagnostics();
105 if(!diagnostics.empty())
106 IO::print("Module diagnostics:\n%s\n", diagnostics);
111 void SpirVModule::load_code(IO::Base &io)
113 uint32_t buffer[1024];
116 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
120 code.reserve(code.size()+len);
121 code.insert(code.end(), buffer, buffer+len);
128 void SpirVModule::compile(SL::Compiler &compiler)
130 compiler.compile(SL::Compiler::SPIRV);
131 code = compiler.get_combined_spirv();
136 void SpirVModule::reflect()
139 throw invalid_module("Empty SPIR-V code");
141 if(code[0]==SPIRV_MAGIC_REVERSED)
143 for(uint32_t &c: code)
144 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
146 else if(code[0]!=SPIRV_MAGIC)
147 throw invalid_module("SPIR-V magic number not found");
149 Reflection reflection;
150 reflection.reflect_code(code);
152 map<const Constant *, unsigned> spec_indices;
153 for(const auto &kvp: reflection.constants)
154 if(kvp.second.constant_id>=0)
156 spec_indices[&kvp.second] = spec_constants.size();
157 spec_constants.push_back(kvp.second);
160 map<const Structure *, unsigned> struct_indices;
161 structs.reserve(reflection.structs.size());
162 for(const auto &kvp: reflection.structs)
164 struct_indices[&kvp.second] = structs.size();
165 structs.push_back(kvp.second);
168 for(Structure &s: structs)
170 for(StructMember &m: s.members)
173 auto i = struct_indices.find(m.struct_type);
174 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
177 const StructMember *last_member = &s.members.back();
178 unsigned last_offset = last_member->offset;
179 while(last_member->struct_type)
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;
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;
195 map<const Variable *, unsigned> var_indices;
196 variables.reserve(reflection.variables.size());
197 for(const auto &kvp: reflection.variables)
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();
204 var_indices[&kvp.second] = variables.size();
205 variables.push_back(kvp.second);
209 for(Variable &v: variables)
212 auto i = struct_indices.find(v.struct_type);
213 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
216 entry_points.reserve(reflection.entry_points.size());
217 for(const auto &kvp: reflection.entry_points)
219 entry_points.push_back(kvp.second);
220 EntryPoint &entry = entry_points.back();
221 for(const Variable *&v: entry.globals)
223 auto i = var_indices.find(v);
224 v = (i!=var_indices.end() ? &variables[i->second] : 0);
228 map<const InstructionBlock *, unsigned> block_indices;
229 blocks.reserve(reflection.blocks.size());
230 for(const auto &kvp: reflection.blocks)
232 block_indices[&kvp.second] = blocks.size();
233 blocks.push_back(kvp.second);
236 for(InstructionBlock &b: blocks)
238 auto i = spec_indices.find(b.condition);
239 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
241 specializable = true;
243 for(const Variable *&v: b.accessed_variables)
245 auto j = var_indices.find(v);
246 v = (j!=var_indices.end() ? &variables[j->second] : 0);
249 for(const InstructionBlock *&s: b.successors)
251 auto j = block_indices.find(s);
252 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
257 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
259 vector<uint8_t> flags(code[3], 1);
261 std::map<unsigned, int> spec_values_by_id;
262 for(const Constant &c: spec_constants)
264 auto i = spec_values.find(c.name);
265 if(i!=spec_values.end())
267 flags[c.id] = (i->second ? 5 : 3);
268 spec_values_by_id[c.constant_id] = i->second;
272 for(const Variable &v: variables)
274 for(const InstructionBlock &b: blocks)
276 for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
279 for(const Variable *v: b->accessed_variables)
283 std::vector<uint32_t> new_code;
284 new_code.reserve(code.size());
286 auto op = code.begin()+5;
287 new_code.insert(new_code.begin(), code.begin(), op);
289 bool skip_block = false;
290 while(op!=code.end())
292 unsigned word_count = *op>>16;
293 unsigned opcode = *op&0xFFFF;
295 bool copy = !skip_block;
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);
304 copy = flags[*(op+1)];
305 else if(opcode==OP_ENTRY_POINT)
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));
315 unsigned word = *(op+i++);
316 new_code.push_back(word);
317 if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
321 for(; i<word_count; ++i)
323 unsigned id = *(op+i);
325 new_code.push_back(id);
328 new_code[start] |= (new_code.size()-start)<<16;
332 else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
334 unsigned id = *(op+2);
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);
344 else if(opcode==OP_VARIABLE)
345 copy = flags[*(op+2)];
346 else if(opcode==OP_DECORATE)
348 unsigned id = *(op+1);
350 if(copy && *(op+2)==DECO_SPEC_ID)
351 copy = !(flags[id]&2);
353 else if(opcode==OP_LABEL)
355 copy = flags[*(op+1)];
358 else if(opcode==OP_SELECTION_MERGE)
360 unsigned next_opcode = *(op+word_count)&0xFFFF;
361 if(next_opcode==OP_BRANCH_CONDITIONAL)
363 unsigned true_id = *(op+word_count+2);
364 unsigned false_id = *(op+word_count+3);
365 if(!flags[true_id] || !flags[false_id])
367 new_code.push_back(0x20000 | OP_BRANCH);
368 new_code.push_back(flags[true_id] ? true_id : false_id);
371 /* Skip the branch instruction when it's encountered on the
381 for(unsigned i=0; i<word_count; ++i)
382 new_code.push_back(*(op+i));
388 SpirVModule *spec_mod = new SpirVModule;
389 spec_mod->code = move(new_code);
396 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
398 vector<uint8_t> visited(blocks.size(), 4);
399 for(unsigned i=0; i<blocks.size(); ++i)
401 const InstructionBlock &b = blocks[i];
406 cond = b.condition->i_value;
407 auto j = spec_values.find(b.condition->constant_id);
408 if(j!=spec_values.end())
410 if(b.negate_condition)
414 visited[i] |= cond*2;
415 for(const InstructionBlock *s: b.successors)
416 visited[s-blocks.data()] &= 3;
419 for(unsigned i=0; i<blocks.size(); ++i)
421 collect_visited_blocks(i, visited);
423 vector<const SpirVModule::InstructionBlock *> result;
424 for(unsigned i=0; i<blocks.size(); ++i)
426 result.push_back(&blocks[i]);
431 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
434 for(const InstructionBlock *s: blocks[i].successors)
436 unsigned j = s-blocks.data();
437 if((visited[j]&3)==2)
438 collect_visited_blocks(j, visited);
443 bool SpirVModule::Variable::operator==(const Variable &other) const
445 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
447 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
449 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
455 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
460 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
465 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
468 for(; op!=op_end; ++op)
471 for(unsigned i=0; i<4; ++i)
484 throw invalid_module("Unterminated SPIR-V string literal");
487 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
489 for(CodeIterator op=code.begin()+5; op!=code.end(); )
491 unsigned word_count = *op>>16;
492 if(word_count>static_cast<unsigned>(code.end()-op))
493 throw invalid_module("Truncated SPIR-V instruction");
495 switch(get_opcode(*op))
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:
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;
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;
532 void SpirVModule::Reflection::reflect_name(CodeIterator op)
534 CodeIterator op_end = get_op_end(op);
535 string &name = names[*(op+1)];
537 name = read_string(op, op_end);
540 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
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);
548 strct.members[index].name = read_string(op, op_end);
551 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
553 CodeIterator op_end = get_op_end(op);
554 unsigned id = *(op+2);
555 EntryPoint &entry = entry_points[id];
557 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
559 entry.name = read_string(op, op_end);
561 entry.globals.reserve(op_end-op);
562 for(; op!=op_end; ++op)
563 entry.globals.push_back(&variables[*op]);
566 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
568 types[*(op+1)].type = VOID;
571 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
573 types[*(op+1)].type = BOOL;
576 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
578 TypeInfo &type = types[*(op+1)];
579 unsigned size = *(op+2);
581 type.type = static_cast<DataType>(size/8 | sign*0x100);
584 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
586 TypeInfo &type = types[*(op+1)];
587 unsigned size = *(op+2);
588 type.type = static_cast<DataType>(size/8 | 0x300);
591 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
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));
599 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
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));
607 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
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);
617 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
619 TypeInfo &type = types[*(op+1)];
620 DataType image = types[*(op+2)].type;
621 type.type = static_cast<DataType>(image | 0x100000);
624 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
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;
631 const Constant &size = constants[*(op+3)];
632 if(size.type==INT || size.type==UNSIGNED_INT)
633 type.array_size = size.i_value;
636 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
638 CodeIterator op_end = get_op_end(op);
639 unsigned id = *(op+1);
640 Structure &strct = structs[id];
641 strct.name = names[id];
643 types[id].struct_type = &strct;
646 strct.members.resize(op_end-op);
647 auto mem = strct.members.begin();
648 for(; op!=op_end; ++op, ++mem)
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;
658 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
660 TypeInfo &type = types[*(op+1)];
661 type = types[*(op+3)];
662 type.storage = static_cast<StorageClass>(*(op+2));
665 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
667 unsigned opcode = get_opcode(*op);
668 unsigned id = *(op+2);
669 Constant &cnst = constants[id];
670 cnst.name = names[id];
672 cnst.type = types[*(op+1)].type;
673 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_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));
683 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
685 unsigned id = *(op+2);
686 Variable &var = variables[id];
687 var.name = names[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;
696 void SpirVModule::Reflection::reflect_access(CodeIterator op)
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())
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);
711 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
713 access_chain_bases[*(op+2)] = *(op+3);
716 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
718 unsigned id = *(op+1);
719 unsigned decoration = *(op+2);
725 constants[id].constant_id = *op;
727 case DECO_ARRAY_STRIDE:
728 types[id].array_stride = *op;
731 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
734 variables[id].location = *op;
737 variables[id].binding = *op;
739 case DECO_DESCRIPTOR_SET:
740 variables[id].descriptor_set = *op;
745 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
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);
754 StructMember &member = strct.members[index];
757 case DECO_MATRIX_STRIDE:
758 member.matrix_stride = *op;
761 member.builtin = static_cast<BuiltinSemantic>(*op);
769 void SpirVModule::Reflection::reflect_label(CodeIterator op)
771 unsigned id = *(op+1);
772 current_block = &blocks[id];
773 current_block->id = id;
776 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
778 InstructionBlock &block = blocks[*(op+1)];
779 block.condition = &true_condition;
780 current_block->successors.push_back(&block);
783 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
785 InstructionBlock &true_block = blocks[*(op+2)];
786 InstructionBlock &false_block = blocks[*(op+3)];
788 auto i = constants.find(*(op+1));
789 if(i!=constants.end() && i->second.constant_id)
791 if(!true_block.condition)
792 true_block.condition = &i->second;
793 if(!false_block.condition)
795 false_block.condition = &i->second;
796 false_block.negate_condition = true;
800 current_block->successors.push_back(&true_block);
801 current_block->successors.push_back(&false_block);