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