]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Fix reflection of image types from Spir-V modules
[libs/gl.git] / source / core / module.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
3 #include "device.h"
4 #include "module.h"
5 #include "resources.h"
6
7 using namespace std;
8
9 enum SpirVConstants
10 {
11         SPIRV_MAGIC = 0x07230203,
12         SPIRV_MAGIC_REVERSED = 0x03022307,
13
14         OP_NAME = 5,
15         OP_MEMBER_NAME = 6,
16         OP_ENTRY_POINT = 15,
17         OP_TYPE_VOID = 19,
18         OP_TYPE_BOOL = 20,
19         OP_TYPE_INT = 21,
20         OP_TYPE_FLOAT = 22,
21         OP_TYPE_VECTOR = 23,
22         OP_TYPE_MATRIX = 24,
23         OP_TYPE_IMAGE = 25,
24         OP_TYPE_SAMPLED_IMAGE = 27,
25         OP_TYPE_ARRAY = 28,
26         OP_TYPE_STRUCT = 30,
27         OP_TYPE_POINTER = 32,
28         OP_CONSTANT_TRUE = 41,
29         OP_CONSTANT_FALSE = 42,
30         OP_CONSTANT = 43,
31         OP_SPEC_CONSTANT_TRUE = 48,
32         OP_SPEC_CONSTANT_FALSE = 49,
33         OP_SPEC_CONSTANT = 50,
34         OP_VARIABLE = 59,
35         OP_DECORATE = 71,
36         OP_MEMBER_DECORATE = 72,
37
38         DECO_SPEC_ID = 1,
39         DECO_ARRAY_STRIDE = 6,
40         DECO_MATRIX_STRIDE = 7,
41         DECO_BUILTIN = 11,
42         DECO_LOCATION = 30,
43         DECO_BINDING = 33,
44         DECO_DESCRIPTOR_SET = 34,
45         DECO_OFFSET = 35
46 };
47
48 namespace Msp {
49 namespace GL {
50
51 void Module::set_source(const string &src)
52 {
53         SL::Compiler compiler(create_features());
54         compiler.set_source(src);
55         compile(compiler);
56 }
57
58 void Module::load_source(IO::Base &io, Resources *res, const string &name)
59 {
60         SL::Compiler compiler(create_features());
61         compiler.load_source(io, res, name);
62         compile(compiler);
63 }
64
65 void Module::load_source(IO::Base &io, const string &name)
66 {
67         load_source(io, 0, name);
68 }
69
70 SL::Features Module::create_features() const
71 {
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;
81         return features;
82 }
83
84
85 void GlslModule::compile(SL::Compiler &compiler)
86 {
87         compiler.compile(SL::Compiler::MODULE);
88         prepared_source = compiler.get_combined_glsl();
89         source_map = compiler.get_source_map();
90
91 #ifdef DEBUG
92         string diagnostics = compiler.get_diagnostics();
93         if(!diagnostics.empty())
94                 IO::print("Module diagnostics:\n%s\n", diagnostics);
95 #endif
96 }
97
98
99 void SpirVModule::load_code(IO::Base &io)
100 {
101         uint32_t buffer[1024];
102         while(1)
103         {
104                 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
105                 if(!len)
106                         break;
107                 len /= 4;
108                 code.reserve(code.size()+len);
109                 code.insert(code.end(), buffer, buffer+len);
110         }
111
112         reflect();
113 }
114
115 void SpirVModule::compile(SL::Compiler &compiler)
116 {
117         compiler.compile(SL::Compiler::SPIRV);
118         code = compiler.get_combined_spirv();
119         reflect();
120 }
121
122 void SpirVModule::reflect()
123 {
124         if(code.empty())
125                 throw invalid_module("Empty SPIR-V code");
126
127         if(code[0]==SPIRV_MAGIC_REVERSED)
128         {
129                 for(uint32_t &c: code)
130                         c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
131         }
132         else if(code[0]!=SPIRV_MAGIC)
133                 throw invalid_module("SPIR-V magic number not found");
134
135         Reflection reflection;
136         reflection.reflect_code(code);
137
138         map<const Constant *, unsigned> spec_indices;
139         for(const auto &kvp: reflection.constants)
140                 if(kvp.second.constant_id>=0)
141                 {
142                         spec_indices[&kvp.second] = spec_constants.size();
143                         spec_constants.push_back(kvp.second);
144                 }
145
146         map<const Structure *, unsigned> struct_indices;
147         structs.reserve(reflection.structs.size());
148         for(const auto &kvp: reflection.structs)
149         {
150                 struct_indices[&kvp.second] = structs.size();
151                 structs.push_back(kvp.second);
152         }
153
154         for(Structure &s: structs)
155         {
156                 for(StructMember &m: s.members)
157                         if(m.struct_type)
158                         {
159                                 auto i = struct_indices.find(m.struct_type);
160                                 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
161                         }
162
163                 const StructMember *last_member = &s.members.back();
164                 unsigned last_offset = last_member->offset;
165                 while(last_member->struct_type)
166                 {
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;
171                         last_member = lm;
172                 }
173                 
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;
179         }
180
181         map<const Variable *, unsigned> var_indices;
182         variables.reserve(reflection.variables.size());
183         for(const auto &kvp: reflection.variables)
184         {
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();
188                 else
189                 {
190                         var_indices[&kvp.second] = variables.size();
191                         variables.push_back(kvp.second);
192                 }
193         }
194
195         for(Variable &v: variables)
196                 if(v.struct_type)
197                 {
198                         auto i = struct_indices.find(v.struct_type);
199                         v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
200                 }
201
202         entry_points.reserve(reflection.entry_points.size());
203         for(const auto &kvp: reflection.entry_points)
204         {
205                 entry_points.push_back(kvp.second);
206                 EntryPoint &entry = entry_points.back();
207                 for(const Variable *&v: entry.globals)
208                 {
209                         auto i = var_indices.find(v);
210                         v = (i!=var_indices.end() ? &variables[i->second] : 0);
211                 }
212         }
213 }
214
215
216 bool SpirVModule::Variable::operator==(const Variable &other) const
217 {
218         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
219                 return false;
220         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
221                 return false;
222         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
223                 return false;
224         return true;
225 }
226
227
228 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
229 {
230         return op&0xFFFF;
231 }
232
233 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
234 {
235         return op+(*op>>16);
236 }
237
238 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
239 {
240         string result;
241         for(; op!=op_end; ++op)
242         {
243                 unsigned word = *op;
244                 for(unsigned i=0; i<4; ++i)
245                 {
246                         char c = word&0xFF;
247                         if(!c)
248                         {
249                                 ++op;
250                                 return result;
251                         }
252                         result += c;
253                         word >>= 8;
254                 }
255         }
256
257         throw invalid_module("Unterminated SPIR-V string literal");
258 }
259
260 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
261 {
262         for(CodeIterator op=code.begin()+5; op!=code.end(); )
263         {
264                 unsigned word_count = *op>>16;
265                 if(word_count>static_cast<unsigned>(code.end()-op))
266                         throw invalid_module("Truncated SPIR-V instruction");
267
268                 switch(get_opcode(*op))
269                 {
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:
286                 case OP_CONSTANT:
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;
293                 }
294
295                 op += word_count;
296         }
297 }
298
299 void SpirVModule::Reflection::reflect_name(CodeIterator op)
300 {
301         CodeIterator op_end = get_op_end(op);
302         string &name = names[*(op+1)];
303         op += 2;
304         name = read_string(op, op_end);
305 }
306
307 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
308 {
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);
314         op += 3;
315         strct.members[index].name = read_string(op, op_end);
316 }
317
318 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
319 {
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
323         op += 3;
324         entry.name = read_string(op, op_end);
325
326         entry.globals.reserve(op_end-op);
327         for(; op!=op_end; ++op)
328                 entry.globals.push_back(&variables[*op]);
329 }
330
331 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
332 {
333         types[*(op+1)].type = VOID;
334 }
335
336 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
337 {
338         types[*(op+1)].type = BOOL;
339 }
340
341 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
342 {
343         TypeInfo &type = types[*(op+1)];
344         unsigned size = *(op+2);
345         bool sign = *(op+3);
346         type.type = static_cast<DataType>(size/8 | sign*0x100);
347 }
348
349 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
350 {
351         TypeInfo &type = types[*(op+1)];
352         unsigned size = *(op+2);
353         type.type = static_cast<DataType>(size/8 | 0x300);
354 }
355
356 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
357 {
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));
362 }
363
364 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
365 {
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));
370 }
371
372 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
373 {
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)<<16) | sample);
380 }
381
382 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
383 {
384         TypeInfo &type = types[*(op+1)];
385         DataType image = types[*(op+2)].type;
386         type.type = static_cast<DataType>(image | 0x100000);
387 }
388
389 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
390 {
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;
395
396         const Constant &size = constants[*(op+3)];
397         if(size.type==INT || size.type==UNSIGNED_INT)
398                 type.array_size = size.i_value;
399 }
400
401 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
402 {
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;
408         op += 2;
409
410         strct.members.resize(op_end-op);
411         auto mem = strct.members.begin();
412         for(; op!=op_end; ++op, ++mem)
413         {
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;
419         }
420 }
421
422 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
423 {
424         TypeInfo &type = types[*(op+1)];
425         type = types[*(op+3)];
426         type.storage = static_cast<StorageClass>(*(op+2));
427 }
428
429 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
430 {
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)
436                 cnst.i_value = 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));
443 }
444
445 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
446 {
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;
455 }
456
457 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
458 {
459         unsigned id = *(op+1);
460         unsigned decoration = *(op+2);
461         op += 3;
462
463         switch(decoration)
464         {
465         case DECO_SPEC_ID:
466                 constants[id].constant_id = *op;
467                 break;
468         case DECO_ARRAY_STRIDE:
469                 types[id].array_stride = *op;
470                 break;
471         case DECO_BUILTIN:
472                 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
473                 break;
474         case DECO_LOCATION:
475                 variables[id].location = *op;
476                 break;
477         case DECO_BINDING:
478                 variables[id].binding = *op;
479                 break;
480         case DECO_DESCRIPTOR_SET:
481                 variables[id].descriptor_set = *op;
482                 break;
483         }
484 }
485
486 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
487 {
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);
493         op += 4;
494
495         StructMember &member = strct.members[index];
496         switch(decoration)
497         {
498         case DECO_MATRIX_STRIDE:
499                 member.matrix_stride = *op;
500                 break;
501         case DECO_BUILTIN:
502                 member.builtin = static_cast<BuiltinSemantic>(*op);
503                 break;
504         case DECO_OFFSET:
505                 member.offset = *op;
506                 break;
507         }
508 }
509
510 } // namespace GL
511 } // namespace Msp