]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/program.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / program.cpp
index 6013e7e82e9f35656f6741bb3743ea25a0237f5b..91f215651b57df64b834315c13aa89b86d57d546 100644 (file)
@@ -12,40 +12,59 @@ Program::Program(const Module &mod, const map<string, int> &spec_values)
        add_stages(mod, spec_values);
 }
 
+Program::Program(Program &&other):
+       ProgramBackend(move(other)),
+       reflect_data(move(other.reflect_data)),
+       specialized_spirv(other.specialized_spirv)
+{
+       other.specialized_spirv = 0;
+}
+
+Program::~Program()
+{
+       delete specialized_spirv;
+}
+
 void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
 {
        if(has_stages())
                throw invalid_operation("Program::add_stages");
 
        reflect_data = ReflectData();
+       const Module *final_module = &mod;
 
-       TransientData transient;
        switch(mod.get_format())
        {
        case Module::GLSL:
-               add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
+               add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
                break;
        case Module::SPIR_V:
-               add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values, transient);
+               if(static_cast<const SpirVModule &>(mod).is_specializable())
+               {
+                       specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
+                       final_module = specialized_spirv;
+               }
+               add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
                break;
        default:
                throw invalid_argument("Program::add_stages");
        }
 
-       finalize(mod, transient);
-
-       if(mod.get_format()==Module::SPIR_V)
+       if(final_module->get_format()==Module::SPIR_V)
        {
-               collect_uniforms(static_cast<const SpirVModule &>(mod), transient.spec_values);
-               collect_attributes(static_cast<const SpirVModule &>(mod));
+               const SpirVModule &spirv_mod = *static_cast<const SpirVModule *>(final_module);
+               collect_uniforms(spirv_mod);
+               collect_attributes(spirv_mod);
+               collect_builtins(spirv_mod);
+
+               for(const SpirVModule::EntryPoint &e: spirv_mod.get_entry_points())
+                       if(e.stage==SpirVModule::COMPUTE)
+                               reflect_data.compute_wg_size = e.compute_local_size;
        }
 
-       for(ReflectData::UniformBlockInfo &b: reflect_data.uniform_blocks)
-               if(!b.data_size && !b.uniforms.empty())
-               {
-                       const ReflectData::UniformInfo &uni = *b.uniforms.back();
-                       b.data_size = uni.location*16+uni.array_size*get_type_size(uni.type);
-               }
+       finalize_uniforms();
+
+       reflect_data.update_used_bindings();
 
        for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
                require_type(u.type);
@@ -53,44 +72,64 @@ 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());
+       reflect_data.uniform_blocks.emplace_back();
        vector<vector<string> > block_uniform_names(1);
 
        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());
+                       reflect_data.uniform_blocks.emplace_back();
                        ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
                        info.name = v.struct_type->name;
-                       info.bind_point = v.binding;
                        info.data_size = v.struct_type->size;
+                       if(v.storage==SpirVModule::PUSH_CONSTANT)
+                       {
+                               info.bind_point = ReflectData::PUSH_CONSTANT;
+                               reflect_data.push_constants_size = info.data_size;
+                       }
+                       else
+                       {
+                               if(v.binding>=0)
+                                       info.bind_point = v.binding | (v.descriptor_set<<20);
+                               else
+                                       info.bind_point = ReflectData::DEFAULT_BLOCK;
+                               reflect_data.n_descriptor_sets = max(reflect_data.n_descriptor_sets, v.descriptor_set+1);
+                       }
 
                        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());
+                       block_uniform_names.emplace_back();
+                       collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
                }
-               else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
+               else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
                {
                        block_uniform_names[0].push_back(v.name);
-                       reflect_data.uniforms.push_back(ReflectData::UniformInfo());
+                       reflect_data.uniforms.emplace_back();
                        ReflectData::UniformInfo &info = reflect_data.uniforms.back();
                        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);
+                       reflect_data.n_descriptor_sets = max(reflect_data.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];
@@ -108,40 +147,31 @@ void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int>
        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)
+                       if(m.array_size)
                        {
-                               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)
-                       {
-                               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
                {
                        string name = prefix+m.name;
                        uniform_names.push_back(name);
-                       reflect_data.uniforms.push_back(ReflectData::UniformInfo());
+                       reflect_data.uniforms.emplace_back();
                        ReflectData::UniformInfo &info = reflect_data.uniforms.back();
                        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;
@@ -157,7 +187,7 @@ void Program::collect_attributes(const SpirVModule &mod)
                        for(const SpirVModule::Variable *v: e.globals)
                                if(v->storage==SpirVModule::INPUT)
                                {
-                                       reflect_data.attributes.push_back(ReflectData::AttributeInfo());
+                                       reflect_data.attributes.emplace_back();
                                        ReflectData::AttributeInfo &info = reflect_data.attributes.back();
                                        info.name = v->name;
                                        info.location = v->location;
@@ -165,6 +195,8 @@ void Program::collect_attributes(const SpirVModule &mod)
                                        info.type = v->type;
                                }
                }
+
+       sort_member(reflect_data.attributes, &ReflectData::AttributeInfo::name);
 }
 
 void Program::collect_builtins(const SpirVModule &mod)
@@ -226,6 +258,12 @@ int Program::get_uniform_binding(Tag tag) const
        return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
 }
 
+bool Program::uses_binding(int binding) const
+{
+       auto i = lower_bound(reflect_data.used_bindings, binding);
+       return i!=reflect_data.used_bindings.end() && *i==binding;
+}
+
 const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
 {
        auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);