]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Unify handling of constants in SpirVModule
[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, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
228                 if(i->second->constant_id>=0)
229                         spec_constants.push_back(i->second);
230 }
231
232
233 SpirVModule::EntryPoint::EntryPoint():
234         stage(VERTEX)
235 { }
236
237
238 SpirVModule::StructMember::StructMember():
239         type(VOID),
240         struct_type(0),
241         offset(0),
242         array_size(0),
243         array_stride(0),
244         matrix_stride(0)
245 { }
246
247
248 SpirVModule::Variable::Variable():
249         type(VOID),
250         struct_type(0),
251         location(-1),
252         descriptor_set(-1),
253         binding(-1)
254 { }
255
256 bool SpirVModule::Variable::operator==(const Variable &other) const
257 {
258         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
259                 return false;
260         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
261                 return false;
262         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
263                 return false;
264         return true;
265 }
266
267
268 SpirVModule::TypeInfo::TypeInfo():
269         type(VOID),
270         struct_type(0),
271         array_size(0),
272         array_stride(0),
273         storage(static_cast<StorageClass>(-1))
274 { }
275
276
277 UInt32 SpirVModule::Reflection::get_opcode(UInt32 op)
278 {
279         return op&0xFFFF;
280 }
281
282 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
283 {
284         return op+(*op>>16);
285 }
286
287 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
288 {
289         string result;
290         for(; op!=op_end; ++op)
291         {
292                 unsigned word = *op;
293                 for(unsigned i=0; i<4; ++i)
294                 {
295                         char c = word&0xFF;
296                         if(!c)
297                         {
298                                 ++op;
299                                 return result;
300                         }
301                         result += c;
302                         word >>= 8;
303                 }
304         }
305
306         throw invalid_module("Unterminated SPIR-V string literal");
307 }
308
309 void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
310 {
311         for(CodeIterator op=code.begin()+5; op!=code.end(); )
312         {
313                 unsigned word_count = *op>>16;
314                 if(word_count>static_cast<unsigned>(code.end()-op))
315                         throw invalid_module("Truncated SPIR-V instruction");
316
317                 switch(get_opcode(*op))
318                 {
319                 case OP_NAME: reflect_name(op); break;
320                 case OP_MEMBER_NAME: reflect_member_name(op); break;
321                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
322                 case OP_TYPE_VOID: reflect_void_type(op); break;
323                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
324                 case OP_TYPE_INT: reflect_int_type(op); break;
325                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
326                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
327                 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
328                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
329                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
330                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
331                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
332                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
333                 case OP_CONSTANT_TRUE:
334                 case OP_CONSTANT_FALSE:
335                 case OP_CONSTANT:
336                 case OP_SPEC_CONSTANT_TRUE:
337                 case OP_SPEC_CONSTANT_FALSE:
338                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
339                 case OP_VARIABLE: reflect_variable(op); break;
340                 case OP_DECORATE: reflect_decorate(op); break;
341                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
342                 }
343
344                 op += word_count;
345         }
346 }
347
348 void SpirVModule::Reflection::reflect_name(CodeIterator op)
349 {
350         CodeIterator op_end = get_op_end(op);
351         string &name = names[*(op+1)];
352         op += 2;
353         name = read_string(op, op_end);
354 }
355
356 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
357 {
358         CodeIterator op_end = get_op_end(op);
359         Structure &strct = structs[*(op+1)];
360         unsigned index = *(op+2);
361         if(index>=strct.members.size())
362                 strct.members.resize(index+1);
363         op += 3;
364         strct.members[index].name = read_string(op, op_end);
365 }
366
367 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
368 {
369         CodeIterator op_end = get_op_end(op);
370         EntryPoint &entry = entry_points[*(op+2)];
371         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
372         op += 3;
373         entry.name = read_string(op, op_end);
374
375         entry.globals.reserve(op_end-op);
376         for(; op!=op_end; ++op)
377                 entry.globals.push_back(&variables[*op]);
378 }
379
380 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
381 {
382         types[*(op+1)].type = VOID;
383 }
384
385 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
386 {
387         types[*(op+1)].type = BOOL;
388 }
389
390 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
391 {
392         TypeInfo &type = types[*(op+1)];
393         unsigned size = *(op+2);
394         bool sign = *(op+3);
395         type.type = static_cast<DataType>(size/8 | sign*0x100);
396 }
397
398 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
399 {
400         TypeInfo &type = types[*(op+1)];
401         unsigned size = *(op+2);
402         type.type = static_cast<DataType>(size/8 | 0x300);
403 }
404
405 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
406 {
407         TypeInfo &type = types[*(op+1)];
408         DataType component = types[*(op+2)].type;
409         unsigned count = *(op+3);
410         type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
411 }
412
413 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
414 {
415         TypeInfo &type = types[*(op+1)];
416         DataType column = types[*(op+2)].type;
417         unsigned count = *(op+3);
418         type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
419 }
420
421 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
422 {
423         TypeInfo &type = types[*(op+1)];
424         DataType sample = types[*(op+2)].type;
425         unsigned dimensions = *(op+3);
426         bool depth = *(op+4)==1;
427         bool array = *(op+5);
428         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
429 }
430
431 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
432 {
433         TypeInfo &type = types[*(op+1)];
434         DataType image = types[*(op+2)].type;
435         type.type = static_cast<DataType>(image | 0x100000);
436 }
437
438 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
439 {
440         TypeInfo &type = types[*(op+1)];
441         const TypeInfo &elem = types[*(op+2)];
442         type.type = elem.type;
443         type.struct_type = elem.struct_type;
444
445         const Constant &size = constants[*(op+3)];
446         if(size.type==INT || size.type==UNSIGNED_INT)
447                 type.array_size = size.i_value;
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         unsigned id = *(op+2);
481         Constant &cnst = constants[id];
482         cnst.name = names[id];
483         cnst.type = types[*(op+1)].type;
484         if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
485                 cnst.i_value = true;
486         else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
487                 cnst.i_value = false;
488         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
489                 cnst.i_value = *(op+3);
490         else if(cnst.type==FLOAT)
491                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
492 }
493
494 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
495 {
496         unsigned id = *(op+2);
497         Variable &var = variables[id];
498         var.name = names[id];
499         const TypeInfo &type = types[*(op+1)];
500         var.storage = type.storage;
501         var.type = type.type;
502         var.struct_type = type.struct_type;
503         var.array_size = type.array_size;
504 }
505
506 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
507 {
508         unsigned id = *(op+1);
509         unsigned decoration = *(op+2);
510         op += 3;
511
512         switch(decoration)
513         {
514         case DECO_SPEC_ID:
515                 constants[id].constant_id = *op;
516                 break;
517         case DECO_ARRAY_STRIDE:
518                 types[id].array_stride = *op;
519                 break;
520         case DECO_LOCATION:
521                 variables[id].location = *op;
522                 break;
523         case DECO_BINDING:
524                 variables[id].binding = *op;
525                 break;
526         case DECO_DESCRIPTOR_SET:
527                 variables[id].descriptor_set = *op;
528                 break;
529         }
530 }
531
532 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
533 {
534         Structure &strct = structs[*(op+1)];
535         unsigned index = *(op+2);
536         if(index>=strct.members.size())
537                 strct.members.resize(index+1);
538         unsigned decoration = *(op+3);
539         op += 4;
540
541         StructMember &member = strct.members[index];
542         switch(decoration)
543         {
544         case DECO_MATRIX_STRIDE:
545                 member.matrix_stride = *op;
546                 break;
547         case DECO_OFFSET:
548                 member.offset = *op;
549                 break;
550         }
551 }
552
553 } // namespace GL
554 } // namespace Msp