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