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,
43 OP_SELECTION_MERGE = 247,
46 OP_BRANCH_CONDITIONAL = 250,
50 OP_RETURN_VALUE = 254,
56 DECO_ARRAY_STRIDE = 6,
57 DECO_MATRIX_STRIDE = 7,
61 DECO_DESCRIPTOR_SET = 34,
68 void Module::set_source(const string &src)
70 SL::Compiler compiler(create_features());
71 compiler.set_source(src);
75 void Module::load_source(IO::Base &io, Resources *res, const string &name)
77 SL::Compiler compiler(create_features());
78 compiler.load_source(io, res, name);
82 void Module::load_source(IO::Base &io, const string &name)
84 load_source(io, 0, name);
87 SL::Features Module::create_features() const
89 const DeviceInfo &dev_info = Device::get_current().get_info();
90 const SL::Features &device_features = dev_info.glsl_features;
91 SL::Features latest_features = SL::Features::latest(dev_info.api);
92 SL::Features features;
93 features.target_api = latest_features.target_api;
94 features.glsl_version = latest_features.glsl_version;
95 features.constant_id_range = device_features.constant_id_range;
96 features.uniform_binding_range = device_features.uniform_binding_range;
97 features.texture_binding_range = device_features.texture_binding_range;
102 void GlslModule::compile(SL::Compiler &compiler)
104 compiler.compile(SL::Compiler::MODULE);
105 prepared_source = compiler.get_combined_glsl();
106 source_map = compiler.get_source_map();
109 string diagnostics = compiler.get_diagnostics();
110 if(!diagnostics.empty())
111 IO::print("Module diagnostics:\n%s\n", diagnostics);
116 void SpirVModule::load_code(IO::Base &io)
118 uint32_t buffer[1024];
121 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
125 code.reserve(code.size()+len);
126 code.insert(code.end(), buffer, buffer+len);
133 void SpirVModule::compile(SL::Compiler &compiler)
135 compiler.compile(SL::Compiler::SPIRV);
136 code = compiler.get_combined_spirv();
141 void SpirVModule::reflect()
144 throw invalid_module("Empty SPIR-V code");
146 if(code[0]==SPIRV_MAGIC_REVERSED)
148 for(uint32_t &c: code)
149 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
151 else if(code[0]!=SPIRV_MAGIC)
152 throw invalid_module("SPIR-V magic number not found");
154 Reflection reflection;
155 reflection.reflect_code(code);
157 map<const Constant *, unsigned> spec_indices;
158 for(const auto &kvp: reflection.constants)
159 if(kvp.second.constant_id>=0)
161 spec_indices[&kvp.second] = spec_constants.size();
162 spec_constants.push_back(kvp.second);
165 map<const Structure *, unsigned> struct_indices;
166 structs.reserve(reflection.structs.size());
167 for(const auto &kvp: reflection.structs)
169 struct_indices[&kvp.second] = structs.size();
170 structs.push_back(kvp.second);
173 for(Structure &s: structs)
175 for(StructMember &m: s.members)
178 auto i = struct_indices.find(m.struct_type);
179 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
182 const StructMember *last_member = &s.members.back();
183 unsigned last_offset = last_member->offset;
184 while(last_member->struct_type)
186 const StructMember *lm = &last_member->struct_type->members.back();
187 if(last_member->array_size)
188 last_offset += last_member->array_stride*(last_member->array_size-1);
189 last_offset += lm->offset;
193 unsigned last_size = get_type_size(last_member->type);
194 if(last_member->array_size)
195 last_size += last_member->array_stride*(last_member->array_size-1);
196 s.size = last_offset+last_size;
197 s.size = (s.size+15)&~15;
200 map<const Variable *, unsigned> var_indices;
201 variables.reserve(reflection.variables.size());
202 for(const auto &kvp: reflection.variables)
204 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
205 if(i!=variables.end())
206 var_indices[&kvp.second] = i-variables.begin();
209 var_indices[&kvp.second] = variables.size();
210 variables.push_back(kvp.second);
214 for(Variable &v: variables)
217 auto i = struct_indices.find(v.struct_type);
218 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
221 entry_points.reserve(reflection.entry_points.size());
222 for(const auto &kvp: reflection.entry_points)
224 entry_points.push_back(kvp.second);
225 EntryPoint &entry = entry_points.back();
226 for(const Variable *&v: entry.globals)
228 auto i = var_indices.find(v);
229 v = (i!=var_indices.end() ? &variables[i->second] : 0);
233 map<const InstructionBlock *, unsigned> block_indices;
234 blocks.reserve(reflection.blocks.size());
235 for(const auto &kvp: reflection.blocks)
237 block_indices[&kvp.second] = blocks.size();
238 blocks.push_back(kvp.second);
241 for(InstructionBlock &b: blocks)
243 auto i = spec_indices.find(b.condition);
244 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
246 specializable = true;
248 for(const Variable *&v: b.accessed_variables)
250 auto j = var_indices.find(v);
251 v = (j!=var_indices.end() ? &variables[j->second] : 0);
254 for(const InstructionBlock *&s: b.successors)
256 auto j = block_indices.find(s);
257 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
262 SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
264 vector<uint8_t> flags(code[3], 1);
266 std::map<unsigned, int> spec_values_by_id;
267 for(const Constant &c: spec_constants)
269 auto i = spec_values.find(c.name);
270 if(i!=spec_values.end())
272 flags[c.id] = (i->second ? 5 : 3);
273 spec_values_by_id[c.constant_id] = i->second;
277 for(const EntryPoint &e: entry_points)
279 for(const Variable &v: variables)
281 for(const InstructionBlock &b: blocks)
283 for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
286 for(const Variable *v: b->accessed_variables)
290 std::vector<uint32_t> new_code;
291 new_code.reserve(code.size());
293 auto op = code.begin()+5;
294 new_code.insert(new_code.begin(), code.begin(), op);
296 bool skip_block = false;
297 while(op!=code.end())
299 unsigned word_count = *op>>16;
300 unsigned opcode = *op&0xFFFF;
302 bool copy = !skip_block;
305 skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
306 opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
311 copy = flags[*(op+1)];
312 else if(opcode==OP_ENTRY_POINT)
314 unsigned start = new_code.size();
315 new_code.push_back(opcode);
316 new_code.push_back(*(op+1));
317 unsigned func_id = *(op+2);
318 new_code.push_back(func_id);
323 unsigned word = *(op+i++);
324 new_code.push_back(word);
325 // Strings are nul-terminated and nul-padded
330 unsigned var_count = 0;
331 for(; i<word_count; ++i)
333 unsigned id = *(op+i);
337 new_code.push_back(id);
344 new_code[start] |= (new_code.size()-start)<<16;
347 new_code.resize(start);
351 else if(opcode==OP_EXECUTION_MODE)
352 copy = flags[*(op+1)];
353 else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
355 unsigned id = *(op+2);
358 new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
359 new_code.push_back(*(op+1));
360 new_code.push_back(id);
365 else if(opcode==OP_VARIABLE)
366 copy = flags[*(op+2)];
367 else if(opcode==OP_DECORATE)
369 unsigned id = *(op+1);
371 if(copy && *(op+2)==DECO_SPEC_ID)
372 copy = !(flags[id]&2);
374 else if(opcode==OP_LABEL)
376 copy = flags[*(op+1)];
379 else if(opcode==OP_SELECTION_MERGE)
381 unsigned next_opcode = *(op+word_count)&0xFFFF;
382 if(next_opcode==OP_BRANCH_CONDITIONAL)
384 unsigned true_id = *(op+word_count+2);
385 unsigned false_id = *(op+word_count+3);
386 if(!flags[true_id] || !flags[false_id])
388 new_code.push_back(0x20000 | OP_BRANCH);
389 new_code.push_back(flags[true_id] ? true_id : false_id);
392 /* Skip the branch instruction when it's encountered on the
398 else if(opcode==OP_PHI)
400 unsigned active_count = 0;
401 unsigned result_id = 0;
402 for(unsigned i=3; i<word_count; i+=2)
411 new_code.push_back(0x40000 | OP_COPY_OBJECT);
412 new_code.push_back(*(op+1));
413 new_code.push_back(*(op+2));
414 new_code.push_back(result_id);
422 for(unsigned i=0; i<word_count; ++i)
423 new_code.push_back(*(op+i));
429 SpirVModule *spec_mod = new SpirVModule;
430 spec_mod->code = move(new_code);
437 vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
439 vector<uint8_t> visited(blocks.size(), 4);
440 for(unsigned i=0; i<blocks.size(); ++i)
442 const InstructionBlock &b = blocks[i];
447 cond = b.condition->i_value;
448 auto j = spec_values.find(b.condition->constant_id);
449 if(j!=spec_values.end())
451 if(b.negate_condition)
455 visited[i] |= cond*2;
456 for(const InstructionBlock *s: b.successors)
457 visited[s-blocks.data()] &= 3;
460 for(unsigned i=0; i<blocks.size(); ++i)
462 collect_visited_blocks(i, visited);
464 vector<const SpirVModule::InstructionBlock *> result;
465 for(unsigned i=0; i<blocks.size(); ++i)
467 result.push_back(&blocks[i]);
472 void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
475 for(const InstructionBlock *s: blocks[i].successors)
477 unsigned j = s-blocks.data();
478 if((visited[j]&3)==2)
479 collect_visited_blocks(j, visited);
484 bool SpirVModule::Variable::operator==(const Variable &other) const
486 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
488 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
490 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
496 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
501 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
506 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
509 for(; op!=op_end; ++op)
512 for(unsigned i=0; i<4; ++i)
525 throw invalid_module("Unterminated SPIR-V string literal");
528 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
530 for(CodeIterator op=code.begin()+5; op!=code.end(); )
532 unsigned word_count = *op>>16;
533 if(word_count>static_cast<unsigned>(code.end()-op))
534 throw invalid_module("Truncated SPIR-V instruction");
536 switch(get_opcode(*op))
538 case OP_NAME: reflect_name(op); break;
539 case OP_MEMBER_NAME: reflect_member_name(op); break;
540 case OP_ENTRY_POINT: reflect_entry_point(op); break;
541 case OP_EXECUTION_MODE: reflect_execution_mode(op); break;
542 case OP_TYPE_VOID: reflect_void_type(op); break;
543 case OP_TYPE_BOOL: reflect_bool_type(op); break;
544 case OP_TYPE_INT: reflect_int_type(op); break;
545 case OP_TYPE_FLOAT: reflect_float_type(op); break;
546 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
547 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
548 case OP_TYPE_IMAGE: reflect_image_type(op); break;
549 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
550 case OP_TYPE_ARRAY: reflect_array_type(op); break;
551 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
552 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
553 case OP_CONSTANT_TRUE:
554 case OP_CONSTANT_FALSE:
556 case OP_SPEC_CONSTANT_TRUE:
557 case OP_SPEC_CONSTANT_FALSE:
558 case OP_SPEC_CONSTANT: reflect_constant(op); break;
559 case OP_VARIABLE: reflect_variable(op); break;
561 case OP_STORE: reflect_access(op); break;
562 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
563 case OP_DECORATE: reflect_decorate(op); break;
564 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
565 case OP_LABEL: reflect_label(op); break;
566 case OP_BRANCH: reflect_branch(op); break;
567 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
574 void SpirVModule::Reflection::reflect_name(CodeIterator op)
576 CodeIterator op_end = get_op_end(op);
577 string &name = names[*(op+1)];
579 name = read_string(op, op_end);
582 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
584 CodeIterator op_end = get_op_end(op);
585 Structure &strct = structs[*(op+1)];
586 unsigned index = *(op+2);
587 if(index>=strct.members.size())
588 strct.members.resize(index+1);
590 strct.members[index].name = read_string(op, op_end);
593 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
595 CodeIterator op_end = get_op_end(op);
596 unsigned id = *(op+2);
597 EntryPoint &entry = entry_points[id];
599 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
601 entry.name = read_string(op, op_end);
603 entry.globals.reserve(op_end-op);
604 for(; op!=op_end; ++op)
605 entry.globals.push_back(&variables[*op]);
608 void SpirVModule::Reflection::reflect_execution_mode(CodeIterator op)
610 EntryPoint &entry = entry_points[*(op+1)];
611 unsigned mode = *(op+2);
612 if(mode==EXEC_LOCAL_SIZE)
614 entry.compute_local_size.x = *(op+3);
615 entry.compute_local_size.y = *(op+4);
616 entry.compute_local_size.z = *(op+5);
620 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
622 types[*(op+1)].type = VOID;
625 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
627 types[*(op+1)].type = BOOL;
630 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
632 TypeInfo &type = types[*(op+1)];
633 unsigned size = *(op+2);
635 type.type = static_cast<DataType>(size/8 | sign*0x100);
638 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
640 TypeInfo &type = types[*(op+1)];
641 unsigned size = *(op+2);
642 type.type = static_cast<DataType>(size/8 | 0x300);
645 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
647 TypeInfo &type = types[*(op+1)];
648 DataType component = types[*(op+2)].type;
649 unsigned count = *(op+3);
650 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
653 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
655 TypeInfo &type = types[*(op+1)];
656 DataType column = types[*(op+2)].type;
657 unsigned count = *(op+3);
658 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
661 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
663 TypeInfo &type = types[*(op+1)];
664 DataType sample_type = types[*(op+2)].type;
665 unsigned dimensions = *(op+3);
666 bool depth = *(op+4)==1;
667 bool array = *(op+5);
668 bool sampled = *(op+7)==1;
669 type.type = static_cast<DataType>((depth*0x200000) | (sampled*0x100000) | (array*0x80000) |
670 ((dimensions+1)<<16) | sample_type);
673 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
675 TypeInfo &type = types[*(op+1)];
676 DataType image = types[*(op+2)].type;
677 type.type = static_cast<DataType>(image | 0x100000);
680 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
682 TypeInfo &type = types[*(op+1)];
683 const TypeInfo &elem = types[*(op+2)];
684 type.type = elem.type;
685 type.struct_type = elem.struct_type;
687 const Constant &size = constants[*(op+3)];
688 if(size.type==INT || size.type==UNSIGNED_INT)
689 type.array_size = size.i_value;
692 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
694 CodeIterator op_end = get_op_end(op);
695 unsigned id = *(op+1);
696 Structure &strct = structs[id];
697 strct.name = names[id];
699 types[id].struct_type = &strct;
702 strct.members.resize(op_end-op);
703 auto mem = strct.members.begin();
704 for(; op!=op_end; ++op, ++mem)
706 TypeInfo &type = types[*op];
707 mem->type = type.type;
708 mem->struct_type = type.struct_type;
709 mem->array_size = type.array_size;
710 mem->array_stride = type.array_stride;
714 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
716 TypeInfo &type = types[*(op+1)];
717 type = types[*(op+3)];
718 type.storage = static_cast<StorageClass>(*(op+2));
721 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
723 unsigned opcode = get_opcode(*op);
724 unsigned id = *(op+2);
725 Constant &cnst = constants[id];
726 cnst.name = names[id];
728 cnst.type = types[*(op+1)].type;
729 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
731 else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
732 cnst.i_value = false;
733 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
734 cnst.i_value = *(op+3);
735 else if(cnst.type==FLOAT)
736 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
739 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
741 unsigned id = *(op+2);
742 Variable &var = variables[id];
743 var.name = names[id];
745 const TypeInfo &type = types[*(op+1)];
746 var.storage = type.storage;
747 var.type = type.type;
748 var.struct_type = type.struct_type;
749 var.array_size = type.array_size;
752 void SpirVModule::Reflection::reflect_access(CodeIterator op)
756 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
757 auto i = access_chain_bases.find(id);
758 if(i!=access_chain_bases.end())
760 Variable &var = variables[id];
761 auto j = find(current_block->accessed_variables, &var);
762 if(j==current_block->accessed_variables.end())
763 current_block->accessed_variables.push_back(&var);
767 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
769 access_chain_bases[*(op+2)] = *(op+3);
772 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
774 unsigned id = *(op+1);
775 unsigned decoration = *(op+2);
781 constants[id].constant_id = *op;
783 case DECO_ARRAY_STRIDE:
784 types[id].array_stride = *op;
787 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
790 variables[id].location = *op;
793 variables[id].binding = *op;
795 case DECO_DESCRIPTOR_SET:
796 variables[id].descriptor_set = *op;
801 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
803 Structure &strct = structs[*(op+1)];
804 unsigned index = *(op+2);
805 if(index>=strct.members.size())
806 strct.members.resize(index+1);
807 unsigned decoration = *(op+3);
810 StructMember &member = strct.members[index];
813 case DECO_MATRIX_STRIDE:
814 member.matrix_stride = *op;
817 member.builtin = static_cast<BuiltinSemantic>(*op);
825 void SpirVModule::Reflection::reflect_label(CodeIterator op)
827 unsigned id = *(op+1);
828 current_block = &blocks[id];
829 current_block->id = id;
832 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
834 InstructionBlock &block = blocks[*(op+1)];
835 block.condition = &true_condition;
836 current_block->successors.push_back(&block);
839 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
841 InstructionBlock &true_block = blocks[*(op+2)];
842 InstructionBlock &false_block = blocks[*(op+3)];
844 auto i = constants.find(*(op+1));
845 if(i!=constants.end() && i->second.constant_id)
847 if(!true_block.condition)
848 true_block.condition = &i->second;
849 if(!false_block.condition)
851 false_block.condition = &i->second;
852 false_block.negate_condition = true;
856 current_block->successors.push_back(&true_block);
857 current_block->successors.push_back(&false_block);