ULRE/src/RenderDevice/Vulkan/VKRenderTarget.cpp

110 lines
3.2 KiB
C++
Raw Normal View History

2019-06-19 21:12:39 +08:00
#include<hgl/graph/vulkan/VKRenderTarget.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKSwapchain.h>
#include<hgl/graph/vulkan/VKCommandBuffer.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
2019-06-19 21:12:39 +08:00
VK_NAMESPACE_BEGIN
namespace
2019-06-19 21:12:39 +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)
{
2020-10-17 14:16:52 +08:00
fb=_fb;
command_buffer=_cb;
depth_texture=nullptr;
render_complete_semaphore=dev->CreateSem();
}
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)
{
fb=_fb;
command_buffer=_cb;
2020-10-17 14:16:52 +08:00
color_texture.Add(ctl,cc);
depth_texture=dt;
render_complete_semaphore=dev->CreateSem();
}
RenderTarget::~RenderTarget()
{
2020-10-17 14:16:52 +08:00
SAFE_CLEAR(depth_texture);
color_texture.Clear();
SAFE_CLEAR(render_complete_semaphore);
SAFE_CLEAR(command_buffer);
}
bool RenderTarget::Submit(Semaphore *present_complete_semaphore)
{
return this->SubmitQueue::Submit(*command_buffer,present_complete_semaphore,render_complete_semaphore);
}
SwapchainRenderTarget::SwapchainRenderTarget(Device *dev,Swapchain *sc):RenderTarget(dev,nullptr,nullptr,sc->GetImageCount())
{
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();
main_rp=device->CreateRenderPass((*sc_color)->GetFormat(),sc_depth->GetFormat(),VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
swap_chain_count=swapchain->GetImageCount();
extent=swapchain->GetExtent();
for(uint i=0;i<swap_chain_count;i++)
{
render_frame.Add(device->CreateFramebuffer(main_rp,(*sc_color)->GetImageView(),sc_depth->GetImageView()));
++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)
{
2020-10-17 14:16:52 +08:00
if(vkAcquireNextImageKHR(device->GetDevice(),vk_swapchain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
return current_frame;
return -1;
}
2020-10-17 14:16:52 +08:00
bool SwapchainRenderTarget::PresentBackbuffer(VkSemaphore *render_complete_semaphore,const uint32_t count)
{
2020-10-17 14:16:52 +08:00
present_info.waitSemaphoreCount =count;
present_info.pWaitSemaphores =render_complete_semaphore;
present_info.pImageIndices =&current_frame;
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