]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/module.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / module.cpp
index 72a6abca43781a20e84f07abc8dbb1373f2eb295..04a37fcc3d7bd25265160b9117c5975a734a1e42 100644 (file)
@@ -1,4 +1,6 @@
+#include <msp/core/algorithm.h>
 #include <msp/io/print.h>
+#include "device.h"
 #include "module.h"
 #include "resources.h"
 
@@ -12,6 +14,7 @@ enum SpirVConstants
        OP_NAME = 5,
        OP_MEMBER_NAME = 6,
        OP_ENTRY_POINT = 15,
+       OP_EXECUTION_MODE = 16,
        OP_TYPE_VOID = 19,
        OP_TYPE_BOOL = 20,
        OP_TYPE_INT = 21,
@@ -30,12 +33,29 @@ 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_COPY_OBJECT = 83,
+       OP_PHI = 245,
+       OP_SELECTION_MERGE = 247,
+       OP_LABEL = 248,
+       OP_BRANCH = 249,
+       OP_BRANCH_CONDITIONAL = 250,
+       OP_SWITCH = 251,
+       OP_KILL = 252,
+       OP_RETURN = 253,
+       OP_RETURN_VALUE = 254,
+       OP_UNREACHABLE = 255,
+
+       EXEC_LOCAL_SIZE = 17,
 
        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,
@@ -47,14 +67,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);
 }
@@ -64,6 +84,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)
 {
@@ -79,44 +113,9 @@ 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(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(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(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()];
-}
-
 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));
@@ -128,6 +127,7 @@ void SpirVModule::load_code(IO::Base &io)
        }
 
        reflect();
+       create();
 }
 
 void SpirVModule::compile(SL::Compiler &compiler)
@@ -135,6 +135,7 @@ void SpirVModule::compile(SL::Compiler &compiler)
        compiler.compile(SL::Compiler::SPIRV);
        code = compiler.get_combined_spirv();
        reflect();
+       create();
 }
 
 void SpirVModule::reflect()
@@ -144,8 +145,8 @@ void SpirVModule::reflect()
 
        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");
@@ -153,26 +154,32 @@ void SpirVModule::reflect()
        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)
-               {
-                       if(j->struct_type)
+               for(StructMember &m: s.members)
+                       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);
                        }
-               }
 
-               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)
                {
@@ -183,74 +190,296 @@ void SpirVModule::reflect()
                        last_member = lm;
                }
                
-               i->size = last_offset+get_type_size(last_member->type);
-               i->size = (i->size+15)&~15;
+               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;
        }
 
        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);
                }
 
        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);
+       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);
+               if(b.condition)
+                       specializable = true;
+
+               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);
+               }
+       }
 }
 
