1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
11 SPIRV_MAGIC = 0x07230203,
12 SPIRV_MAGIC_REVERSED = 0x03022307,
17 OP_EXECUTION_MODE = 16,
25 OP_TYPE_SAMPLED_IMAGE = 27,
29 OP_CONSTANT_TRUE = 41,
30 OP_CONSTANT_FALSE = 42,
32 OP_SPEC_CONSTANT_TRUE = 48,
33 OP_SPEC_CONSTANT_FALSE = 49,
34 OP_SPEC_CONSTANT = 50,
40 OP_MEMBER_DECORATE = 72,
41 OP_SELECTION_MERGE = 247,
44 OP_BRANCH_CONDITIONAL = 250,
48 OP_RETURN_VALUE = 254,
54 DECO_ARRAY_STRIDE = 6,
55 DECO_MATRIX_STRIDE = 7,
59 DECO_DESCRIPTOR_SET = 34,
66 void Module::set_source(const string &src)
68 SL::Compiler compiler(create_features());
69 compiler.set_source(src);
73 void Module::load_source(IO::Base &io, Resources *res, const string &name)
75 SL::Compiler compiler(create_features());
76 compiler.load_source(io, res, name);
80 void Module::load_source(IO::Base &io, const string &name)
82 load_source(io, 0, name);
85 SL::Features Module::create_features() const
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;
100 void GlslModule::compile(SL::Compiler &compiler)
102 compiler.compile(SL::Compiler::MODULE);
103 prepared_source = compiler.get_combined_glsl();
104 source_map = compiler.get_source_map();
107 string diagnostics = compiler.get_diagnostics();
108 if(!diagnostics.empty())
109 IO::print("Module diagnostics:\n%s\n", diagnostics);
114 void SpirVModule::load_code(IO::Base &io)
116 uint32_t buffer[1024];
119 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
123 code.reserve(code.size()+len);
124 code.insert(code.end(), buffer, buffer+len);
131 void SpirVModule::compile(SL::Compiler &compiler)
133 compiler.compile(SL::Compiler::SPIRV);
134 code = compiler.get_combined_spirv();
139 void SpirVModule::reflect()
142 throw invalid_module("Empty SPIR-V code");
144 if(code[0]==SPIRV_MAGIC_REVERSED)
146 for(uint32_t &c: code)
147 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
149 else if(code[0]!=SPIRV_MAGIC)
150 throw invalid_module("SPIR-V magic number not found");
152 Reflection reflection;
153 reflection.reflect_code(code);
155 map<const Constant *, unsigned> spec_indices;
156 for(const auto &kvp: reflection.constants)
157 if(kvp.second.constant_id>=0)
159 spec_indices[&kvp.second] = spec_constants.size();
160 spec_constants.push_back(kvp.second);
163 map<const Structure *, unsigned> struct_indices;
164 structs.reserve(reflection.structs.size());
165 for(const auto &kvp: reflection.structs)
167 struct_indices[&kvp.second] = structs.size();
168 structs.push_back(kvp.second);
171 for(Structure &s: structs)
173 for(StructMember &m: s.members)
176 auto i = struct_indices.find(m.struct_type);
177 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
180 const StructMember *last_member = &s.members.back();
181 unsigned last_offset = last_member->offset;
182 while(last_member->struct_type)
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;
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;
198 map<const Variable *, unsigned> var_indices;
199 variables.reserve(reflection.variables.size());
200 for(const auto &kvp: reflection.variables)
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();
207 var_indices[&kvp.second] = variables.size();
208 variables.push_back(kvp.second);
212 for(Variable &v: variables)
215 auto i = struct_indices.find(v.struct_type);
216 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
219 entry_points.reserve(reflection.entry_points.size());
220 for(const auto &kvp: reflection.entry_points)
222 entry_points.push_back(kvp.second);
223 EntryPoint &entry = entry_points.back();
224 for(const Variable *&v: entry.globals)
226 auto i = var_indices.find(v);
227 v = (i!=var_indices.end() ? &variables[i->second] : 0);
231 map<const InstructionBlock *, unsigned> block_indices;
232 blocks.reserve(reflection.blocks.size());
233 for(const auto &kvp: reflection.blocks)
235 block_indices[&kvp.second] = blocks.size();
236 blocks.push_back(kvp.second);
239 for(InstructionBlock &b: blocks)
241 auto i = spec_indices.find(b.condition);
242 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
244 specializable = true;
246 for(const Variable *&v: b.accessed_variables)
248 auto j = var_indices.find(v);
249 v = (j!=var_indices.end() ? &variables[j->second] : 0);
252 for(const InstructionBlock *&s: b.successors)
254 auto j = block_indices.find(s);
255 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
260 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
262 vector<uint8_t> flags(code[3], 1);
264 std::map<unsigned, int> spec_values_by_id;
265 for(const Constant &c: spec_constants)
267 auto i = spec_values.find(c.name);
268 if(i!=spec_values.end())
270 flags[c.id] = (i->second ? 5 : 3);
271 spec_values_by_id[c.constant_id] = i->second;
275 for(const EntryPoint &e: entry_points)
277 for(const Variable &v: variables)
279 for(const InstructionBlock &b: blocks)
281 for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
284 for(const Variable *v: b->accessed_variables)
288 std::vector<uint32_t> new_code;
289 new_code.reserve(code.size());
291 auto op = code.begin()+5;
292 new_code.insert(new_code.begin(), code.begin(), op);
294 bool skip_block = false;
295 while(op!=code.end())
297 unsigned word_count = *op>>16;
298 unsigned opcode = *op&0xFFFF;
300 bool copy = !skip_block;
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);
309 copy = flags[*(op+1)];
310 else if(opcode==OP_ENTRY_POINT)
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);
321 unsigned word = *(op+i++);
322 new_code.push_back(word);
323 if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
327 unsigned var_count = 0;
328 for(; i<word_count; ++i)
330 unsigned id = *(op+i);
334 new_code.push_back(id);
341 new_code[start] |= (new_code.size()-start)<<16;
344 new_code.resize(start);
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)
352 unsigned id = *(op+2);
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);
362 else if(opcode==OP_VARIABLE)
363 copy = flags[*(op+2)];
364 else if(opcode==OP_DECORATE)
366 unsigned id = *(op+1);
368 if(copy && *(op+2)==DECO_SPEC_ID)
369 copy = !(flags[id]&2);
371 else if(opcode==OP_LABEL)
373 copy = flags[*(op+1)];
376 else if(opcode==OP_SELECTION_MERGE)
378 unsigned next_opcode = *(op+word_count)&0xFFFF;
379 if(next_opcode==OP_BRANCH_CONDITIONAL)
381 unsigned true_id = *(op+word_count+2);
382 unsigned false_id = *(op+word_count+3);
383 if(!flags[true_id] || !flags[false_id])
385 new_code.push_back(0x20000 | OP_BRANCH);
386 new_code.push_back(flags[true_id] ? true_id : false_id);
389 /* Skip the branch instruction when it's encountered on the
399 for(unsigned i=0; i<word_count; ++i)
400 new_code.push_back(*(op+i));
406 SpirVModule *spec_mod = new SpirVModule;
407 spec_mod->code = move(new_code);
414 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
416 vector<uint8_t> visited(blocks.size(), 4);
417 for(unsigned i=0; i<blocks.size(); ++i)
419 const InstructionBlock &b = blocks[i];
424 cond = b.condition->i_value;
425 auto j = spec_values.find(b.condition->constant_id);
426 if(j!=spec_values.end())
428 if(b.negate_condition)
432 visited[i] |= cond*2;
433 for(const InstructionBlock *s: b.successors)
434 visited[s-blocks.data()] &= 3;
437 for(unsigned i=0; i<blocks.size(); ++i)
439 collect_visited_blocks(i, visited);
441 vector<const SpirVModule::InstructionBlock *> result;
442 for(unsigned i=0; i<blocks.size(); ++i)
444 result.push_back(&blocks[i]);
449 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
452 for(const InstructionBlock *s: blocks[i].successors)
454 unsigned j = s-blocks.data();
455 if((visited[j]&3)==2)
456 collect_visited_blocks(j, visited);
461 bool SpirVModule::Variable::operator==(const Variable &other) const
463 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
465 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
467 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
473 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
478 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
483 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
486 for(; op!=op_end; ++op)
489 for(unsigned i=0; i<4; ++i)
502 throw invalid_module("Unterminated SPIR-V string literal");
505 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
507 for(CodeIterator op=code.begin()+5; op!=code.end(); )
509 unsigned word_count = *op>>16;
510 if(word_count>static_cast<unsigned>(code.end()-op))
511 throw invalid_module("Truncated SPIR-V instruction");
513 switch(get_opcode(*op))
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:
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;
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;
551 void SpirVModule::Reflection::reflect_name(CodeIterator op)
553 CodeIterator op_end = get_op_end(op);
554 string &name = names[*(op+1)];
556 name = read_string(op, op_end);
559 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
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);
567 strct.members[index].name = read_string(op, op_end);
570 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
572 CodeIterator op_end = get_op_end(op);
573 unsigned id = *(op+2);
574 EntryPoint &entry = entry_points[id];
576 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
578 entry.name = read_string(op, op_end);
580 entry.globals.reserve(op_end-op);
581 for(; op!=op_end; ++op)
582 entry.globals.push_back(&variables[*op]);
585 void SpirVModule::Reflection::reflect_execution_mode(CodeIterator op)
587 EntryPoint &entry = entry_points[*(op+1)];
588 unsigned mode = *(op+2);
589 if(mode==EXEC_LOCAL_SIZE)
591 entry.compute_local_size.x = *(op+3);
592 entry.compute_local_size.y = *(op+4);
593 entry.compute_local_size.z = *(op+5);
597 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
599 types[*(op+1)].type = VOID;
602 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
604 types[*(op+1)].type = BOOL;
607 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
609 TypeInfo &type = types[*(op+1)];
610 unsigned size = *(op+2);
612 type.type = static_cast<DataType>(size/8 | sign*0x100);
615 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
617 TypeInfo &type = types[*(op+1)];
618 unsigned size = *(op+2);
619 type.type = static_cast<DataType>(size/8 | 0x300);
622 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
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));
630 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
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));
638 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
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);
650 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
652 TypeInfo &type = types[*(op+1)];
653 DataType image = types[*(op+2)].type;
654 type.type = static_cast<DataType>(image | 0x100000);
657 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
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;
664 const Constant &size = constants[*(op+3)];
665 if(size.type==INT || size.type==UNSIGNED_INT)
666 type.array_size = size.i_value;
669 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
671 CodeIterator op_end = get_op_end(op);
672 unsigned id = *(op+1);
673 Structure &strct = structs[id];
674 strct.name = names[id];
676 types[id].struct_type = &strct;
679 strct.members.resize(op_end-op);
680 auto mem = strct.members.begin();
681 for(; op!=op_end; ++op, ++mem)
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;
691 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
693 TypeInfo &type = types[*(op+1)];
694 type = types[*(op+3)];
695 type.storage = static_cast<StorageClass>(*(op+2));
698 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
700 unsigned opcode = get_opcode(*op);
701 unsigned id = *(op+2);
702 Constant &cnst = constants[id];
703 cnst.name = names[id];
705 cnst.type = types[*(op+1)].type;
706 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_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));
716 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
718 unsigned id = *(op+2);
719 Variable &var = variables[id];
720 var.name = names[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;
729 void SpirVModule::Reflection::reflect_access(CodeIterator op)
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())
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);
744 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
746 access_chain_bases[*(op+2)] = *(op+3);
749 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
751 unsigned id = *(op+1);
752 unsigned decoration = *(op+2);
758 constants[id].constant_id = *op;
760 case DECO_ARRAY_STRIDE:
761 types[id].array_stride = *op;
764 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
767 variables[id].location = *op;
770 variables[id].binding = *op;
772 case DECO_DESCRIPTOR_SET:
773 variables[id].descriptor_set = *op;
778 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
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);
787 StructMember &member = strct.members[index];
790 case DECO_MATRIX_STRIDE:
791 member.matrix_stride = *op;
794 member.builtin = static_cast<BuiltinSemantic>(*op);
802 void SpirVModule::Reflection::reflect_label(CodeIterator op)
804 unsigned id = *(op+1);
805 current_block = &blocks[id];
806 current_block->id = id;
809 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
811 InstructionBlock &block = blocks[*(op+1)];
812 block.condition = &true_condition;
813 current_block->successors.push_back(&block);
816 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
818 InstructionBlock &true_block = blocks[*(op+2)];
819 InstructionBlock &false_block = blocks[*(op+3)];
821 auto i = constants.find(*(op+1));
822 if(i!=constants.end() && i->second.constant_id)
824 if(!true_block.condition)
825 true_block.condition = &i->second;
826 if(!false_block.condition)
828 false_block.condition = &i->second;
829 false_block.negate_condition = true;
833 current_block->successors.push_back(&true_block);
834 current_block->successors.push_back(&false_block);