improved Swapchain&SwapchainRenderTarget

This commit is contained in:
hyzboy 2021-12-15 20:46:33 +08:00
parent 14b9369739
commit ee9be8a4a2
8 changed files with 64 additions and 81 deletions

View File

@ -39,7 +39,6 @@ private:
DeviceRenderPassManage *render_pass_manage; DeviceRenderPassManage *render_pass_manage;
RenderPass *device_render_pass; RenderPass *device_render_pass;
Swapchain *swapchain;
SwapchainRenderTarget *swapchainRT; SwapchainRenderTarget *swapchainRT;
SwapchainRenderTarget *CreateSwapchainRenderTarget(); SwapchainRenderTarget *CreateSwapchainRenderTarget();
@ -51,10 +50,11 @@ private:
VkCommandBuffer CreateCommandBuffer(); VkCommandBuffer CreateCommandBuffer();
bool CreateSwapchainColorTexture(); bool CreateSwapchainColorTexture(Swapchain *);
bool CreateSwapchainDepthTexture(); bool CreateSwapchainDepthTexture(Swapchain *);
void CreateSwapchainFBO(Swapchain *);
bool CreateSwapchain(const VkExtent2D &acquire_extent); Swapchain *CreateSwapchain(const VkExtent2D &acquire_extent);
private: private:
@ -81,13 +81,12 @@ public:
VkQueue GetGraphicsQueue () {return attr->graphics_queue;} VkQueue GetGraphicsQueue () {return attr->graphics_queue;}
RenderPass * GetRenderPass () {return device_render_pass;} RenderPass * GetRenderPass () {return device_render_pass;}
Swapchain * GetSwapchain () {return swapchain;}
SwapchainRenderTarget * GetSwapchainRT () {return swapchainRT;} SwapchainRenderTarget * GetSwapchainRT () {return swapchainRT;}
public: const VkExtent2D & GetSwapchainSize ()const {return swapchainRT->GetExtent();}
const VkExtent2D & GetSwapchainSize ()const {return swapchain->extent;} public:
bool Resize (const VkExtent2D &); bool Resize (const VkExtent2D &);
bool Resize (const uint32_t &w,const uint32_t &h) bool Resize (const uint32_t &w,const uint32_t &h)

View File

