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(create_features());
54 compiler.set_source(src);
58 void Module::load_source(IO::Base &io, Resources *res, const string &name)
60 SL::Compiler compiler(create_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);
70 SL::Features Module::create_features() const
72 const SL::Features &device_features = DeviceInfo::get_global().glsl_features;
73 SL::Features latest_features = SL::Features::latest(get_backend_api());
74 SL::Features features;
75 features.target_api = latest_features.target_api;
76 features.glsl_version = latest_features.glsl_version;
77 features.constant_id_range = device_features.constant_id_range;
78 features.uniform_binding_range = device_features.uniform_binding_range;
79 features.texture_binding_range = device_features.texture_binding_range;
84 void GlslModule::compile(SL::Compiler &compiler)
86 compiler.compile(SL::Compiler::MODULE);
87 prepared_source = compiler.get_combined_glsl();
88 source_map = compiler.get_source_map();
91 string diagnostics = compiler.get_diagnostics();
92 if(!diagnostics.empty())
93 IO::print("Module diagnostics:\n%s\n", diagnostics);
98 SpirVModule::SpirVModule(const SpirVModule &other):
100 entry_points(other.entry_points),
101 structs(other.structs),
102 variables(other.variables)
104 remap_pointers_from(other);
107 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
110 entry_points = other.entry_points;
111 structs = other.structs;
112 variables = other.variables;
113 remap_pointers_from(other);
117 void SpirVModule::remap_pointers_from(const SpirVModule &other)
119 for(EntryPoint &e: entry_points)
120 for(const Variable *&v: e.globals)
121 v = &variables[v-&other.variables.front()];
123 for(Variable &v: variables)
125 v.struct_type = &structs[v.struct_type-&other.structs.front()];
127 for(Structure &s: structs)
128 for(StructMember &m: s.members)
130 m.struct_type = &structs[m.struct_type-&other.structs.front()];
133 void SpirVModule::load_code(IO::Base &io)
135 uint32_t buffer[1024];
138 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
142 code.reserve(code.size()+len);
143 code.insert(code.end(), buffer, buffer+len);
149 void SpirVModule::compile(SL::Compiler &compiler)
151 compiler.compile(SL::Compiler::SPIRV);
152 code = compiler.get_combined_spirv();
156 void SpirVModule::reflect()
159 throw invalid_module("Empty SPIR-V code");
161 if(code[0]==SPIRV_MAGIC_REVERSED)
163 for(uint32_t &c: code)
164 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
166 else if(code[0]!=SPIRV_MAGIC)
167 throw invalid_module("SPIR-V magic number not found");
169 Reflection reflection;
170 reflection.reflect_code(code);
172 map<const Constant *, unsigned> spec_indices;
173 for(const auto &kvp: reflection.constants)
174 if(kvp.second.constant_id>=0)
176 spec_indices[&kvp.second] = spec_constants.size();
177 spec_constants.push_back(kvp.second);
180 map<const Structure *, unsigned> struct_indices;
181 structs.reserve(reflection.structs.size());
182 for(const auto &kvp: reflection.structs)
184 struct_indices[&kvp.second] = structs.size();
185 structs.push_back(kvp.second);
188 for(Structure &s: structs)
190 for(StructMember &m: s.members)
193 auto i = struct_indices.find(m.struct_type);
194 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
197 const StructMember *last_member = &s.members.back();
198 unsigned last_offset = last_member->offset;
199 while(last_member->struct_type)
201 const StructMember *lm = &last_member->struct_type->members.back();
202 if(last_member->array_size)
203 last_offset += last_member->array_stride*(last_member->array_size-1);
204 last_offset += lm->offset;
208 unsigned last_size = get_type_size(last_member->type);
209 if(last_member->array_size)
210 last_size += last_member->array_stride*(last_member->array_size-1);
211 s.size = last_offset+last_size;
212 s.size = (s.size+15)&~15;
215 map<const Variable *, unsigned> var_indices;
216 variables.reserve(reflection.variables.size());
217 for(const auto &kvp: reflection.variables)
219 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
220 if(i!=variables.end())
221 var_indices[&kvp.second] = i-variables.begin();
224 var_indices[&kvp.second] = variables.size();
225 variables.push_back(kvp.second);
229 for(Variable &v: variables)
232 auto i = struct_indices.find(v.struct_type);
233 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
236 entry_points.reserve(reflection.entry_points.size());
237 for(const auto &kvp: reflection.entry_points)
239 entry_points.push_back(kvp.second);
240 EntryPoint &entry = entry_points.back();
241 for(const Variable *&v: entry.globals)
243 auto i = var_indices.find(v);
244 v = (i!=var_indices.end() ? &variables[i->second] : 0);
250 bool SpirVModule::Variable::operator==(const Variable &other) const
252 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
254 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
256 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
262 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
267 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
272 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
275 for(; op!=op_end; ++op)
278 for(unsigned i=0; i<4; ++i)
291 throw invalid_module("Unterminated SPIR-V string literal");
294 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
296 for(CodeIterator op=code.begin()+5; op!=code.end(); )
298 unsigned word_count = *op>>16;
299 if(word_count>static_cast<unsigned>(code.end()-op))
300 throw invalid_module("Truncated SPIR-V instruction");
302 switch(get_opcode(*op))
304 case OP_NAME: reflect_name(op); break;
305 case OP_MEMBER_NAME: reflect_member_name(op); break;
306 case OP_ENTRY_POINT: reflect_entry_point(op); break;
307 case OP_TYPE_VOID: reflect_void_type(op); break;
308 case OP_TYPE_BOOL: reflect_bool_type(op); break;
309 case OP_TYPE_INT: reflect_int_type(op); break;
310 case OP_TYPE_FLOAT: reflect_float_type(op); break;
311 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
312 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
313 case OP_TYPE_IMAGE: reflect_image_type(op); break;
314 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
315 case OP_TYPE_ARRAY: reflect_array_type(op); break;
316 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
317 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
318 case OP_CONSTANT_TRUE:
319 case OP_CONSTANT_FALSE:
321 case OP_SPEC_CONSTANT_TRUE:
322 case OP_SPEC_CONSTANT_FALSE:
323 case OP_SPEC_CONSTANT: reflect_constant(op); break;
324 case OP_VARIABLE: reflect_variable(op); break;
325 case OP_DECORATE: reflect_decorate(op); break;
326 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
333 void SpirVModule::Reflection::reflect_name(CodeIterator op)
335 CodeIterator op_end = get_op_end(op);
336 string &name = names[*(op+1)];
338 name = read_string(op, op_end);
341 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
343 CodeIterator op_end = get_op_end(op);
344 Structure &strct = structs[*(op+1)];
345 unsigned index = *(op+2);
346 if(index>=strct.members.size())
347 strct.members.resize(index+1);
349 strct.members[index].name = read_string(op, op_end);
352 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
354 CodeIterator op_end = get_op_end(op);
355 EntryPoint &entry = entry_points[*(op+2)];
356 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
358 entry.name = read_string(op, op_end);
360 entry.globals.reserve(op_end-op);
361 for(; op!=op_end; ++op)
362 entry.globals.push_back(&variables[*op]);
365 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
367 types[*(op+1)].type = VOID;
370 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
372 types[*(op+1)].type = BOOL;
375 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
377 TypeInfo &type = types[*(op+1)];
378 unsigned size = *(op+2);
380 type.type = static_cast<DataType>(size/8 | sign*0x100);
383 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
385 TypeInfo &type = types[*(op+1)];
386 unsigned size = *(op+2);
387 type.type = static_cast<DataType>(size/8 | 0x300);
390 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
392 TypeInfo &type = types[*(op+1)];
393 DataType component = types[*(op+2)].type;
394 unsigned count = *(op+3);
395 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
398 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
400 TypeInfo &type = types[*(op+1)];
401 DataType column = types[*(op+2)].type;
402 unsigned count = *(op+3);
403 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
406 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
408 TypeInfo &type = types[*(op+1)];
409 DataType sample = types[*(op+2)].type;
410 unsigned dimensions = *(op+3);
411 bool depth = *(op+4)==1;
412 bool array = *(op+5);
413 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
416 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
418 TypeInfo &type = types[*(op+1)];
419 DataType image = types[*(op+2)].type;
420 type.type = static_cast<DataType>(image | 0x100000);
423 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
425 TypeInfo &type = types[*(op+1)];
426 const TypeInfo &elem = types[*(op+2)];
427 type.type = elem.type;
428 type.struct_type = elem.struct_type;
430 const Constant &size = constants[*(op+3)];
431 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_stride = type.array_stride;
456 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
458 TypeInfo &type = types[*(op+1)];
459 type = types[*(op+3)];
460 type.storage = static_cast<StorageClass>(*(op+2));
463 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
465 unsigned id = *(op+2);
466 Constant &cnst = constants[id];
467 cnst.name = names[id];
468 cnst.type = types[*(op+1)].type;
469 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
471 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
472 cnst.i_value = false;
473 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
474 cnst.i_value = *(op+3);
475 else if(cnst.type==FLOAT)
476 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
479 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
481 unsigned id = *(op+2);
482 Variable &var = variables[id];
483 var.name = names[id];
484 const TypeInfo &type = types[*(op+1)];
485 var.storage = type.storage;
486 var.type = type.type;
487 var.struct_type = type.struct_type;
488 var.array_size = type.array_size;
491 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
493 unsigned id = *(op+1);
494 unsigned decoration = *(op+2);
500 constants[id].constant_id = *op;
502 case DECO_ARRAY_STRIDE:
503 types[id].array_stride = *op;
506 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
509 variables[id].location = *op;
512 variables[id].binding = *op;
514 case DECO_DESCRIPTOR_SET:
515 variables[id].descriptor_set = *op;
520 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
522 Structure &strct = structs[*(op+1)];
523 unsigned index = *(op+2);
524 if(index>=strct.members.size())
525 strct.members.resize(index+1);
526 unsigned decoration = *(op+3);
529 StructMember &member = strct.members[index];
532 case DECO_MATRIX_STRIDE:
533 member.matrix_stride = *op;
536 member.builtin = static_cast<BuiltinSemantic>(*op);