]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
89d418947f92b1685c9ddd0689d9811612114abd
[libs/gl.git] / source / core / module.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/io/print.h>
3 #include "deviceinfo.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 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;
80         return features;
81 }
82
83
84 void GlslModule::compile(SL::Compiler &compiler)
85 {
86         compiler.compile(SL::Compiler::MODULE);
87         prepared_source = compiler.get_combined_glsl();
88         source_map = compiler.get_source_map();
89
90 #ifdef DEBUG
91         string diagnostics = compiler.get_diagnostics();
92         if(!diagnostics.empty())
93                 IO::print("Module diagnostics:\n%s\n", diagnostics);
94 #endif
95 }
96
97
98 SpirVModule::SpirVModule(const SpirVModule &other):
99         code(other.code),
100         entry_points(other.entry_points),
101         structs(other.structs),
102         variables(other.variables)
103 {
104         remap_pointers_from(other);
105 }
106
107 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
108 {
109         code = other.code;
110         entry_points = other.entry_points;
111         structs = other.structs;
112         variables = other.variables;
113         remap_pointers_from(other);
114         return *this;
115 }
116
117 void SpirVModule::remap_pointers_from(const SpirVModule &other)
118 {
119         for(EntryPoint &e: entry_points)
120                 for(const Variable *&v: e.globals)
121                         v = &variables[v-&other.variables.front()];
122
123         for(Variable &v: variables)
124                 if(v.struct_type)
125                         v.struct_type = &structs[v.struct_type-&other.structs.front()];
126
127         for(Structure &s: structs)
128                 for(StructMember &m: s.members)
129                         if(m.struct_type)
130                                 m.struct_type = &structs[m.struct_type-&other.structs.front()];
131 }
132
133 void SpirVModule::load_code(IO::Base &io)
134 {
135         uint32_t buffer[1024];
136         while(1)
137         {
138                 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
139                 if(!len)
140                         break;
141                 len /= 4;
142                 code.reserve(code.size()+len);
143                 code.insert(code.end(), buffer, buffer+len);
144         }
145
146         reflect();
147 }
148
149 void SpirVModule::compile(SL::Compiler &compiler)
150 {
151         compiler.compile(SL::Compiler::SPIRV);
152         code = compiler.get_combined_spirv();
153         reflect();
154 }
155
156 void SpirVModule::reflect()
157 {
158         if(code.empty())
159                 throw invalid_module("Empty SPIR-V code");
160
161         if(code[0]==SPIRV_MAGIC_REVERSED)
162         {
163                 for(uint32_t &c: code)
164                         c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
165         }
166         else if(code[0]!=SPIRV_MAGIC)
167                 throw invalid_module("SPIR-V magic number not found");
168
169         Reflection reflection;
170         reflection.reflect_code(code);
171
172         map<const Constant *, unsigned> spec_indices;
173         for(const auto &kvp: reflection.constants)
174                 if(kvp.second.constant_id>=0)
175                 {
176                         spec_indices[&kvp.second] = spec_constants.size();
177                         spec_constants.push_back(kvp.second);
178                 }
179
180         map<const Structure *, unsigned> struct_indices;
181         structs.reserve(reflection.structs.size());
182         for(const auto &kvp: reflection.structs)
183         {
184                 struct_indices[&kvp.second] = structs.size();
185                 structs.push_back(kvp.second);
186         }
187
188         for(Structure &s: structs)
189         {
190                 for(StructMember &m: s.members)
191                         if(m.struct_type)
192                         {
193                                 auto i = struct_indices.find(m.struct_type);
194                                 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
195                         }
196
197                 const StructMember *last_member = &s.members.back();
198                 unsigned last_offset = last_member->offset;
199                 while(last_member->struct_type)
200                 {
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;
205                         last_member = lm;
206                 }
207                 
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;
213         }
214
215         map<const Variable *, unsigned> var_indices;
216         variables.reserve(reflection.variables.size());
217         for(const auto &kvp: reflection.variables)
218         {
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();
222                 else
223                 {
224                         var_indices[&kvp.second] = variables.size();
225                         variables.push_back(kvp.second);
226                 }
227         }
228
229         for(Variable &v: variables)
230                 if(v.struct_type)
231                 {
232                         auto i = struct_indices.find(v.struct_type);
233                         v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
234                 }
235
236         entry_points.reserve(reflection.entry_points.size());
237         for(const auto &kvp: reflection.entry_points)
238         {
239                 entry_points.push_back(kvp.second);
240                 EntryPoint &entry = entry_points.back();
241                 for(const Variable *&v: entry.globals)
242                 {
243                         auto i = var_indices.find(v);
244                         v = (i!=var_indices.end() ? &variables[i->second] : 0);
245                 }
246         }
247 }
248
249
250 bool SpirVModule::Variable::operator==(const Variable &other) const
251 {
252         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
253                 return false;
254         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
255                 return false;
256         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
257                 return false;
258         return true;
259 }
260
261
262 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
263 {
264         return op&0xFFFF;
265 }
266
267 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
268 {
269         return op+(*op>>16);
270 }
271
272 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
273 {
274         string result;
275         for(; op!=op_end; ++op)
276         {
277                 unsigned word = *op;
278                 for(unsigned i=0; i<4; ++i)
279                 {
280                         char c = word&0xFF;
281                         if(!c)
282                         {
283                                 ++op;
284                                 return result;
285                         }
286                         result += c;
287                         word >>= 8;
288                 }
289         }
290
291         throw invalid_module("Unterminated SPIR-V string literal");
292 }
293
294 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
295 {
296         for(CodeIterator op=code.begin()+5; op!=code.end(); )
297         {
298                 unsigned word_count = *op>>16;
299                 if(word_count>static_cast<unsigned>(code.end()-op))
300                         throw invalid_module("Truncated SPIR-V instruction");
301
302                 switch(get_opcode(*op))
303                 {
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:
320                 case OP_CONSTANT:
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;
327                 }
328
329                 op += word_count;
330         }
331 }
332
333 void SpirVModule::Reflection::reflect_name(CodeIterator op)
334 {
335         CodeIterator op_end = get_op_end(op);
336         string &name = names[*(op+1)];
337         op += 2;
338         name = read_string(op, op_end);
339 }
340
341 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
342 {
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);
348         op += 3;
349         strct.members[index].name = read_string(op, op_end);
350 }
351
352 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
353 {
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
357         op += 3;
358         entry.name = read_string(op, op_end);
359
360         entry.globals.reserve(op_end-op);
361         for(; op!=op_end; ++op)
362                 entry.globals.push_back(&variables[*op]);
363 }
364
365 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
366 {
367         types[*(op+1)].type = VOID;
368 }
369
370 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
371 {
372         types[*(op+1)].type = BOOL;
373 }
374
375 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
376 {
377         TypeInfo &type = types[*(op+1)];
378         unsigned size = *(op+2);
379         bool sign = *(op+3);
380         type.type = static_cast<DataType>(size/8 | sign*0x100);
381 }
382
383 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
384 {
385         TypeInfo &type = types[*(op+1)];
386         unsigned size = *(op+2);
387         type.type = static_cast<DataType>(size/8 | 0x300);
388 }
389
390 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
391 {
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));
396 }
397
398 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
399 {
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));
404 }
405
406 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
407 {
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);
414 }
415
416 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
417 {
418         TypeInfo &type = types[*(op+1)];
419         DataType image = types[*(op+2)].type;
420         type.type = static_cast<DataType>(image | 0x100000);
421 }
422
423 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
424 {
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;
429
430         const Constant &size = constants[*(op+3)];
431         if(size.type==INT || size.type==UNSIGNED_INT)
432                 type.array_size = size.i_value;
433 }
434
435 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
436 {
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;
442         op += 2;
443
444         strct.members.resize(op_end-op);
445         auto mem = strct.members.begin();
446         for(; op!=op_end; ++op, ++mem)
447         {
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;
453         }
454 }
455
456 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
457 {
458         TypeInfo &type = types[*(op+1)];
459         type = types[*(op+3)];
460         type.storage = static_cast<StorageClass>(*(op+2));
461 }
462
463 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
464 {
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)
470                 cnst.i_value = 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));
477 }
478
479 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
480 {
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;
489 }
490
491 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
492 {
493         unsigned id = *(op+1);
494         unsigned decoration = *(op+2);
495         op += 3;
496
497         switch(decoration)
498         {
499         case DECO_SPEC_ID:
500                 constants[id].constant_id = *op;
501                 break;
502         case DECO_ARRAY_STRIDE:
503                 types[id].array_stride = *op;
504                 break;
505         case DECO_BUILTIN:
506                 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
507                 break;
508         case DECO_LOCATION:
509                 variables[id].location = *op;
510                 break;
511         case DECO_BINDING:
512                 variables[id].binding = *op;
513                 break;
514         case DECO_DESCRIPTOR_SET:
515                 variables[id].descriptor_set = *op;
516                 break;
517         }
518 }
519
520 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
521 {
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);
527         op += 4;
528
529         StructMember &member = strct.members[index];
530         switch(decoration)
531         {
532         case DECO_MATRIX_STRIDE:
533                 member.matrix_stride = *op;
534                 break;
535         case DECO_BUILTIN:
536                 member.builtin = static_cast<BuiltinSemantic>(*op);
537                 break;
538         case DECO_OFFSET:
539                 member.offset = *op;
540                 break;
541         }
542 }
543
544 } // namespace GL
545 } // namespace Msp