2019-07-13 02:38:42 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKSwapchain.h>
|
2019-07-13 02:37:19 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKRenderPass.h>
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-07-15 22:37:00 +08:00
|
|
|
|
Swapchain::Swapchain(Device *dev,SwapchainAttribute *sa)
|
2019-07-13 02:37:19 +08:00
|
|
|
|
{
|
|
|
|
|
device=dev;
|
2019-07-15 22:37:00 +08:00
|
|
|
|
sc_attr=sa;
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
|
|
|
|
pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
|
|
|
|
|
|
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
|
submit_info.pNext = nullptr;
|
2019-07-16 10:26:24 +08:00
|
|
|
|
//submit_info.waitSemaphoreCount = 1;
|
|
|
|
|
//submit_info.pWaitSemaphores = *present_complete_semaphore;
|
2019-07-13 02:37:19 +08:00
|
|
|
|
submit_info.pWaitDstStageMask = &pipe_stage_flags;
|
2019-07-16 10:26:24 +08:00
|
|
|
|
//submit_info.signalSemaphoreCount = 1;
|
|
|
|
|
//submit_info.pSignalSemaphores = *render_complete_semaphore;
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
|
|
|
|
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
|
|
|
present_info.pNext = nullptr;
|
|
|
|
|
present_info.waitSemaphoreCount = 1;
|
2019-07-16 10:26:24 +08:00
|
|
|
|
//present_info.pWaitSemaphores = *render_complete_semaphore;
|
2019-07-13 02:37:19 +08:00
|
|
|
|
present_info.swapchainCount = 1;
|
|
|
|
|
present_info.pResults = nullptr;
|
2019-07-16 10:26:24 +08:00
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
present_info.pSwapchains=&(sc_attr->swap_chain);
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
main_rp=device->CreateRenderPass(sc_attr->sc_color[0]->GetFormat(),sc_attr->sc_depth->GetFormat());
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
for(uint i=0;i<sc_attr->swap_chain_count;i++)
|
2019-07-13 02:37:19 +08:00
|
|
|
|
{
|
2019-07-15 22:37:00 +08:00
|
|
|
|
render_frame.Add(vulkan::CreateFramebuffer(device,main_rp,sc_attr->sc_color[i]->GetImageView(),sc_attr->sc_depth->GetImageView()));
|
2019-07-13 02:37:19 +08:00
|
|
|
|
fence_list.Add(device->CreateFence(true));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_frame=0;
|
|
|
|
|
current_fence=0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 10:26:24 +08:00
|
|
|
|
Swapchain::~Swapchain()
|
|
|
|
|
{
|
|
|
|
|
fence_list.Clear();
|
|
|
|
|
render_frame.Clear();
|
|
|
|
|
|
|
|
|
|
delete main_rp;
|
|
|
|
|
|
|
|
|
|
SAFE_CLEAR(sc_attr);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-13 02:37:19 +08:00
|
|
|
|
bool Swapchain::Wait(bool wait_all,uint64_t time_out)
|
|
|
|
|
{
|
|
|
|
|
VkFence fence=*fence_list[current_fence];
|
|
|
|
|
|
|
|
|
|
VkResult result;
|
|
|
|
|
|
|
|
|
|
result=vkWaitForFences(device->GetDevice(),1,&fence,wait_all,time_out);
|
|
|
|
|
result=vkResetFences(device->GetDevice(),1,&fence);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 10:26:24 +08:00
|
|
|
|
int Swapchain::AcquireNextImage(vulkan::Semaphore *present_complete_semaphore)
|
2019-07-13 02:37:19 +08:00
|
|
|
|
{
|
2019-07-16 10:26:24 +08:00
|
|
|
|
VkSemaphore sem=*present_complete_semaphore;
|
|
|
|
|
|
|
|
|
|
if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,sem,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS)
|
2019-07-13 18:23:43 +08:00
|
|
|
|
return current_frame;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 10:26:24 +08:00
|
|
|
|
bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem)
|
2019-07-13 18:23:43 +08:00
|
|
|
|
{
|
2019-07-16 10:26:24 +08:00
|
|
|
|
VkSemaphore ws=*wait_sem;
|
|
|
|
|
VkSemaphore cs=*complete_sem;
|
|
|
|
|
|
2019-07-13 18:23:43 +08:00
|
|
|
|
submit_info.waitSemaphoreCount =1;
|
2019-07-16 10:26:24 +08:00
|
|
|
|
submit_info.pWaitSemaphores =&ws;
|
2019-07-13 18:23:43 +08:00
|
|
|
|
submit_info.commandBufferCount =1;
|
|
|
|
|
submit_info.pCommandBuffers =&cmd_list;
|
|
|
|
|
submit_info.signalSemaphoreCount=1;
|
2019-07-16 10:26:24 +08:00
|
|
|
|
submit_info.pSignalSemaphores =&cs;
|
2019-07-13 18:23:43 +08:00
|
|
|
|
|
|
|
|
|
VkFence fence=*fence_list[current_fence];
|
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
|
2019-07-13 18:23:43 +08:00
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
if(++current_fence==sc_attr->swap_chain_count)
|
2019-07-13 18:23:43 +08:00
|
|
|
|
current_fence=0;
|
|
|
|
|
|
|
|
|
|
//不在这里立即等待fence完成,是因为有可能queue submit需要久一点工作时间,我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence,而且是另一帧的fence。这样有利于异步处理
|
|
|
|
|
|
|
|
|
|
return(result==VK_SUCCESS);
|
2019-07-13 02:37:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Swapchain::SubmitDraw(List<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_sems)
|
|
|
|
|
{
|
|
|
|
|
if(cmd_lists.GetCount()<=0)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
submit_info.waitSemaphoreCount =wait_sems.GetCount();
|
|
|
|
|
submit_info.pWaitSemaphores =wait_sems.GetData();
|
|
|
|
|
submit_info.commandBufferCount =cmd_lists.GetCount();
|
|
|
|
|
submit_info.pCommandBuffers =cmd_lists.GetData();
|
|
|
|
|
submit_info.signalSemaphoreCount=complete_sems.GetCount();
|
|
|
|
|
submit_info.pSignalSemaphores =complete_sems.GetData();
|
|
|
|
|
|
|
|
|
|
VkFence fence=*fence_list[current_fence];
|
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
if(++current_fence==sc_attr->swap_chain_count)
|
2019-07-13 02:37:19 +08:00
|
|
|
|
current_fence=0;
|
|
|
|
|
|
2019-07-13 02:38:42 +08:00
|
|
|
|
//不在这里立即等待fence完成,是因为有可能queue submit需要久一点工作时间,我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence,而且是另一帧的fence。这样有利于异步处理
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
|
|
|
|
return(result==VK_SUCCESS);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-16 10:26:24 +08:00
|
|
|
|
bool Swapchain::PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore)
|
2019-07-13 02:37:19 +08:00
|
|
|
|
{
|
2019-07-16 10:26:24 +08:00
|
|
|
|
VkSemaphore sem=*render_complete_semaphore;
|
|
|
|
|
|
|
|
|
|
present_info.pWaitSemaphores=&sem;
|
2019-07-13 02:37:19 +08:00
|
|
|
|
present_info.pImageIndices=¤t_frame;
|
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
VkResult result=vkQueuePresentKHR(sc_attr->graphics_queue,&present_info);
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
|
|
|
|
if (!((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR)))
|
|
|
|
|
{
|
|
|
|
|
if (result == VK_ERROR_OUT_OF_DATE_KHR) {
|
|
|
|
|
// Swap chain is no longer compatible with the surface and needs to be recreated
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-15 22:37:00 +08:00
|
|
|
|
result=vkQueueWaitIdle(sc_attr->graphics_queue);
|
2019-07-13 02:37:19 +08:00
|
|
|
|
|
|
|
|
|
if(result!=VK_SUCCESS)
|
|
|
|
|
return(false);
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|