]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/commands_backend.cpp
Rework multisample resolve to use resolve attachments
[libs/gl.git] / source / backends / vulkan / commands_backend.cpp
index 8f32e0b024bd9c0264e8376331eceffca1105eff..2273d91920e555741f86991c1694e761bc83a877 100644 (file)
@@ -8,8 +8,8 @@
 #include "frameformat.h"
 #include "pipelinestate.h"
 #include "rect.h"
+#include "renderpass.h"
 #include "semaphore.h"
-#include "structurebuilder.h"
 #include "swapchaintexture.h"
 #include "vulkan.h"
 
@@ -36,20 +36,20 @@ void VulkanCommands::begin_buffer(VkRenderPass render_pass)
 
        const VulkanFunctions &vk = device.get_functions();
 
-       CommandPool *current_pool = &command_pools[frame_index];
-       if(!current_pool->in_use)
+       CommandPool &current_pool = command_pools[frame_index];
+       if(!current_pool.in_use)
        {
-               current_pool->fence.reset();
-               current_pool->in_use = true;
+               current_pool.fence.reset();
+               current_pool.in_use = true;
        }
 
-       CommandBuffers &buffers = (render_pass ? current_pool->secondary : current_pool->primary);
+       CommandBuffers &buffers = (render_pass ? current_pool.secondary : current_pool.primary);
 
        if(buffers.next_buffer>=buffers.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.commandPool = handle_cast<::VkCommandPool>(current_pool.pool);
                alloc_info.level = (render_pass ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY);
                alloc_info.commandBufferCount = 1;
 
@@ -98,72 +98,41 @@ void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_value
                if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
                        fb_is_swapchain = true;
 
-       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);
-
        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);
-       VkRenderPassBeginInfo *&begin_info = sb.add<VkRenderPassBeginInfo>(1);
-       VkClearValue *&vk_clear_values = sb.add<VkClearValue>(FrameFormat::MAX_ATTACHMENTS);
-
-       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);
+       RenderPass render_pass;
+       render_pass.framebuffer = framebuffer;
+       render_pass.render_area = viewport;
+       render_pass.clear = clear;
+       render_pass.clear_values = clear_values;
+       render_pass.to_present = fb_is_swapchain;
+       render_pass.update(device);
 
-       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;
+       discard_fb_contents = render_pass.discard_fb_contents;
 
-       if(clear_values)
-       {
-               unsigned i = 0;
-               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;
-                       else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
-                               vk_clear_values[i].depthStencil.stencil = clear_values[i].depth_stencil.stencil;
-                       else
-                       {
-                               vk_clear_values[i].color.float32[0] = clear_values[i].color.r;
-                               vk_clear_values[i].color.float32[1] = clear_values[i].color.g;
-                               vk_clear_values[i].color.float32[2] = clear_values[i].color.b;
-                               vk_clear_values[i].color.float32[3] = clear_values[i].color.a;
-                       }
-                       ++i;
-               }
-
-               begin_info->clearValueCount = framebuffer->get_format().size();
-               begin_info->pClearValues = vk_clear_values;
-       }
+       begin_buffer(render_pass.handle);
+       render_pass.fill_begin_info(pass_begin_info);
 }
 
 void VulkanCommands::end_render_pass()
 {
        const VulkanFunctions &vk = device.get_functions();
+       VulkanCommandRecorder vkCmd(vk, primary_buffer);
 
        vk.EndCommandBuffer(pass_buffer);
 
-       device.get_transfer_queue().dispatch_transfers(primary_buffer);
+       device.get_transfer_queue().dispatch_transfers(vkCmd);
 
        Synchronizer &sync = device.get_synchronizer();
        sync.reset();
        if(!fb_is_swapchain)
                framebuffer->prepare_image_layouts(discard_fb_contents);
-       sync.barrier(primary_buffer);
+       sync.barrier(vkCmd);
 
        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);
+       vkCmd.BeginRenderPass(begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS);
+       vkCmd.ExecuteCommands(1, &pass_buffer);
+       vkCmd.EndRenderPass();
 
        framebuffer = 0;
        viewport = Rect::max();
@@ -182,15 +151,17 @@ void VulkanCommands::begin_frame(unsigned index)
                        command_pools.emplace_back(device);
        }
 
-       CommandPool *current_pool = &command_pools[frame_index];
-       if(current_pool->in_use)
+       CommandPool &current_pool = command_pools[frame_index];
+       if(current_pool.in_use)
        {
-               current_pool->fence.wait();
-               vk.ResetCommandPool(current_pool->pool, 0);
-               current_pool->in_use = false;
-               current_pool->primary.next_buffer = 0;
-               current_pool->secondary.next_buffer = 0;
+               current_pool.fence.wait();
+               vk.ResetCommandPool(current_pool.pool, 0);
+               current_pool.in_use = false;
+               current_pool.primary.next_buffer = 0;
+               current_pool.secondary.next_buffer = 0;
        }
+
+       device.get_descriptor_pool().begin_frame();
 }
 
 void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem)
@@ -250,19 +221,36 @@ void VulkanCommands::draw_instanced(const Batch &batch, unsigned count)
        if(!pipeline_state)
                throw invalid_operation("VulkanCommands::draw_instanced");
 
-       const VulkanFunctions &vk = device.get_functions();
-
        if(!framebuffer)
                 begin_render_pass(false, 0);
 
+       VulkanCommandRecorder vkCmd(device.get_functions(), pass_buffer);
+
        pipeline_state->refresh();
-       pipeline_state->apply(pass_buffer, last_pipeline, frame_index, fb_is_swapchain);
+       pipeline_state->apply(vkCmd, 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);
+       vkCmd.DrawIndexed(batch.size(), count, first_index, 0, 0);
+}
+
+void VulkanCommands::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
+{
+       if(!pipeline_state)
+               throw invalid_operation("VulkanCommands::draw_instanced");
+
+       if(framebuffer)
+               end_render_pass();
+
+       VulkanCommandRecorder vkCmd(device.get_functions(), primary_buffer);
+
+       pipeline_state->refresh();
+       pipeline_state->synchronize_resources();
+       device.get_synchronizer().barrier(vkCmd);
+       pipeline_state->apply(vkCmd, 0, frame_index, false);
+       vkCmd.Dispatch(count_x, count_y, count_z);
 }
 
-void VulkanCommands::resolve_multisample(Framebuffer &)
+void VulkanCommands::resolve_multisample()
 {
        throw logic_error("VulkanCommands::resolve_multisample is unimplemented");
 }