From 204322e1b8f3a28c3dcf8a129e5ab8be85ccd121 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sun, 16 Jun 2019 00:08:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9Fence=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/VulkanAppFramework.h | 2 +- inc/hgl/graph/vulkan/VKDevice.h | 28 +++++++------------------ src/RenderDevice/Vulkan/VKDevice.cpp | 31 +++++++++++++++------------- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/example/Vulkan/VulkanAppFramework.h b/example/Vulkan/VulkanAppFramework.h index 87d0451e..701c5de9 100644 --- a/example/Vulkan/VulkanAppFramework.h +++ b/example/Vulkan/VulkanAppFramework.h @@ -192,8 +192,8 @@ public: virtual void Draw() { - device->AcquireNextImage(); device->Wait(); + device->AcquireNextImage(); uint32_t index=device->GetCurrentFrameIndices(); diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index ffe8efa7..af3a893a 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -17,29 +17,14 @@ class Device { DeviceAttribute *attr; + uint swap_chain_count; + Semaphore *present_complete_semaphore=nullptr, *render_complete_semaphore=nullptr; VkPipelineStageFlags pipe_stage_flags; VkSubmitInfo submit_info; - struct RenderFrame - { - Framebuffer *frame_buffer=nullptr; - - Fence *draw_fence=nullptr; - - public: - - ~RenderFrame() - { - SAFE_CLEAR(frame_buffer); - SAFE_CLEAR(draw_fence); - } - };//struct RenderFrame - - void Create(RenderFrame *); - Fence *texture_fence; VkSubmitInfo texture_submit_info; @@ -48,7 +33,10 @@ class Device RenderPass *main_rp; uint32_t current_frame; - ObjectList render_frame; + ObjectList render_frame; + + uint32_t current_fence; + ObjectList fence_list; VkPresentInfoKHR present_info; @@ -83,9 +71,9 @@ public: ImageView *GetDepthImageView () {return attr->depth.view;} RenderPass * GetRenderPass () {return main_rp;} - Framebuffer * GetFramebuffer (int index) {return render_frame[index]->frame_buffer;} + Framebuffer * GetFramebuffer (int index) {return render_frame[index];} const uint32_t GetCurrentFrameIndices () {return current_frame;} - Framebuffer * GetCurrentFramebuffer () {return render_frame[current_frame]->frame_buffer;} + Framebuffer * GetCurrentFramebuffer () {return render_frame[current_frame];} bool Resize (uint,uint); diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index aca436d6..47a55d70 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -50,6 +50,7 @@ Device::Device(DeviceAttribute *da) Device::~Device() { + fence_list.Clear(); render_frame.Clear(); delete present_complete_semaphore; @@ -65,7 +66,9 @@ Device::~Device() void Device::RecreateDevice() { + fence_list.Clear(); render_frame.Clear(); + if(main_rp)delete main_rp; if(texture_cmd_buf)delete texture_cmd_buf; @@ -73,19 +76,17 @@ void Device::RecreateDevice() main_rp=CreateRenderPass(attr->sc_image_views[0]->GetFormat(),attr->depth.view->GetFormat()); - const int sc_count=attr->sc_image_views.GetCount(); + swap_chain_count=attr->sc_image_views.GetCount(); - for(int i=0;iframe_buffer =vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],attr->depth.view); - rf->draw_fence =this->CreateFence(); - - render_frame.Add(rf); + render_frame.Add(vulkan::CreateFramebuffer(this,main_rp,attr->sc_image_views[i],attr->depth.view)); + fence_list.Add(this->CreateFence()); } texture_cmd_buf=CreateCommandBuffer(); + + current_fence=0; } bool Device::Resize(uint width,uint height) @@ -214,7 +215,7 @@ Fence *Device::CreateFence() VkFenceCreateInfo fenceInfo; fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fenceInfo.pNext = nullptr; - fenceInfo.flags = 0; + fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; VkFence fence; @@ -256,20 +257,22 @@ bool Device::SubmitDraw(const VkCommandBuffer *cmd_bufs,const uint32_t count) submit_info.commandBufferCount = count; submit_info.pCommandBuffers = cmd_bufs; - VkFence fence=*(render_frame[current_frame]->draw_fence); + VkFence fence=*fence_list[current_fence]; VkResult result=vkQueueSubmit(attr->graphics_queue,1,&submit_info,fence); + + if(++current_fence==swap_chain_count) + current_fence=0; return(result==VK_SUCCESS); } bool Device::Wait(bool wait_all,uint64_t time_out) { - VkFence fence=*(render_frame[current_frame]->draw_fence); + VkFence fence=*fence_list[current_fence]; - VkResult result=vkWaitForFences(attr->device,1,&fence,wait_all,time_out); - - result=vkResetFences(attr->device,1,&fence); + vkWaitForFences(attr->device,1,&fence,wait_all,time_out); + vkResetFences(attr->device,1,&fence); return(true); }