]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/module.cpp
Initial implementation of Vulkan backend
[libs/gl.git] / source / core / module.cpp
index eaed8a45c7b7ed04003e996d28bcd7c85b99e68c..0c2df8e189678fd3a0854aa81a2857e00095588b 100644 (file)
@@ -1,5 +1,6 @@
 #include <msp/core/algorithm.h>
 #include <msp/io/print.h>
+#include "device.h"
 #include "module.h"
 #include "resources.h"
 
@@ -31,12 +32,19 @@ enum SpirVConstants
        OP_SPEC_CONSTANT_FALSE = 49,
        OP_SPEC_CONSTANT = 50,
        OP_VARIABLE = 59,
+       OP_LOAD = 61,
+       OP_STORE = 62,
+       OP_ACCESS_CHAIN = 65,
        OP_DECORATE = 71,
        OP_MEMBER_DECORATE = 72,
+       OP_LABEL = 248,
+       OP_BRANCH = 249,
+       OP_BRANCH_CONDITIONAL = 250,
 
        DECO_SPEC_ID = 1,
        DECO_ARRAY_STRIDE = 6,
        DECO_MATRIX_STRIDE = 7,
+       DECO_BUILTIN = 11,
        DECO_LOCATION = 30,
        DECO_BINDING = 33,
        DECO_DESCRIPTOR_SET = 34,
@@ -48,14 +56,14 @@ namespace GL {
 
 void Module::set_source(const string &src)
 {
-       SL::Compiler compiler;
+       SL::Compiler compiler(create_features());
        compiler.set_source(src);
        compile(compiler);
 }
 
 void Module::load_source(IO::Base &io, Resources *res, const string &name)
 {
-       SL::Compiler compiler;
+       SL::Compiler compiler(create_features());
        compiler.load_source(io, res, name);
        compile(compiler);
 }
@@ -65,6 +73,20 @@ void Module::load_source(IO::Base &io, const string &name)
        load_source(io, 0, name);
 }
 
+SL::Features Module::create_features() const
+{
+       const DeviceInfo &dev_info = Device::get_current().get_info();
+       const SL::Features &device_features = dev_info.glsl_features;
+       SL::Features latest_features = SL::Features::latest(dev_info.api);
+       SL::Features features;
+       features.target_api = latest_features.target_api;
+       features.glsl_version = latest_features.glsl_version;
+       features.constant_id_range = device_features.constant_id_range;
+       features.uniform_binding_range = device_features.uniform_binding_range;
+       features.texture_binding_range = device_features.texture_binding_range;
+       return features;
+}
+
 
 void GlslModule::compile(SL::Compiler &compiler)
 {
@@ -80,41 +102,6 @@ void GlslModule::compile(SL::Compiler &compiler)
 }
 
 
-SpirVModule::SpirVModule(const SpirVModule &other):
-       code(other.code),
-       entry_points(other.entry_points),
-       structs(other.structs),
-       variables(other.variables)
-{
-       remap_pointers_from(other);
-}
-
-SpirVModule &SpirVModule::operator=(const SpirVModule &other)
-{
-       code = other.code;
-       entry_points = other.entry_points;
-       structs = other.structs;
-       variables = other.variables;
-       remap_pointers_from(other);
-       return *this;
-}
-
-void SpirVModule::remap_pointers_from(const SpirVModule &other)
-{
-       for(EntryPoint &e: entry_points)
-               for(const Variable *&v: e.globals)
-                       v = &variables[v-&other.variables.front()];
-
-       for(Variable &v: variables)
-               if(v.struct_type)
-                       v.struct_type = &structs[v.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_t buffer[1024];
@@ -129,6 +116,7 @@ void SpirVModule::load_code(IO::Base &io)
        }
 
        reflect();
+       create();
 }
 
 void SpirVModule::compile(SL::Compiler &compiler)
@@ -136,6 +124,7 @@ void SpirVModule::compile(SL::Compiler &compiler)
        compiler.compile(SL::Compiler::SPIRV);
        code = compiler.get_combined_spirv();
        reflect();
+       create();
 }
 
 void SpirVModule::reflect()
