X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbackends%2Fvulkan%2Fcommands_backend.cpp;h=2273d91920e555741f86991c1694e761bc83a877;hb=94cadd1618f93239b1cb0acbd4f958257c035c98;hp=1634863b49e227057ccf3ceb29435600dcb00704;hpb=6630b1106493e5a072a9a9f212f0d00648dbedd4;p=libs%2Fgl.git diff --git a/source/backends/vulkan/commands_backend.cpp b/source/backends/vulkan/commands_backend.cpp index 1634863b..2273d919 100644 --- a/source/backends/vulkan/commands_backend.cpp +++ b/source/backends/vulkan/commands_backend.cpp @@ -8,6 +8,7 @@ #include "frameformat.h" #include "pipelinestate.h" #include "rect.h" +#include "renderpass.h" #include "semaphore.h" #include "swapchaintexture.h" #include "vulkan.h" @@ -28,134 +29,144 @@ VulkanCommands::~VulkanCommands() vk.QueueWaitIdle(); } -void VulkanCommands::begin_buffer() +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(); - if(!current_pool->in_use) + CommandPool ¤t_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; } - 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; + CommandBuffers &buffers = (render_pass ? current_pool.secondary : current_pool.primary); - vk.AllocateCommandBuffers(alloc_info, ¤t_buffer); + 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.level = (render_pass ? VK_COMMAND_BUFFER_LEVEL_SECONDARY : VK_COMMAND_BUFFER_LEVEL_PRIMARY); + alloc_info.commandBufferCount = 1; + + VkCommandBuffer buffer; + vk.AllocateCommandBuffers(alloc_info, &buffer); + buffers.buffers.push_back(buffer); + } + + VkCommandBuffer buffer = buffers.buffers[buffers.next_buffer++]; + (render_pass ? pass_buffer : primary_buffer) = buffer; VkCommandBufferBeginInfo begin_info = { }; begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + if(render_pass) + begin_info.flags |= VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT; + + VkCommandBufferInheritanceInfo inherit_info = { }; + if(render_pass) + { + inherit_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; + inherit_info.renderPass = handle_cast<::VkRenderPass>(render_pass); + inherit_info.subpass = 0; - vk.BeginCommandBuffer(current_buffer, begin_info); + begin_info.pInheritanceInfo = &inherit_info; + } + + vk.BeginCommandBuffer(buffer, begin_info); + last_pipeline = 0; } 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(); + viewport = pipeline_state->get_viewport(); - if(!current_buffer) - begin_buffer(); + if(!primary_buffer) + begin_buffer(0); - device.get_transfer_queue().dispatch_transfers(current_buffer); + fb_is_swapchain = false; + unsigned n_attachments = framebuffer->get_format().size(); + for(unsigned i=0; (!fb_is_swapchain && i(framebuffer->get_attachment(i))) + fb_is_swapchain = true; - bool to_present = false; - unsigned n_attachments = target->get_format().size(); - for(unsigned i=0; i(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); + framebuffer->refresh(); - target->refresh(); + 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); - 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); + discard_fb_contents = render_pass.discard_fb_contents; - const Rect *viewport = pipeline_state->get_viewport(); - 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 = target->get_width(); - begin_info.renderArea.extent.height = target->get_height(); - } - - VkClearValue vk_clear_values[7]; - if(clear_values) - { - unsigned i = 0; - for(FrameAttachment a: target->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 = target->get_format().size(); - begin_info.pClearValues = vk_clear_values; - } - - vk.CmdBeginRenderPass(current_buffer, begin_info, VK_SUBPASS_CONTENTS_INLINE); + 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); - vk.CmdEndRenderPass(current_buffer); - render_pass = 0; + 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(vkCmd); + + const VkRenderPassBeginInfo &begin_info = *reinterpret_cast(pass_begin_info.data()); + vkCmd.BeginRenderPass(begin_info, VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS); + vkCmd.ExecuteCommands(1, &pass_buffer); + vkCmd.EndRenderPass(); + + framebuffer = 0; + viewport = Rect::max(); + pass_buffer = 0; } 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(); iin_use) + CommandPool ¤t_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.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) { - if(!current_buffer) + if(!primary_buffer) return; const VulkanFunctions &vk = device.get_functions(); @@ -163,10 +174,10 @@ 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); + vk.EndCommandBuffer(primary_buffer); VkSubmitInfo submit_info = { }; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; @@ -174,29 +185,27 @@ void VulkanCommands::submit_frame(Semaphore *wait_sem, Semaphore *signal_sem) submit_info.pWaitSemaphores = &vk_wait_sem; submit_info.pWaitDstStageMask = &wait_stages; submit_info.commandBufferCount = 1; - submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(¤t_buffer); + submit_info.pCommandBuffers = handle_cast<::VkCommandBuffer *>(&primary_buffer); 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); - current_buffer = 0; + primary_buffer = 0; } 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); @@ -212,17 +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(!render_pass) + if(!framebuffer) begin_render_pass(false, 0); - pipeline_state->apply(current_buffer); + VulkanCommandRecorder vkCmd(device.get_functions(), pass_buffer); + + pipeline_state->refresh(); + 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(current_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"); }