Moved render_cmd_buffer to SwapchainImage

This commit is contained in:
hyzboy 2025-01-25 01:41:00 +08:00
parent 79b0b2ef76
commit c8adc9af24
7 changed files with 99 additions and 121 deletions

View File

@ -78,14 +78,14 @@ public:
RTSwapchain(VkDevice dev,Swapchain *sc,DeviceQueue *q,Semaphore *rcs,Semaphore *pcs,RenderPass *rp); RTSwapchain(VkDevice dev,Swapchain *sc,DeviceQueue *q,Semaphore *rcs,Semaphore *pcs,RenderPass *rp);
~RTSwapchain(); ~RTSwapchain();
Framebuffer * GetFramebuffer ()override {return swapchain->sc_fbo[current_frame];} Framebuffer * GetFramebuffer ()override {return swapchain->sc_image[current_frame].fbo;}
Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_fbo[index];} Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_image[index].fbo;}
const uint32_t GetColorCount ()const override {return 1;} const uint32_t GetColorCount ()const override {return 1;}
const uint32_t GetImageCount ()const {return swapchain->color_count;} const uint32_t GetImageCount ()const {return swapchain->image_count;}
virtual Texture2D * GetColorTexture (const int index=0) override{return swapchain->sc_color[index];} virtual Texture2D * GetColorTexture (const int index=0) override{return swapchain->sc_image[current_frame].color;}
virtual Texture2D * GetDepthTexture () override{return swapchain->sc_depth;} virtual Texture2D * GetDepthTexture () override{return swapchain->sc_image[current_frame].depth;}
public: public:

View File

