]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/module.cpp
Use default member initializers for simple types
[libs/gl.git] / source / core / module.cpp
index 6f7fbdb93e9799dc488b7b10646c48cf2bc0c4ac..eaed8a45c7b7ed04003e996d28bcd7c85b99e68c 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/io/print.h>
 #include "module.h"
 #include "resources.h"
@@ -100,23 +101,23 @@ SpirVModule &SpirVModule::operator=(const SpirVModule &other)
 
 void SpirVModule::remap_pointers_from(const SpirVModule &other)
 {
-       for(vector<EntryPoint>::iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
-               for(vector<const Variable *>::iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
-                       *j = &variables[*j-&other.variables.front()];
+       for(EntryPoint &e: entry_points)
+               for(const Variable *&v: e.globals)
+                       v = &variables[v-&other.variables.front()];
 
-       for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
-               if(i->struct_type)
-                       i->struct_type = &structs[i->struct_type-&other.structs.front()];
+       for(Variable &v: variables)
+               if(v.struct_type)
+                       v.struct_type = &structs[v.struct_type-&other.structs.front()];
 
-       for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
-               for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
-                       if(j->struct_type)
-                               j->struct_type = &structs[j->struct_type-&other.structs.front()];
+       for(Structure &s: structs)
+               for(StructMember &m: s.members)
+                       if(m.struct_type)
+                               m.struct_type = &structs[m.struct_type-&other.structs.front()];
 }
 
 void SpirVModule::load_code(IO::Base &io)
 {
-       UInt32 buffer[1024];
+       uint32_t buffer[1024];
        while(1)
        {
                unsigned len = io.read(reinterpret_cast<char *>(buffer), sizeof(buffer));
@@ -127,13 +128,25 @@ void SpirVModule::load_code(IO::Base &io)
                code.insert(code.end(), buffer, buffer+len);
        }
 
+       reflect();
+}
+
+void SpirVModule::compile(SL::Compiler &compiler)
+{
+       compiler.compile(SL::Compiler::SPIRV);
+       code = compiler.get_combined_spirv();
+       reflect();
+}
+
+void SpirVModule::reflect()
+{
        if(code.empty())
                throw invalid_module("Empty SPIR-V code");
 
        if(code[0]==SPIRV_MAGIC_REVERSED)
        {
-               for(vector<UInt32>::iterator i=code.begin(); i!=code.end(); ++i)
-                       *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>24)&0xFF);
+               for(uint32_t &c: code)
+                       c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
        }
        else if(code[0]!=SPIRV_MAGIC)
                throw invalid_module("SPIR-V magic number not found");
@@ -141,109 +154,96 @@ void SpirVModule::load_code(IO::Base &io)
        Reflection reflection;
        reflection.reflect_code(code);
 
+       map<const Constant *, unsigned> spec_indices;
+       for(const auto &kvp: reflection.constants)
+               if(kvp.second.constant_id>=0)
+               {
+                       spec_indices[&kvp.second] = spec_constants.size();
+                       spec_constants.push_back(kvp.second);
+               }
+
        map<const Structure *, unsigned> struct_indices;
        structs.reserve(reflection.structs.size());
-       for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
+       for(const auto &kvp: reflection.structs)
        {
-               struct_indices[&i->second] = structs.size();
-               structs.push_back(i->second);
+               struct_indices[&kvp.second] = structs.size();
+               structs.push_back(kvp.second);
        }
 
-       for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
+       for(Structure &s: structs)
        {
-               for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
+               for(StructMember &m: s.members)
                {
-                       if(j->struct_type)
+                       if(m.struct_type)
                        {
-                               map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
-                               j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
+                               auto i = struct_indices.find(m.struct_type);
+                               m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
+                       }
+                       if(m.array_size_spec)
+                       {
+                               auto i = spec_indices.find(m.array_size_spec);
+                               m.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
                        }
                }
 
-               const StructMember *last_member = &i->members.back();
+               const StructMember *last_member = &s.members.back();
                unsigned last_offset = last_member->offset;
                while(last_member->struct_type)
                {
                        const StructMember *lm = &last_member->struct_type->members.back();
                        if(last_member->array_size)
                                last_offset += last_member->array_stride*(last_member->array_size-1);
+                       else if(last_member->array_size_spec)
+                               last_offset += last_member->array_stride*(last_member->array_size_spec->i_value-1);
                        last_offset += lm->offset;
                        last_member = lm;
                }
                
-               i->size = last_offset+get_type_size(last_member->type);
-               i->size = (i->size+15)&~15;
+               s.size = last_offset+get_type_size(last_member->type);
+               s.size = (s.size+15)&~15;
        }
 
        map<const Variable *, unsigned> var_indices;
        variables.reserve(reflection.variables.size());
-       for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
+       for(const auto &kvp: reflection.variables)
        {
-               int dup_index = -1;
-               for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
-                       if(*j==i->second)
-                               dup_index = j-variables.begin();
-
-               if(dup_index>=0)
-                       var_indices[&i->second] = dup_index;
+               auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
+               if(i!=variables.end())
+                       var_indices[&kvp.second] = i-variables.begin();
                else
                {
-                       var_indices[&i->second] = variables.size();
-                       variables.push_back(i->second);
+                       var_indices[&kvp.second] = variables.size();
+                       variables.push_back(kvp.second);
                }
        }
 
-       for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
-               if(i->struct_type)
+       for(Variable &v: variables)
+       {
+               if(v.struct_type)
                {
-                       map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
-                       i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
+                       auto i = struct_indices.find(v.struct_type);
+                       v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
                }
+               if(v.array_size_spec)
+               {
+                       auto i = spec_indices.find(v.array_size_spec);
+                       v.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
+               }
+       }
 
        entry_points.reserve(reflection.entry_points.size());
-       for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
+       for(const auto &kvp: reflection.entry_points)
        {
-               entry_points.push_back(i->second);
+               entry_points.push_back(kvp.second);
                EntryPoint &entry = entry_points.back();
-               for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
+               for(const Variable *&v: entry.globals)
                {
-                       map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
-                       *j = (k!=var_indices.end() ? &variables[k->second] : 0);
+                       auto i = var_indices.find(v);
+                       v = (i!=var_indices.end() ? &variables[i->second] : 0);
                }
        }
-
-       for(map<unsigned, SpecConstant>::const_iterator i=reflection.spec_constants.begin(); i!=reflection.spec_constants.end(); ++i)
-               spec_constants.push_back(i->second);
 }
 
