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);
131 throw invalid_module("Empty SPIR-V code");
133 if(code[0]==SPIRV_MAGIC_REVERSED)
135 for(vector<UInt32>::iterator i=code.begin(); i!=code.end(); ++i)
136 *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>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 Structure *, unsigned> struct_indices;
145 structs.reserve(reflection.structs.size());
146 for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
148 struct_indices[&i->second] = structs.size();
149 structs.push_back(i->second);
152 for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
154 for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
158 map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
159 j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
163 const StructMember *last_member = &i->members.back();
164 unsigned last_offset = last_member->offset;
165 while(last_member->struct_type)
167 const StructMember *lm = &last_member->struct_type->members.back();
168 if(last_member->array_size)
169 last_offset += last_member->array_stride*(last_member->array_size-1);
170 last_offset += lm->offset;
174 i->size = last_offset+get_type_size(last_member->type);
175 i->size = (i->size+15)&~15;
178 map<const Variable *, unsigned> var_indices;
179 variables.reserve(reflection.variables.size());
180 for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
183 for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
185 dup_index = j-variables.begin();
188 var_indices[&i->second] = dup_index;
191 var_indices[&i->second] = variables.size();
192 variables.push_back(i->second);
196 for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
199 map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
200 i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
203 entry_points.reserve(reflection.entry_points.size());
204 for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
206 entry_points.push_back(i->second);
207 EntryPoint &entry = entry_points.back();
208 for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
210 map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
211 *j = (k!=var_indices.end() ? &variables[k->second] : 0);
215 for(map<unsigned, SpecConstant>::const_iterator i=reflection.spec_constants.begin(); i!=reflection.spec_constants.end(); ++i)
216 spec_constants.push_back(i->second);
219 void SpirVModule::compile(SL::Compiler &)
221 throw logic_error("Not implemented yet");
225 SpirVModule::EntryPoint::EntryPoint():
230 SpirVModule::StructMember::StructMember():
240 SpirVModule::Variable::Variable():
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 SpirVModule::TypeInfo::TypeInfo():
265 storage(static_cast<StorageClass>(-1))
269 UInt32 SpirVModule::Reflection::get_opcode(UInt32 op)
274 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
279 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
282 for(; op!=op_end; ++op)
285 for(unsigned i=0; i<4; ++i)
298 throw invalid_module("Unterminated SPIR-V string literal");
301 void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
303 for(CodeIterator op=code.begin()+5; op!=code.end(); )
305 unsigned word_count = *op>>16;
306 if(word_count>code.end()-op)
307 throw invalid_module("Truncated SPIR-V instruction");
309 switch(get_opcode(*op))
311 case OP_NAME: reflect_name(op); break;
312 case OP_MEMBER_NAME: reflect_member_name(op); break;
313 case OP_ENTRY_POINT: reflect_entry_point(op); break;
314 case OP_TYPE_VOID: reflect_void_type(op); break;
315 case OP_TYPE_BOOL: reflect_bool_type(op); break;
316 case OP_TYPE_INT: reflect_int_type(op); break;
317 case OP_TYPE_FLOAT: reflect_float_type(op); break;
318 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
319 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
320 case OP_TYPE_IMAGE: reflect_image_type(op); break;
321 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
322 case OP_TYPE_ARRAY: reflect_array_type(op); break;
323 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
324 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
325 case OP_CONSTANT_TRUE: constants[*(op+2)] = true; break;
326 case OP_CONSTANT_FALSE: constants[*(op+2)] = false; break;
327 case OP_CONSTANT: reflect_constant(op); break;
328 case OP_SPEC_CONSTANT_TRUE:
329 case OP_SPEC_CONSTANT_FALSE:
330 case OP_SPEC_CONSTANT: reflect_spec_constant(op); break;
331 case OP_VARIABLE: reflect_variable(op); break;
332 case OP_DECORATE: reflect_decorate(op); break;
333 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
340 void SpirVModule::Reflection::reflect_name(CodeIterator op)
342 CodeIterator op_end = get_op_end(op);
343 string &name = names[*(op+1)];
345 name = read_string(op, op_end);
348 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
350 CodeIterator op_end = get_op_end(op);
351 Structure &strct = structs[*(op+1)];
352 unsigned index = *(op+2);
353 if(index>=strct.members.size())
354 strct.members.resize(index+1);
356 strct.members[index].name = read_string(op, op_end);
359 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
361 CodeIterator op_end = get_op_end(op);
362 EntryPoint &entry = entry_points[*(op+2)];
363 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
365 entry.name = read_string(op, op_end);
367 entry.globals.reserve(op_end-op);
368 for(; op!=op_end; ++op)
369 entry.globals.push_back(&variables[*op]);
372 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
374 types[*(op+1)].type = VOID;
377 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
379 types[*(op+1)].type = BOOL;
382 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
384 TypeInfo &type = types[*(op+1)];
385 unsigned size = *(op+2);
387 type.type = static_cast<DataType>(size/8 | sign*0x100);
390 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
392 TypeInfo &type = types[*(op+1)];
393 unsigned size = *(op+2);
394 type.type = static_cast<DataType>(size/8 | 0x300);
397 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
399 TypeInfo &type = types[*(op+1)];
400 DataType component = types[*(op+2)].type;
401 unsigned count = *(op+3);
402 type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
405 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
407 TypeInfo &type = types[*(op+1)];
408 DataType column = types[*(op+2)].type;
409 unsigned count = *(op+3);
410 type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
413 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
415 TypeInfo &type = types[*(op+1)];
416 DataType sample = types[*(op+2)].type;
417 unsigned dimensions = *(op+3);
418 bool depth = *(op+4)==1;
419 bool array = *(op+5);
420 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
423 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
425 TypeInfo &type = types[*(op+1)];
426 DataType image = types[*(op+2)].type;
427 type.type = static_cast<DataType>(image | 0x100000);
430 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
432 TypeInfo &type = types[*(op+1)];
433 const TypeInfo &elem = types[*(op+2)];
434 type.type = elem.type;
435 type.struct_type = elem.struct_type;
436 const Variant &size = constants[*(op+3)];
437 if(size.check_type<int>())
438 type.array_size = size.value<int>();
439 else if(size.check_type<unsigned>())
440 type.array_size = size.value<unsigned>();
443 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
445 CodeIterator op_end = get_op_end(op);
446 unsigned id = *(op+1);
447 Structure &strct = structs[id];
448 strct.name = names[id];
449 types[id].struct_type = &strct;
452 strct.members.resize(op_end-op);
453 vector<StructMember>::iterator mem = strct.members.begin();
454 for(; op!=op_end; ++op, ++mem)
456 TypeInfo &type = types[*op];
457 mem->type = type.type;
458 mem->struct_type = type.struct_type;
459 mem->array_size = type.array_size;
460 mem->array_stride = type.array_stride;
464 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
466 TypeInfo &type = types[*(op+1)];
467 type = types[*(op+3)];
468 type.storage = static_cast<StorageClass>(*(op+2));
471 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
473 const TypeInfo &type = types[*(op+1)];
474 unsigned id = *(op+2);
476 constants[id] = static_cast<int>(*(op+3));
477 else if(type.type==UNSIGNED_INT)
478 constants[id] = static_cast<unsigned>(*(op+3));
479 else if(type.type==FLOAT)
480 constants[id] = *reinterpret_cast<const float *>(&*(op+3));
483 void SpirVModule::Reflection::reflect_spec_constant(CodeIterator op)
485 unsigned id = *(op+2);
486 SpecConstant &spec = spec_constants[id];
487 spec.name = names[id];
488 spec.type = types[*(op+1)].type;
491 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
493 unsigned id = *(op+2);
494 Variable &var = variables[id];
495 var.name = names[id];
496 const TypeInfo &type = types[*(op+1)];
497 var.storage = type.storage;
498 var.type = type.type;
499 var.struct_type = type.struct_type;
500 var.array_size = type.array_size;
503 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
505 unsigned id = *(op+1);
506 unsigned decoration = *(op+2);
512 spec_constants[id].constant_id = *op;
514 case DECO_ARRAY_STRIDE:
515 types[id].array_stride = *op;
518 variables[id].location = *op;
521 variables[id].binding = *op;
523 case DECO_DESCRIPTOR_SET:
524 variables[id].descriptor_set = *op;
529 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
531 Structure &strct = structs[*(op+1)];
532 unsigned index = *(op+2);
533 if(index>=strct.members.size())
534 strct.members.resize(index+1);
535 unsigned decoration = *(op+3);
538 StructMember &member = strct.members[index];
541 case DECO_MATRIX_STRIDE:
542 member.matrix_stride = *op;