]> git.tdb.fi Git - libs/gl.git/commitdiff
Track the in-use state of resource bindings in PipelineState
authorMikko Rasa <tdb@tdb.fi>
Sun, 21 Nov 2021 13:26:22 +0000 (15:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 21 Nov 2021 23:44:36 +0000 (01:44 +0200)
source/backends/opengl/pipelinestate_backend.cpp
source/backends/vulkan/pipelinestate_backend.cpp
source/core/pipelinestate.cpp
source/core/pipelinestate.h
source/core/program.cpp
source/core/program.h
source/core/reflectdata.h

index 9616b18be326d99cbccdfa0c30ad0f195732a60f..78ba7978042dd84587480c7128a10d1df6fd1378 100644 (file)
@@ -108,7 +108,7 @@ void OpenGLPipelineState::apply() const
                for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
                        if(u.changed || mask==~0U)
                        {
-                               if(u.block)
+                               if(u.used)
                                {
                                        if(u.binding>=0)
                                        {
@@ -132,7 +132,7 @@ void OpenGLPipelineState::apply() const
                for(const PipelineState::BoundTexture &t: self.textures)
                        if(t.changed || mask==~0U)
                        {
-                               if(t.texture && t.sampler)
+                               if(t.used)
                                {
                                        if(ARB_direct_state_access)
                                                glBindTextureUnit(t.binding, t.texture->id);
index 32672ec42970b2a5599409375eb9ffcb48c88d4b..4a47b223d992c31ead569b39d49e441bbc76f633 100644 (file)
@@ -44,9 +44,11 @@ void VulkanPipelineState::update() const
        {
                unsigned changed_sets = (self.changes&PipelineState::SHPROG ? ~0U : 0U);
                for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
-                       if(u.changed && u.binding>=0)
+                       if(u.changed || changed_sets==~0U)
                        {
-                               changed_sets |= 1<<(u.binding>>20);
+                               u.used = self.shprog->uses_binding(u.binding);
+                               if(u.binding>=0)
+                                       changed_sets |= 1<<(u.binding>>20);
                                u.changed = false;
                        }
 
@@ -258,7 +260,7 @@ uint64_t VulkanPipelineState::compute_descriptor_set_hash(unsigned index) const
 
        uint64_t result = hash<64>(0, 0);
        for(const PipelineState::BoundUniformBlock &b: self.uniform_blocks)
-               if(b.block && b.binding>=0 && static_cast<unsigned>(b.binding>>20)==index)
+               if(b.used && b.binding>=0 && static_cast<unsigned>(b.binding>>20)==index)
                {
                        result = hash_update<64>(result, b.binding);
                        result = hash_update<64>(result, reinterpret_cast<uintptr_t>(b.block));
@@ -278,7 +280,7 @@ unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector<char
 
        unsigned n_buffers = 0;
        for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
-               if(u.block && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
+               if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
                        ++n_buffers;
 
        StructureBuilder sb(buffer, 2);
@@ -289,7 +291,7 @@ unsigned VulkanPipelineState::fill_descriptor_writes(unsigned index, vector<char
        VkDescriptorBufferInfo *buffer_ptr = buffers;
 
        for(const PipelineState::BoundUniformBlock &u: self.uniform_blocks)
-               if(u.block && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
+               if(u.used && u.binding>=0 && static_cast<unsigned>(u.binding>>20)==index)
                {
                        buffer_ptr->buffer = handle_cast<::VkBuffer>(u.block->get_buffer()->handle);
                        buffer_ptr->offset = u.block->get_offset();
@@ -324,7 +326,7 @@ void VulkanPipelineState::apply(VkCommandBuffer command_buffer) const
        if(!self.uniform_blocks.empty())
        {
                const PipelineState::BoundUniformBlock &first_block = self.uniform_blocks.front();
-               if(first_block.block && first_block.binding==ReflectData::PUSH_CONSTANT && first_block.changed)
+               if(first_block.used && first_block.binding==ReflectData::PUSH_CONSTANT && first_block.changed)
                {
                        const UniformBlock &pc_block = *first_block.block;
                        vk.CmdPushConstants(command_buffer, self.shprog->layout_handle, VK_SHADER_STAGE_ALL,
index bc13d3ba562b51535b749320b7341ba64eaf64e9..076eb333d29b127a0d08ef38ec158223fb957179 100644 (file)
@@ -42,6 +42,7 @@ void PipelineState::set_uniform_block(int binding, const UniformBlock *block)
        auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
        if(i==uniform_blocks.end() || i->binding!=binding)
                i = uniform_blocks.insert(i, BoundUniformBlock(binding));
+       i->used = block;
        if(block!=i->block || binding<0)
        {
                i->block = block;
@@ -58,6 +59,7 @@ void PipelineState::set_texture(unsigned binding, const Texture *tex, const Samp
        auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
        if(i==textures.end() || i->binding!=binding)
                i = textures.insert(i, BoundTexture(binding));
+       i->used = (tex && samp);
        if(tex!=i->texture || samp!=i->sampler)
        {
                i->texture = tex;
index 105003d1750de54720d9071360eaf7f1236a9cb7..89cf1cd7261df3c29f20cb769eddc6e6c6c93bb7 100644 (file)
@@ -35,6 +35,7 @@ private:
        {
                unsigned binding = 0;
                mutable bool changed = false;
+               mutable bool used = false;
                const Texture *texture = 0;
                const Sampler *sampler = 0;
 
@@ -45,6 +46,7 @@ private:
        {
                int binding = 0;
                mutable bool changed = false;
+               mutable bool used = false;
                const UniformBlock *block = 0;
 
                BoundUniformBlock(int b): binding(b) { }
index 471803c5b4ab0edfefcfe9253dafd42cca95f0a5..533dca610c9585f649420edadd34bf4f5e858617 100644 (file)
@@ -63,6 +63,13 @@ void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
 
        finalize_uniforms();
 
+       for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
+               if(u.binding>=0)
+                       reflect_data.used_bindings.push_back(u.binding);
+       for(const ReflectData::UniformBlockInfo &b: reflect_data.uniform_blocks)
+               reflect_data.used_bindings.push_back(b.bind_point);
+       sort(reflect_data.used_bindings);
+
        for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
                require_type(u.type);
        for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
@@ -252,6 +259,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);
index b6b639bfcbb49b877bcc46254193daa36ab41400..9d5b8b2b6522f4dcc1d19038295abf3d40583e2a 100644 (file)
@@ -82,6 +82,7 @@ public:
        int get_uniform_location(const std::string &) const;
        int get_uniform_location(Tag) const;
        int get_uniform_binding(Tag) const;
+       bool uses_binding(int) const;
        const std::vector<ReflectData::AttributeInfo> &get_attributes() const { return reflect_data.attributes; }
        const ReflectData::AttributeInfo &get_attribute_info(const std::string &) const;
        int get_attribute_location(const std::string &) const;
index a89e7fcf2972c973c89a803501ebbd6739f7f11c..cc360d04d28e7ec70925c77d45397e40bb16da0b 100644 (file)
@@ -66,6 +66,7 @@ struct ReflectData
        std::vector<AttributeInfo> attributes;
        unsigned n_clip_distances = 0;
        unsigned n_descriptor_sets = 0;
+       std::vector<int> used_bindings;
 
        void update_layout_hash();
 };