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);
121 void SpirVModule::compile(SL::Compiler &compiler)
123 compiler.compile(SL::Compiler::SPIRV);
124 code = compiler.get_combined_spirv();
128 void SpirVModule::reflect()
131 throw invalid_module("Empty SPIR-V code");
133 if(code[0]==SPIRV_MAGIC_REVERSED)
135 for(uint32_t &c: code)
136 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
138 else if(code[0]!=SPIRV_MAGIC)
139 throw invalid_module("SPIR-V magic number not found");
141 Reflection reflection;
142 reflection.reflect_code(code);
144 map<const Constant *, unsigned> spec_indices;
145 for(const auto &kvp: reflection.constants)
146 if(kvp.second.constant_id>=0)
148 spec_indices[&kvp.second] = spec_constants.size();
149 spec_constants.push_back(kvp.second);
152 map<const Structure *, unsigned> struct_indices;
153 structs.reserve(reflection.structs.size());
154 for(const auto &kvp: reflection.structs)
156 struct_indices[&kvp.second] = structs.size();
157 structs.push_back(kvp.second);
160 for(Structure &s: structs)
162 for(StructMember &m: s.members)
165 auto i = struct_indices.find(m.struct_type);
166 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
169 const StructMember *last_member = &s.members.back();
170 unsigned last_offset = last_member->offset;
171 while(last_member->struct_type)
173 const StructMember *lm = &last_member->struct_type->members.back();
174 if(last_member->array_size)
175 last_offset += last_member->array_stride*(last_member->array_size-1);
176 last_offset += lm->offset;
180 unsigned last_size = get_type_size(last_member->type);
181 if(last_member->array_size)
182 last_size += last_member->array_stride*(last_member->array_size-1);
183 s.size = last_offset+last_size;
184 s.size = (s.size+15)&~15;
187 map<const Variable *, unsigned> var_indices;
188 variables.reserve(reflection.variables.size());
189 for(const auto &kvp: reflection.variables)
191 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
192 if(i!=variables.end())
193 var_indices[&kvp.second] = i-variables.begin();
196 var_indices[&kvp.second] = variables.size();
197 variables.push_back(kvp.second);
201 for(Variable &v: variables)
204 auto i = struct_indices.find(v.struct_type);
205 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
208 entry_points.reserve(reflection.entry_points.size());
209 for(const auto &kvp: reflection.entry_points)
211 entry_points.push_back(kvp.second);
212 EntryPoint &entry = entry_points.back();
213 for(const Variable *&v: entry.globals)
215 auto i = var_indices.find(v);
216 v = (i!=var_indices.end() ? &variables[i->second] : 0);
220 map<const InstructionBlock *, unsigned> block_indices;
221 blocks.reserve(reflection.blocks.size());
222 for(const auto &kvp: reflection.blocks)
224 block_indices[&kvp.second] = blocks.size();
225 blocks.push_back(kvp.second);
228 for(InstructionBlock &b: blocks)
230 auto i = spec_indices.find(b.condition);
231 b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
233 for(const Variable *&v: b.accessed_variables)
235 auto j = var_indices.find(v);
236 v = (j!=var_indices.end() ? &variables[j->second] : 0);
239 for(const InstructionBlock *&s: b.successors)
241 auto j = block_indices.find(s);
242 s = (j!=block_indices.end() ? &blocks[j->second] : 0);
248 bool SpirVModule::Variable::operator==(const Variable &other) const
250 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
252 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
254 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
260 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
265 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
270 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
273 for(; op!=op_end; ++op)
276 for(unsigned i=0; i<4; ++i)
289 throw invalid_module("Unterminated SPIR-V string literal");
292 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
294 for(CodeIterator op=code.begin()+5; op!=code.end(); )
296 unsigned word_count = *op>>16;
297 if(word_count>static_cast<unsigned>(code.end()-op))
298 throw invalid_module("Truncated SPIR-V instruction");
300 switch(get_opcode(*op))
302 case OP_NAME: reflect_name(op); break;
303 case OP_MEMBER_NAME: reflect_member_name(op); break;
304 case OP_ENTRY_POINT: reflect_entry_point(op); break;
305 case OP_TYPE_VOID: reflect_void_type(op); break;
306 case OP_TYPE_BOOL: reflect_bool_type(op); break;
307 case OP_TYPE_INT: reflect_int_type(op); break;
308 case OP_TYPE_FLOAT: reflect_float_type(op); break;
309 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
310 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
311 case OP_TYPE_IMAGE: reflect_image_type(op); break;
312 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
313 case OP_TYPE_ARRAY: reflect_array_type(op); break;
314 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
315 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
316 case OP_CONSTANT_TRUE:
317 case OP_CONSTANT_FALSE:
319 case OP_SPEC_CONSTANT_TRUE:
320 case OP_SPEC_CONSTANT_FALSE:
321 case OP_SPEC_CONSTANT: reflect_constant(op); break;
322 case OP_VARIABLE: reflect_variable(op); break;
324 case OP_STORE: reflect_access(op); break;
325 case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
326 case OP_DECORATE: reflect_decorate(op); break;
327 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
328 case OP_LABEL: reflect_label(op); break;
329 case OP_BRANCH: reflect_branch(op); break;
330 case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
337 void SpirVModule::Reflection::reflect_name(CodeIterator op)
339 CodeIterator op_end = get_op_end(op);
340 string &name = names[*(op+1)];
342 name = read_string(op, op_end);
345 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
347 CodeIterator op_end = get_op_end(op);
348 Structure &strct = structs[*(op+1)];
349 unsigned index = *(op+2);
350 if(index>=strct.members.size())
351 strct.members.resize(index+1);
353 strct.members[index].name = read_string(op, op_end);
356 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
358 CodeIterator op_end = get_op_end(op);
359 EntryPoint &entry = entry_points[*(op+2)];
360 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
362 entry.name = read_string(op, op_end);
364 entry.globals.reserve(op_end-op);
365 for(; op!=op_end; ++op)
366 entry.globals.push_back(&variables[*op]);
369 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
371 types[*(op+1)].type = VOID;
374 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
376 types[*(op+1)].type = BOOL;
379 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
381 TypeInfo &type = types[*(op+1)];
382 unsigned size = *(op+2);
384 type.type = static_cast<DataType>(size/8 | sign*0x100);
387 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
389 TypeInfo &type = types[*(op+1)];
390 unsigned size = *(op+2);
391 type.type = static_cast<DataType>(size/8 | 0x300);
394 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
396 TypeInfo &type = types[*(op+1)];
397 DataType component = types[*(op+2)].type;
398 unsigned count = *(op+3);
399 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
402 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
404 TypeInfo &type = types[*(op+1)];
405 DataType column = types[*(op+2)].type;
406 unsigned count = *(op+3);
407 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
410 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
412 TypeInfo &type = types[*(op+1)];
413 DataType sample = types[*(op+2)].type;
414 unsigned dimensions = *(op+3);
415 bool depth = *(op+4)==1;
416 bool array = *(op+5);
417 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
420 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
422 TypeInfo &type = types[*(op+1)];
423 DataType image = types[*(op+2)].type;
424 type.type = static_cast<DataType>(image | 0x100000);
427 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
429 TypeInfo &type = types[*(op+1)];
430 const TypeInfo &elem = types[*(op+2)];
431 type.type = elem.type;
432 type.struct_type = elem.struct_type;
434 const Constant &size = constants[*(op+3)];
435 if(size.type==INT || size.type==UNSIGNED_INT)
436 type.array_size = size.i_value;
439 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
441 CodeIterator op_end = get_op_end(op);
442 unsigned id = *(op+1);
443 Structure &strct = structs[id];
444 strct.name = names[id];
445 types[id].struct_type = &strct;
448 strct.members.resize(op_end-op);
449 auto mem = strct.members.begin();
450 for(; op!=op_end; ++op, ++mem)
452 TypeInfo &type = types[*op];
453 mem->type = type.type;
454 mem->struct_type = type.struct_type;
455 mem->array_size = type.array_size;
456 mem->array_stride = type.array_stride;
460 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
462 TypeInfo &type = types[*(op+1)];
463 type = types[*(op+3)];
464 type.storage = static_cast<StorageClass>(*(op+2));
467 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
469 unsigned opcode = get_opcode(*op);
470 unsigned id = *(op+2);
471 Constant &cnst = constants[id];
472 cnst.name = names[id];
473 cnst.type = types[*(op+1)].type;
474 if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
476 else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
477 cnst.i_value = false;
478 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
479 cnst.i_value = *(op+3);
480 else if(cnst.type==FLOAT)
481 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
484 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
486 unsigned id = *(op+2);
487 Variable &var = variables[id];
488 var.name = names[id];
489 const TypeInfo &type = types[*(op+1)];
490 var.storage = type.storage;
491 var.type = type.type;
492 var.struct_type = type.struct_type;
493 var.array_size = type.array_size;
496 void SpirVModule::Reflection::reflect_access(CodeIterator op)
500 unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
501 auto i = access_chain_bases.find(id);
502 if(i!=access_chain_bases.end())
504 Variable &var = variables[id];
505 auto j = find(current_block->accessed_variables, &var);
506 if(j==current_block->accessed_variables.end())
507 current_block->accessed_variables.push_back(&var);
511 void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
513 access_chain_bases[*(op+2)] = *(op+3);
516 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
518 unsigned id = *(op+1);
519 unsigned decoration = *(op+2);
525 constants[id].constant_id = *op;
527 case DECO_ARRAY_STRIDE:
528 types[id].array_stride = *op;
531 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
534 variables[id].location = *op;
537 variables[id].binding = *op;
539 case DECO_DESCRIPTOR_SET:
540 variables[id].descriptor_set = *op;
545 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
547 Structure &strct = structs[*(op+1)];
548 unsigned index = *(op+2);
549 if(index>=strct.members.size())
550 strct.members.resize(index+1);
551 unsigned decoration = *(op+3);
554 StructMember &member = strct.members[index];
557 case DECO_MATRIX_STRIDE:
558 member.matrix_stride = *op;
561 member.builtin = static_cast<BuiltinSemantic>(*op);
569 void SpirVModule::Reflection::reflect_label(CodeIterator op)
571 current_block = &blocks[*(op+1)];
574 void SpirVModule::Reflection::reflect_branch(CodeIterator op)
576 InstructionBlock &block = blocks[*(op+1)];
577 block.condition = &true_condition;
578 current_block->successors.push_back(&block);
581 void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
583 InstructionBlock &true_block = blocks[*(op+2)];
584 InstructionBlock &false_block = blocks[*(op+3)];
586 auto i = constants.find(*(op+1));
587 if(i!=constants.end() && i->second.constant_id)
589 if(!true_block.condition)
590 true_block.condition = &i->second;
591 if(!false_block.condition)
593 false_block.condition = &i->second;
594 false_block.negate_condition = true;
598 current_block->successors.push_back(&true_block);
599 current_block->successors.push_back(&false_block);