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