@@ -173,18 +162,11 @@ void SpirVModule::reflect()
        for(Structure &s: structs)
        {
                for(StructMember &m: s.members)
-               {
                        if(m.struct_type)
                        {
                                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 = &s.members.back();
                unsigned last_offset = last_member->offset;
@@ -193,13 +175,14 @@ void SpirVModule::reflect()
                        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;
                }
                
-               s.size = last_offset+get_type_size(last_member->type);
+               unsigned last_size = get_type_size(last_member->type); 
+               if(last_member->array_size)
+                       last_size += last_member->array_stride*(last_member->array_size-1);
+               s.size = last_offset+last_size;
                s.size = (s.size+15)&~15;
        }
 
@@ -218,18 +201,11 @@ void SpirVModule::reflect()
        }
 
        for(Variable &v: variables)
-       {
                if(v.struct_type)
                {
                        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(const auto &kvp: reflection.entry_points)
@@ -242,6 +218,32 @@ void SpirVModule::reflect()
                        v = (i!=var_indices.end() ? &variables[i->second] : 0);
                }
        }
+
+       map<const InstructionBlock *, unsigned> block_indices;
+       blocks.reserve(reflection.blocks.size());
+       for(const auto &kvp: reflection.blocks)
+       {
+               block_indices[&kvp.second] = blocks.size();
+               blocks.push_back(kvp.second);
+       }
+
+       for(InstructionBlock &b: blocks)
+       {
+               auto i = spec_indices.find(b.condition);
+               b.condition = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
+
+               for(const Variable *&v: b.accessed_variables)
+               {
+                       auto j = var_indices.find(v);
+                       v = (j!=var_indices.end() ? &variables[j->second] : 0);
+               }
+
+               for(const InstructionBlock *&s: b.successors)
+               {
+                       auto j = block_indices.find(s);
+                       s = (j!=block_indices.end() ? &blocks[j->second] : 0);
+               }
+       }
 }
 
 
@@ -307,7 +309,7 @@ void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
                case OP_TYPE_INT: reflect_int_type(op); break;
                case OP_TYPE_FLOAT: reflect_float_type(op); break;
                case OP_TYPE_VECTOR: reflect_vector_type(op); break;
-               case OP_TYPE_MATRIX: reflect_vector_type(op); break;
+               case OP_TYPE_MATRIX: reflect_matrix_type(op); break;
                case OP_TYPE_IMAGE: reflect_image_type(op); break;
                case OP_TYPE_SAMPLED_IMAGE: reflect_sampled_image_type(op); break;
                case OP_TYPE_ARRAY: reflect_array_type(op); break;
@@ -320,8 +322,14 @@ void SpirVModule::Reflection::reflect_code(const vector<uint32_t> &code)
                case OP_SPEC_CONSTANT_FALSE:
                case OP_SPEC_CONSTANT: reflect_constant(op); break;
                case OP_VARIABLE: reflect_variable(op); break;
+               case OP_LOAD:
+               case OP_STORE: reflect_access(op); break;
+               case OP_ACCESS_CHAIN: reflect_access_chain(op); break;
                case OP_DECORATE: reflect_decorate(op); break;
                case OP_MEMBER_DECORATE: reflect_member_decorate(op); break;
+               case OP_LABEL: reflect_label(op); break;
+               case OP_BRANCH: reflect_branch(op); break;
+               case OP_BRANCH_CONDITIONAL: reflect_branch_conditional(op); break;
                }
 
                op += word_count;
@@ -390,7 +398,7 @@ void SpirVModule::Reflection::reflect_vector_type(CodeIterator op)
        TypeInfo &type = types[*(op+1)];
        DataType component = types[*(op+2)].type;
        unsigned count = *(op+3);
-       type.type = static_cast<DataType>((count<<12) | (component&0xF00) | ((component&0xFF)*count));
+       type.type = static_cast<DataType>(((count-1)<<12) | (component&0xF00) | ((component&0xFF)*count));
 }
 
 void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
@@ -398,7 +406,7 @@ void SpirVModule::Reflection::reflect_matrix_type(CodeIterator op)
        TypeInfo &type = types[*(op+1)];
        DataType column = types[*(op+2)].type;
        unsigned count = *(op+3);
-       type.type = static_cast<DataType>((count<<16) | (column&0xF00) | ((column&0xFF)*count));
+       type.type = static_cast<DataType>(((count-1)<<14) | (column&0x3F00) | ((column&0xFF)*count));
 }
 
 void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