-void SpirVModule::compile(SL::Compiler &)
-{
-       throw logic_error("Not implemented yet");
-}
-
-
-SpirVModule::EntryPoint::EntryPoint():
-       stage(VERTEX)
-{ }
-
-
-SpirVModule::StructMember::StructMember():
-       type(VOID),
-       struct_type(0),
-       offset(0),
-       array_size(0),
-       array_stride(0),
-       matrix_stride(0)
-{ }
-
-
-SpirVModule::Variable::Variable():
-       type(VOID),
-       struct_type(0),
-       location(-1),
-       descriptor_set(-1),
-       binding(-1)
-{ }
 
 bool SpirVModule::Variable::operator==(const Variable &other) const
 {
@@ -257,16 +257,7 @@ bool SpirVModule::Variable::operator==(const Variable &other) const
 }
 
 
-SpirVModule::TypeInfo::TypeInfo():
-       type(VOID),
-       struct_type(0),
-       array_size(0),
-       array_stride(0),
-       storage(static_cast<StorageClass>(-1))
-{ }
-
-
-UInt32 SpirVModule::Reflection::get_opcode(UInt32 op)
+uint32_t SpirVModule::Reflection::get_opcode(uint32_t op)
 {
        return op&0xFFFF;
 }
@@ -298,7 +289,7 @@ string SpirVModule::Reflection::read_string(CodeIterator &op, const CodeIterator
        throw invalid_module("Unterminated SPIR-V string literal");
 }
 
