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