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 // Strings are nul-terminated and nul-padded
328 unsigned var_count = 0;
329 for(; i<word_count; ++i)
331 unsigned id = *(op+i);
335 new_code.push_back(id);
342 new_code[start] |= (new_code.size()-start)<<16;
345 new_code.resize(start);
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)
353 unsigned id = *(op+2);
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);
363 else if(opcode==OP_VARIABLE)
364 copy = flags[*(op+2)];
365 else if(opcode==OP_DECORATE)
367 unsigned id = *(op+1);
369 if(copy && *(op+2)==DECO_SPEC_ID)
370 copy = !(flags[id]&2);
372 else if(opcode==OP_LABEL)
374 copy = flags[*(op+1)];
377 else if(opcode==OP_SELECTION_MERGE)
379 unsigned next_opcode = *(op+word_count)&0xFFFF;
380 if(next_opcode==OP_BRANCH_CONDITIONAL)
382 unsigned true_id = *(op+word_count+2);
383 unsigned false_id = *(op+word_count+3);
384 if(!flags[true_id] || !flags[false_id])
386 new_code.push_back(0x20000 | OP_BRANCH);
387 new_code.push_back(flags[true_id] ? true_id : false_id);
390 /* Skip the branch instruction when it's encountered on the
400 for(unsigned i=0; i<word_count; ++i)
401 new_code.push_back(*(op+i));
407 SpirVModule *spec_mod = new SpirVModule;
408 spec_mod->code = move(new_code);
415 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
417 vector<uint8_t> visited(blocks.size(), 4);
418 for(unsigned i=0; i<blocks.size(); ++i)
420 const InstructionBlock &b = blocks[i];
425 cond = b.condition->i_value;
426 auto j = spec_values.find(b.condition->constant_id);
427 if(j!=spec_values.end())
429 if(b.negate_condition)
433 visited[i] |= cond*2;
434 for(const InstructionBlock *s: b.successors)
435 visited[s-blocks.data()] &= 3;
438 for(unsigned i=0; i<blocks.size(); ++i)
440 collect_visited_blocks(i, visited);
442 vector<const SpirVModule::InstructionBlock *> result;
443 for(unsigned i=0; i<blocks.size(); ++i)
445 result.push_back(&blocks[i]);
450 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
453 for(const InstructionBlock *s: blocks[i].successors)
455 unsigned j = s-blocks.data();
456 if((visited[j]&3)==2)
457 collect_visited_blocks(j, visited);
462 bool SpirVModule::Variable::operator==(const Variable &other) const
464 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
466 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
468 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
474 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
479 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
484 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
487 for(; op!=op_end; ++op)
490 for(unsigned i=0; i<4; ++i)
503 throw invalid_module("Unterminated SPIR-V string literal");
506 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
508 for(CodeIterator op=code.begin()+5; op!=code.end(); )
510 unsigned word_count = *op>>16;
511 if(word_count>static_cast<unsigned>(code.end()-op))
512 throw invalid_module("Truncated SPIR-V instruction");
514 switch(get_opcode(*op))
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:
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;
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;
552 void SpirVModule::Reflection::reflect_name(CodeIterator op)
554 CodeIterator op_end = get_op_end(op);
555 string &name = names[*(op+1)];
557 name = read_string(op, op_end);
560 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
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);
568 strct.members[index].name = read_string(op, op_end);
571 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
573 CodeIterator op_end = get_op_end(op);
574 unsigned id = *(op+2);
575 EntryPoint &entry = entry_points[id];
577 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
579 entry.name = read_string(op, op_end);
581 entry.globals.reserve(op_end-op);
582 for(; op!=op_end; ++op)
583 entry.globals.push_back(&variables[*op]);
586 void SpirVModule::Reflection::reflect_execution_mode(CodeIterator op)
588 EntryPoint &entry = entry_points[*(op+1)];
589 unsigned mode = *(op+2);
590 if(mode==EXEC_LOCAL_SIZE)
592 entry.compute_local_size.x = *(op+3);
593 entry.compute_local_size.y = *(op+4);
594 entry.compute_local_size.z = *(op+5);
598 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
600 types[*(op+1)].type = VOID;
603 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
605 types[*(op+1)].type = BOOL;
608 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
610 TypeInfo &type = types[*(op+1)];
611 unsigned size = *(op+2);
613 type.type = static_cast<DataType>(size/8 | sign*0x100);
616 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
618 TypeInfo &type = types[*(op+1)];
619 unsigned size = *(op+2);
620 type.type = static_cast<DataType>(size/8 | 0x300);
623 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
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));
631 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
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));
639 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
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);
651 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
653 TypeInfo &type = types[*(op+1)];
654 DataType image = types[*(op+2)].type;
655 type.type = static_cast<DataType>(image | 0x100000);
658 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
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;
665 const Constant &size = constants[*(op+3)];
666 if(size.type==INT || size.type==UNSIGNED_INT)
667 type.array_size = size.i_value;
670 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
672 CodeIterator op_end = get_op_end(op);
673 unsigned id = *(op+1);
674 Structure &strct = structs[id];
675 strct.name = names[id];
677 types[id].struct_type = &strct;
680 strct.members.resize(op_end-op);
681 auto mem = strct.members.begin();
682 for(; op!=op_end; ++op, ++mem)
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;
692 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
694 TypeInfo &type = types[*(op+1)];
695 type = types[*(op+3)];
696 type.storage = static_cast<StorageClass>(*(op+2));
699 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
701 unsigned opcode = get_opcode(*op);
702 unsigned id = *(op+2);
703 Constant &cnst = constants[id];
704 cnst.name = names[id];
706 cnst.type = types[*(op+1)].type;
707 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_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));
717 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
719 unsigned id = *(op+2);
720 Variable &var = variables[id];
721 var.name = names[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;
730 void SpirVModule::Reflection::reflect_access(CodeIterator op)
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())
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);
745 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
747 access_chain_bases[*(op+2)] = *(op+3);
750 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
752 unsigned id = *(op+1);
753 unsigned decoration = *(op+2);
759 constants[id].constant_id = *op;
761 case DECO_ARRAY_STRIDE:
762 types[id].array_stride = *op;
765 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
768 variables[id].location = *op;
771 variables[id].binding = *op;
773 case DECO_DESCRIPTOR_SET:
774 variables[id].descriptor_set = *op;
779 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
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);
788 StructMember &member = strct.members[index];
791 case DECO_MATRIX_STRIDE:
792 member.matrix_stride = *op;
795 member.builtin = static_cast<BuiltinSemantic>(*op);
803 void SpirVModule::Reflection::reflect_label(CodeIterator op)
805 unsigned id = *(op+1);
806 current_block = &blocks[id];
807 current_block->id = id;
810 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
812 InstructionBlock &block = blocks[*(op+1)];
813 block.condition = &true_condition;
814 current_block->successors.push_back(&block);
817 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
819 InstructionBlock &true_block = blocks[*(op+2)];
820 InstructionBlock &false_block = blocks[*(op+3)];
822 auto i = constants.find(*(op+1));
823 if(i!=constants.end() && i->second.constant_id)
825 if(!true_block.condition)
826 true_block.condition = &i->second;
827 if(!false_block.condition)
829 false_block.condition = &i->second;
830 false_block.negate_condition = true;
834 current_block->successors.push_back(&true_block);
835 current_block->successors.push_back(&false_block);