1 #include <msp/io/print.h>
9 SPIRV_MAGIC = 0x07230203,
10 SPIRV_MAGIC_REVERSED = 0x03022307,
22 OP_TYPE_SAMPLED_IMAGE = 27,
26 OP_CONSTANT_TRUE = 41,
27 OP_CONSTANT_FALSE = 42,
29 OP_SPEC_CONSTANT_TRUE = 48,
30 OP_SPEC_CONSTANT_FALSE = 49,
31 OP_SPEC_CONSTANT = 50,
34 OP_MEMBER_DECORATE = 72,
37 DECO_ARRAY_STRIDE = 6,
38 DECO_MATRIX_STRIDE = 7,
41 DECO_DESCRIPTOR_SET = 34,
48 void Module::set_source(const string &src)
50 SL::Compiler compiler;
51 compiler.set_source(src);
55 void Module::load_source(IO::Base &io, Resources *res, const string &name)
57 SL::Compiler compiler;
58 compiler.load_source(io, res, name);
62 void Module::load_source(IO::Base &io, const string &name)
64 load_source(io, 0, name);
68 void GlslModule::compile(SL::Compiler &compiler)
70 compiler.compile(SL::Compiler::MODULE);
71 prepared_source = compiler.get_combined_glsl();
72 source_map = compiler.get_source_map();
75 string diagnostics = compiler.get_diagnostics();
76 if(!diagnostics.empty())
77 IO::print("Module diagnostics:\n%s\n", diagnostics);
82 SpirVModule::SpirVModule(const SpirVModule &other):
84 entry_points(other.entry_points),
85 structs(other.structs),
86 variables(other.variables)
88 remap_pointers_from(other);
91 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
94 entry_points = other.entry_points;
95 structs = other.structs;
96 variables = other.variables;
97 remap_pointers_from(other);
101 void SpirVModule::remap_pointers_from(const SpirVModule &other)
103 for(vector<EntryPoint>::iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
104 for(vector<const Variable *>::iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
105 *j = &variables[*j-&other.variables.front()];
107 for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
109 i->struct_type = &structs[i->struct_type-&other.structs.front()];
111 for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
112 for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
114 j->struct_type = &structs[j->struct_type-&other.structs.front()];
117 void SpirVModule::load_code(IO::Base &io)
122 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
126 code.reserve(code.size()+len);
127 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();
140 void SpirVModule::reflect()
143 throw invalid_module("Empty SPIR-V code");
145 if(code[0]==SPIRV_MAGIC_REVERSED)
147 for(vector<UInt32>::iterator i=code.begin(); i!=code.end(); ++i)
148 *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>24)&0xFF);
150 else if(code[0]!=SPIRV_MAGIC)
151 throw invalid_module("SPIR-V magic number not found");
153 Reflection reflection;
154 reflection.reflect_code(code);
156 map<const Constant *, unsigned> spec_indices;
157 for(map<unsigned, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
158 if(i->second.constant_id>=0)
160 spec_indices[&i->second] = spec_constants.size();
161 spec_constants.push_back(i->second);
164 map<const Structure *, unsigned> struct_indices;
165 structs.reserve(reflection.structs.size());
166 for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
168 struct_indices[&i->second] = structs.size();
169 structs.push_back(i->second);
172 for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
174 for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
178 map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
179 j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
181 if(j->array_size_spec)
183 map<const Constant *, unsigned>::const_iterator k = spec_indices.find(j->array_size_spec);
184 j->array_size_spec = (k!=spec_indices.end() ? &spec_constants[k->second] : 0);
188 const StructMember *last_member = &i->members.back();
189 unsigned last_offset = last_member->offset;
190 while(last_member->struct_type)
192 const StructMember *lm = &last_member->struct_type->members.back();
193 if(last_member->array_size)
194 last_offset += last_member->array_stride*(last_member->array_size-1);
195 else if(last_member->array_size_spec)
196 last_offset += last_member->array_stride*(last_member->array_size_spec->i_value-1);
197 last_offset += lm->offset;
201 i->size = last_offset+get_type_size(last_member->type);
202 i->size = (i->size+15)&~15;
205 map<const Variable *, unsigned> var_indices;
206 variables.reserve(reflection.variables.size());
207 for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
210 for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
212 dup_index = j-variables.begin();
215 var_indices[&i->second] = dup_index;
218 var_indices[&i->second] = variables.size();
219 variables.push_back(i->second);
223 for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
227 map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
228 i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
230 if(i->array_size_spec)
232 map<const Constant *, unsigned>::const_iterator j = spec_indices.find(i->array_size_spec);
233 i->array_size_spec = (j!=spec_indices.end() ? &spec_constants[j->second] : 0);
237 entry_points.reserve(reflection.entry_points.size());
238 for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
240 entry_points.push_back(i->second);
241 EntryPoint &entry = entry_points.back();
242 for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
244 map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
245 *j = (k!=var_indices.end() ? &variables[k->second] : 0);
251 SpirVModule::EntryPoint::EntryPoint():
256 SpirVModule::StructMember::StructMember():
267 SpirVModule::Variable::Variable():
275 bool SpirVModule::Variable::operator==(const Variable &other) const
277 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
279 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
281 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
287 SpirVModule::TypeInfo::TypeInfo():
293 storage(static_cast<StorageClass>(-1))
297 UInt32 SpirVModule::Reflection::get_opcode(UInt32 op)
302 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
307 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
310 for(; op!=op_end; ++op)
313 for(unsigned i=0; i<4; ++i)
326 throw invalid_module("Unterminated SPIR-V string literal");
329 void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
331 for(CodeIterator op=code.begin()+5; op!=code.end(); )
333 unsigned word_count = *op>>16;
334 if(word_count>static_cast<unsigned>(code.end()-op))
335 throw invalid_module("Truncated SPIR-V instruction");
337 switch(get_opcode(*op))
339 case OP_NAME: reflect_name(op); break;
340 case OP_MEMBER_NAME: reflect_member_name(op); break;
341 case OP_ENTRY_POINT: reflect_entry_point(op); break;
342 case OP_TYPE_VOID: reflect_void_type(op); break;
343 case OP_TYPE_BOOL: reflect_bool_type(op); break;
344 case OP_TYPE_INT: reflect_int_type(op); break;
345 case OP_TYPE_FLOAT: reflect_float_type(op); break;
346 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
347 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
348 case OP_TYPE_IMAGE: reflect_image_type(op); break;
349 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
350 case OP_TYPE_ARRAY: reflect_array_type(op); break;
351 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
352 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
353 case OP_CONSTANT_TRUE:
354 case OP_CONSTANT_FALSE:
356 case OP_SPEC_CONSTANT_TRUE:
357 case OP_SPEC_CONSTANT_FALSE:
358 case OP_SPEC_CONSTANT: reflect_constant(op); break;
359 case OP_VARIABLE: reflect_variable(op); break;
360 case OP_DECORATE: reflect_decorate(op); break;
361 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
368 void SpirVModule::Reflection::reflect_name(CodeIterator op)
370 CodeIterator op_end = get_op_end(op);
371 string &name = names[*(op+1)];
373 name = read_string(op, op_end);
376 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
378 CodeIterator op_end = get_op_end(op);
379 Structure &strct = structs[*(op+1)];
380 unsigned index = *(op+2);
381 if(index>=strct.members.size())
382 strct.members.resize(index+1);
384 strct.members[index].name = read_string(op, op_end);
387 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
389 CodeIterator op_end = get_op_end(op);
390 EntryPoint &entry = entry_points[*(op+2)];
391 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
393 entry.name = read_string(op, op_end);
395 entry.globals.reserve(op_end-op);
396 for(; op!=op_end; ++op)
397 entry.globals.push_back(&variables[*op]);
400 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
402 types[*(op+1)].type = VOID;
405 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
407 types[*(op+1)].type = BOOL;
410 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
412 TypeInfo &type = types[*(op+1)];
413 unsigned size = *(op+2);
415 type.type = static_cast<DataType>(size/8 | sign*0x100);
418 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
420 TypeInfo &type = types[*(op+1)];
421 unsigned size = *(op+2);
422 type.type = static_cast<DataType>(size/8 | 0x300);
425 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
427 TypeInfo &type = types[*(op+1)];
428 DataType component = types[*(op+2)].type;
429 unsigned count = *(op+3);
430 type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
433 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
435 TypeInfo &type = types[*(op+1)];
436 DataType column = types[*(op+2)].type;
437 unsigned count = *(op+3);
438 type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
441 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
443 TypeInfo &type = types[*(op+1)];
444 DataType sample = types[*(op+2)].type;
445 unsigned dimensions = *(op+3);
446 bool depth = *(op+4)==1;
447 bool array = *(op+5);
448 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
451 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
453 TypeInfo &type = types[*(op+1)];
454 DataType image = types[*(op+2)].type;
455 type.type = static_cast<DataType>(image | 0x100000);
458 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
460 TypeInfo &type = types[*(op+1)];
461 const TypeInfo &elem = types[*(op+2)];
462 type.type = elem.type;
463 type.struct_type = elem.struct_type;
465 const Constant &size = constants[*(op+3)];
466 if(size.constant_id>=0)
467 type.array_size_spec = &size;
468 else if(size.type==INT || size.type==UNSIGNED_INT)
469 type.array_size = size.i_value;
472 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
474 CodeIterator op_end = get_op_end(op);
475 unsigned id = *(op+1);
476 Structure &strct = structs[id];
477 strct.name = names[id];
478 types[id].struct_type = &strct;
481 strct.members.resize(op_end-op);
482 vector<StructMember>::iterator mem = strct.members.begin();
483 for(; op!=op_end; ++op, ++mem)
485 TypeInfo &type = types[*op];
486 mem->type = type.type;
487 mem->struct_type = type.struct_type;
488 mem->array_size = type.array_size;
489 mem->array_size_spec = type.array_size_spec;
490 mem->array_stride = type.array_stride;
494 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
496 TypeInfo &type = types[*(op+1)];
497 type = types[*(op+3)];
498 type.storage = static_cast<StorageClass>(*(op+2));
501 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
503 unsigned id = *(op+2);
504 Constant &cnst = constants[id];
505 cnst.name = names[id];
506 cnst.type = types[*(op+1)].type;
507 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
509 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
510 cnst.i_value = false;
511 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
512 cnst.i_value = *(op+3);
513 else if(cnst.type==FLOAT)
514 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
517 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
519 unsigned id = *(op+2);
520 Variable &var = variables[id];
521 var.name = names[id];
522 const TypeInfo &type = types[*(op+1)];
523 var.storage = type.storage;
524 var.type = type.type;
525 var.struct_type = type.struct_type;
526 var.array_size = type.array_size;
527 var.array_size_spec = type.array_size_spec;
530 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
532 unsigned id = *(op+1);
533 unsigned decoration = *(op+2);
539 constants[id].constant_id = *op;
541 case DECO_ARRAY_STRIDE:
542 types[id].array_stride = *op;
545 variables[id].location = *op;
548 variables[id].binding = *op;
550 case DECO_DESCRIPTOR_SET:
551 variables[id].descriptor_set = *op;
556 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
558 Structure &strct = structs[*(op+1)];
559 unsigned index = *(op+2);
560 if(index>=strct.members.size())
561 strct.members.resize(index+1);
562 unsigned decoration = *(op+3);
565 StructMember &member = strct.members[index];
568 case DECO_MATRIX_STRIDE:
569 member.matrix_stride = *op;