]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/commands_backend.cpp
Apply only changed parts of VulkanPipelineState
[libs/gl.git] / source / backends / vulkan / commands_backend.cpp
index 137a817deac20764fa860e2c338eb172b37e1ffb..8f32e0b024bd9c0264e8376331eceffca1105eff 100644 (file)
@@ -31,11 +31,12 @@ VulkanCommands::~VulkanCommands()
 
 void VulkanCommands::begin_buffer(VkRenderPass render_pass)
 {
-       if(!current_pool)
+       if(frame_index>=command_pools.size())
                throw invalid_operation("VulkanCommands::begin_buffer");
 
        const VulkanFunctions &vk = device.get_functions();
 
+       CommandPool *current_pool = &command_pools[frame_index];
        if(!current_pool->in_use)
        {
                current_pool->fence.reset();
@@ -77,6 +78,7 @@ void VulkanCommands::begin_buffer(VkRenderPass render_pass)
        }
 
        vk.BeginCommandBuffer(buffer, begin_info);
+       last_pipeline = 0;
 }
 
 void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
@@ -90,22 +92,21 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
        if(!primary_buffer)
                begin_buffer(0);
 
-       Synchronizer &sync = device.get_synchronizer();
-       sync.reset();
-
        fb_is_swapchain = false;
        unsigned n_attachments = framebuffer->get_format().size();
        for(unsigned i=0; (!fb_is_swapchain && i<n_attachments); ++i)
                if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
                        fb_is_swapchain = true;
-       if(!fb_is_swapchain)
-               framebuffer->prepare_image_layouts(clear && !viewport);
-       VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, (!clear_values && !viewport), fb_is_swapchain);
 
-       framebuffer->refresh();
+       Rect fb_rect = framebuffer->get_rect();
+       Rect render_area = fb_rect.intersect(viewport);
+       bool full_viewport = render_area==fb_rect;
+       discard_fb_contents = (clear && full_viewport);
 
-       sync.barrier(primary_buffer);
+       framebuffer->refresh();
 
+       VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(),
+               clear, (!clear_values && full_viewport), fb_is_swapchain);
        begin_buffer(render_pass);
 
        StructureBuilder sb(pass_begin_info, 2);
@@ -116,18 +117,10 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
        begin_info->renderPass = handle_cast<::VkRenderPass>(render_pass);
        begin_info->framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
 
-       if(viewport)
-       {
-               begin_info->renderArea.offset.x = viewport->left;
-               begin_info->renderArea.offset.y = viewport->bottom;
-               begin_info->renderArea.extent.width = viewport->width;
-               begin_info->renderArea.extent.height = viewport->height;
-       }
-       else
-       {
-               begin_info->renderArea.extent.width = framebuffer->get_width();
-               begin_info->renderArea.extent.height = framebuffer->get_height();
-       }
+       begin_info->renderArea.offset.x = render_area.left;
+       begin_info->renderArea.offset.y = render_area.bottom;
+       begin_info->renderArea.extent.width = render_area.width;
+       begin_info->renderArea.extent.height = render_area.height;
 
        if(clear_values)
        {
@@ -159,13 +152,21 @@ void VulkanCommands::end_render_pass()
 
        vk.EndCommandBuffer(pass_buffer);
 
+       device.get_transfer_queue().dispatch_transfers(primary_buffer);
+
+       Synchronizer &sync = device.get_synchronizer();
+       sync.reset();
+       if(!fb_is_swapchain)
+               framebuffer->prepare_image_layouts(discard_fb_contents);
+       sync.barrier(primary_buffer);
+
        const VkRenderPassBeginInfo &begin_info = *reinterpret_cast<const VkRenderPassBeginInfo *>(pass_begin_info.data());
        vk.CmdBeginRenderPass(primary_buffer, begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
        vk.CmdExecuteCommands(primary_buffer, 1, &pass_buffer);
        vk.CmdEndRenderPass(primary_buffer);
 
        framebuffer = 0;
-       viewport = 0;
+       viewport = Rect::max();
        pass_buffer = 0;
 }
 
@@ -173,15 +174,15 @@ void VulkanCommands::begin_frame(unsigned index)
 {
        const VulkanFunctions &vk = device.get_functions();
 
-       unsigned pool_index = index%device.get_n_frames_in_flight();
-       if(pool_index>=command_pools.size())
+       frame_index = index%device.get_n_frames_in_flight();
+       if(frame_index>=command_pools.size())
        {
-               command_pools.reserve(pool_index+1);
-               for(unsigned i=command_pools.size(); i<pool_index+1; ++i)
+               command_pools.reserve(frame_index+1);
+               for(unsigned i=command_pools.size(); i<frame_index+1; ++i)
                        command_pools.emplace_back(device);
        }
 
-       current_pool = &command_pools[pool_index];
+       CommandPool *current_pool = &command_pools[frame_index];
        if(current_pool->in_use)
        {
                current_pool->fence.wait();
@@ -217,7 +218,7 @@ void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
        submit_info.signalSemaphoreCount = (signal_sem ? 1 : 0);
        submit_info.pSignalSemaphores = &vk_signal_sem;
 
-       vk.QueueSubmit(1, &submit_info, current_pool->fence.handle);
+       vk.QueueSubmit(1, &submit_info, command_pools[frame_index].fence.handle);
 
        primary_buffer = 0;
 }
@@ -255,7 +256,8 @@ void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
                 begin_render_pass(false, 0);
 
        pipeline_state->refresh();
-       pipeline_state->apply(pass_buffer, fb_is_swapchain);
+       pipeline_state->apply(pass_buffer, last_pipeline, frame_index, fb_is_swapchain);
+       last_pipeline = pipeline_state;
        unsigned first_index = batch.get_offset()/batch.get_index_size();
        vk.CmdDrawIndexed(pass_buffer, batch.size(), count, first_index, 0, 0);
 }