diff --git a/example/Vulkan/VulkanAppFramework.h b/example/Vulkan/VulkanAppFramework.h index e96bd676..34062d59 100644 --- a/example/Vulkan/VulkanAppFramework.h +++ b/example/Vulkan/VulkanAppFramework.h @@ -223,14 +223,7 @@ public: int AcquireNextImage() { - if(sc_render_target->Wait()) - { - int cur=sc_render_target->AcquireNextImage(present_complete_semaphore); - - return cur; - } - - return -1; + return sc_render_target->AcquireNextImage(present_complete_semaphore); } void SubmitDraw(int index) @@ -239,6 +232,7 @@ public: sc_render_target->Submit(cb,present_complete_semaphore,render_complete_semaphore); sc_render_target->PresentBackbuffer(render_complete_semaphore); + sc_render_target->Wait(); } virtual void Draw() diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 3a527ff2..253338fd 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -16,9 +16,7 @@ class Device { DeviceAttribute *attr; - Fence *texture_fence; - - VkSubmitInfo texture_submit_info; + SubmitQueue *textureSQ; CommandBuffer *texture_cmd_buf; Swapchain *swapchain; diff --git a/inc/hgl/graph/vulkan/VKRenderTarget.h b/inc/hgl/graph/vulkan/VKRenderTarget.h index 356390bd..ab6748bf 100644 --- a/inc/hgl/graph/vulkan/VKRenderTarget.h +++ b/inc/hgl/graph/vulkan/VKRenderTarget.h @@ -25,7 +25,8 @@ public: virtual ~SubmitQueue(); bool Wait(const bool wait_wall=true,const uint64 time_out=HGL_NANO_SEC_PER_SEC); - bool Submit(VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem); + bool Submit(const VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem); + bool Submit(const VkCommandBuffer *cmd_buf,const uint32_t count,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem); };//class SumbitQueue /** diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index 927bb8c7..88a462fe 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -17,11 +17,7 @@ Device::Device(DeviceAttribute *da) { attr=da; - texture_fence=this->CreateFence(false); - - hgl_zero(texture_submit_info); - texture_submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - + textureSQ=nullptr; texture_cmd_buf=nullptr; swapchain=nullptr; @@ -34,8 +30,8 @@ Device::~Device() SAFE_CLEAR(swapchainRT); SAFE_CLEAR(swapchain); - delete texture_cmd_buf; - delete texture_fence; + SAFE_CLEAR(textureSQ); + SAFE_CLEAR(texture_cmd_buf); delete attr; } @@ -45,11 +41,13 @@ bool Device::Resize(const VkExtent2D &extent) SAFE_CLEAR(swapchainRT); SAFE_CLEAR(swapchain); - swapchain=CreateSwapchain(attr,extent); - - if(texture_cmd_buf)delete texture_cmd_buf; + SAFE_CLEAR(textureSQ); + SAFE_CLEAR(texture_cmd_buf); - texture_cmd_buf=CreateCommandBuffer(swapchain->GetExtent(),0); + swapchain=CreateSwapchain(attr,extent); + + texture_cmd_buf=CreateCommandBuffer(extent,0); + textureSQ=new SubmitQueue(this,attr->graphics_queue,1); swapchainRT=new SwapchainRenderTarget(this,swapchain); diff --git a/src/RenderDevice/Vulkan/VKDeviceTexture.cpp b/src/RenderDevice/Vulkan/VKDeviceTexture.cpp index ca223140..4112e77a 100644 --- a/src/RenderDevice/Vulkan/VKDeviceTexture.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceTexture.cpp @@ -156,14 +156,8 @@ bool Device::SubmitTexture(const VkCommandBuffer *cmd_bufs,const uint32_t count) if(!cmd_bufs||count<=0) return(false); - texture_submit_info.commandBufferCount = count; - texture_submit_info.pCommandBuffers = cmd_bufs; - - VkFence fence=*texture_fence; - - if(vkQueueSubmit(attr->graphics_queue, 1, &texture_submit_info, fence))return(false); - if(vkWaitForFences(attr->device, 1, &fence, VK_TRUE, HGL_NANO_SEC_PER_SEC*0.1)!=VK_SUCCESS)return(false); - vkResetFences(attr->device,1,&fence); + textureSQ->Submit(cmd_bufs,count,nullptr,nullptr); + textureSQ->Wait(); return(true); } diff --git a/src/RenderDevice/Vulkan/VKRenderTarget.cpp b/src/RenderDevice/Vulkan/VKRenderTarget.cpp index fea3a548..1cafd3e3 100644 --- a/src/RenderDevice/Vulkan/VKRenderTarget.cpp +++ b/src/RenderDevice/Vulkan/VKRenderTarget.cpp @@ -15,17 +15,13 @@ SubmitQueue::SubmitQueue(Device *dev,VkQueue q,const uint32_t fence_count) queue=q; for(uint32_t i=0;iCreateFence(true)); + fence_list.Add(device->CreateFence(false)); current_fence=0; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submit_info.pNext = nullptr; - //submit_info.waitSemaphoreCount = 1; - //submit_info.pWaitSemaphores = *present_complete_semaphore; submit_info.pWaitDstStageMask = &pipe_stage_flags; - //submit_info.signalSemaphoreCount = 1; - //submit_info.pSignalSemaphores = *render_complete_semaphore; } SubmitQueue::~SubmitQueue() @@ -42,32 +38,59 @@ bool SubmitQueue::Wait(const bool wait_all,uint64_t time_out) result=vkWaitForFences(device->GetDevice(),1,&fence,wait_all,time_out); result=vkResetFences(device->GetDevice(),1,&fence); + if(++current_fence==fence_list.GetCount()) + current_fence=0; + return(true); } -bool SubmitQueue::Submit(VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem) +bool SubmitQueue::Submit(const VkCommandBuffer *cmd_buf,const uint32_t cb_count,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem) { - VkSemaphore ws=*wait_sem; - VkSemaphore cs=*complete_sem; + VkSemaphore ws; + VkSemaphore cs; - submit_info.waitSemaphoreCount =1; - submit_info.pWaitSemaphores =&ws; - submit_info.commandBufferCount =1; - submit_info.pCommandBuffers =&cmd_buf; - submit_info.signalSemaphoreCount=1; - submit_info.pSignalSemaphores =&cs; + if(wait_sem) + { + ws=*wait_sem; + + submit_info.waitSemaphoreCount =1; + submit_info.pWaitSemaphores =&ws; + } + else + { + submit_info.waitSemaphoreCount =0; + submit_info.pWaitSemaphores =nullptr; + } + + if(complete_sem) + { + cs=*complete_sem; + + submit_info.signalSemaphoreCount=1; + submit_info.pSignalSemaphores =&cs; + } + else + { + submit_info.signalSemaphoreCount=0; + submit_info.pSignalSemaphores =nullptr; + } + + submit_info.commandBufferCount =cb_count; + submit_info.pCommandBuffers =cmd_buf; VkFence fence=*fence_list[current_fence]; VkResult result=vkQueueSubmit(queue,1,&submit_info,fence); - if(++current_fence==fence_list.GetCount()) - current_fence=0; - //不在这里立即等待fence完成,是因为有可能queue submit需要久一点工作时间,我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence,而且是另一帧的fence。这样有利于异步处理 return(result==VK_SUCCESS); } + +bool SubmitQueue::Submit(const VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem) +{ + return Submit(&cmd_buf,1,wait_sem,complete_sem); +} RenderTarget::RenderTarget(Device *dev,Framebuffer *_fb,const uint32_t fence_count):SubmitQueue(dev,dev->GetGraphicsQueue(),fence_count) {