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 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_vector_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;
323 case OP_DECORATE: reflect_decorate(op); break;
324 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
331 void SpirVModule::Reflection::reflect_name(CodeIterator op)
333 CodeIterator op_end = get_op_end(op);
334 string &name = names[*(op+1)];
336 name = read_string(op, op_end);
339 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
341 CodeIterator op_end = get_op_end(op);
342 Structure &strct = structs[*(op+1)];
343 unsigned index = *(op+2);
344 if(index>=strct.members.size())
345 strct.members.resize(index+1);
347 strct.members[index].name = read_string(op, op_end);
350 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
352 CodeIterator op_end = get_op_end(op);
353 EntryPoint &entry = entry_points[*(op+2)];
354 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
356 entry.name = read_string(op, op_end);
358 entry.globals.reserve(op_end-op);
359 for(; op!=op_end; ++op)
360 entry.globals.push_back(&variables[*op]);
363 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
365 types[*(op+1)].type = VOID;
368 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
370 types[*(op+1)].type = BOOL;
373 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
375 TypeInfo &type = types[*(op+1)];
376 unsigned size = *(op+2);
378 type.type = static_cast<DataType>(size/8 | sign*0x100);
381 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
383 TypeInfo &type = types[*(op+1)];
384 unsigned size = *(op+2);
385 type.type = static_cast<DataType>(size/8 | 0x300);
388 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
390 TypeInfo &type = types[*(op+1)];
391 DataType component = types[*(op+2)].type;
392 unsigned count = *(op+3);
393 type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
396 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
398 TypeInfo &type = types[*(op+1)];
399 DataType column = types[*(op+2)].type;
400 unsigned count = *(op+3);
401 type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
404 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
406 TypeInfo &type = types[*(op+1)];
407 DataType sample = types[*(op+2)].type;
408 unsigned dimensions = *(op+3);
409 bool depth = *(op+4)==1;
410 bool array = *(op+5);
411 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
414 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
416 TypeInfo &type = types[*(op+1)];
417 DataType image = types[*(op+2)].type;
418 type.type = static_cast<DataType>(image | 0x100000);
421 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
423 TypeInfo &type = types[*(op+1)];
424 const TypeInfo &elem = types[*(op+2)];
425 type.type = elem.type;
426 type.struct_type = elem.struct_type;
428 const Constant &size = constants[*(op+3)];
429 if(size.constant_id>=0)
430 type.array_size_spec = &size;
431 else if(size.type==INT || size.type==UNSIGNED_INT)
432 type.array_size = size.i_value;
435 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
437 CodeIterator op_end = get_op_end(op);
438 unsigned id = *(op+1);
439 Structure &strct = structs[id];
440 strct.name = names[id];
441 types[id].struct_type = &strct;
444 strct.members.resize(op_end-op);
445 auto mem = strct.members.begin();
446 for(; op!=op_end; ++op, ++mem)
448 TypeInfo &type = types[*op];
449 mem->type = type.type;
450 mem->struct_type = type.struct_type;
451 mem->array_size = type.array_size;
452 mem->array_size_spec = type.array_size_spec;
453 mem->array_stride = type.array_stride;
457 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
459 TypeInfo &type = types[*(op+1)];
460 type = types[*(op+3)];
461 type.storage = static_cast<StorageClass>(*(op+2));
464 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
466 unsigned id = *(op+2);
467 Constant &cnst = constants[id];
468 cnst.name = names[id];
469 cnst.type = types[*(op+1)].type;
470 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
472 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
473 cnst.i_value = false;
474 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
475 cnst.i_value = *(op+3);
476 else if(cnst.type==FLOAT)
477 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
480 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
482 unsigned id = *(op+2);
483 Variable &var = variables[id];
484 var.name = names[id];
485 const TypeInfo &type = types[*(op+1)];
486 var.storage = type.storage;
487 var.type = type.type;
488 var.struct_type = type.struct_type;
489 var.array_size = type.array_size;
490 var.array_size_spec = type.array_size_spec;
493 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
495 unsigned id = *(op+1);
496 unsigned decoration = *(op+2);
502 constants[id].constant_id = *op;
504 case DECO_ARRAY_STRIDE:
505 types[id].array_stride = *op;
508 variables[id].location = *op;
511 variables[id].binding = *op;
513 case DECO_DESCRIPTOR_SET:
514 variables[id].descriptor_set = *op;
519 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
521 Structure &strct = structs[*(op+1)];
522 unsigned index = *(op+2);
523 if(index>=strct.members.size())
524 strct.members.resize(index+1);
525 unsigned decoration = *(op+3);
528 StructMember &member = strct.members[index];
531 case DECO_MATRIX_STRIDE:
532 member.matrix_stride = *op;