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();
74 string diagnostics = compiler.get_diagnostics();
75 if(!diagnostics.empty())
76 IO::print("Module diagnostics:\n%s\n", diagnostics);
81 SpirVModule::SpirVModule(const SpirVModule &other):
83 entry_points(other.entry_points),
84 structs(other.structs),
85 variables(other.variables)
87 remap_pointers_from(other);
90 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
93 entry_points = other.entry_points;
94 structs = other.structs;
95 variables = other.variables;
96 remap_pointers_from(other);
100 void SpirVModule::remap_pointers_from(const SpirVModule &other)
102 for(vector<EntryPoint>::iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
103 for(vector<const Variable *>::iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
104 *j = &variables[*j-&other.variables.front()];
106 for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
108 i->struct_type = &structs[i->struct_type-&other.structs.front()];
110 for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
111 for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
113 j->struct_type = &structs[j->struct_type-&other.structs.front()];
116 void SpirVModule::load_code(IO::Base &io)
121 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
125 code.reserve(code.size()+len);
126 code.insert(code.end(), buffer, buffer+len);
130 throw invalid_module("Empty SPIR-V code");
132 if(code[0]==SPIRV_MAGIC_REVERSED)
134 for(vector<UInt32>::iterator i=code.begin(); i!=code.end(); ++i)
135 *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>24)&0xFF);
137 else if(code[0]!=SPIRV_MAGIC)
138 throw invalid_module("SPIR-V magic number not found");
140 Reflection reflection;
141 reflection.reflect_code(code);
143 map<const Structure *, unsigned> struct_indices;
144 structs.reserve(reflection.structs.size());
145 for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
147 struct_indices[&i->second] = structs.size();
148 structs.push_back(i->second);
151 for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
153 for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
157 map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
158 j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
162 const StructMember *last_member = &i->members.back();
163 unsigned last_offset = last_member->offset;
164 while(last_member->struct_type)
166 const StructMember *lm = &last_member->struct_type->members.back();
167 if(last_member->array_size)
168 last_offset += last_member->array_stride*(last_member->array_size-1);
169 last_offset += lm->offset;
173 i->size = last_offset+get_type_size(last_member->type);
174 i->size = (i->size+15)&~15;
177 map<const Variable *, unsigned> var_indices;
178 variables.reserve(reflection.variables.size());
179 for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
182 for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
184 dup_index = j-variables.begin();
187 var_indices[&i->second] = dup_index;
190 var_indices[&i->second] = variables.size();
191 variables.push_back(i->second);
195 for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
198 map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
199 i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
202 entry_points.reserve(reflection.entry_points.size());
203 for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
205 entry_points.push_back(i->second);
206 EntryPoint &entry = entry_points.back();
207 for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
209 map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
210 *j = (k!=var_indices.end() ? &variables[k->second] : 0);
214 for(map<unsigned, SpecConstant>::const_iterator i=reflection.spec_constants.begin(); i!=reflection.spec_constants.end(); ++i)
215 spec_constants.push_back(i->second);
218 void SpirVModule::compile(SL::Compiler &)
220 throw logic_error("Not implemented yet");
224 SpirVModule::EntryPoint::EntryPoint():
229 SpirVModule::StructMember::StructMember():
239 SpirVModule::Variable::Variable():
247 bool SpirVModule::Variable::operator==(const Variable &other) const
249 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
251 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
253 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
259 SpirVModule::TypeInfo::TypeInfo():
264 storage(static_cast<StorageClass>(-1))
268 UInt32 SpirVModule::Reflection::get_opcode(UInt32 op)
273 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
278 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
281 for(; op!=op_end; ++op)
284 for(unsigned i=0; i<4; ++i)
297 throw invalid_module("Unterminated SPIR-V string literal");
300 void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
302 for(CodeIterator op=code.begin()+5; op!=code.end(); )
304 unsigned word_count = *op>>16;
305 if(word_count>code.end()-op)
306 throw invalid_module("Truncated SPIR-V instruction");
308 switch(get_opcode(*op))
310 case OP_NAME: reflect_name(op); break;
311 case OP_MEMBER_NAME: reflect_member_name(op); break;
312 case OP_ENTRY_POINT: reflect_entry_point(op); break;
313 case OP_TYPE_VOID: reflect_void_type(op); break;
314 case OP_TYPE_BOOL: reflect_bool_type(op); break;
315 case OP_TYPE_INT: reflect_int_type(op); break;
316 case OP_TYPE_FLOAT: reflect_float_type(op); break;
317 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
318 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
319 case OP_TYPE_IMAGE: reflect_image_type(op); break;
320 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
321 case OP_TYPE_ARRAY: reflect_array_type(op); break;
322 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
323 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
324 case OP_CONSTANT_TRUE: constants[*(op+2)] = true; break;
325 case OP_CONSTANT_FALSE: constants[*(op+2)] = false; break;
326 case OP_CONSTANT: reflect_constant(op); break;
327 case OP_SPEC_CONSTANT_TRUE:
328 case OP_SPEC_CONSTANT_FALSE:
329 case OP_SPEC_CONSTANT: reflect_spec_constant(op); break;
330 case OP_VARIABLE: reflect_variable(op); break;
331 case OP_DECORATE: reflect_decorate(op); break;
332 case OP_MEMBER_DECORATE: reflect_member_decorate(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<<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<<16) | (column&0xF00) | ((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) | 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;
435 const Variant &size = constants[*(op+3)];
436 if(size.check_type<int>())
437 type.array_size = size.value<int>();
438 else if(size.check_type<unsigned>())
439 type.array_size = size.value<unsigned>();
442 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
444 CodeIterator op_end = get_op_end(op);
445 unsigned id = *(op+1);
446 Structure &strct = structs[id];
447 strct.name = names[id];
448 types[id].struct_type = &strct;
451 strct.members.resize(op_end-op);
452 vector<StructMember>::iterator mem = strct.members.begin();
453 for(; op!=op_end; ++op, ++mem)
455 TypeInfo &type = types[*op];
456 mem->type = type.type;
457 mem->struct_type = type.struct_type;
458 mem->array_size = type.array_size;
459 mem->array_stride = type.array_stride;
463 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
465 TypeInfo &type = types[*(op+1)];
466 type = types[*(op+3)];
467 type.storage = static_cast<StorageClass>(*(op+2));
470 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
472 const TypeInfo &type = types[*(op+1)];
473 unsigned id = *(op+2);
475 constants[id] = static_cast<int>(*(op+3));
476 else if(type.type==UNSIGNED_INT)
477 constants[id] = static_cast<unsigned>(*(op+3));
478 else if(type.type==FLOAT)
479 constants[id] = *reinterpret_cast<const float *>(&*(op+3));
482 void SpirVModule::Reflection::reflect_spec_constant(CodeIterator op)
484 unsigned id = *(op+2);
485 SpecConstant &spec = spec_constants[id];
486 spec.name = names[id];
487 spec.type = types[*(op+1)].type;
490 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
492 unsigned id = *(op+2);
493 Variable &var = variables[id];
494 var.name = names[id];
495 const TypeInfo &type = types[*(op+1)];
496 var.storage = type.storage;
497 var.type = type.type;
498 var.struct_type = type.struct_type;
499 var.array_size = type.array_size;
502 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
504 unsigned id = *(op+1);
505 unsigned decoration = *(op+2);
511 spec_constants[id].constant_id = *op;
513 case DECO_ARRAY_STRIDE:
514 types[id].array_stride = *op;
517 variables[id].location = *op;
520 variables[id].binding = *op;
522 case DECO_DESCRIPTOR_SET:
523 variables[id].descriptor_set = *op;
528 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
530 Structure &strct = structs[*(op+1)];
531 unsigned index = *(op+2);
532 if(index>=strct.members.size())
533 strct.members.resize(index+1);
534 unsigned decoration = *(op+3);
537 StructMember &member = strct.members[index];
540 case DECO_MATRIX_STRIDE:
541 member.matrix_stride = *op;