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,
52 DECO_ARRAY_STRIDE = 6,
53 DECO_MATRIX_STRIDE = 7,
57 DECO_DESCRIPTOR_SET = 34,
64 void Module::set_source(const string &src)
66 SL::Compiler compiler(create_features());
67 compiler.set_source(src);
71 void Module::load_source(IO::Base &io, Resources *res, const string &name)
73 SL::Compiler compiler(create_features());
74 compiler.load_source(io, res, name);
78 void Module::load_source(IO::Base &io, const string &name)
80 load_source(io, 0, name);
83 SL::Features Module::create_features() const
85 const DeviceInfo &dev_info = Device::get_current().get_info();
86 const SL::Features &device_features = dev_info.glsl_features;
87 SL::Features latest_features = SL::Features::latest(dev_info.api);
88 SL::Features features;
89 features.target_api = latest_features.target_api;
90 features.glsl_version = latest_features.glsl_version;
91 features.constant_id_range = device_features.constant_id_range;
92 features.uniform_binding_range = device_features.uniform_binding_range;
93 features.texture_binding_range = device_features.texture_binding_range;
98 void GlslModule::compile(SL::Compiler &compiler)
100 compiler.compile(SL::Compiler::MODULE);
101 prepared_source = compiler.get_combined_glsl();
102 source_map = compiler.get_source_map();
105 string diagnostics = compiler.get_diagnostics();
106 if(!diagnostics.empty())
107 IO::print("Module diagnostics:\n%s\n", diagnostics);
112 void SpirVModule::load_code(IO::Base &io)
114 uint32_t buffer[1024];
117 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
121 code.reserve(code.size()+len);
122 code.insert(code.end(), buffer, buffer+len);
129 void SpirVModule::compile(SL::Compiler &compiler)
131 compiler.compile(SL::Compiler::SPIRV);
132 code = compiler.get_combined_spirv();
137 void SpirVModule::reflect()
140 throw invalid_module("Empty SPIR-V code");
142 if(code[0]==SPIRV_MAGIC_REVERSED)
144 for(uint32_t &c: code)
145 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
147 else if(code[0]!=SPIRV_MAGIC)
148 throw invalid_module("SPIR-V magic number not found");
150 Reflection reflection;
151 reflection.reflect_code(code);
153 map<const Constant *, unsigned> spec_indices;
154 for(const auto &kvp: reflection.constants)
155 if(kvp.second.constant_id>=0)
157 spec_indices[&kvp.second] = spec_constants.size();
158 spec_constants.push_back(kvp.second);
161 map<const Structure *, unsigned> struct_indices;
162 structs.reserve(reflection.structs.size());
163 for(const auto &kvp: reflection.structs)
165 struct_indices[&kvp.second] = structs.size();
166 structs.push_back(kvp.second);
169 for(Structure &s: structs)
171 for(StructMember &m: s.members)
174 auto i = struct_indices.find(m.struct_type);
175 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
178 const StructMember *last_member = &s.members.back();
179 unsigned last_offset = last_member->offset;
180 while(last_member->struct_type)
182 const StructMember *lm = &last_member->struct_type->members.back();
183 if(last_member->array_size)
184 last_offset += last_member->array_stride*(last_member->array_size-1);
185 last_offset += lm->offset;
189 unsigned last_size = get_type_size(last_member->type);
190 if(last_member->array_size)
191 last_size += last_member->array_stride*(last_member->array_size-1);
192 s.size = last_offset+last_size;
193 s.size = (s.size+15)&~15;
196 map<const Variable *, unsigned> var_indices;
197 variables.reserve(reflection.variables.size());
198 for(const auto &kvp: reflection.variables)
200 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
201 if(i!=variables.end())
202 var_indices[&kvp.second] = i-variables.begin();
205 var_indices[&kvp.second] = variables.size();
206 variables.push_back(kvp.second);
210 for(Variable &v: variables)
213 auto i = struct_indices.find(v.struct_type);
214 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
217 entry_points.reserve(reflection.entry_points.size());
218 for(const auto &kvp: reflection.entry_points)
220 entry_points.push_back(kvp.second);
221 EntryPoint &entry = entry_points.back();
222 for(const Variable *&v: entry.globals)
224 auto i = var_indices.find(v);
225 v = (i!=var_indices.end() ? &variables[i->second] : 0);
229 map<const InstructionBlock *, unsigned> block_indices;
230 blocks.reserve(reflection.blocks.size());
231 for(const auto &kvp: reflection.blocks)
233 block_indices[&kvp.second] = blocks.size();
234 blocks.push_back(kvp.second);
237 for(InstructionBlock &b: blocks)
239 auto i = spec_indices.find(b.condition);
240 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
242 specializable = true;
244 for(const Variable *&v: b.accessed_variables)
246 auto j = var_indices.find(v);
247 v = (j!=var_indices.end() ? &variables[j->second] : 0);
250 for(const InstructionBlock *&s: b.successors)
252 auto j = block_indices.find(s);
253 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
258 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
260 vector<uint8_t> flags(code[3], 1);
262 std::map<unsigned, int> spec_values_by_id;
263 for(const Constant &c: spec_constants)
265 auto i = spec_values.find(c.name);
266 if(i!=spec_values.end())
268 flags[c.id] = (i->second ? 5 : 3);
269 spec_values_by_id[c.constant_id] = i->second;
273 for(const EntryPoint &e: entry_points)
275 for(const Variable &v: variables)
277 for(const InstructionBlock &b: blocks)
279 for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
282 for(const Variable *v: b->accessed_variables)
286 std::vector<uint32_t> new_code;
287 new_code.reserve(code.size());
289 auto op = code.begin()+5;
290 new_code.insert(new_code.begin(), code.begin(), op);
292 bool skip_block = false;
293 while(op!=code.end())
295 unsigned word_count = *op>>16;
296 unsigned opcode = *op&0xFFFF;
298 bool copy = !skip_block;
301 skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
302 opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
307 copy = flags[*(op+1)];
308 else if(opcode==OP_ENTRY_POINT)
310 unsigned start = new_code.size();
311 new_code.push_back(opcode);
312 new_code.push_back(*(op+1));
313 unsigned func_id = *(op+2);
314 new_code.push_back(func_id);
319 unsigned word = *(op+i++);
320 new_code.push_back(word);
321 if(!(word&(word>>8)&(word>>16)&(word>>24)&0xFF))
325 unsigned var_count = 0;
326 for(; i<word_count; ++i)
328 unsigned id = *(op+i);
332 new_code.push_back(id);
339 new_code[start] |= (new_code.size()-start)<<16;
342 new_code.resize(start);
346 else if(opcode==OP_EXECUTION_MODE)
347 copy = flags[*(op+1)];
348 else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
350 unsigned id = *(op+2);
353 new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
354 new_code.push_back(*(op+1));
355 new_code.push_back(id);
360 else if(opcode==OP_VARIABLE)
361 copy = flags[*(op+2)];
362 else if(opcode==OP_DECORATE)
364 unsigned id = *(op+1);
366 if(copy && *(op+2)==DECO_SPEC_ID)
367 copy = !(flags[id]&2);
369 else if(opcode==OP_LABEL)
371 copy = flags[*(op+1)];
374 else if(opcode==OP_SELECTION_MERGE)
376 unsigned next_opcode = *(op+word_count)&0xFFFF;
377 if(next_opcode==OP_BRANCH_CONDITIONAL)
379 unsigned true_id = *(op+word_count+2);
380 unsigned false_id = *(op+word_count+3);
381 if(!flags[true_id] || !flags[false_id])
383 new_code.push_back(0x20000 | OP_BRANCH);
384 new_code.push_back(flags[true_id] ? true_id : false_id);
387 /* Skip the branch instruction when it's encountered on the
397 for(unsigned i=0; i<word_count; ++i)
398 new_code.push_back(*(op+i));
404 SpirVModule *spec_mod = new SpirVModule;
405 spec_mod->code = move(new_code);
412 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
414 vector<uint8_t> visited(blocks.size(), 4);
415 for(unsigned i=0; i<blocks.size(); ++i)
417 const InstructionBlock &b = blocks[i];
422 cond = b.condition->i_value;
423 auto j = spec_values.find(b.condition->constant_id);
424 if(j!=spec_values.end())
426 if(b.negate_condition)
430 visited[i] |= cond*2;
431 for(const InstructionBlock *s: b.successors)
432 visited[s-blocks.data()] &= 3;
435 for(unsigned i=0; i<blocks.size(); ++i)
437 collect_visited_blocks(i, visited);
439 vector<const SpirVModule::InstructionBlock *> result;
440 for(unsigned i=0; i<blocks.size(); ++i)
442 result.push_back(&blocks[i]);
447 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
450 for(const InstructionBlock *s: blocks[i].successors)
452 unsigned j = s-blocks.data();
453 if((visited[j]&3)==2)
454 collect_visited_blocks(j, visited);
459 bool SpirVModule::Variable::operator==(const Variable &other) const
461 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
463 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
465 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
471 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
476 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
481 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
484 for(; op!=op_end; ++op)
487 for(unsigned i=0; i<4; ++i)
500 throw invalid_module("Unterminated SPIR-V string literal");
503 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
505 for(CodeIterator op=code.begin()+5; op!=code.end(); )
507 unsigned word_count = *op>>16;
508 if(word_count>static_cast<unsigned>(code.end()-op))
509 throw invalid_module("Truncated SPIR-V instruction");
511 switch(get_opcode(*op))
513 case OP_NAME: reflect_name(op); break;
514 case OP_MEMBER_NAME: reflect_member_name(op); break;
515 case OP_ENTRY_POINT: reflect_entry_point(op); break;
516 case OP_TYPE_VOID: reflect_void_type(op); break;
517 case OP_TYPE_BOOL: reflect_bool_type(op); break;
518 case OP_TYPE_INT: reflect_int_type(op); break;
519 case OP_TYPE_FLOAT: reflect_float_type(op); break;
520 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
521 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
522 case OP_TYPE_IMAGE: reflect_image_type(op); break;
523 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
524 case OP_TYPE_ARRAY: reflect_array_type(op); break;
525 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
526 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
527 case OP_CONSTANT_TRUE:
528 case OP_CONSTANT_FALSE:
530 case OP_SPEC_CONSTANT_TRUE:
531 case OP_SPEC_CONSTANT_FALSE:
532 case OP_SPEC_CONSTANT: reflect_constant(op); break;
533 case OP_VARIABLE: reflect_variable(op); break;
535 case OP_STORE: reflect_access(op); break;
536 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
537 case OP_DECORATE: reflect_decorate(op); break;
538 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
539 case OP_LABEL: reflect_label(op); break;
540 case OP_BRANCH: reflect_branch(op); break;
541 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
548 void SpirVModule::Reflection::reflect_name(CodeIterator op)
550 CodeIterator op_end = get_op_end(op);
551 string &name = names[*(op+1)];
553 name = read_string(op, op_end);
556 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
558 CodeIterator op_end = get_op_end(op);
559 Structure &strct = structs[*(op+1)];
560 unsigned index = *(op+2);
561 if(index>=strct.members.size())
562 strct.members.resize(index+1);
564 strct.members[index].name = read_string(op, op_end);
567 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
569 CodeIterator op_end = get_op_end(op);
570 unsigned id = *(op+2);
571 EntryPoint &entry = entry_points[id];
573 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
575 entry.name = read_string(op, op_end);
577 entry.globals.reserve(op_end-op);
578 for(; op!=op_end; ++op)
579 entry.globals.push_back(&variables[*op]);
582 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
584 types[*(op+1)].type = VOID;
587 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
589 types[*(op+1)].type = BOOL;
592 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
594 TypeInfo &type = types[*(op+1)];
595 unsigned size = *(op+2);
597 type.type = static_cast<DataType>(size/8 | sign*0x100);
600 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
602 TypeInfo &type = types[*(op+1)];
603 unsigned size = *(op+2);
604 type.type = static_cast<DataType>(size/8 | 0x300);
607 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
609 TypeInfo &type = types[*(op+1)];
610 DataType component = types[*(op+2)].type;
611 unsigned count = *(op+3);
612 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
615 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
617 TypeInfo &type = types[*(op+1)];
618 DataType column = types[*(op+2)].type;
619 unsigned count = *(op+3);
620 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
623 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
625 TypeInfo &type = types[*(op+1)];
626 DataType sample_type = types[*(op+2)].type;
627 unsigned dimensions = *(op+3);
628 bool depth = *(op+4)==1;
629 bool array = *(op+5);
630 bool sampled = *(op+7)==1;
631 type.type = static_cast<DataType>((depth*0x200000) | (sampled*0x100000) | (array*0x80000) |
632 ((dimensions+1)<<16) | sample_type);
635 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
637 TypeInfo &type = types[*(op+1)];
638 DataType image = types[*(op+2)].type;
639 type.type = static_cast<DataType>(image | 0x100000);
642 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
644 TypeInfo &type = types[*(op+1)];
645 const TypeInfo &elem = types[*(op+2)];
646 type.type = elem.type;
647 type.struct_type = elem.struct_type;
649 const Constant &size = constants[*(op+3)];
650 if(size.type==INT || size.type==UNSIGNED_INT)
651 type.array_size = size.i_value;
654 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
656 CodeIterator op_end = get_op_end(op);
657 unsigned id = *(op+1);
658 Structure &strct = structs[id];
659 strct.name = names[id];
661 types[id].struct_type = &strct;
664 strct.members.resize(op_end-op);
665 auto mem = strct.members.begin();
666 for(; op!=op_end; ++op, ++mem)
668 TypeInfo &type = types[*op];
669 mem->type = type.type;
670 mem->struct_type = type.struct_type;
671 mem->array_size = type.array_size;
672 mem->array_stride = type.array_stride;
676 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
678 TypeInfo &type = types[*(op+1)];
679 type = types[*(op+3)];
680 type.storage = static_cast<StorageClass>(*(op+2));
683 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
685 unsigned opcode = get_opcode(*op);
686 unsigned id = *(op+2);
687 Constant &cnst = constants[id];
688 cnst.name = names[id];
690 cnst.type = types[*(op+1)].type;
691 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
693 else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
694 cnst.i_value = false;
695 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
696 cnst.i_value = *(op+3);
697 else if(cnst.type==FLOAT)
698 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
701 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
703 unsigned id = *(op+2);
704 Variable &var = variables[id];
705 var.name = names[id];
707 const TypeInfo &type = types[*(op+1)];
708 var.storage = type.storage;
709 var.type = type.type;
710 var.struct_type = type.struct_type;
711 var.array_size = type.array_size;
714 void SpirVModule::Reflection::reflect_access(CodeIterator op)
718 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
719 auto i = access_chain_bases.find(id);
720 if(i!=access_chain_bases.end())
722 Variable &var = variables[id];
723 auto j = find(current_block->accessed_variables, &var);
724 if(j==current_block->accessed_variables.end())
725 current_block->accessed_variables.push_back(&var);
729 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
731 access_chain_bases[*(op+2)] = *(op+3);
734 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
736 unsigned id = *(op+1);
737 unsigned decoration = *(op+2);
743 constants[id].constant_id = *op;
745 case DECO_ARRAY_STRIDE:
746 types[id].array_stride = *op;
749 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
752 variables[id].location = *op;
755 variables[id].binding = *op;
757 case DECO_DESCRIPTOR_SET:
758 variables[id].descriptor_set = *op;
763 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
765 Structure &strct = structs[*(op+1)];
766 unsigned index = *(op+2);
767 if(index>=strct.members.size())
768 strct.members.resize(index+1);
769 unsigned decoration = *(op+3);
772 StructMember &member = strct.members[index];
775 case DECO_MATRIX_STRIDE:
776 member.matrix_stride = *op;
779 member.builtin = static_cast<BuiltinSemantic>(*op);
787 void SpirVModule::Reflection::reflect_label(CodeIterator op)
789 unsigned id = *(op+1);
790 current_block = &blocks[id];
791 current_block->id = id;
794 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
796 InstructionBlock &block = blocks[*(op+1)];
797 block.condition = &true_condition;
798 current_block->successors.push_back(&block);
801 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
803 InstructionBlock &true_block = blocks[*(op+2)];
804 InstructionBlock &false_block = blocks[*(op+3)];
806 auto i = constants.find(*(op+1));
807 if(i!=constants.end() && i->second.constant_id)
809 if(!true_block.condition)
810 true_block.condition = &i->second;
811 if(!false_block.condition)
813 false_block.condition = &i->second;
814 false_block.negate_condition = true;
818 current_block->successors.push_back(&true_block);
819 current_block->successors.push_back(&false_block);