1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
3 #include "deviceinfo.h"
11 SPIRV_MAGIC = 0x07230203,
12 SPIRV_MAGIC_REVERSED = 0x03022307,
24 OP_TYPE_SAMPLED_IMAGE = 27,
28 OP_CONSTANT_TRUE = 41,
29 OP_CONSTANT_FALSE = 42,
31 OP_SPEC_CONSTANT_TRUE = 48,
32 OP_SPEC_CONSTANT_FALSE = 49,
33 OP_SPEC_CONSTANT = 50,
36 OP_MEMBER_DECORATE = 72,
39 DECO_ARRAY_STRIDE = 6,
40 DECO_MATRIX_STRIDE = 7,
44 DECO_DESCRIPTOR_SET = 34,
51 void Module::set_source(const string &src)
53 SL::Compiler compiler(DeviceInfo::get_global().glsl_features);
54 compiler.set_source(src);
58 void Module::load_source(IO::Base &io, Resources *res, const string &name)
60 SL::Compiler compiler(DeviceInfo::get_global().glsl_features);
61 compiler.load_source(io, res, name);
65 void Module::load_source(IO::Base &io, const string &name)
67 load_source(io, 0, name);
71 void GlslModule::compile(SL::Compiler &compiler)
73 compiler.compile(SL::Compiler::MODULE);
74 prepared_source = compiler.get_combined_glsl();
75 source_map = compiler.get_source_map();
78 string diagnostics = compiler.get_diagnostics();
79 if(!diagnostics.empty())
80 IO::print("Module diagnostics:\n%s\n", diagnostics);
85 SpirVModule::SpirVModule(const SpirVModule &other):
87 entry_points(other.entry_points),
88 structs(other.structs),
89 variables(other.variables)
91 remap_pointers_from(other);
94 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
97 entry_points = other.entry_points;
98 structs = other.structs;
99 variables = other.variables;
100 remap_pointers_from(other);
104 void SpirVModule::remap_pointers_from(const SpirVModule &other)
106 for(EntryPoint &e: entry_points)
107 for(const Variable *&v: e.globals)
108 v = &variables[v-&other.variables.front()];
110 for(Variable &v: variables)
112 v.struct_type = &structs[v.struct_type-&other.structs.front()];
114 for(Structure &s: structs)
115 for(StructMember &m: s.members)
117 m.struct_type = &structs[m.struct_type-&other.structs.front()];
120 void SpirVModule::load_code(IO::Base &io)
122 uint32_t buffer[1024];
125 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
129 code.reserve(code.size()+len);
130 code.insert(code.end(), buffer, buffer+len);
136 void SpirVModule::compile(SL::Compiler &compiler)
138 compiler.compile(SL::Compiler::SPIRV);
139 code = compiler.get_combined_spirv();
143 void SpirVModule::reflect()
146 throw invalid_module("Empty SPIR-V code");
148 if(code[0]==SPIRV_MAGIC_REVERSED)
150 for(uint32_t &c: code)
151 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
153 else if(code[0]!=SPIRV_MAGIC)
154 throw invalid_module("SPIR-V magic number not found");
156 Reflection reflection;
157 reflection.reflect_code(code);
159 map<const Constant *, unsigned> spec_indices;
160 for(const auto &kvp: reflection.constants)
161 if(kvp.second.constant_id>=0)
163 spec_indices[&kvp.second] = spec_constants.size();
164 spec_constants.push_back(kvp.second);
167 map<const Structure *, unsigned> struct_indices;
168 structs.reserve(reflection.structs.size());
169 for(const auto &kvp: reflection.structs)
171 struct_indices[&kvp.second] = structs.size();
172 structs.push_back(kvp.second);
175 for(Structure &s: structs)
177 for(StructMember &m: s.members)
180 auto i = struct_indices.find(m.struct_type);
181 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
184 const StructMember *last_member = &s.members.back();
185 unsigned last_offset = last_member->offset;
186 while(last_member->struct_type)
188 const StructMember *lm = &last_member->struct_type->members.back();
189 if(last_member->array_size)
190 last_offset += last_member->array_stride*(last_member->array_size-1);
191 last_offset += lm->offset;
195 unsigned last_size = get_type_size(last_member->type);
196 if(last_member->array_size)
197 last_size += last_member->array_stride*(last_member->array_size-1);
198 s.size = last_offset+last_size;
199 s.size = (s.size+15)&~15;
202 map<const Variable *, unsigned> var_indices;
203 variables.reserve(reflection.variables.size());
204 for(const auto &kvp: reflection.variables)
206 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
207 if(i!=variables.end())
208 var_indices[&kvp.second] = i-variables.begin();
211 var_indices[&kvp.second] = variables.size();
212 variables.push_back(kvp.second);
216 for(Variable &v: variables)
219 auto i = struct_indices.find(v.struct_type);
220 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
223 entry_points.reserve(reflection.entry_points.size());
224 for(const auto &kvp: reflection.entry_points)
226 entry_points.push_back(kvp.second);
227 EntryPoint &entry = entry_points.back();
228 for(const Variable *&v: entry.globals)
230 auto i = var_indices.find(v);
231 v = (i!=var_indices.end() ? &variables[i->second] : 0);
237 bool SpirVModule::Variable::operator==(const Variable &other) const
239 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
241 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
243 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
249 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
254 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
259 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
262 for(; op!=op_end; ++op)
265 for(unsigned i=0; i<4; ++i)
278 throw invalid_module("Unterminated SPIR-V string literal");
281 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
283 for(CodeIterator op=code.begin()+5; op!=code.end(); )
285 unsigned word_count = *op>>16;
286 if(word_count>static_cast<unsigned>(code.end()-op))
287 throw invalid_module("Truncated SPIR-V instruction");
289 switch(get_opcode(*op))
291 case OP_NAME: reflect_name(op); break;
292 case OP_MEMBER_NAME: reflect_member_name(op); break;
293 case OP_ENTRY_POINT: reflect_entry_point(op); break;
294 case OP_TYPE_VOID: reflect_void_type(op); break;
295 case OP_TYPE_BOOL: reflect_bool_type(op); break;
296 case OP_TYPE_INT: reflect_int_type(op); break;
297 case OP_TYPE_FLOAT: reflect_float_type(op); break;
298 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
299 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
300 case OP_TYPE_IMAGE: reflect_image_type(op); break;
301 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
302 case OP_TYPE_ARRAY: reflect_array_type(op); break;
303 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
304 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
305 case OP_CONSTANT_TRUE:
306 case OP_CONSTANT_FALSE:
308 case OP_SPEC_CONSTANT_TRUE:
309 case OP_SPEC_CONSTANT_FALSE:
310 case OP_SPEC_CONSTANT: reflect_constant(op); break;
311 case OP_VARIABLE: reflect_variable(op); break;
312 case OP_DECORATE: reflect_decorate(op); break;
313 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
320 void SpirVModule::Reflection::reflect_name(CodeIterator op)
322 CodeIterator op_end = get_op_end(op);
323 string &name = names[*(op+1)];
325 name = read_string(op, op_end);
328 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
330 CodeIterator op_end = get_op_end(op);
331 Structure &strct = structs[*(op+1)];
332 unsigned index = *(op+2);
333 if(index>=strct.members.size())
334 strct.members.resize(index+1);
336 strct.members[index].name = read_string(op, op_end);
339 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
341 CodeIterator op_end = get_op_end(op);
342 EntryPoint &entry = entry_points[*(op+2)];
343 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
345 entry.name = read_string(op, op_end);
347 entry.globals.reserve(op_end-op);
348 for(; op!=op_end; ++op)
349 entry.globals.push_back(&variables[*op]);
352 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
354 types[*(op+1)].type = VOID;
357 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
359 types[*(op+1)].type = BOOL;
362 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
364 TypeInfo &type = types[*(op+1)];
365 unsigned size = *(op+2);
367 type.type = static_cast<DataType>(size/8 | sign*0x100);
370 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
372 TypeInfo &type = types[*(op+1)];
373 unsigned size = *(op+2);
374 type.type = static_cast<DataType>(size/8 | 0x300);
377 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
379 TypeInfo &type = types[*(op+1)];
380 DataType component = types[*(op+2)].type;
381 unsigned count = *(op+3);
382 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
385 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
387 TypeInfo &type = types[*(op+1)];
388 DataType column = types[*(op+2)].type;
389 unsigned count = *(op+3);
390 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
393 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
395 TypeInfo &type = types[*(op+1)];
396 DataType sample = types[*(op+2)].type;
397 unsigned dimensions = *(op+3);
398 bool depth = *(op+4)==1;
399 bool array = *(op+5);
400 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
403 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
405 TypeInfo &type = types[*(op+1)];
406 DataType image = types[*(op+2)].type;
407 type.type = static_cast<DataType>(image | 0x100000);
410 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
412 TypeInfo &type = types[*(op+1)];
413 const TypeInfo &elem = types[*(op+2)];
414 type.type = elem.type;
415 type.struct_type = elem.struct_type;
417 const Constant &size = constants[*(op+3)];
418 if(size.type==INT || size.type==UNSIGNED_INT)
419 type.array_size = size.i_value;
422 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
424 CodeIterator op_end = get_op_end(op);
425 unsigned id = *(op+1);
426 Structure &strct = structs[id];
427 strct.name = names[id];
428 types[id].struct_type = &strct;
431 strct.members.resize(op_end-op);
432 auto mem = strct.members.begin();
433 for(; op!=op_end; ++op, ++mem)
435 TypeInfo &type = types[*op];
436 mem->type = type.type;
437 mem->struct_type = type.struct_type;
438 mem->array_size = type.array_size;
439 mem->array_stride = type.array_stride;
443 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
445 TypeInfo &type = types[*(op+1)];
446 type = types[*(op+3)];
447 type.storage = static_cast<StorageClass>(*(op+2));
450 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
452 unsigned id = *(op+2);
453 Constant &cnst = constants[id];
454 cnst.name = names[id];
455 cnst.type = types[*(op+1)].type;
456 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
458 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
459 cnst.i_value = false;
460 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
461 cnst.i_value = *(op+3);
462 else if(cnst.type==FLOAT)
463 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
466 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
468 unsigned id = *(op+2);
469 Variable &var = variables[id];
470 var.name = names[id];
471 const TypeInfo &type = types[*(op+1)];
472 var.storage = type.storage;
473 var.type = type.type;
474 var.struct_type = type.struct_type;
475 var.array_size = type.array_size;
478 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
480 unsigned id = *(op+1);
481 unsigned decoration = *(op+2);
487 constants[id].constant_id = *op;
489 case DECO_ARRAY_STRIDE:
490 types[id].array_stride = *op;
493 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
496 variables[id].location = *op;
499 variables[id].binding = *op;
501 case DECO_DESCRIPTOR_SET:
502 variables[id].descriptor_set = *op;
507 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
509 Structure &strct = structs[*(op+1)];
510 unsigned index = *(op+2);
511 if(index>=strct.members.size())
512 strct.members.resize(index+1);
513 unsigned decoration = *(op+3);
516 StructMember &member = strct.members[index];
519 case DECO_MATRIX_STRIDE:
520 member.matrix_stride = *op;
523 member.builtin = static_cast<BuiltinSemantic>(*op);