@@ -408,7 +416,7 @@ void SpirVModule::Reflection::reflect_image_type(CodeIterator op)
        unsigned dimensions = *(op+3);
        bool depth = *(op+4)==1;
        bool array = *(op+5);
-       type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | (dimensions+1) | sample);
+       type.type = static_cast<DataType>((depth*0x200000) | (array*0x80000) | ((dimensions+1)<<16) | sample);
 }
 
 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
@@ -426,9 +434,7 @@ void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
        type.struct_type = elem.struct_type;
 
        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)
+       if(size.type==INT || size.type==UNSIGNED_INT)
                type.array_size = size.i_value;
 }
 
@@ -449,7 +455,6 @@ void SpirVModule::Reflection::reflect_struct_type(CodeIterator 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;
        }
 }
@@ -463,13 +468,14 @@ void SpirVModule::Reflection::reflect_pointer_type(CodeIterator op)
 
 void SpirVModule::Reflection::reflect_constant(CodeIterator op)
 {
+       unsigned opcode = get_opcode(*op);
        unsigned id = *(op+2);
        Constant &cnst = constants[id];
        cnst.name = names[id];
        cnst.type = types[*(op+1)].type;
-       if(*op==OP_CONSTANT_TRUE || *op==OP_SPEC_CONSTANT_TRUE)
+       if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
                cnst.i_value = true;
-       else if(*op==OP_CONSTANT_FALSE || *op==OP_SPEC_CONSTANT_FALSE)
+       else if(opcode==OP_CONSTANT_FALSE || opcode==OP_SPEC_CONSTANT_FALSE)
                cnst.i_value = false;
        else if(cnst.type==INT || cnst.type==UNSIGNED_INT)
                cnst.i_value = *(op+3);
@@ -487,7 +493,26 @@ 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_access(CodeIterator op)
+{
+       if(current_block)
+       {
+               unsigned id = (get_opcode(*op)==OP_LOAD ? *(op+3) : *(op+1));
+               auto i = access_chain_bases.find(id);
+               if(i!=access_chain_bases.end())
+                       id = i->second;
+               Variable &var = variables[id];
+               auto j = find(current_block->accessed_variables, &var);
+               if(j==current_block->accessed_variables.end())
+                       current_block->accessed_variables.push_back(&var);
+       }
+}
+
+void SpirVModule::Reflection::reflect_access_chain(CodeIterator op)
+{
+       access_chain_bases[*(op+2)] = *(op+3);
 }
 
 void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
@@ -504,6 +529,9 @@ void SpirVModule::Reflection::reflect_decorate(CodeIterator op)
        case DECO_ARRAY_STRIDE:
                types[id].array_stride = *op;
                break;
+       case DECO_BUILTIN:
+               variables[id].builtin = static_cast<BuiltinSemantic>(*op);
+               break;
        case DECO_LOCATION:
                variables[id].location = *op;
                break;
@@ -531,11 +559,47 @@ void SpirVModule::Reflection::reflect_member_decorate(CodeIterator op)
        case DECO_MATRIX_STRIDE:
                member.matrix_stride = *op;
                break;
+       case DECO_BUILTIN:
+               member.builtin = static_cast<BuiltinSemantic>(*op);
+               break;
        case DECO_OFFSET:
                member.offset = *op;
                break;
        }
 }
 
+void SpirVModule::Reflection::reflect_label(CodeIterator op)
+{
+       current_block = &blocks[*(op+1)];
+}
+
+void SpirVModule::Reflection::reflect_branch(CodeIterator op)
+{
+       InstructionBlock &block = blocks[*(op+1)];
+       block.condition = &true_condition;
+       current_block->successors.push_back(&block);
+}
+
+void SpirVModule::Reflection::reflect_branch_conditional(CodeIterator op)
+{
+       InstructionBlock &true_block = blocks[*(op+2)];
+       InstructionBlock &false_block = blocks[*(op+3)];
+
+       auto i = constants.find(*(op+1));
+       if(i!=constants.end() && i->second.constant_id)
+       {
+               if(!true_block.condition)
+                       true_block.condition = &i->second;
+               if(!false_block.condition)
+               {
+                       false_block.condition = &i->second;
+                       false_block.negate_condition = true;
+               }
+       }
+
+       current_block->successors.push_back(&true_block);
+       current_block->successors.push_back(&false_block);
+}
+
 } // namespace GL
 } // namespace Msp