]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/module.cpp
Handle SPIR-V arrays with specialization constant for size
[libs/gl.git] / source / core / module.cpp
index ce540505fdf32d75a71c8430cb564b0537a3ca61..2474feaeb4cfffc8907caf4c1de730d40e5718cd 100644 (file)
@@ -153,6 +153,14 @@ void SpirVModule::reflect()
        Reflection reflection;
        reflection.reflect_code(code);
 
+       map<const Constant *, unsigned> spec_indices;
+       for(map<unsigned, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
+               if(i->second.constant_id>=0)
+               {
+                       spec_indices[&i->second] = spec_constants.size();
+                       spec_constants.push_back(i->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)
@@ -170,6 +178,11 @@ void SpirVModule::reflect()
                                map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
                                j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
                        }
+                       if(j->array_size_spec)
+                       {
+                               map<const Constant *, unsigned>::const_iterator k = spec_indices.find(j->array_size_spec);
+                               j->array_size_spec = (k!=spec_indices.end() ? &spec_constants[k->second] : 0);
+                       }
                }
 
                const StructMember *last_member = &i->members.back();
@@ -179,6 +192,8 @@ 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;
                }
@@ -206,11 +221,18 @@ void SpirVModule::reflect()
        }
 
        for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
+       {
                if(i->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);
                }
+               if(i->array_size_spec)
+               {
+                       map<const Constant *, unsigned>::const_iterator j = spec_indices.find(i->array_size_spec);
+                       i->array_size_spec = (j!=spec_indices.end() ? &spec_constants[j->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)
@@ -223,10 +245,6 @@ void SpirVModule::reflect()
                        *j = (k!=var_indices.end() ? &variables[k->second] : 0);
                }
        }
-
-       for(map<unsigned, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
-               if(i->second->constant_id>=0)
-                       spec_constants.push_back(i->second);
 }
 
 
@@ -240,6 +258,7 @@ SpirVModule::StructMember::StructMember():
        struct_type(0),
        offset(0),
        array_size(0),
+       array_size_spec(0),
        array_stride(0),
        matrix_stride(0)
 { }
@@ -268,6 +287,7 @@ bool SpirVModule::Variable::operator==(const Variable &other) const
 SpirVModule::TypeInfo::TypeInfo():
        type(VOID),
        struct_type(0),
+       array_size_spec(0),
        array_size(0),
        array_stride(0),
        storage(static_cast<StorageClass>(-1))
@@ -443,7 +463,9 @@ void SpirVModule::Reflection::reflect_array_type(CodeIterator op)
        type.struct_type = elem.struct_type;
 
        const Constant &size = constants[*(op+3)];
-       if(size.type==INT || size.type==UNSIGNED_INT)
+       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;
 }
 
@@ -464,6 +486,7 @@ 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;
        }
 }
@@ -501,6 +524,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)