+SpirVModule *SpirVModule::specialize(const map<string, int> &spec_values) const
+{
+       vector<uint8_t> flags(code[3], 1);
 
-SpirVModule::EntryPoint::EntryPoint():
-       stage(VERTEX)
-{ }
+       std::map<unsigned, int> spec_values_by_id;
+       for(const Constant &c: spec_constants)
+       {
+               auto i = spec_values.find(c.name);
+               if(i!=spec_values.end())
+               {
+                       flags[c.id] = (i->second ? 5 : 3);
+                       spec_values_by_id[c.constant_id] = i->second;
+               }
+       }
 
+       for(const EntryPoint &e: entry_points)
+               flags[e.id] = 0;
+       for(const Variable &v: variables)
+               flags[v.id] = 0;
+       for(const InstructionBlock &b: blocks)
+               flags[b.id] = 0;
+       for(const InstructionBlock *b: collect_visited_blocks(spec_values_by_id))
+       {
+               flags[b->id] = 1;
+               for(const Variable *v: b->accessed_variables)
+                       flags[v->id] = 1;
+       }
 
-SpirVModule::StructMember::StructMember():
-       type(VOID),
-       struct_type(0),
-       offset(0),
-       array_size(0),
-       array_stride(0),
-       matrix_stride(0)
-{ }
+       std::vector<uint32_t> new_code;
+       new_code.reserve(code.size());
 
+       auto op = code.begin()+5;
+       new_code.insert(new_code.begin(), code.begin(), op);
+
+       bool skip_block = false;
+       while(op!=code.end())
+       {
+               unsigned word_count = *op>>16;
+               unsigned opcode = *op&0xFFFF;
+
+               bool copy = !skip_block;
+               if(skip_block)
+               {
+                       skip_block = (opcode!=OP_BRANCH && opcode!=OP_BRANCH_CONDITIONAL && opcode!=OP_SWITCH &&
+                               opcode!=OP_KILL && opcode!=OP_RETURN && opcode!=OP_RETURN_VALUE && opcode!=OP_UNREACHABLE);
+               }
+               else
+               {
+                       if(opcode==OP_NAME)
+                               copy = flags[*(op+1)];
+                       else if(opcode==OP_ENTRY_POINT)
+                       {
+                               unsigned start = new_code.size();
+                               new_code.push_back(opcode);
+                               new_code.push_back(*(op+1));
+                               unsigned func_id = *(op+2);
+                               new_code.push_back(func_id);
+
+                               unsigned i = 3;
+                               while(i<word_count)
+                               {
+                                       unsigned word = *(op+i++);
+                                       new_code.push_back(word);
+                                       // Strings are nul-terminated and nul-padded
+                                       if(!(word>>24))
+                                               break;
+                               }
+
+                               unsigned var_count = 0;
+                               for(; i<word_count; ++i)
+                               {
+                                       unsigned id = *(op+i);
+                                       if(flags[id])
+                                       {
+                                               ++var_count;
+                                               new_code.push_back(id);
+                                       }
+                               }
+
+                               if(var_count)
+                               {
+                                       flags[func_id] = 1;
+                                       new_code[start] |= (new_code.size()-start)<<16;
+                               }
+                               else
+                                       new_code.resize(start);
+
+                               copy = false;
+                       }
+                       else if(opcode==OP_EXECUTION_MODE)
+                               copy = flags[*(op+1)];
+                       else if(opcode==OP_SPEC_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_FALSE)
+                       {
+                               unsigned id = *(op+2);
+                               if(flags[id]&2)
+                               {
+                                       new_code.push_back(0x30000 | (flags[id]&4 ? OP_CONSTANT_TRUE : OP_CONSTANT_FALSE));
+                                       new_code.push_back(*(op+1));
+                                       new_code.push_back(id);
+
+                                       copy = false;
+                               }
+                       }
+                       else if(opcode==OP_VARIABLE)
+                               copy = flags[*(op+2)];
+                       else if(opcode==OP_DECORATE)
+                       {
+                               unsigned id = *(op+1);
+                               copy = flags[id];
+                               if(copy && *(op+2)==DECO_SPEC_ID)
+                                       copy = !(flags[id]&2);
+                       }
+                       else if(opcode==OP_LABEL)
+                       {
+                               copy = flags[*(op+1)];
+                               skip_block = !copy;
+                       }
+                       else if(opcode==OP_SELECTION_MERGE)
+                       {
+                               unsigned next_opcode = *(op+word_count)&0xFFFF;
+                               if(next_opcode==OP_BRANCH_CONDITIONAL)
+                               {
+                                       unsigned true_id = *(op+word_count+2);
+                                       unsigned false_id = *(op+word_count+3);
+                                       if(!flags[true_id] || !flags[false_id])
+                                       {
+                                               new_code.push_back(0x20000 | OP_BRANCH);
+                                               new_code.push_back(flags[true_id] ? true_id : false_id);
+                                               copy = false;
+
+                                               /* Skip the branch instruction when it's encountered on the
+                                               next iteration */
+                                               skip_block = true;
+                                       }
+                               }
+                       }
+                       else if(opcode==OP_PHI)
+                       {
+                               unsigned active_count = 0;
+                               unsigned result_id = 0;
+                               for(unsigned i=3; i<word_count; i+=2)
+                                       if(flags[*(op+i+1)])
+                                       {
+                                               ++active_count;
+                                               result_id = *(op+i);
+                                       }
+
+                               if(active_count==1)
+                               {
+                                       new_code.push_back(0x40000 | OP_COPY_OBJECT);
+                                       new_code.push_back(*(op+1));
+                                       new_code.push_back(*(op+2));
+                                       new_code.push_back(result_id);
+                                       copy = false;
+                               }
+                       }
+               }
+
+               if(copy)
+               {
+                       for(unsigned i=0; i<word_count; ++i)
+                               new_code.push_back(*(op+i));
+               }
+
+               op += word_count;
+       }
+
+       SpirVModule *spec_mod = new SpirVModule;
+       spec_mod->code = move(new_code);
+       spec_mod->reflect();
+       spec_mod->create();
+
+       return spec_mod;
+}
+
+vector<const SpirVModule::InstructionBlock *> SpirVModule::collect_visited_blocks(const map<unsigned, int> &spec_values) const
+{
+       vector<uint8_t> visited(blocks.size(), 4);
+       for(unsigned i=0; i<blocks.size(); ++i)
+       {
+               const InstructionBlock &b = blocks[i];
+
+               bool cond = true;
+               if(b.condition)
+               {
+                       cond = b.condition->i_value;
+                       auto j = spec_values.find(b.condition->constant_id);
+                       if(j!=spec_values.end())
+                               cond = j->second;
+                       if(b.negate_condition)
+                               cond = !cond;
+               }
+
+               visited[i] |= cond*2;
+               for(const InstructionBlock *s: b.successors)
+                       visited[s-blocks.data()] &= 3;
+       }
+
+       for(unsigned i=0; i<blocks.size(); ++i)
+               if(visited[i]&4)
+                       collect_visited_blocks(i, visited);
+
+       vector<const SpirVModule::InstructionBlock *> result;
+       for(unsigned i=0; i<blocks.size(); ++i)
+               if(visited[i]&1)
+                       result.push_back(&blocks[i]);
+
+       return result;
+}
+
+void SpirVModule::collect_visited_blocks(unsigned i, vector<uint8_t> &visited) const
+{
+       visited[i] |= 1;
+       for(const InstructionBlock *s: blocks[i].successors)
+       {
+               unsigned j = s-blocks.data();
+               if((visited[j]&3)==2)
+                       collect_visited_blocks(j, visited);
+       }
+}
 
