]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Require passing features to SL::Compiler constructor
[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(DeviceInfo::get_global().glsl_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(DeviceInfo::get_global().glsl_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
71 void GlslModule::compile(SL::Compiler &compiler)
72 {
73         compiler.compile(SL::Compiler::MODULE);
74         prepared_source = compiler.get_combined_glsl();
75         source_map = compiler.get_source_map();
76
77 #ifdef DEBUG
78         string diagnostics = compiler.get_diagnostics();
79         if(!diagnostics.empty())
80                 IO::print("Module diagnostics:\n%s\n", diagnostics);
81 #endif
82 }
83
84
85 SpirVModule::SpirVModule(const SpirVModule &other):
86         code(other.code),
87         entry_points(other.entry_points),
88         structs(other.structs),
89         variables(other.variables)
90 {
91         remap_pointers_from(other);
92 }
93
94 SpirVModule &SpirVModule::operator=(const SpirVModule &other)
95 {
96         code = other.code;
97         entry_points = other.entry_points;
98         structs = other.structs;
99         variables = other.variables;
100         remap_pointers_from(other);
101         return *this;
102 }
103
104 void SpirVModule::remap_pointers_from(const SpirVModule &other)
105 {
106         for(EntryPoint &e: entry_points)
107                 for(const Variable *&v: e.globals)
108                         v = &variables[v-&other.variables.front()];
109
110         for(Variable &v: variables)
111                 if(v.struct_type)
112                         v.struct_type = &structs[v.struct_type-&other.structs.front()];
113
114         for(Structure &s: structs)
115                 for(StructMember &m: s.members)
116                         if(m.struct_type)
117                                 m.struct_type = &structs[m.struct_type-&other.structs.front()];
118 }
119
120 void SpirVModule::load_code(IO::Base &io)
121 {
122         uint32_t buffer[1024];
123         while(1)
124         {
125                 unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
126                 if(!len)
127                         break;
128                 len /= 4;
129                 code.reserve(code.size()+len);
130                 code.insert(code.end(), buffer, buffer+len);
131         }
132
133         reflect();
134 }
135
136 void SpirVModule::compile(SL::Compiler &compiler)
137 {
138         compiler.compile(SL::Compiler::SPIRV);
139         code = compiler.get_combined_spirv();
140         reflect();
141 }
142
143 void SpirVModule::reflect()
144 {
145         if(code.empty())
146                 throw invalid_module("Empty SPIR-V code");
147
148         if(code[0]==SPIRV_MAGIC_REVERSED)
149         {
150                 for(uint32_t &c: code)
151                         c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
152         }
153         else if(code[0]!=SPIRV_MAGIC)
154                 throw invalid_module("SPIR-V magic number not found");
155
156         Reflection reflection;
157         reflection.reflect_code(code);
158
159         map<const Constant *, unsigned> spec_indices;
160         for(const auto &kvp: reflection.constants)
161                 if(kvp.second.constant_id>=0)
162                 {
163                         spec_indices[&kvp.second] = spec_constants.size();
164                         spec_constants.push_back(kvp.second);
165                 }
166
167         map<const Structure *, unsigned> struct_indices;
168         structs.reserve(reflection.structs.size());
169         for(const auto &kvp: reflection.structs)
170         {
171                 struct_indices[&kvp.second] = structs.size();
172                 structs.push_back(kvp.second);
173         }
174
175         for(Structure &s: structs)
176         {
177                 for(StructMember &m: s.members)
178                         if(m.struct_type)
179                         {
180                                 auto i = struct_indices.find(m.struct_type);
181                                 m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
182                         }
183
184                 const StructMember *last_member = &s.members.back();
185                 unsigned last_offset = last_member->offset;
186                 while(last_member->struct_type)
187                 {
188                         const StructMember *lm = &last_member->struct_type->members.back();
189                         if(last_member->array_size)
190                                 last_offset += last_member->array_stride*(last_member->array_size-1);
191                         last_offset += lm->offset;
192                         last_member = lm;
193                 }
194                 
195                 unsigned last_size = get_type_size(last_member->type); 
196                 if(last_member->array_size)
197                         last_size += last_member->array_stride*(last_member->array_size-1);
198                 s.size = last_offset+last_size;
199                 s.size = (s.size+15)&~15;
200         }
201
202         map<const Variable *, unsigned> var_indices;
203         variables.reserve(reflection.variables.size());
204         for(const auto &kvp: reflection.variables)
205         {
206                 auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
207                 if(i!=variables.end())
208                         var_indices[&kvp.second] = i-variables.begin();
209                 else
210                 {
211                         var_indices[&kvp.second] = variables.size();
212                         variables.push_back(kvp.second);
213                 }
214         }
215
216         for(Variable &v: variables)
217                 if(v.struct_type)
218                 {
219                         auto i = struct_indices.find(v.struct_type);
220                         v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
221                 }
222
223         entry_points.reserve(reflection.entry_points.size());
224         for(const auto &kvp: reflection.entry_points)
225         {
226                 entry_points.push_back(kvp.second);
227                 EntryPoint &entry = entry_points.back();
228                 for(const Variable *&v: entry.globals)
229                 {
230                         auto i = var_indices.find(v);
231                         v = (i!=var_indices.end() ? &variables[i->second] : 0);
232                 }
233         }
234 }
235
236
237 bool SpirVModule::Variable::operator==(const Variable &other) const
238 {
239         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
240                 return false;
241         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
242                 return false;
243         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
244                 return false;
245         return true;
246 }
247
248
249 uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
250 {
251         return op&0xFFFF;
252 }
253
254 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
255 {
256         return op+(*op>>16);
257 }
258
259 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
260 {
261         string result;
262         for(; op!=op_end; ++op)
263         {
264                 unsigned word = *op;
265                 for(unsigned i=0; i<4; ++i)
266                 {
267                         char c = word&0xFF;
268                         if(!c)
269                         {
270                                 ++op;
271                                 return result;
272                         }
273                         result += c;
274                         word >>= 8;
275                 }
276         }
277
278         throw invalid_module("Unterminated SPIR-V string literal");
279 }
280
281 void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
282 {
283         for(CodeIterator op=code.begin()+5; op!=code.end(); )
284         {
285                 unsigned word_count = *op>>16;
286                 if(word_count>static_cast<unsigned>(code.end()-op))
287                         throw invalid_module("Truncated SPIR-V instruction");
288
289                 switch(get_opcode(*op))
290                 {
291                 case OP_NAME: reflect_name(op); break;
292                 case OP_MEMBER_NAME: reflect_member_name(op); break;
293                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
294                 case OP_TYPE_VOID: reflect_void_type(op); break;
295                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
296                 case OP_TYPE_INT: reflect_int_type(op); break;
297                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
298                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
299                 case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
300                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
301                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
302                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
303                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
304                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
305                 case OP_CONSTANT_TRUE:
306                 case OP_CONSTANT_FALSE:
307                 case OP_CONSTANT:
308                 case OP_SPEC_CONSTANT_TRUE:
309                 case OP_SPEC_CONSTANT_FALSE:
310                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
311                 case OP_VARIABLE: reflect_variable(op); break;
312                 case OP_DECORATE: reflect_decorate(op); break;
313                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
314                 }
315
316                 op += word_count;
317         }
318 }
319
320 void SpirVModule::Reflection::reflect_name(CodeIterator op)
321 {
322         CodeIterator op_end = get_op_end(op);
323         string &name = names[*(op+1)];
324         op += 2;
325         name = read_string(op, op_end);
326 }
327
328 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
329 {
330         CodeIterator op_end = get_op_end(op);
331         Structure &strct = structs[*(op+1)];
332         unsigned index = *(op+2);
333         if(index>=strct.members.size())
334                 strct.members.resize(index+1);
335         op += 3;
336         strct.members[index].name = read_string(op, op_end);
337 }
338
339 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
340 {
341         CodeIterator op_end = get_op_end(op);
342         EntryPoint &entry = entry_points[*(op+2)];
343         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
344         op += 3;
345         entry.name = read_string(op, op_end);
346
347         entry.globals.reserve(op_end-op);
348         for(; op!=op_end; ++op)
349                 entry.globals.push_back(&variables[*op]);
350 }
351
352 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
353 {
354         types[*(op+1)].type = VOID;
355 }
356
357 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
358 {
359         types[*(op+1)].type = BOOL;
360 }
361
362 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
363 {
364         TypeInfo &type = types[*(op+1)];
365         unsigned size = *(op+2);
366         bool sign = *(op+3);
367         type.type = static_cast<DataType>(size/8 | sign*0x100);
368 }
369
370 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
371 {
372         TypeInfo &type = types[*(op+1)];
373         unsigned size = *(op+2);
374         type.type = static_cast<DataType>(size/8 | 0x300);
375 }
376
377 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
378 {
379         TypeInfo &type = types[*(op+1)];
380         DataType component = types[*(op+2)].type;
381         unsigned count = *(op+3);
382         type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
383 }
384
385 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
386 {
387         TypeInfo &type = types[*(op+1)];
388         DataType column = types[*(op+2)].type;
389         unsigned count = *(op+3);
390         type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
391 }
392
393 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
394 {
395         TypeInfo &type = types[*(op+1)];
396         DataType sample = types[*(op+2)].type;
397         unsigned dimensions = *(op+3);
398         bool depth = *(op+4)==1;
399         bool array = *(op+5);
400         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
401 }
402
403 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
404 {
405         TypeInfo &type = types[*(op+1)];
406         DataType image = types[*(op+2)].type;
407         type.type = static_cast<DataType>(image | 0x100000);
408 }
409
410 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
411 {
412         TypeInfo &type = types[*(op+1)];
413         const TypeInfo &elem = types[*(op+2)];
414         type.type = elem.type;
415         type.struct_type = elem.struct_type;
416
417         const Constant &size = constants[*(op+3)];
418         if(size.type==INT || size.type==UNSIGNED_INT)
419                 type.array_size = size.i_value;
420 }
421
422 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
423 {
424         CodeIterator op_end = get_op_end(op);
425         unsigned id = *(op+1);
426         Structure &strct = structs[id];
427         strct.name = names[id];
428         types[id].struct_type = &strct;
429         op += 2;
430
431         strct.members.resize(op_end-op);
432         auto mem = strct.members.begin();
433         for(; op!=op_end; ++op, ++mem)
434         {
435                 TypeInfo &type = types[*op];
436                 mem->type = type.type;
437                 mem->struct_type = type.struct_type;
438                 mem->array_size = type.array_size;
439                 mem->array_stride = type.array_stride;
440         }
441 }
442
443 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
444 {
445         TypeInfo &type = types[*(op+1)];
446         type = types[*(op+3)];
447         type.storage = static_cast<StorageClass>(*(op+2));
448 }
449
450 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
451 {
452         unsigned id = *(op+2);
453         Constant &cnst = constants[id];
454         cnst.name = names[id];
455         cnst.type = types[*(op+1)].type;
456         if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
457                 cnst.i_value = true;
458         else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
459                 cnst.i_value = false;
460         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
461                 cnst.i_value = *(op+3);
462         else if(cnst.type==FLOAT)
463                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
464 }
465
466 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
467 {
468         unsigned id = *(op+2);
469         Variable &var = variables[id];
470         var.name = names[id];
471         const TypeInfo &type = types[*(op+1)];
472         var.storage = type.storage;
473         var.type = type.type;
474         var.struct_type = type.struct_type;
475         var.array_size = type.array_size;
476 }
477
478 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
479 {
480         unsigned id = *(op+1);
481         unsigned decoration = *(op+2);
482         op += 3;
483
484         switch(decoration)
485         {
486         case DECO_SPEC_ID:
487                 constants[id].constant_id = *op;
488                 break;
489         case DECO_ARRAY_STRIDE:
490                 types[id].array_stride = *op;
491                 break;
492         case DECO_BUILTIN:
493                 variables[id].builtin = static_cast<BuiltinSemantic>(*op);
494                 break;
495         case DECO_LOCATION:
496                 variables[id].location = *op;
497                 break;
498         case DECO_BINDING:
499                 variables[id].binding = *op;
500                 break;
501         case DECO_DESCRIPTOR_SET:
502                 variables[id].descriptor_set = *op;
503                 break;
504         }
505 }
506
507 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
508 {
509         Structure &strct = structs[*(op+1)];
510         unsigned index = *(op+2);
511         if(index>=strct.members.size())
512                 strct.members.resize(index+1);
513         unsigned decoration = *(op+3);
514         op += 4;
515
516         StructMember &member = strct.members[index];
517         switch(decoration)
518         {
519         case DECO_MATRIX_STRIDE:
520                 member.matrix_stride = *op;
521                 break;
522         case DECO_BUILTIN:
523                 member.builtin = static_cast<BuiltinSemantic>(*op);
524                 break;
525         case DECO_OFFSET:
526                 member.offset = *op;
527                 break;
528         }
529 }
530
531 } // namespace GL
532 } // namespace Msp