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,
42 OP_BRANCH_CONDITIONAL = 250,
45 DECO_ARRAY_STRIDE = 6,
46 DECO_MATRIX_STRIDE = 7,
50 DECO_DESCRIPTOR_SET = 34,
57 void Module::set_source(const string &src)
59 SL::Compiler compiler(create_features());
60 compiler.set_source(src);
64 void Module::load_source(IO::Base &io, Resources *res, const string &name)
66 SL::Compiler compiler(create_features());
67 compiler.load_source(io, res, name);
71 void Module::load_source(IO::Base &io, const string &name)
73 load_source(io, 0, name);
76 SL::Features Module::create_features() const
78 const DeviceInfo &dev_info = Device::get_current().get_info();
79 const SL::Features &device_features = dev_info.glsl_features;
80 SL::Features latest_features = SL::Features::latest(dev_info.api);
81 SL::Features features;
82 features.target_api = latest_features.target_api;
83 features.glsl_version = latest_features.glsl_version;
84 features.constant_id_range = device_features.constant_id_range;
85 features.uniform_binding_range = device_features.uniform_binding_range;
86 features.texture_binding_range = device_features.texture_binding_range;
91 void GlslModule::compile(SL::Compiler &compiler)
93 compiler.compile(SL::Compiler::MODULE);
94 prepared_source = compiler.get_combined_glsl();
95 source_map = compiler.get_source_map();
98 string diagnostics = compiler.get_diagnostics();
99 if(!diagnostics.empty())
100 IO::print("Module diagnostics:\n%s\n", diagnostics);
105 void SpirVModule::load_code(IO::Base &io)
107 uint32_t buffer[1024];
110 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
114 code.reserve(code.size()+len);
115 code.insert(code.end(), buffer, buffer+len);
122 void SpirVModule::compile(SL::Compiler &compiler)
124 compiler.compile(SL::Compiler::SPIRV);
125 code = compiler.get_combined_spirv();
130 void SpirVModule::reflect()
133 throw invalid_module("Empty SPIR-V code");
135 if(code[0]==SPIRV_MAGIC_REVERSED)
137 for(uint32_t &c: code)
138 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
140 else if(code[0]!=SPIRV_MAGIC)
141 throw invalid_module("SPIR-V magic number not found");
143 Reflection reflection;
144 reflection.reflect_code(code);
146 map<const Constant *, unsigned> spec_indices;
147 for(const auto &kvp: reflection.constants)
148 if(kvp.second.constant_id>=0)
150 spec_indices[&kvp.second] = spec_constants.size();
151 spec_constants.push_back(kvp.second);
154 map<const Structure *, unsigned> struct_indices;
155 structs.reserve(reflection.structs.size());
156 for(const auto &kvp: reflection.structs)
158 struct_indices[&kvp.second] = structs.size();
159 structs.push_back(kvp.second);
162 for(Structure &s: structs)
164 for(StructMember &m: s.members)
167 auto i = struct_indices.find(m.struct_type);
168 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
171 const StructMember *last_member = &s.members.back();
172 unsigned last_offset = last_member->offset;
173 while(last_member->struct_type)
175 const StructMember *lm = &last_member->struct_type->members.back();
176 if(last_member->array_size)
177 last_offset += last_member->array_stride*(last_member->array_size-1);
178 last_offset += lm->offset;
182 unsigned last_size = get_type_size(last_member->type);
183 if(last_member->array_size)
184 last_size += last_member->array_stride*(last_member->array_size-1);
185 s.size = last_offset+last_size;
186 s.size = (s.size+15)&~15;
189 map<const Variable *, unsigned> var_indices;
190 variables.reserve(reflection.variables.size());
191 for(const auto &kvp: reflection.variables)
193 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
194 if(i!=variables.end())
195 var_indices[&kvp.second] = i-variables.begin();
198 var_indices[&kvp.second] = variables.size();
199 variables.push_back(kvp.second);
203 for(Variable &v: variables)
206 auto i = struct_indices.find(v.struct_type);
207 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
210 entry_points.reserve(reflection.entry_points.size());
211 for(const auto &kvp: reflection.entry_points)
213 entry_points.push_back(kvp.second);
214 EntryPoint &entry = entry_points.back();
215 for(const Variable *&v: entry.globals)
217 auto i = var_indices.find(v);
218 v = (i!=var_indices.end() ? &variables[i->second] : 0);
222 map<const InstructionBlock *, unsigned> block_indices;
223 blocks.reserve(reflection.blocks.size());
224 for(const auto &kvp: reflection.blocks)
226 block_indices[&kvp.second] = blocks.size();
227 blocks.push_back(kvp.second);
230 for(InstructionBlock &b: blocks)
232 auto i = spec_indices.find(b.condition);
233 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
235 for(const Variable *&v: b.accessed_variables)
237 auto j = var_indices.find(v);
238 v = (j!=var_indices.end() ? &variables[j->second] : 0);
241 for(const InstructionBlock *&s: b.successors)
243 auto j = block_indices.find(s);
244 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
250 bool SpirVModule::Variable::operator==(const Variable &other) const
252 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
254 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
256 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
262 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
267 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
272 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
275 for(; op!=op_end; ++op)
278 for(unsigned i=0; i<4; ++i)
291 throw invalid_module("Unterminated SPIR-V string literal");
294 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
296 for(CodeIterator op=code.begin()+5; op!=code.end(); )
298 unsigned word_count = *op>>16;
299 if(word_count>static_cast<unsigned>(code.end()-op))
300 throw invalid_module("Truncated SPIR-V instruction");
302 switch(get_opcode(*op))
304 case OP_NAME: reflect_name(op); break;
305 case OP_MEMBER_NAME: reflect_member_name(op); break;
306 case OP_ENTRY_POINT: reflect_entry_point(op); break;
307 case OP_TYPE_VOID: reflect_void_type(op); break;
308 case OP_TYPE_BOOL: reflect_bool_type(op); break;
309 case OP_TYPE_INT: reflect_int_type(op); break;
310 case OP_TYPE_FLOAT: reflect_float_type(op); break;
311 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
312 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
313 case OP_TYPE_IMAGE: reflect_image_type(op); break;
314 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
315 case OP_TYPE_ARRAY: reflect_array_type(op); break;
316 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
317 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
318 case OP_CONSTANT_TRUE:
319 case OP_CONSTANT_FALSE:
321 case OP_SPEC_CONSTANT_TRUE:
322 case OP_SPEC_CONSTANT_FALSE:
323 case OP_SPEC_CONSTANT: reflect_constant(op); break;
324 case OP_VARIABLE: reflect_variable(op); break;
326 case OP_STORE: reflect_access(op); break;
327 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
328 case OP_DECORATE: reflect_decorate(op); break;
329 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
330 case OP_LABEL: reflect_label(op); break;
331 case OP_BRANCH: reflect_branch(op); break;
332 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
339 void SpirVModule::Reflection::reflect_name(CodeIterator op)
341 CodeIterator op_end = get_op_end(op);
342 string &name = names[*(op+1)];
344 name = read_string(op, op_end);
347 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
349 CodeIterator op_end = get_op_end(op);
350 Structure &strct = structs[*(op+1)];
351 unsigned index = *(op+2);
352 if(index>=strct.members.size())
353 strct.members.resize(index+1);
355 strct.members[index].name = read_string(op, op_end);
358 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
360 CodeIterator op_end = get_op_end(op);
361 EntryPoint &entry = entry_points[*(op+2)];
362 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
364 entry.name = read_string(op, op_end);
366 entry.globals.reserve(op_end-op);
367 for(; op!=op_end; ++op)
368 entry.globals.push_back(&variables[*op]);
371 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
373 types[*(op+1)].type = VOID;
376 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
378 types[*(op+1)].type = BOOL;
381 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
383 TypeInfo &type = types[*(op+1)];
384 unsigned size = *(op+2);
386 type.type = static_cast<DataType>(size/8 | sign*0x100);
389 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
391 TypeInfo &type = types[*(op+1)];
392 unsigned size = *(op+2);
393 type.type = static_cast<DataType>(size/8 | 0x300);
396 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
398 TypeInfo &type = types[*(op+1)];
399 DataType component = types[*(op+2)].type;
400 unsigned count = *(op+3);
401 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
404 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
406 TypeInfo &type = types[*(op+1)];
407 DataType column = types[*(op+2)].type;
408 unsigned count = *(op+3);
409 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
412 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
414 TypeInfo &type = types[*(op+1)];
415 DataType sample = types[*(op+2)].type;
416 unsigned dimensions = *(op+3);
417 bool depth = *(op+4)==1;
418 bool array = *(op+5);
419 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
422 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
424 TypeInfo &type = types[*(op+1)];
425 DataType image = types[*(op+2)].type;
426 type.type = static_cast<DataType>(image | 0x100000);
429 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
431 TypeInfo &type = types[*(op+1)];
432 const TypeInfo &elem = types[*(op+2)];
433 type.type = elem.type;
434 type.struct_type = elem.struct_type;
436 const Constant &size = constants[*(op+3)];
437 if(size.type==INT || size.type==UNSIGNED_INT)
438 type.array_size = size.i_value;
441 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
443 CodeIterator op_end = get_op_end(op);
444 unsigned id = *(op+1);
445 Structure &strct = structs[id];
446 strct.name = names[id];
447 types[id].struct_type = &strct;
450 strct.members.resize(op_end-op);
451 auto mem = strct.members.begin();
452 for(; op!=op_end; ++op, ++mem)
454 TypeInfo &type = types[*op];
455 mem->type = type.type;
456 mem->struct_type = type.struct_type;
457 mem->array_size = type.array_size;
458 mem->array_stride = type.array_stride;
462 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
464 TypeInfo &type = types[*(op+1)];
465 type = types[*(op+3)];
466 type.storage = static_cast<StorageClass>(*(op+2));
469 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
471 unsigned opcode = get_opcode(*op);
472 unsigned id = *(op+2);
473 Constant &cnst = constants[id];
474 cnst.name = names[id];
475 cnst.type = types[*(op+1)].type;
476 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
478 else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
479 cnst.i_value = false;
480 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
481 cnst.i_value = *(op+3);
482 else if(cnst.type==FLOAT)
483 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
486 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
488 unsigned id = *(op+2);
489 Variable &var = variables[id];
490 var.name = names[id];
491 const TypeInfo &type = types[*(op+1)];
492 var.storage = type.storage;
493 var.type = type.type;
494 var.struct_type = type.struct_type;
495 var.array_size = type.array_size;
498 void SpirVModule::Reflection::reflect_access(CodeIterator op)
502 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
503 auto i = access_chain_bases.find(id);
504 if(i!=access_chain_bases.end())
506 Variable &var = variables[id];
507 auto j = find(current_block->accessed_variables, &var);
508 if(j==current_block->accessed_variables.end())
509 current_block->accessed_variables.push_back(&var);
513 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
515 access_chain_bases[*(op+2)] = *(op+3);
518 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
520 unsigned id = *(op+1);
521 unsigned decoration = *(op+2);
527 constants[id].constant_id = *op;
529 case DECO_ARRAY_STRIDE:
530 types[id].array_stride = *op;
533 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
536 variables[id].location = *op;
539 variables[id].binding = *op;
541 case DECO_DESCRIPTOR_SET:
542 variables[id].descriptor_set = *op;
547 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
549 Structure &strct = structs[*(op+1)];
550 unsigned index = *(op+2);
551 if(index>=strct.members.size())
552 strct.members.resize(index+1);
553 unsigned decoration = *(op+3);
556 StructMember &member = strct.members[index];
559 case DECO_MATRIX_STRIDE:
560 member.matrix_stride = *op;
563 member.builtin = static_cast<BuiltinSemantic>(*op);
571 void SpirVModule::Reflection::reflect_label(CodeIterator op)
573 current_block = &blocks[*(op+1)];
576 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
578 InstructionBlock &block = blocks[*(op+1)];
579 block.condition = &true_condition;
580 current_block->successors.push_back(&block);
583 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
585 InstructionBlock &true_block = blocks[*(op+2)];
586 InstructionBlock &false_block = blocks[*(op+3)];
588 auto i = constants.find(*(op+1));
589 if(i!=constants.end() && i->second.constant_id)
591 if(!true_block.condition)
592 true_block.condition = &i->second;
593 if(!false_block.condition)
595 false_block.condition = &i->second;
596 false_block.negate_condition = true;
600 current_block->successors.push_back(&true_block);
601 current_block->successors.push_back(&false_block);