]> git.tdb.fi Git - libs/gl.git/blob - source/core/module.cpp
Simplify pipeline state management
[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 Constant *, unsigned> spec_indices;
157         for(map<unsigned, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
158                 if(i->second.constant_id>=0)
159                 {
160                         spec_indices[&i->second] = spec_constants.size();
161                         spec_constants.push_back(i->second);
162                 }
163
164         map<const Structure *, unsigned> struct_indices;
165         structs.reserve(reflection.structs.size());
166         for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
167         {
168                 struct_indices[&i->second] = structs.size();
169                 structs.push_back(i->second);
170         }
171
172         for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
173         {
174                 for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
175                 {
176                         if(j->struct_type)
177                         {
178                                 map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
179                                 j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
180                         }
181                         if(j->array_size_spec)
182                         {
183                                 map<const Constant *, unsigned>::const_iterator k = spec_indices.find(j->array_size_spec);
184                                 j->array_size_spec = (k!=spec_indices.end() ? &spec_constants[k->second] : 0);
185                         }
186                 }
187
188                 const StructMember *last_member = &i->members.back();
189                 unsigned last_offset = last_member->offset;
190                 while(last_member->struct_type)
191                 {
192                         const StructMember *lm = &last_member->struct_type->members.back();
193                         if(last_member->array_size)
194                                 last_offset += last_member->array_stride*(last_member->array_size-1);
195                         else if(last_member->array_size_spec)
196                                 last_offset += last_member->array_stride*(last_member->array_size_spec->i_value-1);
197                         last_offset += lm->offset;
198                         last_member = lm;
199                 }
200                 
201                 i->size = last_offset+get_type_size(last_member->type);
202                 i->size = (i->size+15)&~15;
203         }
204
205         map<const Variable *, unsigned> var_indices;
206         variables.reserve(reflection.variables.size());
207         for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
208         {
209                 int dup_index = -1;
210                 for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
211                         if(*j==i->second)
212                                 dup_index = j-variables.begin();
213
214                 if(dup_index>=0)
215                         var_indices[&i->second] = dup_index;
216                 else
217                 {
218                         var_indices[&i->second] = variables.size();
219                         variables.push_back(i->second);
220                 }
221         }
222
223         for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
224         {
225                 if(i->struct_type)
226                 {
227                         map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
228                         i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
229                 }
230                 if(i->array_size_spec)
231                 {
232                         map<const Constant *, unsigned>::const_iterator j = spec_indices.find(i->array_size_spec);
233                         i->array_size_spec = (j!=spec_indices.end() ? &spec_constants[j->second] : 0);
234                 }
235         }
236
237         entry_points.reserve(reflection.entry_points.size());
238         for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
239         {
240                 entry_points.push_back(i->second);
241                 EntryPoint &entry = entry_points.back();
242                 for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
243                 {
244                         map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
245                         *j = (k!=var_indices.end() ? &variables[k->second] : 0);
246                 }
247         }
248 }
249
250
251 SpirVModule::EntryPoint::EntryPoint():
252         stage(VERTEX)
253 { }
254
255
256 SpirVModule::StructMember::StructMember():
257         type(VOID),
258         struct_type(0),
259         offset(0),
260         array_size(0),
261         array_size_spec(0),
262         array_stride(0),
263         matrix_stride(0)
264 { }
265
266
267 SpirVModule::Variable::Variable():
268         type(VOID),
269         struct_type(0),
270         location(-1),
271         descriptor_set(-1),
272         binding(-1)
273 { }
274
275 bool SpirVModule::Variable::operator==(const Variable &other) const
276 {
277         if(storage!=UNIFORM_CONSTANT && storage!=UNIFORM)
278                 return false;
279         if(storage!=other.storage || type!=other.type || struct_type!=other.struct_type)
280                 return false;
281         if(location!=other.location || descriptor_set!=other.descriptor_set || binding!=other.binding)
282                 return false;
283         return true;
284 }
285
286
287 SpirVModule::TypeInfo::TypeInfo():
288         type(VOID),
289         struct_type(0),
290         array_size_spec(0),
291         array_size(0),
292         array_stride(0),
293         storage(static_cast<StorageClass>(-1))
294 { }
295
296
297 UInt32 SpirVModule::Reflection::get_opcode(UInt32 op)
298 {
299         return op&0xFFFF;
300 }
301
302 SpirVModule::Reflection::CodeIterator SpirVModule::Reflection::get_op_end(const CodeIterator &op)
303 {
304         return op+(*op>>16);
305 }
306
307 string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator &op_end)
308 {
309         string result;
310         for(; op!=op_end; ++op)
311         {
312                 unsigned word = *op;
313                 for(unsigned i=0; i<4; ++i)
314                 {
315                         char c = word&0xFF;
316                         if(!c)
317                         {
318                                 ++op;
319                                 return result;
320                         }
321                         result += c;
322                         word >>= 8;
323                 }
324         }
325
326         throw invalid_module("Unterminated SPIR-V string literal");
327 }
328
329 void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
330 {
331         for(CodeIterator op=code.begin()+5; op!=code.end(); )
332         {
333                 unsigned word_count = *op>>16;
334                 if(word_count>static_cast<unsigned>(code.end()-op))
335                         throw invalid_module("Truncated SPIR-V instruction");
336
337                 switch(get_opcode(*op))
338                 {
339                 case OP_NAME: reflect_name(op); break;
340                 case OP_MEMBER_NAME: reflect_member_name(op); break;
341                 case OP_ENTRY_POINT: reflect_entry_point(op); break;
342                 case OP_TYPE_VOID: reflect_void_type(op); break;
343                 case OP_TYPE_BOOL: reflect_bool_type(op); break;
344                 case OP_TYPE_INT: reflect_int_type(op); break;
345                 case OP_TYPE_FLOAT: reflect_float_type(op); break;
346                 case OP_TYPE_VECTOR: reflect_vector_type(op); break;
347                 case OP_TYPE_MATRIX: reflect_vector_type(op); break;
348                 case OP_TYPE_IMAGE: reflect_image_type(op); break;
349                 case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
350                 case OP_TYPE_ARRAY: reflect_array_type(op); break;
351                 case OP_TYPE_STRUCT: reflect_struct_type(op); break;
352                 case OP_TYPE_POINTER: reflect_pointer_type(op); break;
353                 case OP_CONSTANT_TRUE:
354                 case OP_CONSTANT_FALSE:
355                 case OP_CONSTANT:
356                 case OP_SPEC_CONSTANT_TRUE:
357                 case OP_SPEC_CONSTANT_FALSE:
358                 case OP_SPEC_CONSTANT: reflect_constant(op); break;
359                 case OP_VARIABLE: reflect_variable(op); break;
360                 case OP_DECORATE: reflect_decorate(op); break;
361                 case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
362                 }
363
364                 op += word_count;
365         }
366 }
367
368 void SpirVModule::Reflection::reflect_name(CodeIterator op)
369 {
370         CodeIterator op_end = get_op_end(op);
371         string &name = names[*(op+1)];
372         op += 2;
373         name = read_string(op, op_end);
374 }
375
376 void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
377 {
378         CodeIterator op_end = get_op_end(op);
379         Structure &strct = structs[*(op+1)];
380         unsigned index = *(op+2);
381         if(index>=strct.members.size())
382                 strct.members.resize(index+1);
383         op += 3;
384         strct.members[index].name = read_string(op, op_end);
385 }
386
387 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
388 {
389         CodeIterator op_end = get_op_end(op);
390         EntryPoint &entry = entry_points[*(op+2)];
391         entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
392         op += 3;
393         entry.name = read_string(op, op_end);
394
395         entry.globals.reserve(op_end-op);
396         for(; op!=op_end; ++op)
397                 entry.globals.push_back(&variables[*op]);
398 }
399
400 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
401 {
402         types[*(op+1)].type = VOID;
403 }
404
405 void SpirVModule::Reflection::reflect_bool_type(CodeIterator op)
406 {
407         types[*(op+1)].type = BOOL;
408 }
409
410 void SpirVModule::Reflection::reflect_int_type(CodeIterator op)
411 {
412         TypeInfo &type = types[*(op+1)];
413         unsigned size = *(op+2);
414         bool sign = *(op+3);
415         type.type = static_cast<DataType>(size/8 | sign*0x100);
416 }
417
418 void SpirVModule::Reflection::reflect_float_type(CodeIterator op)
419 {
420         TypeInfo &type = types[*(op+1)];
421         unsigned size = *(op+2);
422         type.type = static_cast<DataType>(size/8 | 0x300);
423 }
424
425 void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
426 {
427         TypeInfo &type = types[*(op+1)];
428         DataType component = types[*(op+2)].type;
429         unsigned count = *(op+3);
430         type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
431 }
432
433 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
434 {
435         TypeInfo &type = types[*(op+1)];
436         DataType column = types[*(op+2)].type;
437         unsigned count = *(op+3);
438         type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
439 }
440
441 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
442 {
443         TypeInfo &type = types[*(op+1)];
444         DataType sample = types[*(op+2)].type;
445         unsigned dimensions = *(op+3);
446         bool depth = *(op+4)==1;
447         bool array = *(op+5);
448         type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
449 }
450
451 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
452 {
453         TypeInfo &type = types[*(op+1)];
454         DataType image = types[*(op+2)].type;
455         type.type = static_cast<DataType>(image | 0x100000);
456 }
457
458 void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
459 {
460         TypeInfo &type = types[*(op+1)];
461         const TypeInfo &elem = types[*(op+2)];
462         type.type = elem.type;
463         type.struct_type = elem.struct_type;
464
465         const Constant &size = constants[*(op+3)];
466         if(size.constant_id>=0)
467                 type.array_size_spec = &size;
468         else if(size.type==INT || size.type==UNSIGNED_INT)
469                 type.array_size = size.i_value;
470 }
471
472 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
473 {
474         CodeIterator op_end = get_op_end(op);
475         unsigned id = *(op+1);
476         Structure &strct = structs[id];
477         strct.name = names[id];
478         types[id].struct_type = &strct;
479         op += 2;
480
481         strct.members.resize(op_end-op);
482         vector<StructMember>::iterator mem = strct.members.begin();
483         for(; op!=op_end; ++op, ++mem)
484         {
485                 TypeInfo &type = types[*op];
486                 mem->type = type.type;
487                 mem->struct_type = type.struct_type;
488                 mem->array_size = type.array_size;
489                 mem->array_size_spec = type.array_size_spec;
490                 mem->array_stride = type.array_stride;
491         }
492 }
493
494 void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
495 {
496         TypeInfo &type = types[*(op+1)];
497         type = types[*(op+3)];
498         type.storage = static_cast<StorageClass>(*(op+2));
499 }
500
501 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
502 {
503         unsigned id = *(op+2);
504         Constant &cnst = constants[id];
505         cnst.name = names[id];
506         cnst.type = types[*(op+1)].type;
507         if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
508                 cnst.i_value = true;
509         else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
510                 cnst.i_value = false;
511         else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
512                 cnst.i_value = *(op+3);
513         else if(cnst.type==FLOAT)
514                 cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
515 }
516
517 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
518 {
519         unsigned id = *(op+2);
520         Variable &var = variables[id];
521         var.name = names[id];
522         const TypeInfo &type = types[*(op+1)];
523         var.storage = type.storage;
524         var.type = type.type;
525         var.struct_type = type.struct_type;
526         var.array_size = type.array_size;
527         var.array_size_spec = type.array_size_spec;
528 }
529
530 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
531 {
532         unsigned id = *(op+1);
533         unsigned decoration = *(op+2);
534         op += 3;
535
536         switch(decoration)
537         {
538         case DECO_SPEC_ID:
539                 constants[id].constant_id = *op;
540                 break;
541         case DECO_ARRAY_STRIDE:
542                 types[id].array_stride = *op;
543                 break;
544         case DECO_LOCATION:
545                 variables[id].location = *op;
546                 break;
547         case DECO_BINDING:
548                 variables[id].binding = *op;
549                 break;
550         case DECO_DESCRIPTOR_SET:
551                 variables[id].descriptor_set = *op;
552                 break;
553         }
554 }
555
556 void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
557 {
558         Structure &strct = structs[*(op+1)];
559         unsigned index = *(op+2);
560         if(index>=strct.members.size())
561                 strct.members.resize(index+1);
562         unsigned decoration = *(op+3);
563         op += 4;
564
565         StructMember &member = strct.members[index];
566         switch(decoration)
567         {
568         case DECO_MATRIX_STRIDE:
569                 member.matrix_stride = *op;
570                 break;
571         case DECO_OFFSET:
572                 member.offset = *op;
573                 break;
574         }
575 }
576
577 } // namespace GL
578 } // namespace Msp