1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.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 DeviceInfo &dev_info = Device::get_current().get_info();
73 const SL::Features &device_features = dev_info.glsl_features;
74 SL::Features latest_features = SL::Features::latest(dev_info.api);
75 SL::Features features;
76 features.target_api = latest_features.target_api;
77 features.glsl_version = latest_features.glsl_version;
78 features.constant_id_range = device_features.constant_id_range;
79 features.uniform_binding_range = device_features.uniform_binding_range;
80 features.texture_binding_range = device_features.texture_binding_range;
85 void GlslModule::compile(SL::Compiler &compiler)
87 compiler.compile(SL::Compiler::MODULE);
88 prepared_source = compiler.get_combined_glsl();
89 source_map = compiler.get_source_map();
92 string diagnostics = compiler.get_diagnostics();
93 if(!diagnostics.empty())
94 IO::print("Module diagnostics:\n%s\n", diagnostics);
99 SpirVModule::SpirVModule(const SpirVModule &other):
101 entry_points(other.entry_points),
102 structs(other.structs),
103 variables(other.variables)
105 remap_pointers_from(other);
108 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
111 entry_points = other.entry_points;
112 structs = other.structs;
113 variables = other.variables;
114 remap_pointers_from(other);
118 void SpirVModule::remap_pointers_from(const SpirVModule &other)
120 for(EntryPoint &e: entry_points)
121 for(const Variable *&v: e.globals)
122 v = &variables[v-&other.variables.front()];
124 for(Variable &v: variables)
126 v.struct_type = &structs[v.struct_type-&other.structs.front()];
128 for(Structure &s: structs)
129 for(StructMember &m: s.members)
131 m.struct_type = &structs[m.struct_type-&other.structs.front()];
134 void SpirVModule::load_code(IO::Base &io)
136 uint32_t buffer[1024];
139 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
143 code.reserve(code.size()+len);
144 code.insert(code.end(), buffer, buffer+len);
150 void SpirVModule::compile(SL::Compiler &compiler)
152 compiler.compile(SL::Compiler::SPIRV);
153 code = compiler.get_combined_spirv();
157 void SpirVModule::reflect()
160 throw invalid_module("Empty SPIR-V code");
162 if(code[0]==SPIRV_MAGIC_REVERSED)
164 for(uint32_t &c: code)
165 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
167 else if(code[0]!=SPIRV_MAGIC)
168 throw invalid_module("SPIR-V magic number not found");
170 Reflection reflection;
171 reflection.reflect_code(code);
173 map<const Constant *, unsigned> spec_indices;
174 for(const auto &kvp: reflection.constants)
175 if(kvp.second.constant_id>=0)
177 spec_indices[&kvp.second] = spec_constants.size();
178 spec_constants.push_back(kvp.second);
181 map<const Structure *, unsigned> struct_indices;
182 structs.reserve(reflection.structs.size());
183 for(const auto &kvp: reflection.structs)
185 struct_indices[&kvp.second] = structs.size();
186 structs.push_back(kvp.second);
189 for(Structure &s: structs)
191 for(StructMember &m: s.members)
194 auto i = struct_indices.find(m.struct_type);
195 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
198 const StructMember *last_member = &s.members.back();
199 unsigned last_offset = last_member->offset;
200 while(last_member->struct_type)
202 const StructMember *lm = &last_member->struct_type->members.back();
203 if(last_member->array_size)
204 last_offset += last_member->array_stride*(last_member->array_size-1);
205 last_offset += lm->offset;
209 unsigned last_size = get_type_size(last_member->type);
210 if(last_member->array_size)
211 last_size += last_member->array_stride*(last_member->array_size-1);
212 s.size = last_offset+last_size;
213 s.size = (s.size+15)&~15;
216 map<const Variable *, unsigned> var_indices;
217 variables.reserve(reflection.variables.size());
218 for(const auto &kvp: reflection.variables)
220 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
221 if(i!=variables.end())
222 var_indices[&kvp.second] = i-variables.begin();
225 var_indices[&kvp.second] = variables.size();
226 variables.push_back(kvp.second);
230 for(Variable &v: variables)
233 auto i = struct_indices.find(v.struct_type);
234 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
237 entry_points.reserve(reflection.entry_points.size());
238 for(const auto &kvp: reflection.entry_points)
240 entry_points.push_back(kvp.second);
241 EntryPoint &entry = entry_points.back();
242 for(const Variable *&v: entry.globals)
244 auto i = var_indices.find(v);
245 v = (i!=var_indices.end() ? &variables[i->second] : 0);
251 bool SpirVModule::Variable::operator==(const Variable &other) const
253 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
255 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
257 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
263 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
268 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
273 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
276 for(; op!=op_end; ++op)
279 for(unsigned i=0; i<4; ++i)
292 throw invalid_module("Unterminated SPIR-V string literal");
295 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
297 for(CodeIterator op=code.begin()+5; op!=code.end(); )
299 unsigned word_count = *op>>16;
300 if(word_count>static_cast<unsigned>(code.end()-op))
301 throw invalid_module("Truncated SPIR-V instruction");
303 switch(get_opcode(*op))
305 case OP_NAME: reflect_name(op); break;
306 case OP_MEMBER_NAME: reflect_member_name(op); break;
307 case OP_ENTRY_POINT: reflect_entry_point(op); break;
308 case OP_TYPE_VOID: reflect_void_type(op); break;
309 case OP_TYPE_BOOL: reflect_bool_type(op); break;
310 case OP_TYPE_INT: reflect_int_type(op); break;
311 case OP_TYPE_FLOAT: reflect_float_type(op); break;
312 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
313 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
314 case OP_TYPE_IMAGE: reflect_image_type(op); break;
315 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
316 case OP_TYPE_ARRAY: reflect_array_type(op); break;
317 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
318 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
319 case OP_CONSTANT_TRUE:
320 case OP_CONSTANT_FALSE:
322 case OP_SPEC_CONSTANT_TRUE:
323 case OP_SPEC_CONSTANT_FALSE:
324 case OP_SPEC_CONSTANT: reflect_constant(op); break;
325 case OP_VARIABLE: reflect_variable(op); break;
326 case OP_DECORATE: reflect_decorate(op); break;
327 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
334 void SpirVModule::Reflection::reflect_name(CodeIterator op)
336 CodeIterator op_end = get_op_end(op);
337 string &name = names[*(op+1)];
339 name = read_string(op, op_end);
342 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
344 CodeIterator op_end = get_op_end(op);
345 Structure &strct = structs[*(op+1)];
346 unsigned index = *(op+2);
347 if(index>=strct.members.size())
348 strct.members.resize(index+1);
350 strct.members[index].name = read_string(op, op_end);
353 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
355 CodeIterator op_end = get_op_end(op);
356 EntryPoint &entry = entry_points[*(op+2)];
357 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
359 entry.name = read_string(op, op_end);
361 entry.globals.reserve(op_end-op);
362 for(; op!=op_end; ++op)
363 entry.globals.push_back(&variables[*op]);
366 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
368 types[*(op+1)].type = VOID;
371 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
373 types[*(op+1)].type = BOOL;
376 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
378 TypeInfo &type = types[*(op+1)];
379 unsigned size = *(op+2);
381 type.type = static_cast<DataType>(size/8 | sign*0x100);
384 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
386 TypeInfo &type = types[*(op+1)];
387 unsigned size = *(op+2);
388 type.type = static_cast<DataType>(size/8 | 0x300);
391 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
393 TypeInfo &type = types[*(op+1)];
394 DataType component = types[*(op+2)].type;
395 unsigned count = *(op+3);
396 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
399 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
401 TypeInfo &type = types[*(op+1)];
402 DataType column = types[*(op+2)].type;
403 unsigned count = *(op+3);
404 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
407 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
409 TypeInfo &type = types[*(op+1)];
410 DataType sample = types[*(op+2)].type;
411 unsigned dimensions = *(op+3);
412 bool depth = *(op+4)==1;
413 bool array = *(op+5);
414 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
417 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
419 TypeInfo &type = types[*(op+1)];
420 DataType image = types[*(op+2)].type;
421 type.type = static_cast<DataType>(image | 0x100000);
424 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
426 TypeInfo &type = types[*(op+1)];
427 const TypeInfo &elem = types[*(op+2)];
428 type.type = elem.type;
429 type.struct_type = elem.struct_type;
431 const Constant &size = constants[*(op+3)];
432 if(size.type==INT || size.type==UNSIGNED_INT)
433 type.array_size = size.i_value;
436 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
438 CodeIterator op_end = get_op_end(op);
439 unsigned id = *(op+1);
440 Structure &strct = structs[id];
441 strct.name = names[id];
442 types[id].struct_type = &strct;
445 strct.members.resize(op_end-op);
446 auto mem = strct.members.begin();
447 for(; op!=op_end; ++op, ++mem)
449 TypeInfo &type = types[*op];
450 mem->type = type.type;
451 mem->struct_type = type.struct_type;
452 mem->array_size = type.array_size;
453 mem->array_stride = type.array_stride;
457 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
459 TypeInfo &type = types[*(op+1)];
460 type = types[*(op+3)];
461 type.storage = static_cast<StorageClass>(*(op+2));
464 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
466 unsigned id = *(op+2);
467 Constant &cnst = constants[id];
468 cnst.name = names[id];
469 cnst.type = types[*(op+1)].type;
470 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
472 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
473 cnst.i_value = false;
474 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
475 cnst.i_value = *(op+3);
476 else if(cnst.type==FLOAT)
477 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
480 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
482 unsigned id = *(op+2);
483 Variable &var = variables[id];
484 var.name = names[id];
485 const TypeInfo &type = types[*(op+1)];
486 var.storage = type.storage;
487 var.type = type.type;
488 var.struct_type = type.struct_type;
489 var.array_size = type.array_size;
492 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
494 unsigned id = *(op+1);
495 unsigned decoration = *(op+2);
501 constants[id].constant_id = *op;
503 case DECO_ARRAY_STRIDE:
504 types[id].array_stride = *op;
507 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
510 variables[id].location = *op;
513 variables[id].binding = *op;
515 case DECO_DESCRIPTOR_SET:
516 variables[id].descriptor_set = *op;
521 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
523 Structure &strct = structs[*(op+1)];
524 unsigned index = *(op+2);
525 if(index>=strct.members.size())
526 strct.members.resize(index+1);
527 unsigned decoration = *(op+3);
530 StructMember &member = strct.members[index];
533 case DECO_MATRIX_STRIDE:
534 member.matrix_stride = *op;
537 member.builtin = static_cast<BuiltinSemantic>(*op);