]> 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 84bc104a3e372fd1f19832d8a9a52562ec7411c4..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;
@@ -62,6 +69,8 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
        if(!framebuffer)
                throw invalid_operation("VulkanCommands::begin_render_pass");
 
+       viewport = pipeline_state->get_viewport();
+
        const VulkanFunctions &vk = device.get_functions();
 
        if(!current_buffer)
@@ -71,23 +80,25 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
 
        Synchronizer &sync = device.get_synchronizer();
        sync.reset();
-       sync.barrier(current_buffer);
 
-       bool to_present = false;
+       fb_is_swapchain = false;
        unsigned n_attachments = framebuffer->get_format().size();
-       for(unsigned i=0; i<n_attachments; ++i)
+       for(unsigned i=0; (!fb_is_swapchain && i<n_attachments); ++i)
                if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
-                       to_present = true;
-       VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, !clear_values, to_present);
+                       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();
 
+       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>(framebuffer->handle);
 
-       viewport = pipeline_state->get_viewport();
        if(viewport)
        {
                begin_info.renderArea.offset.x = viewport->left;
@@ -155,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;
        }
 }
 
@@ -221,7 +233,7 @@ void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
                 begin_render_pass(false, 0);
 
        pipeline_state->refresh();
-       pipeline_state->apply(current_buffer);
+       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);
 }