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 void SpirVModule::load_code(IO::Base &io)
101 uint32_t buffer[1024];
104 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
108 code.reserve(code.size()+len);
109 code.insert(code.end(), buffer, buffer+len);
115 void SpirVModule::compile(SL::Compiler &compiler)
117 compiler.compile(SL::Compiler::SPIRV);
118 code = compiler.get_combined_spirv();
122 void SpirVModule::reflect()
125 throw invalid_module("Empty SPIR-V code");
127 if(code[0]==SPIRV_MAGIC_REVERSED)
129 for(uint32_t &c: code)
130 c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
132 else if(code[0]!=SPIRV_MAGIC)
133 throw invalid_module("SPIR-V magic number not found");
135 Reflection reflection;
136 reflection.reflect_code(code);
138 map<const Constant *, unsigned> spec_indices;
139 for(const auto &kvp: reflection.constants)
140 if(kvp.second.constant_id>=0)
142 spec_indices[&kvp.second] = spec_constants.size();
143 spec_constants.push_back(kvp.second);
146 map<const Structure *, unsigned> struct_indices;
147 structs.reserve(reflection.structs.size());
148 for(const auto &kvp: reflection.structs)
150 struct_indices[&kvp.second] = structs.size();
151 structs.push_back(kvp.second);
154 for(Structure &s: structs)
156 for(StructMember &m: s.members)
159 auto i = struct_indices.find(m.struct_type);
160 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
163 const StructMember *last_member = &s.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 unsigned last_size = get_type_size(last_member->type);
175 if(last_member->array_size)
176 last_size += last_member->array_stride*(last_member->array_size-1);
177 s.size = last_offset+last_size;
178 s.size = (s.size+15)&~15;
181 map<const Variable *, unsigned> var_indices;
182 variables.reserve(reflection.variables.size());
183 for(const auto &kvp: reflection.variables)
185 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
186 if(i!=variables.end())
187 var_indices[&kvp.second] = i-variables.begin();
190 var_indices[&kvp.second] = variables.size();
191 variables.push_back(kvp.second);
195 for(Variable &v: variables)
198 auto i = struct_indices.find(v.struct_type);
199 v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
202 entry_points.reserve(reflection.entry_points.size());
203 for(const auto &kvp: reflection.entry_points)
205 entry_points.push_back(kvp.second);
206 EntryPoint &entry = entry_points.back();
207 for(const Variable *&v: entry.globals)
209 auto i = var_indices.find(v);
210 v = (i!=var_indices.end() ? &variables[i->second] : 0);
216 bool SpirVModule::Variable::operator==(const Variable &other) const
218 if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
220 if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
222 if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
228 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
233 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
238 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
241 for(; op!=op_end; ++op)
244 for(unsigned i=0; i<4; ++i)
257 throw invalid_module("Unterminated SPIR-V string literal");
260 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
262 for(CodeIterator op=code.begin()+5; op!=code.end(); )
264 unsigned word_count = *op>>16;
265 if(word_count>static_cast<unsigned>(code.end()-op))
266 throw invalid_module("Truncated SPIR-V instruction");
268 switch(get_opcode(*op))
270 case OP_NAME: reflect_name(op); break;
271 case OP_MEMBER_NAME: reflect_member_name(op); break;
272 case OP_ENTRY_POINT: reflect_entry_point(op); break;
273 case OP_TYPE_VOID: reflect_void_type(op); break;
274 case OP_TYPE_BOOL: reflect_bool_type(op); break;
275 case OP_TYPE_INT: reflect_int_type(op); break;
276 case OP_TYPE_FLOAT: reflect_float_type(op); break;
277 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
278 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
279 case OP_TYPE_IMAGE: reflect_image_type(op); break;
280 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
281 case OP_TYPE_ARRAY: reflect_array_type(op); break;
282 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
283 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
284 case OP_CONSTANT_TRUE:
285 case OP_CONSTANT_FALSE:
287 case OP_SPEC_CONSTANT_TRUE:
288 case OP_SPEC_CONSTANT_FALSE:
289 case OP_SPEC_CONSTANT: reflect_constant(op); break;
290 case OP_VARIABLE: reflect_variable(op); break;
291 case OP_DECORATE: reflect_decorate(op); break;
292 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
299 void SpirVModule::Reflection::reflect_name(CodeIterator op)
301 CodeIterator op_end = get_op_end(op);
302 string &name = names[*(op+1)];
304 name = read_string(op, op_end);
307 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
309 CodeIterator op_end = get_op_end(op);
310 Structure &strct = structs[*(op+1)];
311 unsigned index = *(op+2);
312 if(index>=strct.members.size())
313 strct.members.resize(index+1);
315 strct.members[index].name = read_string(op, op_end);
318 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
320 CodeIterator op_end = get_op_end(op);
321 EntryPoint &entry = entry_points[*(op+2)];
322 entry.stage = static_cast<Stage>(*(op+1)); // Execution model in SPIR-V spec
324 entry.name = read_string(op, op_end);
326 entry.globals.reserve(op_end-op);
327 for(; op!=op_end; ++op)
328 entry.globals.push_back(&variables[*op]);
331 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
333 types[*(op+1)].type = VOID;
336 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
338 types[*(op+1)].type = BOOL;
341 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
343 TypeInfo &type = types[*(op+1)];
344 unsigned size = *(op+2);
346 type.type = static_cast<DataType>(size/8 | sign*0x100);
349 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
351 TypeInfo &type = types[*(op+1)];
352 unsigned size = *(op+2);
353 type.type = static_cast<DataType>(size/8 | 0x300);
356 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
358 TypeInfo &type = types[*(op+1)];
359 DataType component = types[*(op+2)].type;
360 unsigned count = *(op+3);
361 type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
364 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
366 TypeInfo &type = types[*(op+1)];
367 DataType column = types[*(op+2)].type;
368 unsigned count = *(op+3);
369 type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
372 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
374 TypeInfo &type = types[*(op+1)];
375 DataType sample = types[*(op+2)].type;
376 unsigned dimensions = *(op+3);
377 bool depth = *(op+4)==1;
378 bool array = *(op+5);
379 type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
382 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
384 TypeInfo &type = types[*(op+1)];
385 DataType image = types[*(op+2)].type;
386 type.type = static_cast<DataType>(image | 0x100000);
389 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
391 TypeInfo &type = types[*(op+1)];
392 const TypeInfo &elem = types[*(op+2)];
393 type.type = elem.type;
394 type.struct_type = elem.struct_type;
396 const Constant &size = constants[*(op+3)];
397 if(size.type==INT || size.type==UNSIGNED_INT)
398 type.array_size = size.i_value;
401 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
403 CodeIterator op_end = get_op_end(op);
404 unsigned id = *(op+1);
405 Structure &strct = structs[id];
406 strct.name = names[id];
407 types[id].struct_type = &strct;
410 strct.members.resize(op_end-op);
411 auto mem = strct.members.begin();
412 for(; op!=op_end; ++op, ++mem)
414 TypeInfo &type = types[*op];
415 mem->type = type.type;
416 mem->struct_type = type.struct_type;
417 mem->array_size = type.array_size;
418 mem->array_stride = type.array_stride;
422 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
424 TypeInfo &type = types[*(op+1)];
425 type = types[*(op+3)];
426 type.storage = static_cast<StorageClass>(*(op+2));
429 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
431 unsigned id = *(op+2);
432 Constant &cnst = constants[id];
433 cnst.name = names[id];
434 cnst.type = types[*(op+1)].type;
435 if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
437 else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
438 cnst.i_value = false;
439 else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
440 cnst.i_value = *(op+3);
441 else if(cnst.type==FLOAT)
442 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
445 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
447 unsigned id = *(op+2);
448 Variable &var = variables[id];
449 var.name = names[id];
450 const TypeInfo &type = types[*(op+1)];
451 var.storage = type.storage;
452 var.type = type.type;
453 var.struct_type = type.struct_type;
454 var.array_size = type.array_size;
457 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
459 unsigned id = *(op+1);
460 unsigned decoration = *(op+2);
466 constants[id].constant_id = *op;
468 case DECO_ARRAY_STRIDE:
469 types[id].array_stride = *op;
472 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
475 variables[id].location = *op;
478 variables[id].binding = *op;
480 case DECO_DESCRIPTOR_SET:
481 variables[id].descriptor_set = *op;
486 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
488 Structure &strct = structs[*(op+1)];
489 unsigned index = *(op+2);
490 if(index>=strct.members.size())
491 strct.members.resize(index+1);
492 unsigned decoration = *(op+3);
495 StructMember &member = strct.members[index];
498 case DECO_MATRIX_STRIDE:
499 member.matrix_stride = *op;
502 member.builtin = static_cast<BuiltinSemantic>(*op);