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();
Framebuffer * GetFramebuffer ()override {return swapchain->sc_fbo[current_frame];}
Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_fbo[index];}
Framebuffer * GetFramebuffer ()override {return swapchain->sc_image[current_frame].fbo;}
Framebuffer * GetFramebuffer (const uint32_t index) {return swapchain->sc_image[index].fbo;}
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 * GetDepthTexture () override{return swapchain->sc_depth;}
virtual Texture2D * GetColorTexture (const int index=0) override{return swapchain->sc_image[current_frame].color;}
virtual Texture2D * GetDepthTexture () override{return swapchain->sc_image[current_frame].depth;}
public:

View File

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

View File

@ -17,19 +17,12 @@ GRAPH_MODULE_CLASS(SwapchainModule)
RTSwapchain *swapchain_rt=nullptr;
RenderPass *swapchain_rp=nullptr;
RenderCmdBuffer **cmd_buf=nullptr;
protected:
bool CreateSwapchainFBO();
bool CreateSwapchain();
bool CreateSwapchainRenderTarget();
void InitRenderCmdBuffer();
void ClearRenderCmdBuffer();
public:
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);
}
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)
{
if(!info)

View File

@ -19,6 +19,17 @@ Texture2D *TextureManager::CreateTexture2D(TextureData *tex_data)
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)
{
if(!tci)return(nullptr);

View File

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

View File

@ -88,57 +88,49 @@ namespace
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);
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);
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_depth)
if(!swapchain->sc_image[i].color)
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);
}
//#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
#endif//_DEBUG
}
return(true);
@ -159,6 +151,15 @@ bool SwapchainModule::CreateSwapchain()
swapchain->surface_format =dev_attr->surface_format;
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);
if(swapchain->swap_chain)
@ -176,7 +177,7 @@ bool SwapchainModule::CreateSwapchainRenderTarget()
{
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 *present_complete_semaphore=device->CreateGPUSemaphore();
@ -185,40 +186,14 @@ bool SwapchainModule::CreateSwapchainRenderTarget()
q,
render_complete_semaphore,
present_complete_semaphore,
swapchain_rp
swapchain->render_pass
);
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()
{
ClearRenderCmdBuffer();
SAFE_CLEAR(swapchain_rp);
SAFE_CLEAR(swapchain_rt);
SAFE_CLEAR(swapchain);
}
@ -228,10 +203,6 @@ SwapchainModule::SwapchainModule(GPUDevice *dev,TextureManager *tm,RenderTargetM
tex_manager=tm;
rt_manager=rtm;
SwapchainRenderbufferInfo rbi(swapchain->surface_format.format,swapchain->depth_format);
swapchain_rp=rpm->AcquireRenderPass(&rbi);
if(!CreateSwapchain())
return;
@ -242,28 +213,22 @@ SwapchainModule::SwapchainModule(GPUDevice *dev,TextureManager *tm,RenderTargetM
if(!CreateSwapchainRenderTarget())
return;
InitRenderCmdBuffer();
}
void SwapchainModule::OnResize(const VkExtent2D &extent)
{
ClearRenderCmdBuffer();
SAFE_CLEAR(swapchain_rt)
GetDeviceAttribute()->RefreshSurfaceCaps();
CreateSwapchainRenderTarget();
InitRenderCmdBuffer();
}
bool SwapchainModule::BeginFrame()
{
uint32_t index=swapchain_rt->AcquireNextImage();
if(index>=swapchain->color_count)
if(index>=swapchain->image_count)
return(false);
return(true);
@ -273,7 +238,7 @@ void SwapchainModule::EndFrame()
{
int index=swapchain_rt->GetCurrentFrameIndices();
VkCommandBuffer cb=*(cmd_buf[index]);
VkCommandBuffer cb=*(swapchain->sc_image[index].cmd_buf);
swapchain_rt->Submit(cb);
swapchain_rt->PresentBackbuffer();
@ -285,13 +250,13 @@ RenderCmdBuffer *SwapchainModule::Use()
{
uint32_t index=swapchain_rt->GetCurrentFrameIndices();
if(index>=swapchain->color_count)
if(index>=swapchain->image_count)
return(nullptr);
RenderCmdBuffer *rcb=cmd_buf[index];
RenderCmdBuffer *rcb=swapchain->sc_image[index].cmd_buf;
rcb->Begin();
rcb->BindFramebuffer(swapchain_rp,swapchain_rt->GetFramebuffer());
rcb->BindFramebuffer(swapchain->render_pass,swapchain_rt->GetFramebuffer());
return rcb;
}