-SpirVModule::Variable::Variable():
-       type(VOID),
-       struct_type(0),
-       location(-1),
-       descriptor_set(-1),
-       binding(-1)
-{ }
 
 bool SpirVModule::Variable::operator==(const Variable &other) const
 {
@@ -264,16 +493,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;
 }
@@ -305,7 +525,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(); )
        {
@@ -318,26 +538,33 @@ void SpirVModule::Reflection::reflect_code(const vector<UInt32> &code)
                case OP_NAME: reflect_name(op); break;
                case OP_MEMBER_NAME: reflect_member_name(op); break;
                case OP_ENTRY_POINT: reflect_entry_point(op); break;
+               case OP_EXECUTION_MODE: reflect_execution_mode(op); break;
                case OP_TYPE_VOID: reflect_void_type(op); break;
                case OP_TYPE_BOOL: reflect_bool_type(op); break;
                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;
                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_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;
@@ -366,7 +593,9 @@ void SpirVModule::Reflection::reflect_member_name(CodeIterator op)
 void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
 {
        CodeIterator op_end = get_op_end(op);
-       EntryPoint &entry = entry_points[*(op+2)];
+       unsigned id = *(op+2);
+       EntryPoint &entry = entry_points[id];
+       entry.id = id;
        entry.stage = static_cast<Stage>(*(op+1));  // Execution model in SPIR-V spec
        op += 3;
        entry.name = read_string(op, op_end);
@@ -376,6 +605,18 @@ void SpirVModule::Reflection::reflect_entry_point(CodeIterator op)
                entry.globals.push_back(&variables[*op]);
 }
 
+void SpirVModule::Reflection::reflect_execution_mode(CodeIterator op)
+{
+       EntryPoint &entry = entry_points[*(op+1)];
+       unsigned mode = *(op+2);
+       if(mode==EXEC_LOCAL_SIZE)
+       {
+               entry.compute_local_size.x = *(op+3);
+               entry.compute_local_size.y = *(op+4);
+               entry.compute_local_size.z = *(op+5);
+       }
+}
+
 void SpirVModule::Reflection::reflect_void_type(CodeIterator op)
 {
        types[*(op+1)].type = VOID;
@@ -406,7 +647,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)
@@ -414,17 +655,19 @@ 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)
 {
        TypeInfo &type = types[*(op+1)];
-       DataType sample = types[*(op+2)].type;
+       DataType sample_type = types[*(op+2)].type;
        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);
+       bool sampled = *(op+7)==1;
+       type.type = static_cast<DataType>((depth*0x200000) | (sampled*0x100000) | (array*0x80000) |
+               ((dimensions+1)<<16) | sample_type);
 }
 
 void SpirVModule::Reflection::reflect_sampled_image_type(CodeIterator op)
@@ -440,11 +683,10 @@ 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.type==INT || size.type==UNSIGNED_INT)
+               type.array_size = size.i_value;
 }
 
 void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
@@ -453,11 +695,12 @@ void SpirVModule::Reflection::reflect_struct_type(CodeIterator op)
        unsigned id = *(op+1);
        Structure &strct = structs[id];
        strct.name = names[id];
+       strct.id = id;
        types[id].struct_type = &strct;
        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];
@@ -477,22 +720,20 @@ 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 opcode = get_opcode(*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.id = id;
+       cnst.type = types[*(op+1)].type;
+       if(opcode==OP_CONSTANT_TRUE || opcode==OP_SPEC_CONSTANT_TRUE)
+               cnst.i_value = true;
+       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);
+       else if(cnst.type==FLOAT)
+               cnst.f_value = *reinterpret_cast<const float *>(&*(op+3));
 }
 
 void SpirVModule::Reflection::reflect_variable(CodeIterator op)
@@ -500,6 +741,7 @@ void SpirVModule::Reflection::reflect_variable(CodeIterator op)
        unsigned id = *(op+2);
        Variable &var = variables[id];
        var.name = names[id];
+       var.id = id;
        const TypeInfo &type = types[*(op+1)];
        var.storage = type.storage;
        var.type = type.type;
@@ -507,6 +749,26 @@ void SpirVModule::Reflection::reflect_variable(CodeIterator op)
        var.array_size = type.array_size;
 }
 
+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)
 {
        unsigned id = *(op+1);
@@ -516,11 +778,14 @@ 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;
                break;
+       case DECO_BUILTIN:
+               variables[id].builtin = static_cast<BuiltinSemantic>(*op);
+               break;
        case DECO_LOCATION:
                variables[id].location = *op;
                break;
@@ -548,11 +813,49 @@ 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)
+{
+       unsigned id = *(op+1);
+       current_block = &blocks[id];
+       current_block->id = id;
+}
+
+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