]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/program.cpp
Fix reflection of image types from Spir-V modules
[libs/gl.git] / source / core / program.cpp
index 2934d240c7ef0c3bda346636d51725c8729b7bd1..acdb20e88a5488063c3778a598af10f411b1b541 100644 (file)
@@ -26,7 +26,7 @@ void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
                add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
                break;
        case Module::SPIR_V:
-               add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values, transient);
+               add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
                break;
        default:
                throw invalid_argument("Program::add_stages");
@@ -36,8 +36,9 @@ void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
 
        if(mod.get_format()==Module::SPIR_V)
        {
-               collect_uniforms(static_cast<const SpirVModule &>(mod), transient.spec_values);
+               collect_uniforms(static_cast<const SpirVModule &>(mod));
                collect_attributes(static_cast<const SpirVModule &>(mod));
+               collect_builtins(static_cast<const SpirVModule &>(mod));
        }
 
        finalize_uniforms();
@@ -48,27 +49,37 @@ void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
                require_type(a.type);
 }
 
-void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int> &spec_values)
+void Program::collect_uniforms(const SpirVModule &mod)
 {
        // Prepare the default block
        reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
        vector<vector<string> > block_uniform_names(1);
 
+       unsigned n_descriptor_sets = 0;
        for(const SpirVModule::Variable &v: mod.get_variables())
        {
-               if(v.storage==SpirVModule::UNIFORM && v.struct_type)
+               if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
                {
                        reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
                        ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
                        info.name = v.struct_type->name;
-                       info.bind_point = v.binding;
+                       if(v.storage==SpirVModule::PUSH_CONSTANT)
+                               info.bind_point = ReflectData::PUSH_CONSTANT;
+                       else
+                       {
+                               if(v.binding>=0)
+                                       info.bind_point = v.binding | (v.descriptor_set<<20);
+                               else
+                                       info.bind_point = ReflectData::DEFAULT_BLOCK;
+                               n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
+                       }
                        info.data_size = v.struct_type->size;
 
                        string prefix;
                        if(!v.name.empty())
                                prefix = v.struct_type->name+".";
                        block_uniform_names.push_back(vector<string>());
-                       collect_block_uniforms(*v.struct_type, prefix, 0, spec_values, block_uniform_names.back());
+                       collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
                }
                else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
                {
@@ -78,14 +89,22 @@ void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int>
                        info.name = v.name;
                        info.tag = v.name;
                        info.location = v.location;
-                       info.binding = v.binding;
-                       info.array_size = v.array_size;
+                       if(v.binding>=0)
+                               info.binding = v.binding | (v.descriptor_set<<20);
+                       n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
+                       info.array_size = max(v.array_size, 1U);
                        info.type = v.type;
                }
        }
 
        sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
 
+       if(block_uniform_names.front().empty())
+       {
+               reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
+               block_uniform_names.erase(block_uniform_names.begin());
+       }
+
        for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
        {
                ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
@@ -100,32 +119,24 @@ void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int>
                block.update_layout_hash();
        }
 
+       reflect_data.n_descriptor_sets = n_descriptor_sets;
        reflect_data.update_layout_hash();
 }
 
-void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, const map<unsigned, int> &spec_values, vector<string> &uniform_names)
+void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
 {
        for(const SpirVModule::StructMember &m: strct.members)
        {
                unsigned offset = base_offset+m.offset;
                if(m.struct_type)
                {
-                       unsigned array_size = m.array_size;
-                       if(m.array_size_spec)
-                       {
-                               array_size = m.array_size_spec->i_value;
-                               auto j = spec_values.find(m.array_size_spec->constant_id);
-                               if(j!=spec_values.end())
-                                       array_size = j->second;
-                       }
-
-                       if(array_size)
+                       if(m.array_size)
                        {
-                               for(unsigned j=0; j<array_size; ++j, offset+=m.array_stride)
-                                       collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, spec_values, uniform_names);
+                               for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
+                                       collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
                        }
                        else
-                               collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, spec_values, uniform_names);
+                               collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
                }
                else
                {
@@ -136,7 +147,7 @@ void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const
                        info.name = name;
                        info.tag = name;
                        info.offset = offset;
-                       info.array_size = m.array_size;
+                       info.array_size = max(m.array_size, 1U);
                        info.array_stride = m.array_stride;
                        info.matrix_stride = m.matrix_stride;
                        info.type = m.type;