1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
10 SPIRV_MAGIC = 0x07230203,
11 SPIRV_MAGIC_REVERSED = 0x03022307,
23 OP_TYPE_SAMPLED_IMAGE = 27,
27 OP_CONSTANT_TRUE = 41,
28 OP_CONSTANT_FALSE = 42,
30 OP_SPEC_CONSTANT_TRUE = 48,
31 OP_SPEC_CONSTANT_FALSE = 49,
32 OP_SPEC_CONSTANT = 50,
35 OP_MEMBER_DECORATE = 72,
38 DECO_ARRAY_STRIDE = 6,
39 DECO_MATRIX_STRIDE = 7,
43 DECO_DESCRIPTOR_SET = 34,
50 void Module::set_source(const string &src)
52 SL::Compiler compiler;
53 compiler.set_source(src);
57 void Module::load_source(IO::Base &io, Resources *res, const string &name)
59 SL::Compiler compiler;
60 compiler.load_source(io, res, name);
64 void Module::load_source(IO::Base &io, const string &name)
66 load_source(io, 0, name);
70 void GlslModule::compile(SL::Compiler &compiler)
72 compiler.compile(SL::Compiler::MODULE);
73 prepared_source = compiler.get_combined_glsl();
74 source_map = compiler.get_source_map();
77 string diagnostics = compiler.get_diagnostics();
78 if(!diagnostics.empty())
79 IO::print("Module diagnostics:\n%s\n", diagnostics);
84 SpirVModule::SpirVModule(const SpirVModule &other):
86 entry_points(other.entry_points),
87 structs(other.structs),
88 variables(other.variables)
90 remap_pointers_from(other);
93 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
96 entry_points = other.entry_points;
97 structs = other.structs;
98 variables = other.variables;
99 remap_pointers_from(other);
103 void SpirVModule::remap_pointers_from(const SpirVModule &other)
105 for(EntryPoint &e: entry_points)
106 for(const Variable *&v: e.globals)
107 v = &variables[v-&other.variables.front()];
109 for(Variable &v: variables)
111 v.struct_type = &structs[v.struct_type-&other.structs.front()];
113 for(Structure &s: structs)
114 for(StructMember &m: s.members)
116 m.struct_type = &structs[m.struct_type-&other.structs.front()];
119 void SpirVModule::load_code(IO::Base &io)
121 uint32_t buffer[1024];
124 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
128 code.reserve(code.size()+len);
129 code.insert(code.end(), buffer, buffer+len);
135 void SpirVModule::compile(SL::Compiler &compiler)
137 compiler.compile(SL::Compiler::SPIRV);
138 code = compiler.get_combined_spirv();
142 void SpirVModule::reflect()
145 throw invalid_module("Empty SPIR-V code");
147 if(code[0]==SPIRV_MAGIC_REVERSED)
149 for(uint32_t &c: code)
150 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
152 else if(code[0]!=SPIRV_MAGIC)
153 throw invalid_module("SPIR-V magic number not found");
155 Reflection reflection;
156 reflection.reflect_code(code);
158 map<const Constant *, unsigned> spec_indices;
159 for(const auto &kvp: reflection.constants)
160 if(kvp.second.constant_id>=0)
162 spec_indices[&kvp.second] = spec_constants.size();
163 spec_constants.push_back(kvp.second);
166 map<const Structure *, unsigned> struct_indices;
167 structs.reserve(reflection.structs.size());
168 for(const auto &kvp: reflection.structs)
170 struct_indices[&kvp.second] = structs.size();
171 structs.push_back(kvp.second);
174 for(Structure &s: structs)
176 for(StructMember &m: s.members)
179 auto i = struct_indices.find(m.struct_type);
180 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
183 const StructMember *last_member = &s.members.back();
184 unsigned last_offset = last_member->offset;
185 while(last_member->struct_type)
187 const StructMember *lm = &last_member->struct_type->members.back();
188 if(last_member->array_size)
189 last_offset += last_member->array_stride*(last_member->array_size-1);
190 last_offset += lm->offset;
194 unsigned last_size = get_type_size(last_member->type);
195 if(last_member->array_size)
196 last_size += last_member->array_stride*(last_member->array_size-1);
197 s.size = last_offset+last_size;
198 s.size = (s.size+15)&~15;
201 map<const Variable *, unsigned> var_indices;
202 variables.reserve(reflection.variables.size());
203 for(const auto &kvp: reflection.variables)
205 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
206 if(i!=variables.end())
207 var_indices[&kvp.second] = i-variables.begin();
210 var_indices[&kvp.second] = variables.size();
211 variables.push_back(kvp.second);
215 for(Variable &v: variables)
218 auto i = struct_indices.find(v.struct_type);
219 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
222 entry_points.reserve(reflection.entry_points.size());
223 for(const auto &kvp: reflection.entry_points)
225 entry_points.push_back(kvp.second);
226 EntryPoint &entry = entry_points.back();
227 for(const Variable *&v: entry.globals)
229 auto i = var_indices.find(v);
230 v = (i!=var_indices.end() ? &variables[i->second] : 0);
236 bool SpirVModule::Variable::operator==(const Variable &other) const
238 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
240 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
242 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
248 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
253 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
258 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
261 for(; op!=op_end; ++op)
264 for(unsigned i=0; i<4; ++i)
277 throw invalid_module("Unterminated SPIR-V string literal");
280 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
282 for(CodeIterator op=code.begin()+5; op!=code.end(); )
284 unsigned word_count = *op>>16;
285 if(word_count>static_cast<unsigned>(code.end()-op))
286 throw invalid_module("Truncated SPIR-V instruction");
288 switch(get_opcode(*op))
290 case OP_NAME: reflect_name(op); break;
291 case OP_MEMBER_NAME: reflect_member_name(op); break;
292 case OP_ENTRY_POINT: reflect_entry_point(op); break;
293 case OP_TYPE_VOID: reflect_void_type(op); break;
294 case OP_TYPE_BOOL: reflect_bool_type(op); break;
295 case OP_TYPE_INT: reflect_int_type(op); break;
296 case OP_TYPE_FLOAT: reflect_float_type(op); break;
297 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
298 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
299 case OP_TYPE_IMAGE: reflect_image_type(op); break;
300 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
301 case OP_TYPE_ARRAY: reflect_array_type(op); break;
302 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
303 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
304 case OP_CONSTANT_TRUE:
305 case OP_CONSTANT_FALSE:
307 case OP_SPEC_CONSTANT_TRUE:
308 case OP_SPEC_CONSTANT_FALSE:
309 case OP_SPEC_CONSTANT: reflect_constant(op); break;
310 case OP_VARIABLE: reflect_variable(op); break;
311 case OP_DECORATE: reflect_decorate(op); break;
312 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
319 void SpirVModule::Reflection::reflect_name(CodeIterator op)
321 CodeIterator op_end = get_op_end(op);
322 string &name = names[*(op+1)];
324 name = read_string(op, op_end);
327 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
329 CodeIterator op_end = get_op_end(op);
330 Structure &strct = structs[*(op+1)];
331 unsigned index = *(op+2);
332 if(index>=strct.members.size())
333 strct.members.resize(index+1);
335 strct.members[index].name = read_string(op, op_end);
338 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
340 CodeIterator op_end = get_op_end(op);
341 EntryPoint &entry = entry_points[*(op+2)];
342 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
344 entry.name = read_string(op, op_end);
346 entry.globals.reserve(op_end-op);
347 for(; op!=op_end; ++op)
348 entry.globals.push_back(&variables[*op]);
351 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
353 types[*(op+1)].type = VOID;
356 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
358 types[*(op+1)].type = BOOL;
361 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
363 TypeInfo &type = types[*(op+1)];
364 unsigned size = *(op+2);
366 type.type = static_cast<DataType>(size/8 | sign*0x100);
369 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
371 TypeInfo &type = types[*(op+1)];
372 unsigned size = *(op+2);
373 type.type = static_cast<DataType>(size/8 | 0x300);
376 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
378 TypeInfo &type = types[*(op+1)];
379 DataType component = types[*(op+2)].type;
380 unsigned count = *(op+3);
381 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
384 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
386 TypeInfo &type = types[*(op+1)];
387 DataType column = types[*(op+2)].type;
388 unsigned count = *(op+3);
389 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
392 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
394 TypeInfo &type = types[*(op+1)];
395 DataType sample = types[*(op+2)].type;
396 unsigned dimensions = *(op+3);
397 bool depth = *(op+4)==1;
398 bool array = *(op+5);
399 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
402 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
404 TypeInfo &type = types[*(op+1)];
405 DataType image = types[*(op+2)].type;
406 type.type = static_cast<DataType>(image | 0x100000);
409 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
411 TypeInfo &type = types[*(op+1)];
412 const TypeInfo &elem = types[*(op+2)];
413 type.type = elem.type;
414 type.struct_type = elem.struct_type;
416 const Constant &size = constants[*(op+3)];
417 if(size.type==INT || size.type==UNSIGNED_INT)
418 type.array_size = size.i_value;
421 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
423 CodeIterator op_end = get_op_end(op);
424 unsigned id = *(op+1);
425 Structure &strct = structs[id];
426 strct.name = names[id];
427 types[id].struct_type = &strct;
430 strct.members.resize(op_end-op);
431 auto mem = strct.members.begin();
432 for(; op!=op_end; ++op, ++mem)
434 TypeInfo &type = types[*op];
435 mem->type = type.type;
436 mem->struct_type = type.struct_type;
437 mem->array_size = type.array_size;
438 mem->array_stride = type.array_stride;
442 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
444 TypeInfo &type = types[*(op+1)];
445 type = types[*(op+3)];
446 type.storage = static_cast<StorageClass>(*(op+2));
449 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
451 unsigned id = *(op+2);
452 Constant &cnst = constants[id];
453 cnst.name = names[id];
454 cnst.type = types[*(op+1)].type;
455 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
457 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
458 cnst.i_value = false;
459 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
460 cnst.i_value = *(op+3);
461 else if(cnst.type==FLOAT)
462 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
465 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
467 unsigned id = *(op+2);
468 Variable &var = variables[id];
469 var.name = names[id];
470 const TypeInfo &type = types[*(op+1)];
471 var.storage = type.storage;
472 var.type = type.type;
473 var.struct_type = type.struct_type;
474 var.array_size = type.array_size;
477 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
479 unsigned id = *(op+1);
480 unsigned decoration = *(op+2);
486 constants[id].constant_id = *op;
488 case DECO_ARRAY_STRIDE:
489 types[id].array_stride = *op;
492 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
495 variables[id].location = *op;
498 variables[id].binding = *op;
500 case DECO_DESCRIPTOR_SET:
501 variables[id].descriptor_set = *op;
506 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
508 Structure &strct = structs[*(op+1)];
509 unsigned index = *(op+2);
510 if(index>=strct.members.size())
511 strct.members.resize(index+1);
512 unsigned decoration = *(op+3);
515 StructMember &member = strct.members[index];
518 case DECO_MATRIX_STRIDE:
519 member.matrix_stride = *op;
522 member.builtin = static_cast<BuiltinSemantic>(*op);