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 for(const Variable *&v: b.accessed_variables)
243 auto j = var_indices.find(v);
244 v = (j!=var_indices.end() ? &variables[j->second] : 0);
247 for(const InstructionBlock *&s: b.successors)
249 auto j = block_indices.find(s);
250 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
255 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
257 vector<uint8_t> flags(code[3], 1);
259 std::map<unsigned, int> spec_values_by_id;
260 for(const Constant &c: spec_constants)
262 auto i = spec_values.find(c.name);
263 if(i!=spec_values.end())
265 flags[c.id] = (i->second ? 5 : 3);
266 spec_values_by_id[c.constant_id] = i->second;
270 for(const Variable &v: variables)
272 for(const InstructionBlock &b: blocks)
274 for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
277 for(const Variable *v: b->accessed_variables)
281 std::vector<uint32_t> new_code;
282 new_code.reserve(code.size());
284 auto op = code.begin()+5;
285 new_code.insert(new_code.begin(), code.begin(), op);
287 bool skip_block = false;
288 while(op!=code.end())
290 unsigned word_count = *op>>16;
291 unsigned opcode = *op&0xFFFF;
293 bool copy = !skip_block;
296 skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
297 opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
302 copy = flags[*(op+1)];
303 else if(opcode==OP_ENTRY_POINT)
305 unsigned start = new_code.size();
306 new_code.push_back(opcode);
307 new_code.push_back(*(op+1));
308 new_code.push_back(*(op+2));
313 unsigned word = *(op+i++);
314 new_code.push_back(word);
315 if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
319 for(; i<word_count; ++i)
321 unsigned id = *(op+i);
323 new_code.push_back(id);
326 new_code[start] |= (new_code.size()-start)<<16;
330 else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
332 unsigned id = *(op+2);
335 new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
336 new_code.push_back(*(op+1));
337 new_code.push_back(id);
342 else if(opcode==OP_VARIABLE)
343 copy = flags[*(op+2)];
344 else if(opcode==OP_DECORATE)
346 unsigned id = *(op+1);
348 if(copy && *(op+2)==DECO_SPEC_ID)
349 copy = !(flags[id]&2);
351 else if(opcode==OP_LABEL)
353 copy = flags[*(op+1)];
356 else if(opcode==OP_SELECTION_MERGE)
358 unsigned next_opcode = *(op+word_count)&0xFFFF;
359 if(next_opcode==OP_BRANCH_CONDITIONAL)
361 unsigned true_id = *(op+word_count+2);
362 unsigned false_id = *(op+word_count+3);
363 if(!flags[true_id] || !flags[false_id])
365 new_code.push_back(0x20000 | OP_BRANCH);
366 new_code.push_back(flags[true_id] ? true_id : false_id);
369 /* Skip the branch instruction when it's encountered on the
379 for(unsigned i=0; i<word_count; ++i)
380 new_code.push_back(*(op+i));
386 SpirVModule *spec_mod = new SpirVModule;
387 spec_mod->code = move(new_code);
394 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
396 vector<uint8_t> visited(blocks.size(), 4);
397 for(unsigned i=0; i<blocks.size(); ++i)
399 const InstructionBlock &b = blocks[i];
404 cond = b.condition->i_value;
405 auto j = spec_values.find(b.condition->constant_id);
406 if(j!=spec_values.end())
408 if(b.negate_condition)
412 visited[i] |= cond*2;
413 for(const InstructionBlock *s: b.successors)
414 visited[s-blocks.data()] &= 3;
417 for(unsigned i=0; i<blocks.size(); ++i)
419 collect_visited_blocks(i, visited);
421 vector<const SpirVModule::InstructionBlock *> result;
422 for(unsigned i=0; i<blocks.size(); ++i)
424 result.push_back(&blocks[i]);
429 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
432 for(const InstructionBlock *s: blocks[i].successors)
434 unsigned j = s-blocks.data();
435 if((visited[j]&3)==2)
436 collect_visited_blocks(j, visited);
441 bool SpirVModule::Variable::operator==(const Variable &other) const
443 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
445 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
447 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
453 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
458 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
463 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
466 for(; op!=op_end; ++op)
469 for(unsigned i=0; i<4; ++i)
482 throw invalid_module("Unterminated SPIR-V string literal");
485 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
487 for(CodeIterator op=code.begin()+5; op!=code.end(); )
489 unsigned word_count = *op>>16;
490 if(word_count>static_cast<unsigned>(code.end()-op))
491 throw invalid_module("Truncated SPIR-V instruction");
493 switch(get_opcode(*op))
495 case OP_NAME: reflect_name(op); break;
496 case OP_MEMBER_NAME: reflect_member_name(op); break;
497 case OP_ENTRY_POINT: reflect_entry_point(op); break;
498 case OP_TYPE_VOID: reflect_void_type(op); break;
499 case OP_TYPE_BOOL: reflect_bool_type(op); break;
500 case OP_TYPE_INT: reflect_int_type(op); break;
501 case OP_TYPE_FLOAT: reflect_float_type(op); break;
502 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
503 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
504 case OP_TYPE_IMAGE: reflect_image_type(op); break;
505 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
506 case OP_TYPE_ARRAY: reflect_array_type(op); break;
507 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
508 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
509 case OP_CONSTANT_TRUE:
510 case OP_CONSTANT_FALSE:
512 case OP_SPEC_CONSTANT_TRUE:
513 case OP_SPEC_CONSTANT_FALSE:
514 case OP_SPEC_CONSTANT: reflect_constant(op); break;
515 case OP_VARIABLE: reflect_variable(op); break;
517 case OP_STORE: reflect_access(op); break;
518 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
519 case OP_DECORATE: reflect_decorate(op); break;
520 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
521 case OP_LABEL: reflect_label(op); break;
522 case OP_BRANCH: reflect_branch(op); break;
523 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
530 void SpirVModule::Reflection::reflect_name(CodeIterator op)
532 CodeIterator op_end = get_op_end(op);
533 string &name = names[*(op+1)];
535 name = read_string(op, op_end);
538 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
540 CodeIterator op_end = get_op_end(op);
541 Structure &strct = structs[*(op+1)];
542 unsigned index = *(op+2);
543 if(index>=strct.members.size())
544 strct.members.resize(index+1);
546 strct.members[index].name = read_string(op, op_end);
549 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
551 CodeIterator op_end = get_op_end(op);
552 unsigned id = *(op+2);
553 EntryPoint &entry = entry_points[id];
555 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
557 entry.name = read_string(op, op_end);
559 entry.globals.reserve(op_end-op);
560 for(; op!=op_end; ++op)
561 entry.globals.push_back(&variables[*op]);
564 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
566 types[*(op+1)].type = VOID;
569 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
571 types[*(op+1)].type = BOOL;
574 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
576 TypeInfo &type = types[*(op+1)];
577 unsigned size = *(op+2);
579 type.type = static_cast<DataType>(size/8 | sign*0x100);
582 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
584 TypeInfo &type = types[*(op+1)];
585 unsigned size = *(op+2);
586 type.type = static_cast<DataType>(size/8 | 0x300);
589 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
591 TypeInfo &type = types[*(op+1)];
592 DataType component = types[*(op+2)].type;
593 unsigned count = *(op+3);
594 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
597 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
599 TypeInfo &type = types[*(op+1)];
600 DataType column = types[*(op+2)].type;
601 unsigned count = *(op+3);
602 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
605 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
607 TypeInfo &type = types[*(op+1)];
608 DataType sample = types[*(op+2)].type;
609 unsigned dimensions = *(op+3);
610 bool depth = *(op+4)==1;
611 bool array = *(op+5);
612 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
615 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
617 TypeInfo &type = types[*(op+1)];
618 DataType image = types[*(op+2)].type;
619 type.type = static_cast<DataType>(image | 0x100000);
622 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
624 TypeInfo &type = types[*(op+1)];
625 const TypeInfo &elem = types[*(op+2)];
626 type.type = elem.type;
627 type.struct_type = elem.struct_type;
629 const Constant &size = constants[*(op+3)];
630 if(size.type==INT || size.type==UNSIGNED_INT)
631 type.array_size = size.i_value;
634 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
636 CodeIterator op_end = get_op_end(op);
637 unsigned id = *(op+1);
638 Structure &strct = structs[id];
639 strct.name = names[id];
641 types[id].struct_type = &strct;
644 strct.members.resize(op_end-op);
645 auto mem = strct.members.begin();
646 for(; op!=op_end; ++op, ++mem)
648 TypeInfo &type = types[*op];
649 mem->type = type.type;
650 mem->struct_type = type.struct_type;
651 mem->array_size = type.array_size;
652 mem->array_stride = type.array_stride;
656 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
658 TypeInfo &type = types[*(op+1)];
659 type = types[*(op+3)];
660 type.storage = static_cast<StorageClass>(*(op+2));
663 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
665 unsigned opcode = get_opcode(*op);
666 unsigned id = *(op+2);
667 Constant &cnst = constants[id];
668 cnst.name = names[id];
670 cnst.type = types[*(op+1)].type;
671 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
673 else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
674 cnst.i_value = false;
675 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
676 cnst.i_value = *(op+3);
677 else if(cnst.type==FLOAT)
678 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
681 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
683 unsigned id = *(op+2);
684 Variable &var = variables[id];
685 var.name = names[id];
687 const TypeInfo &type = types[*(op+1)];
688 var.storage = type.storage;
689 var.type = type.type;
690 var.struct_type = type.struct_type;
691 var.array_size = type.array_size;
694 void SpirVModule::Reflection::reflect_access(CodeIterator op)
698 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
699 auto i = access_chain_bases.find(id);
700 if(i!=access_chain_bases.end())
702 Variable &var = variables[id];
703 auto j = find(current_block->accessed_variables, &var);
704 if(j==current_block->accessed_variables.end())
705 current_block->accessed_variables.push_back(&var);
709 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
711 access_chain_bases[*(op+2)] = *(op+3);
714 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
716 unsigned id = *(op+1);
717 unsigned decoration = *(op+2);
723 constants[id].constant_id = *op;
725 case DECO_ARRAY_STRIDE:
726 types[id].array_stride = *op;
729 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
732 variables[id].location = *op;
735 variables[id].binding = *op;
737 case DECO_DESCRIPTOR_SET:
738 variables[id].descriptor_set = *op;
743 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
745 Structure &strct = structs[*(op+1)];
746 unsigned index = *(op+2);
747 if(index>=strct.members.size())
748 strct.members.resize(index+1);
749 unsigned decoration = *(op+3);
752 StructMember &member = strct.members[index];
755 case DECO_MATRIX_STRIDE:
756 member.matrix_stride = *op;
759 member.builtin = static_cast<BuiltinSemantic>(*op);
767 void SpirVModule::Reflection::reflect_label(CodeIterator op)
769 unsigned id = *(op+1);
770 current_block = &blocks[id];
771 current_block->id = id;
774 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
776 InstructionBlock &block = blocks[*(op+1)];
777 block.condition = &true_condition;
778 current_block->successors.push_back(&block);
781 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
783 InstructionBlock &true_block = blocks[*(op+2)];
784 InstructionBlock &false_block = blocks[*(op+3)];
786 auto i = constants.find(*(op+1));
787 if(i!=constants.end() && i->second.constant_id)
789 if(!true_block.condition)
790 true_block.condition = &i->second;
791 if(!false_block.condition)
793 false_block.condition = &i->second;
794 false_block.negate_condition = true;
798 current_block->successors.push_back(&true_block);
799 current_block->successors.push_back(&false_block);