1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
10 SPIRV_MAGIC = 0x07230203,
11 SPIRV_MAGIC_REVERSED = 0x03022307,
23 OP_TYPE_SAMPLED_IMAGE = 27,
27 OP_CONSTANT_TRUE = 41,
28 OP_CONSTANT_FALSE = 42,
30 OP_SPEC_CONSTANT_TRUE = 48,
31 OP_SPEC_CONSTANT_FALSE = 49,
32 OP_SPEC_CONSTANT = 50,
35 OP_MEMBER_DECORATE = 72,
38 DECO_ARRAY_STRIDE = 6,
39 DECO_MATRIX_STRIDE = 7,
42 DECO_DESCRIPTOR_SET = 34,
49 void Module::set_source(const string &src)
51 SL::Compiler compiler;
52 compiler.set_source(src);
56 void Module::load_source(IO::Base &io, Resources *res, const string &name)
58 SL::Compiler compiler;
59 compiler.load_source(io, res, name);
63 void Module::load_source(IO::Base &io, const string &name)
65 load_source(io, 0, name);
69 void GlslModule::compile(SL::Compiler &compiler)
71 compiler.compile(SL::Compiler::MODULE);
72 prepared_source = compiler.get_combined_glsl();
73 source_map = compiler.get_source_map();
76 string diagnostics = compiler.get_diagnostics();
77 if(!diagnostics.empty())
78 IO::print("Module diagnostics:\n%s\n", diagnostics);
83 SpirVModule::SpirVModule(const SpirVModule &other):
85 entry_points(other.entry_points),
86 structs(other.structs),
87 variables(other.variables)
89 remap_pointers_from(other);
92 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
95 entry_points = other.entry_points;
96 structs = other.structs;
97 variables = other.variables;
98 remap_pointers_from(other);
102 void SpirVModule::remap_pointers_from(const SpirVModule &other)
104 for(EntryPoint &e: entry_points)
105 for(const Variable *&v: e.globals)
106 v = &variables[v-&other.variables.front()];
108 for(Variable &v: variables)
110 v.struct_type = &structs[v.struct_type-&other.structs.front()];
112 for(Structure &s: structs)
113 for(StructMember &m: s.members)
115 m.struct_type = &structs[m.struct_type-&other.structs.front()];
118 void SpirVModule::load_code(IO::Base &io)
120 uint32_t buffer[1024];
123 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
127 code.reserve(code.size()+len);
128 code.insert(code.end(), buffer, buffer+len);
134 void SpirVModule::compile(SL::Compiler &compiler)
136 compiler.compile(SL::Compiler::SPIRV);
137 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)
179 auto i = struct_indices.find(m.struct_type);
180 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
182 if(m.array_size_spec)
184 auto i = spec_indices.find(m.array_size_spec);
185 m.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
189 const StructMember *last_member = &s.members.back();
190 unsigned last_offset = last_member->offset;
191 while(last_member->struct_type)
193 const StructMember *lm = &last_member->struct_type->members.back();
194 if(last_member->array_size)
195 last_offset += last_member->array_stride*(last_member->array_size-1);
196 else if(last_member->array_size_spec)
197 last_offset += last_member->array_stride*(last_member->array_size_spec->i_value-1);
198 last_offset += lm->offset;
202 s.size = last_offset+get_type_size(last_member->type);
203 s.size = (s.size+15)&~15;
206 map<const Variable *, unsigned> var_indices;
207 variables.reserve(reflection.variables.size());
208 for(const auto &kvp: reflection.variables)
210 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
211 if(i!=variables.end())
212 var_indices[&kvp.second] = i-variables.begin();
215 var_indices[&kvp.second] = variables.size();
216 variables.push_back(kvp.second);
220 for(Variable &v: variables)
224 auto i = struct_indices.find(v.struct_type);
225 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
227 if(v.array_size_spec)
229 auto i = spec_indices.find(v.array_size_spec);
230 v.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
234 entry_points.reserve(reflection.entry_points.size());
235 for(const auto &kvp: reflection.entry_points)
237 entry_points.push_back(kvp.second);
238 EntryPoint &entry = entry_points.back();
239 for(const Variable *&v: entry.globals)
241 auto i = var_indices.find(v);
242 v = (i!=var_indices.end() ? &variables[i->second] : 0);
248 SpirVModule::EntryPoint::EntryPoint():
253 SpirVModule::StructMember::StructMember():
264 SpirVModule::Variable::Variable():
272 bool SpirVModule::Variable::operator==(const Variable &other) const
274 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
276 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
278 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
284 SpirVModule::TypeInfo::TypeInfo():
290 storage(static_cast<StorageClass>(-1))
294 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
299 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
304 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
307 for(; op!=op_end; ++op)
310 for(unsigned i=0; i<4; ++i)
323 throw invalid_module("Unterminated SPIR-V string literal");
326 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
328 for(CodeIterator op=code.begin()+5; op!=code.end(); )
330 unsigned word_count = *op>>16;
331 if(word_count>static_cast<unsigned>(code.end()-op))
332 throw invalid_module("Truncated SPIR-V instruction");
334 switch(get_opcode(*op))
336 case OP_NAME: reflect_name(op); break;
337 case OP_MEMBER_NAME: reflect_member_name(op); break;
338 case OP_ENTRY_POINT: reflect_entry_point(op); break;
339 case OP_TYPE_VOID: reflect_void_type(op); break;
340 case OP_TYPE_BOOL: reflect_bool_type(op); break;
341 case OP_TYPE_INT: reflect_int_type(op); break;
342 case OP_TYPE_FLOAT: reflect_float_type(op); break;
343 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
344 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
345 case OP_TYPE_IMAGE: reflect_image_type(op); break;
346 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
347 case OP_TYPE_ARRAY: reflect_array_type(op); break;
348 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
349 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
350 case OP_CONSTANT_TRUE:
351 case OP_CONSTANT_FALSE:
353 case OP_SPEC_CONSTANT_TRUE:
354 case OP_SPEC_CONSTANT_FALSE:
355 case OP_SPEC_CONSTANT: reflect_constant(op); break;
356 case OP_VARIABLE: reflect_variable(op); break;
357 case OP_DECORATE: reflect_decorate(op); break;
358 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
365 void SpirVModule::Reflection::reflect_name(CodeIterator op)
367 CodeIterator op_end = get_op_end(op);
368 string &name = names[*(op+1)];
370 name = read_string(op, op_end);
373 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
375 CodeIterator op_end = get_op_end(op);
376 Structure &strct = structs[*(op+1)];
377 unsigned index = *(op+2);
378 if(index>=strct.members.size())
379 strct.members.resize(index+1);
381 strct.members[index].name = read_string(op, op_end);
384 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
386 CodeIterator op_end = get_op_end(op);
387 EntryPoint &entry = entry_points[*(op+2)];
388 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
390 entry.name = read_string(op, op_end);
392 entry.globals.reserve(op_end-op);
393 for(; op!=op_end; ++op)
394 entry.globals.push_back(&variables[*op]);
397 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
399 types[*(op+1)].type = VOID;
402 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
404 types[*(op+1)].type = BOOL;
407 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
409 TypeInfo &type = types[*(op+1)];
410 unsigned size = *(op+2);
412 type.type = static_cast<DataType>(size/8 | sign*0x100);
415 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
417 TypeInfo &type = types[*(op+1)];
418 unsigned size = *(op+2);
419 type.type = static_cast<DataType>(size/8 | 0x300);
422 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
424 TypeInfo &type = types[*(op+1)];
425 DataType component = types[*(op+2)].type;
426 unsigned count = *(op+3);
427 type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
430 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
432 TypeInfo &type = types[*(op+1)];
433 DataType column = types[*(op+2)].type;
434 unsigned count = *(op+3);
435 type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
438 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
440 TypeInfo &type = types[*(op+1)];
441 DataType sample = types[*(op+2)].type;
442 unsigned dimensions = *(op+3);
443 bool depth = *(op+4)==1;
444 bool array = *(op+5);
445 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
448 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
450 TypeInfo &type = types[*(op+1)];
451 DataType image = types[*(op+2)].type;
452 type.type = static_cast<DataType>(image | 0x100000);
455 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
457 TypeInfo &type = types[*(op+1)];
458 const TypeInfo &elem = types[*(op+2)];
459 type.type = elem.type;
460 type.struct_type = elem.struct_type;
462 const Constant &size = constants[*(op+3)];
463 if(size.constant_id>=0)
464 type.array_size_spec = &size;
465 else if(size.type==INT || size.type==UNSIGNED_INT)
466 type.array_size = size.i_value;
469 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
471 CodeIterator op_end = get_op_end(op);
472 unsigned id = *(op+1);
473 Structure &strct = structs[id];
474 strct.name = names[id];
475 types[id].struct_type = &strct;
478 strct.members.resize(op_end-op);
479 auto mem = strct.members.begin();
480 for(; op!=op_end; ++op, ++mem)
482 TypeInfo &type = types[*op];
483 mem->type = type.type;
484 mem->struct_type = type.struct_type;
485 mem->array_size = type.array_size;
486 mem->array_size_spec = type.array_size_spec;
487 mem->array_stride = type.array_stride;
491 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
493 TypeInfo &type = types[*(op+1)];
494 type = types[*(op+3)];
495 type.storage = static_cast<StorageClass>(*(op+2));
498 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
500 unsigned id = *(op+2);
501 Constant &cnst = constants[id];
502 cnst.name = names[id];
503 cnst.type = types[*(op+1)].type;
504 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
506 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
507 cnst.i_value = false;
508 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
509 cnst.i_value = *(op+3);
510 else if(cnst.type==FLOAT)
511 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
514 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
516 unsigned id = *(op+2);
517 Variable &var = variables[id];
518 var.name = names[id];
519 const TypeInfo &type = types[*(op+1)];
520 var.storage = type.storage;
521 var.type = type.type;
522 var.struct_type = type.struct_type;
523 var.array_size = type.array_size;
524 var.array_size_spec = type.array_size_spec;
527 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
529 unsigned id = *(op+1);
530 unsigned decoration = *(op+2);
536 constants[id].constant_id = *op;
538 case DECO_ARRAY_STRIDE:
539 types[id].array_stride = *op;
542 variables[id].location = *op;
545 variables[id].binding = *op;
547 case DECO_DESCRIPTOR_SET:
548 variables[id].descriptor_set = *op;
553 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
555 Structure &strct = structs[*(op+1)];
556 unsigned index = *(op+2);
557 if(index>=strct.members.size())
558 strct.members.resize(index+1);
559 unsigned decoration = *(op+3);
562 StructMember &member = strct.members[index];
565 case DECO_MATRIX_STRIDE:
566 member.matrix_stride = *op;