]> git.tdb.fi Git - libs/gl.git/commitdiff
Cache framebuffer and viewport in VulkanCommands
authorMikko Rasa <tdb@tdb.fi>
Fri, 19 Nov 2021 22:42:00 +0000 (00:42 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 19 Nov 2021 23:04:35 +0000 (01:04 +0200)
Since PipelineState is not immutable, this is necessary to detect when
they have changed.

source/backends/vulkan/commands_backend.cpp
source/backends/vulkan/commands_backend.h

index 1634863b49e227057ccf3ceb29435600dcb00704..1752e9dad68cf37422ab4dfdc07c0d23f708cfc1 100644 (file)
@@ -58,8 +58,8 @@ void VulkanCommands::begin_buffer()
 
 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
 {
-       const Framebuffer *target = pipeline_state->get_framebuffer();
-       if(!target)
+       framebuffer = pipeline_state->get_framebuffer();
+       if(!framebuffer)
                throw invalid_operation("VulkanCommands::begin_render_pass");
 
        const VulkanFunctions &vk = device.get_functions();
@@ -70,20 +70,20 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
        device.get_transfer_queue().dispatch_transfers(current_buffer);
 
        bool to_present = false;
-       unsigned n_attachments = target->get_format().size();
+       unsigned n_attachments = framebuffer->get_format().size();
        for(unsigned i=0; i<n_attachments; ++i)
-               if(dynamic_cast<const SwapChainTexture *>(target->VulkanFramebuffer::get_attachment(i)))
+               if(dynamic_cast<const SwapChainTexture *>(framebuffer->VulkanFramebuffer::get_attachment(i)))
                        to_present = true;
-       VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(target->get_format(), clear, !clear_values, to_present);
+       VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, !clear_values, to_present);
 
-       target->refresh();
+       framebuffer->refresh();
 
        VkRenderPassBeginInfo begin_info = { };
        begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
        begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
-       begin_info.framebuffer = handle_cast<::VkFramebuffer>(target->handle);
+       begin_info.framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
 
-       const Rect *viewport = pipeline_state->get_viewport();
+       viewport = pipeline_state->get_viewport();
        if(viewport)
        {
                begin_info.renderArea.offset.x = viewport->left;
@@ -93,15 +93,15 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
        }
        else
        {
-               begin_info.renderArea.extent.width = target->get_width();
-               begin_info.renderArea.extent.height = target->get_height();
+               begin_info.renderArea.extent.width = framebuffer->get_width();
+               begin_info.renderArea.extent.height = framebuffer->get_height();
        }
 
        VkClearValue vk_clear_values[7];
        if(clear_values)
        {
                unsigned i = 0;
-               for(FrameAttachment a: target->get_format())
+               for(FrameAttachment a: framebuffer->get_format())
                {
                        if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
                                vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
@@ -117,7 +117,7 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
                        ++i;
                }
 
-               begin_info.clearValueCount = target->get_format().size();
+               begin_info.clearValueCount = framebuffer->get_format().size();
                begin_info.pClearValues = vk_clear_values;
        }
 
@@ -129,7 +129,8 @@ void VulkanCommands::end_render_pass()
        const VulkanFunctions &vk = device.get_functions();
 
        vk.CmdEndRenderPass(current_buffer);
-       render_pass = 0;
+       framebuffer = 0;
+       viewport = 0;
 }
 
 void VulkanCommands::begin_frame(unsigned index)
@@ -163,7 +164,7 @@ void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
        ::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
        VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
 
-       if(render_pass)
+       if(framebuffer)
                end_render_pass();
 
        vk.EndCommandBuffer(current_buffer);
@@ -185,8 +186,8 @@ void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
 
 void VulkanCommands::use_pipeline(const PipelineState *ps)
 {
-       if(!pipeline_state || !ps || ps->get_framebuffer()!=pipeline_state->get_framebuffer() || ps->get_viewport()!=pipeline_state->get_viewport())
-               if(render_pass)
+       if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
+               if(framebuffer)
                        end_render_pass();
 
        pipeline_state = ps;
@@ -196,7 +197,7 @@ void VulkanCommands::use_pipeline(const PipelineState *ps)
 
 void VulkanCommands::clear(const ClearValue *values)
 {
-       if(render_pass)
+       if(framebuffer)
                throw invalid_operation("VulkanCommands::clear");
 
        begin_render_pass(true, values);
@@ -214,7 +215,7 @@ void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
 
        const VulkanFunctions &vk = device.get_functions();
 
-       if(!render_pass)
+       if(!framebuffer)
                 begin_render_pass(false, 0);
 
        pipeline_state->apply(current_buffer);
index 4de712f57092947409fb554826139e9300ee4198..b87d73e0b71b5bffb22ff5c13133cf8e53ac2f21 100644 (file)
@@ -14,6 +14,7 @@ class Device;
 class Framebuffer;
 class PipelineState;
 class QueryPool;
+struct Rect;
 class Semaphore;
 class SwapChain;
 
@@ -37,7 +38,8 @@ protected:
        CommandPool *current_pool = 0;
        VkCommandBuffer current_buffer = 0;
        const PipelineState *pipeline_state = 0;
-       VkRenderPass render_pass = 0;
+       const Framebuffer *framebuffer = 0;
+       const Rect *viewport = 0;
 
        VulkanCommands();
        ~VulkanCommands();