@ -66,29 +66,25 @@ class SwapchainRenderTarget:public RenderTarget
{ {
VkDevice device; VkDevice device;
Swapchain *swapchain; Swapchain *swapchain;
VkSwapchainKHR vk_swapchain;
PresentInfo present_info; PresentInfo present_info;
GPUSemaphore *present_complete_semaphore=nullptr; GPUSemaphore *present_complete_semaphore=nullptr;
uint32_t swap_chain_count;
uint32_t current_frame; uint32_t current_frame;
Framebuffer **render_frame;
public: public:
SwapchainRenderTarget(VkDevice dev,Swapchain *sc,GPUQueue *q,GPUSemaphore *rcs,GPUSemaphore *pcs,RenderPass *rp,Framebuffer **fbo_list); SwapchainRenderTarget(VkDevice dev,Swapchain *sc,GPUQueue *q,GPUSemaphore *rcs,GPUSemaphore *pcs,RenderPass *rp);
~SwapchainRenderTarget(); ~SwapchainRenderTarget();
Framebuffer * GetFramebuffer ()override {return render_frame[current_frame];} Framebuffer * GetFramebuffer ()override {return swapchain->render_frame[current_frame];}
Framebuffer * GetFramebuffer (const uint32_t index) {return render_frame[index];} Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->render_frame[index];}
const uint32_t GetColorCount ()const override {return 1;} const uint32_t GetColorCount ()const override {return 1;}
const uint32_t GetImageCount ()const {return swap_chain_count;} const uint32_t GetImageCount ()const {return swapchain->color_count;}
virtual Texture2D * GetColorTexture (const int index=0) override{return swapchain->GetColorTexture(index);} virtual Texture2D * GetColorTexture (const int index=0) override{return swapchain->sc_color[index];}
virtual Texture2D * GetDepthTexture () override{return swapchain->GetDepthTexture();} virtual Texture2D * GetDepthTexture () override{return swapchain->sc_depth;}
public: public:

View File

@ -13,23 +13,14 @@ public:
VkExtent2D extent; VkExtent2D extent;
VkQueue graphics_queue =VK_NULL_HANDLE;
VkSwapchainKHR swap_chain =VK_NULL_HANDLE; VkSwapchainKHR swap_chain =VK_NULL_HANDLE;
uint32_t swap_chain_count=0; uint32_t color_count=0;
ObjectList<Texture2D> sc_color; Texture2D ** sc_color =nullptr;
Texture2D * sc_depth =nullptr; Texture2D * sc_depth =nullptr;
public: Framebuffer ** render_frame =nullptr;
VkSwapchainKHR GetSwapchain () {return swap_chain;}
const VkExtent2D & GetExtent ()const {return extent;}
const uint32_t GetImageCount ()const {return sc_color.GetCount();}
Texture2D ** GetColorTextures () {return sc_color.GetData();}
Texture2D * GetColorTexture (int index) {return sc_color[index];}
Texture2D * GetDepthTexture () {return sc_depth;}
public: public:

View File

@ -5,7 +5,6 @@
#include<hgl/graph/VKImageView.h> #include<hgl/graph/VKImageView.h>
#include<hgl/graph/VKPipeline.h> #include<hgl/graph/VKPipeline.h>
#include<hgl/graph/VKCommandBuffer.h> #include<hgl/graph/VKCommandBuffer.h>
//#include<hgl/graph/VKDescriptorSet.h>
#include<hgl/graph/VKRenderPass.h> #include<hgl/graph/VKRenderPass.h>
#include<hgl/graph/VKFramebuffer.h> #include<hgl/graph/VKFramebuffer.h>
#include<hgl/graph/VKDescriptorSets.h> #include<hgl/graph/VKDescriptorSets.h>
@ -42,7 +41,6 @@ GPUDevice::GPUDevice(GPUDeviceAttribute *da)
InitRenderPassManage(); InitRenderPassManage();
swapchain=nullptr;
swapchainRT=nullptr; swapchainRT=nullptr;
Resize(attr->surface_caps.currentExtent); Resize(attr->surface_caps.currentExtent);
@ -57,7 +55,6 @@ GPUDevice::~GPUDevice()
ClearRenderPassManage(); ClearRenderPassManage();
SAFE_CLEAR(swapchainRT); SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
SAFE_CLEAR(texture_queue); SAFE_CLEAR(texture_queue);
SAFE_CLEAR(texture_cmd_buf); SAFE_CLEAR(texture_cmd_buf);
@ -68,13 +65,9 @@ GPUDevice::~GPUDevice()
bool GPUDevice::Resize(const VkExtent2D &extent) bool GPUDevice::Resize(const VkExtent2D &extent)
{ {
SAFE_CLEAR(swapchainRT); SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
attr->RefreshSurfaceCaps(); attr->RefreshSurfaceCaps();
if(!CreateSwapchain(attr->surface_caps.currentExtent))
return(false);
swapchainRT=CreateSwapchainRenderTarget(); swapchainRT=CreateSwapchainRenderTarget();
return(true); return(true);

View File

@ -59,30 +59,21 @@ RenderTarget *GPUDevice::CreateRenderTarget(const FramebufferInfo *fbi,const uin
SwapchainRenderTarget *GPUDevice::CreateSwapchainRenderTarget() SwapchainRenderTarget *GPUDevice::CreateSwapchainRenderTarget()
{ {
const uint32_t count=swapchain->GetImageCount(); Swapchain *sc=CreateSwapchain(attr->surface_caps.currentExtent);
GPUQueue *q=CreateQueue(count,false); if(!sc)
return(false);
GPUQueue *q=CreateQueue(sc->color_count,false);
GPUSemaphore *render_complete_semaphore=CreateGPUSemaphore(); GPUSemaphore *render_complete_semaphore=CreateGPUSemaphore();
GPUSemaphore *present_complete_semaphore=CreateGPUSemaphore(); GPUSemaphore *present_complete_semaphore=CreateGPUSemaphore();
Texture2D **sc_color=swapchain->GetColorTextures();
Texture2D *sc_depth=swapchain->GetDepthTexture();
Framebuffer **render_frame=new Framebuffer *[count];
for(uint32_t i=0;i<count;i++)
{
render_frame[i]=CreateFramebuffer(device_render_pass,(*sc_color)->GetImageView(),sc_depth->GetImageView());
++sc_color;
}
SwapchainRenderTarget *srt=new SwapchainRenderTarget( attr->device, SwapchainRenderTarget *srt=new SwapchainRenderTarget( attr->device,
swapchain, sc,
q, q,
render_complete_semaphore, render_complete_semaphore,
present_complete_semaphore, present_complete_semaphore,
device_render_pass, device_render_pass
render_frame
); );
return srt; return srt;

View File

@ -68,46 +68,63 @@ namespace
} }
}//namespace }//namespace
bool GPUDevice::CreateSwapchainColorTexture() bool GPUDevice::CreateSwapchainColorTexture(Swapchain *swapchain)
{ {
if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->swap_chain_count),nullptr)!=VK_SUCCESS) if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS)
return(false); return(false);
AutoDeleteArray<VkImage> sc_images(swapchain->swap_chain_count); AutoDeleteArray<VkImage> sc_images(swapchain->color_count);
if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->swap_chain_count),sc_images)!=VK_SUCCESS) if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS)
return(false); return(false);
for(VkImage ip:sc_images) swapchain->sc_color=new Texture2D *[swapchain->color_count];
swapchain->sc_color.Add(CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,ip)));
for(uint32_t i=0;i<swapchain->color_count;i++)
swapchain->sc_color[i]=CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,sc_images[i]));
return(true); return(true);
} }
bool GPUDevice::CreateSwapchainDepthTexture() bool GPUDevice::CreateSwapchainDepthTexture(Swapchain *swapchain)
{ {
swapchain->sc_depth=CreateTexture2D(new SwapchainDepthTextureCreateInfo(attr->physical_device->GetDepthFormat(),swapchain->extent)); swapchain->sc_depth=CreateTexture2D(new SwapchainDepthTextureCreateInfo(attr->physical_device->GetDepthFormat(),swapchain->extent));
return swapchain->sc_depth; return swapchain->sc_depth;
} }
bool GPUDevice::CreateSwapchain(const VkExtent2D &acquire_extent) void GPUDevice::CreateSwapchainFBO(Swapchain *swapchain)
{ {
swapchain=new Swapchain; swapchain->render_frame=new Framebuffer *[swapchain->color_count];
for(uint32_t i=0;i<swapchain->color_count;i++)
{
swapchain->render_frame[i]=CreateFramebuffer( device_render_pass,
swapchain->sc_color[i]->GetImageView(),
swapchain->sc_depth->GetImageView());
}
}
Swapchain *GPUDevice::CreateSwapchain(const VkExtent2D &acquire_extent)
{
Swapchain *swapchain=new Swapchain;
swapchain->device =attr->device; swapchain->device =attr->device;
swapchain->extent =acquire_extent; swapchain->extent =acquire_extent;
swapchain->graphics_queue =attr->graphics_queue;
swapchain->swap_chain =CreateSwapChain(attr,swapchain->extent); swapchain->swap_chain =CreateSwapChain(attr,acquire_extent);
if(swapchain->swap_chain) if(swapchain->swap_chain)
if(CreateSwapchainColorTexture()) if(CreateSwapchainColorTexture(swapchain))
if(CreateSwapchainDepthTexture()) if(CreateSwapchainDepthTexture(swapchain))
return(true); {
CreateSwapchainFBO(swapchain);
return(swapchain);
}
delete swapchain; delete swapchain;
swapchain=nullptr; swapchain=nullptr;
return(false); return(nullptr);
} }
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -1,10 +1,12 @@
#include<hgl/graph/VKSwapchain.h> #include<hgl/graph/VKSwapchain.h>
#include<hgl/graph/VKFramebuffer.h>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Swapchain::~Swapchain() Swapchain::~Swapchain()
{ {
SAFE_CLEAR_OBJECT_ARRAY(render_frame,color_count);
SAFE_CLEAR(sc_depth); SAFE_CLEAR(sc_depth);
sc_color.Clear(); SAFE_CLEAR_OBJECT_ARRAY(sc_color,color_count)
if(swap_chain) if(swap_chain)
{ {
@ -12,6 +14,6 @@ Swapchain::~Swapchain()
swap_chain=VK_NULL_HANDLE; swap_chain=VK_NULL_HANDLE;
} }
swap_chain_count=0; color_count=0;
} }
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -3,26 +3,21 @@
#include<hgl/graph/VKSemaphore.h> #include<hgl/graph/VKSemaphore.h>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
SwapchainRenderTarget::SwapchainRenderTarget(VkDevice dev,Swapchain *sc,GPUQueue *q,GPUSemaphore *rcs,GPUSemaphore *pcs,RenderPass *rp,Framebuffer **fbo_list):RenderTarget(q,rcs) SwapchainRenderTarget::SwapchainRenderTarget(VkDevice dev,Swapchain *sc,GPUQueue *q,GPUSemaphore *rcs,GPUSemaphore *pcs,RenderPass *rp):RenderTarget(q,rcs)
{ {
device=dev; device=dev;
swapchain=sc; swapchain=sc;
vk_swapchain=swapchain->GetSwapchain();
present_info.waitSemaphoreCount = 0; present_info.waitSemaphoreCount = 0;
present_info.pWaitSemaphores = nullptr; present_info.pWaitSemaphores = nullptr;
present_info.swapchainCount = 1; present_info.swapchainCount = 1;
present_info.pResults = nullptr; present_info.pResults = nullptr;
present_info.pSwapchains = &vk_swapchain; present_info.pSwapchains = &(swapchain->swap_chain);
render_pass=rp; render_pass=rp;
swap_chain_count=swapchain->GetImageCount(); extent=swapchain->extent;
extent=swapchain->GetExtent();
render_frame=fbo_list;
current_frame=0; current_frame=0;
@ -31,14 +26,13 @@ SwapchainRenderTarget::SwapchainRenderTarget(VkDevice dev,Swapchain *sc,GPUQueue
SwapchainRenderTarget::~SwapchainRenderTarget() SwapchainRenderTarget::~SwapchainRenderTarget()
{ {
SAFE_CLEAR_OBJECT_ARRAY(render_frame,swap_chain_count);
delete present_complete_semaphore; delete present_complete_semaphore;
delete swapchain;
} }
int SwapchainRenderTarget::AcquireNextImage() int SwapchainRenderTarget::AcquireNextImage()
{ {
if(vkAcquireNextImageKHR(device,vk_swapchain,UINT64_MAX,*(this->present_complete_semaphore),VK_NULL_HANDLE,&current_frame)==VK_SUCCESS) if(vkAcquireNextImageKHR(device,swapchain->swap_chain,UINT64_MAX,*(this->present_complete_semaphore),VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
return current_frame; return current_frame;
return -1; return -1;