]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/commands_backend.cpp
Refactor handling of viewport Y axis
[libs/gl.git] / source / backends / vulkan / commands_backend.cpp
index 1634863b49e227057ccf3ceb29435600dcb00704..9c074c87da184e279c1a0d9b0120b74e0ed12324 100644 (file)
@@ -41,13 +41,20 @@ void VulkanCommands::begin_buffer()
                current_pool->in_use = true;
        }
 
-       VkCommandBufferAllocateInfo alloc_info = { };
-       alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
-       alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
-       alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
-       alloc_info.commandBufferCount = 1;
+       if(current_pool->next_buffer>=current_pool->buffers.size())
+       {
+               VkCommandBufferAllocateInfo alloc_info = { };
+               alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+               alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
+               alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
+               alloc_info.commandBufferCount = 1;
+
+               VkCommandBuffer buffer;
+               vk.AllocateCommandBuffers(alloc_info, &buffer);
+               current_pool->buffers.push_back(buffer);
+       }
 
-       vk.AllocateCommandBuffers(alloc_info, &current_buffer);
+       current_buffer = current_pool->buffers[current_pool->next_buffer++];
 
        VkCommandBufferBeginInfo begin_info = { };
        begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@@ -58,10 +65,12 @@ 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");
 
+       viewport = pipeline_state->get_viewport();
+
        const VulkanFunctions &vk = device.get_functions();
 
        if(!current_buffer)
@@ -69,21 +78,27 @@ 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();
-       for(unsigned i=0; i<n_attachments; ++i)
-               if(dynamic_cast<const SwapChainTexture *>(target->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);
+       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();
 
-       target->refresh();
+       sync.barrier(current_buffer);
 
        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();
        if(viewport)
        {
                begin_info.renderArea.offset.x = viewport->left;
@@ -93,15 +108,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 +132,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 +144,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)
@@ -150,6 +166,7 @@ void VulkanCommands::begin_frame(unsigned index)
                current_pool->fence.wait();
                vk.ResetCommandPool(current_pool->pool, 0);
                current_pool->in_use = false;
+               current_pool->next_buffer = 0;
        }
 }
 
@@ -163,7 +180,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,18 +202,16 @@ 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;
-       if(pipeline_state)
-               pipeline_state->refresh();
 }
 
 void VulkanCommands::clear(const ClearValue *values)
 {
-       if(render_pass)
+       if(framebuffer)
                throw invalid_operation("VulkanCommands::clear");
 
        begin_render_pass(true, values);
@@ -214,10 +229,11 @@ 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);
+       pipeline_state->refresh();
+       pipeline_state->apply(current_buffer, fb_is_swapchain);
        unsigned first_index = batch.get_offset()/batch.get_index_size();
        vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);
 }