-void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
+void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
 {
        for(CodeIterator op=code.begin()+5; op!=code.end(); )
        {
@@ -322,12 +313,12 @@ void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
                case OP_TYPE_ARRAY: reflect_array_type(op); break;
                case OP_TYPE_STRUCT: reflect_struct_type(op); break;
                case OP_TYPE_POINTER: reflect_pointer_type(op); break;
-               case OP_CONSTANT_TRUE: constants[*(op+2)] = true; break;
-               case OP_CONSTANT_FALSE: constants[*(op+2)] = false; break;
-               case OP_CONSTANT: reflect_constant(op); break;
+               case OP_CONSTANT_TRUE:
+               case OP_CONSTANT_FALSE:
+               case OP_CONSTANT:
                case OP_SPEC_CONSTANT_TRUE:
                case OP_SPEC_CONSTANT_FALSE:
-               case OP_SPEC_CONSTANT: reflect_spec_constant(op); break;
+               case OP_SPEC_CONSTANT: reflect_constant(op); break;
                case OP_VARIABLE: reflect_variable(op); break;
                case OP_DECORATE: reflect_decorate(op); break;
                case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
@@ -433,11 +424,12 @@ void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
        const TypeInfo &elem = types[*(op+2)];
        type.type = elem.type;
        type.struct_type = elem.struct_type;
-       const Variant &size = constants[*(op+3)];
-       if(size.check_type<int>())
-               type.array_size = size.value<int>();
-       else if(size.check_type<unsigned>())
-               type.array_size = size.value<unsigned>();
+
+       const Constant &size = constants[*(op+3)];
+       if(size.constant_id>=0)
+               type.array_size_spec = &size;
+       else if(size.type==INT || size.type==UNSIGNED_INT)
+               type.array_size = size.i_value;
 }
 
 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
@@ -450,13 +442,14 @@ void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
        op += 2;
 
        strct.members.resize(op_end-op);
-       vector<StructMember>::iterator mem = strct.members.begin();
+       auto mem = strct.members.begin();
        for(; op!=op_end; ++op, ++mem)
        {
                TypeInfo &type = types[*op];
                mem->type = type.type;
                mem->struct_type = type.struct_type;
                mem->array_size = type.array_size;
+               mem->array_size_spec = type.array_size_spec;
                mem->array_stride = type.array_stride;
        }
 }
@@ -469,23 +462,19 @@ void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
 }
 
 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
-{
-       const TypeInfo &type = types[*(op+1)];
-       unsigned id = *(op+2);
-       if(type.type==INT)
-               constants[id] = static_cast<int>(*(op+3));
-       else if(type.type==UNSIGNED_INT)
-               constants[id] = static_cast<unsigned>(*(op+3));
-       else if(type.type==FLOAT)
-               constants[id] = *reinterpret_cast<const float *>(&*(op+3));
-}
-
-void SpirVModule::Reflection::reflect_spec_constant(CodeIterator op)
 {
        unsigned id = *(op+2);
-       SpecConstant &spec = spec_constants[id];
-       spec.name = names[id];
-       spec.type = types[*(op+1)].type;
+       Constant &cnst = constants[id];
+       cnst.name = names[id];
+       cnst.type = types[*(op+1)].type;
+       if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
+               cnst.i_value = true;
+       else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
+               cnst.i_value = false;
+       else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
+               cnst.i_value = *(op+3);
+       else if(cnst.type==FLOAT)
+               cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
 }
 
 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
@@ -498,6 +487,7 @@ void SpirVModule::Reflection::reflect_variable(CodeIterator op)
        var.type = type.type;
        var.struct_type = type.struct_type;
        var.array_size = type.array_size;
+       var.array_size_spec = type.array_size_spec;
 }
 
 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
@@ -509,7 +499,7 @@ void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
        switch(decoration)
        {
        case DECO_SPEC_ID:
-               spec_constants[id].constant_id = *op;
+               constants[id].constant_id = *op;
                break;
        case DECO_ARRAY_STRIDE:
                types[id].array_stride = *op;