@ -1,10 +1,33 @@
#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE #pragma once
#define HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE
#include<hgl/graph/VK.h> #include<hgl/graph/VK.h>
#include<hgl/graph/VKTexture.h> #include<hgl/graph/VKTexture.h>
#include<hgl/type/List.h> #include<hgl/type/List.h>
#include<hgl/graph/VKFramebuffer.h>
#include<hgl/graph/VKCommandBuffer.h>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
struct SwapchainImage
{
Texture2D * color =nullptr;
Texture2D * depth =nullptr;
Framebuffer * fbo =nullptr;
RenderCmdBuffer * cmd_buf =nullptr;
public:
~SwapchainImage()
{
delete cmd_buf;
delete fbo;
delete depth;
delete color;
}
};//struct SwapchainImage
struct Swapchain struct Swapchain
{ {
public: public:
@ -15,19 +38,17 @@ public:
VkSurfaceTransformFlagBitsKHR transform; VkSurfaceTransformFlagBitsKHR transform;
VkSwapchainKHR swap_chain =VK_NULL_HANDLE; VkSwapchainKHR swap_chain =VK_NULL_HANDLE;
VkSurfaceFormatKHR surface_format; VkSurfaceFormatKHR surface_format {};
VkFormat depth_format; VkFormat depth_format =VK_FORMAT_UNDEFINED;
uint32_t color_count =0; RenderPass * render_pass =nullptr;
Texture2D ** sc_color =nullptr; uint32_t image_count =0;
Texture2D * sc_depth =nullptr;
Framebuffer ** sc_fbo =nullptr; SwapchainImage * sc_image =nullptr;
public: public:
virtual ~Swapchain(); virtual ~Swapchain();
};//struct Swapchain };//struct Swapchain
VK_NAMESPACE_END VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE

View File

@ -17,19 +17,12 @@ GRAPH_MODULE_CLASS(SwapchainModule)
RTSwapchain *swapchain_rt=nullptr; RTSwapchain *swapchain_rt=nullptr;
RenderPass *swapchain_rp=nullptr;
RenderCmdBuffer **cmd_buf=nullptr;
protected: protected:
bool CreateSwapchainFBO(); bool CreateSwapchainFBO();
bool CreateSwapchain(); bool CreateSwapchain();
bool CreateSwapchainRenderTarget(); bool CreateSwapchainRenderTarget();
void InitRenderCmdBuffer();
void ClearRenderCmdBuffer();
public: public:
virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变 virtual void OnResize(const VkExtent2D &)override; ///<窗口大小改变

View File

@ -23,17 +23,6 @@ bool TextureManager::CheckFormatSupport(const VkFormat format,const uint32_t bit
return(fp.linearTilingFeatures&bits); return(fp.linearTilingFeatures&bits);
} }
void TextureManager::Clear(TextureCreateInfo *tci)
{
if(!tci)return;
if(tci->image)DestroyImage(tci->image);
if(tci->image_view)delete tci->image_view;
if(tci->memory)delete tci->memory;
delete tci;
}
bool TextureManager::CopyBufferToImage(const CopyBufferToImageInfo *info,VkPipelineStageFlags destinationStage) bool TextureManager::CopyBufferToImage(const CopyBufferToImageInfo *info,VkPipelineStageFlags destinationStage)
{ {
if(!info) if(!info)

View File

@ -19,6 +19,17 @@ Texture2D *TextureManager::CreateTexture2D(TextureData *tex_data)
return tex; return tex;
} }
void TextureManager::Clear(TextureCreateInfo *tci)
{
if(!tci)return;
if(tci->image)DestroyImage(tci->image);
if(tci->image_view)delete tci->image_view;
if(tci->memory)delete tci->memory;
delete tci;
}
Texture2D *TextureManager::CreateTexture2D(TextureCreateInfo *tci) Texture2D *TextureManager::CreateTexture2D(TextureCreateInfo *tci)
{ {
if(!tci)return(nullptr); if(!tci)return(nullptr);

View File

@ -1,12 +1,11 @@
#include<hgl/graph/VKSwapchain.h> #include<hgl/graph/VKSwapchain.h>
#include<hgl/graph/VKFramebuffer.h> #include<hgl/graph/VKFramebuffer.h>
#include<hgl/graph/VKRenderPass.h>
VK_NAMESPACE_BEGIN VK_NAMESPACE_BEGIN
Swapchain::~Swapchain() Swapchain::~Swapchain()
{ {
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_fbo,color_count); SAFE_CLEAR_ARRAY(sc_image);
SAFE_CLEAR(sc_depth);
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(sc_color,color_count)
if(swap_chain) if(swap_chain)
{ {
@ -14,6 +13,6 @@ Swapchain::~Swapchain()
swap_chain=VK_NULL_HANDLE; swap_chain=VK_NULL_HANDLE;
} }
color_count=0; image_count=0;
} }
VK_NAMESPACE_END VK_NAMESPACE_END

View File

@ -88,57 +88,49 @@ namespace
bool SwapchainModule::CreateSwapchainFBO() bool SwapchainModule::CreateSwapchainFBO()
{ {
if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->image_count),nullptr)!=VK_SUCCESS)
return(false); return(false);
AutoDeleteArray<VkImage> sc_images(swapchain->color_count); AutoDeleteArray<VkImage> sc_images(swapchain->image_count);
if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->image_count),sc_images)!=VK_SUCCESS)
return(false); return(false);
swapchain->sc_image=hgl_zero_new<SwapchainImage>(swapchain->image_count);
AnsiString num_string;
GPUDevice *device=GetDevice();
auto *dev_attr=GetDeviceAttribute();
for(uint32_t i=0;i<swapchain->image_count;i++)
{ {
auto sc_depth_tci=new SwapchainDepthTextureCreateInfo(GetPhysicalDevice()->GetDepthFormat(),swapchain->extent); swapchain->sc_image[i].color=tex_manager->CreateTexture2D(new SwapchainColorTextureCreateInfo(swapchain->surface_format.format,swapchain->extent,sc_images[i]));
swapchain->sc_depth =tex_manager->CreateTexture2D(sc_depth_tci); if(!swapchain->sc_image[i].color)
if(!swapchain->sc_depth)
return(false); return(false);
swapchain->sc_image[i].depth =tex_manager->CreateTexture2D(new SwapchainDepthTextureCreateInfo(swapchain->depth_format,swapchain->extent));
if(!swapchain->sc_image[i].depth)
return(false);
swapchain->sc_image[i].fbo=rt_manager->CreateFBO( swapchain->render_pass,
swapchain->sc_image[i].color->GetImageView(),
swapchain->sc_image[i].depth->GetImageView());
AnsiString num_string=AnsiString::numberOf(i);
swapchain->sc_image[i].cmd_buf=device->CreateRenderCommandBuffer(AnsiString("Swapchain_RenderCmdBuffer_")+num_string);
#ifdef _DEBUG
if(dev_attr->debug_utils)
{
dev_attr->debug_utils->SetTexture(swapchain->sc_image[i].color,"SwapchainColor_"+num_string);
dev_attr->debug_utils->SetTexture(swapchain->sc_image[i].depth,"SwapchainDepth_"+num_string);
dev_attr->debug_utils->SetFramebuffer(swapchain->sc_image[i].fbo->GetFramebuffer(),"SwapchainFBO_"+num_string);
} }
#endif//_DEBUG
//#ifdef _DEBUG
// if(dev_attr->debug_utils)
// {
// dev_attr->debug_utils->SetImage(swapchain->sc_depth->GetImage(),"SwapchainDepthImage");
// dev_attr->debug_utils->SetImageView(swapchain->sc_depth->GetVulkanImageView(),"SwapchainDepthImageView");
// dev_attr->debug_utils->SetDeviceMemory(swapchain->sc_depth->GetDeviceMemory(),"SwapchainDepthMemory");
// }
//#endif//_DEBUG
swapchain->sc_color =hgl_zero_new<Texture2D *>(swapchain->color_count);
swapchain->sc_fbo =hgl_zero_new<Framebuffer *>(swapchain->color_count);
for(uint32_t i=0;i<swapchain->color_count;i++)
{
auto sc_color_tci=new SwapchainColorTextureCreateInfo(swapchain->surface_format.format,swapchain->extent,sc_images[i]);
swapchain->sc_color[i]=tex_manager->CreateTexture2D(sc_color_tci);
if(!swapchain->sc_color[i])
return(false);
swapchain->sc_fbo[i]=rt_manager->CreateFBO(swapchain_rp,
swapchain->sc_color[i]->GetImageView(),
swapchain->sc_depth->GetImageView());
//#ifdef _DEBUG
// if(dev_attr->debug_utils)
// {
// dev_attr->debug_utils->SetImage(swapchain->sc_color[i]->GetImage(),"SwapchainColorImage_"+AnsiString::numberOf(i));
// dev_attr->debug_utils->SetImageView(swapchain->sc_color[i]->GetVulkanImageView(),"SwapchainColorImageView_"+AnsiString::numberOf(i));
// dev_attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i));
// }
//#endif//_DEBUG
} }
return(true); return(true);
@ -159,6 +151,15 @@ bool SwapchainModule::CreateSwapchain()
swapchain->surface_format =dev_attr->surface_format; swapchain->surface_format =dev_attr->surface_format;
swapchain->depth_format =dev_attr->physical_device->GetDepthFormat(); swapchain->depth_format =dev_attr->physical_device->GetDepthFormat();
SwapchainRenderbufferInfo rbi(swapchain->surface_format.format,swapchain->depth_format);
swapchain->render_pass =rp_manager->AcquireRenderPass(&rbi);
#ifdef _DEBUG
if(dev_attr->debug_utils)
dev_attr->debug_utils->SetRenderPass(swapchain->render_pass->GetVkRenderPass(),"MainDeviceRenderPass");
#endif//_DEBUG
swapchain->swap_chain=CreateVulkanSwapChain(dev_attr); swapchain->swap_chain=CreateVulkanSwapChain(dev_attr);
if(swapchain->swap_chain) if(swapchain->swap_chain)
@ -176,7 +177,7 @@ bool SwapchainModule::CreateSwapchainRenderTarget()
{ {
GPUDevice *device=GetDevice(); GPUDevice *device=GetDevice();
DeviceQueue *q=device->CreateQueue(swapchain->color_count,false); DeviceQueue *q=device->CreateQueue(swapchain->image_count,false);
Semaphore *render_complete_semaphore=device->CreateGPUSemaphore(); Semaphore *render_complete_semaphore=device->CreateGPUSemaphore();
Semaphore *present_complete_semaphore=device->CreateGPUSemaphore(); Semaphore *present_complete_semaphore=device->CreateGPUSemaphore();
@ -185,40 +186,14 @@ bool SwapchainModule::CreateSwapchainRenderTarget()
q, q,
render_complete_semaphore, render_complete_semaphore,
present_complete_semaphore, present_complete_semaphore,
swapchain_rp swapchain->render_pass
); );
return true; return true;
} }
void SwapchainModule::ClearRenderCmdBuffer()
{
SAFE_CLEAR_OBJECT_ARRAY_OBJECT(cmd_buf,swapchain->color_count);
}
void SwapchainModule::InitRenderCmdBuffer()
{
ClearRenderCmdBuffer();
cmd_buf=hgl_zero_new<RenderCmdBuffer *>(swapchain->color_count);
GPUDevice *device=GetDevice();
AnsiString cmd_buf_name;
for(uint32_t i=0;i<swapchain->color_count;i++)
{
cmd_buf_name=device->GetPhysicalDevice()->GetDeviceName()+AnsiString(":RenderCmdBuffer_")+AnsiString::numberOf(i);
cmd_buf[i]=device->CreateRenderCommandBuffer(cmd_buf_name);
}
}
SwapchainModule::~SwapchainModule() SwapchainModule::~SwapchainModule()
{ {
ClearRenderCmdBuffer();
SAFE_CLEAR(swapchain_rp);
SAFE_CLEAR(swapchain_rt); SAFE_CLEAR(swapchain_rt);
SAFE_CLEAR(swapchain); SAFE_CLEAR(swapchain);
} }
@ -228,10 +203,6 @@ SwapchainModule::SwapchainModule(GPUDevice *dev,TextureManager *tm,RenderTargetM
tex_manager=tm; tex_manager=tm;
rt_manager=rtm; rt_manager=rtm;
SwapchainRenderbufferInfo rbi(swapchain->surface_format.format,swapchain->depth_format);
swapchain_rp=rpm->AcquireRenderPass(&rbi);
if(!CreateSwapchain()) if(!CreateSwapchain())
return; return;
@ -242,28 +213,22 @@ SwapchainModule::SwapchainModule(GPUDevice *dev,TextureManager *tm,RenderTargetM
if(!CreateSwapchainRenderTarget()) if(!CreateSwapchainRenderTarget())
return; return;
InitRenderCmdBuffer();
} }
void SwapchainModule::OnResize(const VkExtent2D &extent) void SwapchainModule::OnResize(const VkExtent2D &extent)
{ {
ClearRenderCmdBuffer();
SAFE_CLEAR(swapchain_rt) SAFE_CLEAR(swapchain_rt)
GetDeviceAttribute()->RefreshSurfaceCaps(); GetDeviceAttribute()->RefreshSurfaceCaps();
CreateSwapchainRenderTarget(); CreateSwapchainRenderTarget();
InitRenderCmdBuffer();
} }
bool SwapchainModule::BeginFrame() bool SwapchainModule::BeginFrame()
{ {
uint32_t index=swapchain_rt->AcquireNextImage(); uint32_t index=swapchain_rt->AcquireNextImage();
if(index>=swapchain->color_count) if(index>=swapchain->image_count)
return(false); return(false);
return(true); return(true);
@ -273,7 +238,7 @@ void SwapchainModule::EndFrame()
{ {
int index=swapchain_rt->GetCurrentFrameIndices(); int index=swapchain_rt->GetCurrentFrameIndices();
VkCommandBuffer cb=*(cmd_buf[index]); VkCommandBuffer cb=*(swapchain->sc_image[index].cmd_buf);
swapchain_rt->Submit(cb); swapchain_rt->Submit(cb);
swapchain_rt->PresentBackbuffer(); swapchain_rt->PresentBackbuffer();
@ -285,13 +250,13 @@ RenderCmdBuffer *SwapchainModule::Use()
{ {
uint32_t index=swapchain_rt->GetCurrentFrameIndices(); uint32_t index=swapchain_rt->GetCurrentFrameIndices();
if(index>=swapchain->color_count) if(index>=swapchain->image_count)
return(nullptr); return(nullptr);
RenderCmdBuffer *rcb=cmd_buf[index]; RenderCmdBuffer *rcb=swapchain->sc_image[index].cmd_buf;
rcb->Begin(); rcb->Begin();
rcb->BindFramebuffer(swapchain_rp,swapchain_rt->GetFramebuffer()); rcb->BindFramebuffer(swapchain->render_pass,swapchain_rt->GetFramebuffer());
return rcb; return rcb;
} }