2019-06-19 21:12:39 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKRenderTarget.h>
|
2019-07-16 19:59:53 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKSwapchain.h>
|
2020-10-15 22:13:15 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKCommandBuffer.h>
|
2020-10-17 22:12:16 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKSemaphore.h>
|
2019-06-19 21:12:39 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-07-16 19:59:53 +08:00
|
|
|
|
namespace
|
2019-06-19 21:12:39 +08:00
|
|
|
|
{
|
2019-07-16 19:59:53 +08:00
|
|
|
|
const VkPipelineStageFlags pipe_stage_flags=VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
|
}//namespace
|
|
|
|
|
|
2020-10-17 14:16:52 +08:00
|
|
|
|
RenderTarget::RenderTarget(Device *dev,Framebuffer *_fb,CommandBuffer *_cb,const uint32_t fence_count):SubmitQueue(dev,dev->GetGraphicsQueue(),fence_count)
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
2020-10-17 14:16:52 +08:00
|
|
|
|
fb=_fb;
|
|
|
|
|
command_buffer=_cb;
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
2020-10-17 22:12:16 +08:00
|
|
|
|
depth_texture=nullptr;
|
|
|
|
|
render_complete_semaphore=dev->CreateSem();
|
2019-07-16 19:59:53 +08:00
|
|
|
|
}
|
2019-07-19 18:25:05 +08:00
|
|
|
|
|
2020-10-17 14:16:52 +08:00
|
|
|
|
RenderTarget::RenderTarget(Device *dev,Framebuffer *_fb,CommandBuffer *_cb,Texture2D **ctl,const uint32_t cc,Texture2D *dt,const uint32_t fence_count):SubmitQueue(dev,dev->GetGraphicsQueue(),fence_count)
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
|
|
|
|
fb=_fb;
|
2020-10-15 17:30:03 +08:00
|
|
|
|
command_buffer=_cb;
|
2020-10-17 14:16:52 +08:00
|
|
|
|
|
|
|
|
|
color_texture.Add(ctl,cc);
|
|
|
|
|
depth_texture=dt;
|
2020-10-17 22:12:16 +08:00
|
|
|
|
render_complete_semaphore=dev->CreateSem();
|
2019-07-16 19:59:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
RenderTarget::~RenderTarget()
|
2020-10-17 22:12:16 +08:00
|
|
|
|
{
|
2020-10-17 14:16:52 +08:00
|
|
|
|
SAFE_CLEAR(depth_texture);
|
|
|
|
|
color_texture.Clear();
|
2020-10-17 22:12:16 +08:00
|
|
|
|
|
|
|
|
|
SAFE_CLEAR(render_complete_semaphore);
|
2020-10-15 22:13:15 +08:00
|
|
|
|
SAFE_CLEAR(command_buffer);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 22:12:16 +08:00
|
|
|
|
bool RenderTarget::Submit(Semaphore *present_complete_semaphore)
|
|
|
|
|
{
|
|
|
|
|
return this->SubmitQueue::Submit(*command_buffer,present_complete_semaphore,render_complete_semaphore);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
SwapchainRenderTarget::SwapchainRenderTarget(Device *dev,Swapchain *sc):RenderTarget(dev,nullptr,nullptr,sc->GetImageCount())
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
|
|
|
|
swapchain=sc;
|
|
|
|
|
vk_swapchain=swapchain->GetSwapchain();
|
|
|
|
|
|
|
|
|
|
present_info.waitSemaphoreCount = 0;
|
|
|
|
|
present_info.pWaitSemaphores = nullptr;
|
|
|
|
|
present_info.swapchainCount = 1;
|
|
|
|
|
present_info.pResults = nullptr;
|
|
|
|
|
present_info.pSwapchains = &vk_swapchain;
|
|
|
|
|
|
|
|
|
|
Texture2D **sc_color=swapchain->GetColorTextures();
|
|
|
|
|
Texture2D *sc_depth=swapchain->GetDepthTexture();
|
|
|
|
|
|
2020-10-17 16:28:07 +08:00
|
|
|
|
main_rp=device->CreateRenderPass((*sc_color)->GetFormat(),sc_depth->GetFormat(),VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
|
|
|
|
swap_chain_count=swapchain->GetImageCount();
|
|
|
|
|
|
|
|
|
|
extent=swapchain->GetExtent();
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<swap_chain_count;i++)
|
|
|
|
|
{
|
2020-10-16 19:26:28 +08:00
|
|
|
|
render_frame.Add(device->CreateFramebuffer(main_rp,(*sc_color)->GetImageView(),sc_depth->GetImageView()));
|
2019-07-16 19:59:53 +08:00
|
|
|
|
++sc_color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
current_frame=0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SwapchainRenderTarget::~SwapchainRenderTarget()
|
|
|
|
|
{
|
|
|
|
|
render_frame.Clear();
|
|
|
|
|
|
|
|
|
|
delete main_rp;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 14:16:52 +08:00
|
|
|
|
int SwapchainRenderTarget::AcquireNextImage(VkSemaphore present_complete_semaphore)
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
2020-10-17 14:16:52 +08:00
|
|
|
|
if(vkAcquireNextImageKHR(device->GetDevice(),vk_swapchain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS)
|
2019-07-16 19:59:53 +08:00
|
|
|
|
return current_frame;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 14:16:52 +08:00
|
|
|
|
bool SwapchainRenderTarget::PresentBackbuffer(VkSemaphore *render_complete_semaphore,const uint32_t count)
|
2019-07-16 19:59:53 +08:00
|
|
|
|
{
|
2020-10-17 14:16:52 +08:00
|
|
|
|
present_info.waitSemaphoreCount =count;
|
|
|
|
|
present_info.pWaitSemaphores =render_complete_semaphore;
|
2019-07-19 18:25:05 +08:00
|
|
|
|
present_info.pImageIndices =¤t_frame;
|
2019-07-16 19:59:53 +08:00
|
|
|
|
|
|
|
|
|
VkResult result=vkQueuePresentKHR(queue,&present_info);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
2019-06-19 21:12:39 +08:00
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|