From 336688b4f02c186ff8fde23dfe3f6622e8dc7773 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 9 Nov 2024 00:10:27 +0800 Subject: [PATCH] moved few codes to SwapchainModule --- inc/hgl/graph/VKDevice.h | 20 --- inc/hgl/graph/VKSwapchain.h | 2 - inc/hgl/graph/module/SwapchainModule.h | 15 +- src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp | 157 -------------------- src/SceneGraph/module/SwapchainModule.cpp | 103 ++++++++++--- 5 files changed, 95 insertions(+), 202 deletions(-) delete mode 100644 src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp diff --git a/inc/hgl/graph/VKDevice.h b/inc/hgl/graph/VKDevice.h index cc73d686..7bb7ae30 100644 --- a/inc/hgl/graph/VKDevice.h +++ b/inc/hgl/graph/VKDevice.h @@ -40,10 +40,6 @@ private: DeviceRenderPassManage *render_pass_manage; RenderPass *device_render_pass; - RTSwapchain *sc_rt; - - RTSwapchain *CreateSwapchainRenderTarget(); - void InitRenderPassManage(); void ClearRenderPassManage(); @@ -77,10 +73,6 @@ public: RenderPass * GetRenderPass () {return device_render_pass;} - RTSwapchain * GetSwapchainRT () {return sc_rt;} - - const VkExtent2D & GetSwapchainSize ()const {return sc_rt->GetExtent();} - void WaitIdle ()const {vkDeviceWaitIdle(attr->device);} DebugUtils * GetDebugUtils (){return attr->debug_utils;} @@ -182,18 +174,6 @@ public: Semaphore * CreateGPUSemaphore(); DeviceQueue *CreateQueue(const uint32_t fence_count=1,const bool create_signaled=false); - -public: //FrameBuffer相关 - - Framebuffer *CreateFBO(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth); -// Framebuffer *CreateFBO(RenderPass *,List &color,ImageView *depth); - Framebuffer *CreateFBO(RenderPass *,ImageView *color,ImageView *depth); - Framebuffer *CreateFBO(RenderPass *,ImageView *); - -public: - - RenderTarget *CreateRT( const FramebufferInfo *fbi,RenderPass *,const uint32_t fence_count=1); - RenderTarget *CreateRT( const FramebufferInfo *fbi,const uint32_t fence_count=1); };//class GPUDevice VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_DEVICE_INCLUDE diff --git a/inc/hgl/graph/VKSwapchain.h b/inc/hgl/graph/VKSwapchain.h index 9a509193..6ccfdf5d 100644 --- a/inc/hgl/graph/VKSwapchain.h +++ b/inc/hgl/graph/VKSwapchain.h @@ -11,8 +11,6 @@ public: VkDevice device =VK_NULL_HANDLE; - VkExtent2D extent; - VkSwapchainKHR swap_chain =VK_NULL_HANDLE; uint32_t color_count =0; diff --git a/inc/hgl/graph/module/SwapchainModule.h b/inc/hgl/graph/module/SwapchainModule.h index 47a89919..0c6267f0 100644 --- a/inc/hgl/graph/module/SwapchainModule.h +++ b/inc/hgl/graph/module/SwapchainModule.h @@ -10,18 +10,25 @@ class SwapchainModule:public GraphModule RTSwapchain *swapchain_rt=nullptr; -private: +protected: + + bool CreateSwapchain(); + + bool CreateSwapchainRenderTarget(); bool CreateSwapchainFBO(Swapchain *); - Swapchain *CreateSwapchain(const VkExtent2D &acquire_extent); public: - GRAPH_MODULE_CONSTRUCT(Swapchain) + GRAPH_MODULE_CONSTRUCT(SwapchainModule) virtual ~SwapchainModule()=default; bool Init() override; -};//class SwapchainModule:public RenderModule + + RTSwapchain * GetRenderTarget () {return swapchain_rt;} + + const VkExtent2D & GetSwapchainSize()const {return swapchain_rt->GetExtent();} +};//class SwapchainModule:public GraphModule VK_NAMESPACE_END diff --git a/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp b/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp deleted file mode 100644 index ada8e4e4..00000000 --- a/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp +++ /dev/null @@ -1,157 +0,0 @@ -#include -#include -#include -#include -#include -#include - -VK_NAMESPACE_BEGIN -namespace -{ - //VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) - //{ - // VkExtent2D swapchain_extent; - - // swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); - // swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); - - // return swapchain_extent; - //} - - VkSwapchainKHR CreateSwapChain(const GPUDeviceAttribute *dev_attr,const VkExtent2D &extent) - { - VkSwapchainCreateInfoKHR swapchain_ci; - - swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; - swapchain_ci.pNext =nullptr; - swapchain_ci.flags =0; - swapchain_ci.surface =dev_attr->surface; - swapchain_ci.minImageCount =3;//rsa->surface_caps.minImageCount; - swapchain_ci.imageFormat =dev_attr->surface_format.format; - swapchain_ci.imageColorSpace =dev_attr->surface_format.colorSpace; - swapchain_ci.imageExtent =extent; - swapchain_ci.imageArrayLayers =1; - swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - swapchain_ci.queueFamilyIndexCount =0; - swapchain_ci.pQueueFamilyIndices =nullptr; - swapchain_ci.preTransform =dev_attr->preTransform; - swapchain_ci.compositeAlpha =dev_attr->compositeAlpha; - swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR; - swapchain_ci.clipped =VK_TRUE; - swapchain_ci.oldSwapchain =VK_NULL_HANDLE; - - if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT) - swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT; - - if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT) - swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT; - - uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family}; - if(dev_attr->graphics_family!=dev_attr->present_family) - { - // If the graphics and present queues are from different queue families, - // we either have to explicitly transfer ownership of images between - // the queues, or we have to create the swapchain with imageSharingMode - // as VK_SHARING_MODE_CONCURRENT - swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT; - swapchain_ci.queueFamilyIndexCount=2; - swapchain_ci.pQueueFamilyIndices=queueFamilyIndices; - } - else - { - swapchain_ci.imageSharingMode = VkSharingMode(SharingMode::Exclusive); - } - - VkSwapchainKHR swap_chain; - VkResult result; - - result=vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain); - - if(result!=VK_SUCCESS) - { - //LOG_ERROR(OS_TEXT("vkCreateSwapchainKHR failed, result = ")+OSString(result)); - os_err<<"vkCreateSwapchainKHR failed, result="<debug_utils) - dev_attr->debug_utils->SetSwapchainKHR(swap_chain,"SwapChain"); - #endif//_DEBUG - - return(swap_chain); - } -}//namespace - -bool SwapchainModule::CreateSwapchainFBO(Swapchain *swapchain) -{ - if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) - return(false); - - AutoDeleteArray sc_images(swapchain->color_count); - - if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) - return(false); - - swapchain->sc_depth =CreateTexture2D(new SwapchainDepthTextureCreateInfo(GetPhysicalDevice()->GetDepthFormat(),swapchain->extent)); - - if(!swapchain->sc_depth) - return(false); - - #ifdef _DEBUG - if(attr->debug_utils) - { - attr->debug_utils->SetImage(swapchain->sc_depth->GetImage(),"SwapchainDepthImage"); - attr->debug_utils->SetImageView(swapchain->sc_depth->GetVulkanImageView(),"SwapchainDepthImageView"); - attr->debug_utils->SetDeviceMemory(swapchain->sc_depth->GetDeviceMemory(),"SwapchainDepthMemory"); - } - #endif//_DEBUG - - swapchain->sc_color =hgl_zero_new(swapchain->color_count); - swapchain->sc_fbo =hgl_zero_new(swapchain->color_count); - - for(uint32_t i=0;icolor_count;i++) - { - swapchain->sc_color[i]=CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,sc_images[i])); - - if(!swapchain->sc_color[i]) - return(false); - - swapchain->sc_fbo[i]=CreateFBO( device_render_pass, - swapchain->sc_color[i]->GetImageView(), - swapchain->sc_depth->GetImageView()); - - #ifdef _DEBUG - if(attr->debug_utils) - { - attr->debug_utils->SetImage(swapchain->sc_color[i]->GetImage(),"SwapchainColorImage_"+AnsiString::numberOf(i)); - attr->debug_utils->SetImageView(swapchain->sc_color[i]->GetVulkanImageView(),"SwapchainColorImageView_"+AnsiString::numberOf(i)); - - attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i)); - } - #endif//_DEBUG - } - - return(true); -} - -Swapchain *SwapchainModule::CreateSwapchain(const VkExtent2D &acquire_extent) -{ - Swapchain *swapchain=new Swapchain; - - swapchain->device =GetVkDevice(); - swapchain->extent =acquire_extent; - - swapchain->swap_chain =CreateSwapChain(GetDeviceAttribute(),acquire_extent); - - if(swapchain->swap_chain) - if(CreateSwapchainFBO(swapchain)) - return(swapchain); - - delete swapchain; - swapchain=nullptr; - - return(nullptr); -} -VK_NAMESPACE_END diff --git a/src/SceneGraph/module/SwapchainModule.cpp b/src/SceneGraph/module/SwapchainModule.cpp index 87297d4c..d6e5ca4f 100644 --- a/src/SceneGraph/module/SwapchainModule.cpp +++ b/src/SceneGraph/module/SwapchainModule.cpp @@ -16,7 +16,7 @@ namespace // return swapchain_extent; //} - VkSwapchainKHR CreateSwapChain(const GPUDeviceAttribute *dev_attr) + VkSwapchainKHR CreateVulkanSwapChain(const GPUDeviceAttribute *dev_attr) { VkSwapchainCreateInfoKHR swapchain_ci; @@ -80,25 +80,68 @@ namespace return(swap_chain); } - - bool CreateSwapchainFBO(Swapchain *swapchain) - { - if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) - return(false); - - AutoDeleteArray sc_images(swapchain->color_count); - - if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) - return(false); - - swapchain->sc_depth=CreateTexture2D(new SwapchainDepthTextureCreateInfo(GetDeviceAttribute()->physical_device->GetDepthFormat(),swapchain->extent)); - - if(!swapchain->sc_depth) - return(false); - } }//namespace -bool SwapchainModule::Init() +bool SwapchainModule::CreateSwapchainFBO(Swapchain *swapchain) +{ + if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) + return(false); + + AutoDeleteArray sc_images(swapchain->color_count); + + if(vkGetSwapchainImagesKHR(swapchain->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) + return(false); + + TextureManager *tex_manager=GetModule(); + + if(!tex_manager) + return(false); + + swapchain->sc_depth =tex_manager->CreateTexture2D(new SwapchainDepthTextureCreateInfo(GetPhysicalDevice()->GetDepthFormat(),swapchain->extent)); + + if(!swapchain->sc_depth) + return(false); + + //#ifdef _DEBUG + // if(attr->debug_utils) + // { + // attr->debug_utils->SetImage(swapchain->sc_depth->GetImage(),"SwapchainDepthImage"); + // attr->debug_utils->SetImageView(swapchain->sc_depth->GetVulkanImageView(),"SwapchainDepthImageView"); + // attr->debug_utils->SetDeviceMemory(swapchain->sc_depth->GetDeviceMemory(),"SwapchainDepthMemory"); + // } + //#endif//_DEBUG + + swapchain->sc_color =hgl_zero_new(swapchain->color_count); + swapchain->sc_fbo =hgl_zero_new(swapchain->color_count); + + const auto *attr=GetDeviceAttribute(); + + for(uint32_t i=0;icolor_count;i++) + { + swapchain->sc_color[i]=tex_manager->CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,sc_images[i])); + + if(!swapchain->sc_color[i]) + return(false); + + swapchain->sc_fbo[i]=tex_manager->CreateFBO( device_render_pass, + swapchain->sc_color[i]->GetImageView(), + swapchain->sc_depth->GetImageView()); + + //#ifdef _DEBUG + // if(attr->debug_utils) + // { + // attr->debug_utils->SetImage(swapchain->sc_color[i]->GetImage(),"SwapchainColorImage_"+AnsiString::numberOf(i)); + // attr->debug_utils->SetImageView(swapchain->sc_color[i]->GetVulkanImageView(),"SwapchainColorImageView_"+AnsiString::numberOf(i)); + + // attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i)); + // } + //#endif//_DEBUG + } + + return(true); +} + +bool SwapchainModule::CreateSwapchain() { if(swapchain) return(false); @@ -112,7 +155,7 @@ bool SwapchainModule::Init() swapchain->device=dev_attr->device; - swapchain->swap_chain=CreateSwapChain(dev_attr); + swapchain->swap_chain=CreateVulkanSwapChain(dev_attr); if(swapchain->swap_chain) { @@ -125,4 +168,26 @@ bool SwapchainModule::Init() return(false); } +bool SwapchainModule::CreateSwapchainRenderTarget() +{ + if(!CreateSwapchain()) + return(false); + + GPUDevice *device=GetDevice(); + + DeviceQueue *q=device->CreateQueue(swapchain->color_count,false); + Semaphore *render_complete_semaphore=device->CreateGPUSemaphore(); + Semaphore *present_complete_semaphore=device->CreateGPUSemaphore(); + + swapchain_rt=new RTSwapchain( device->GetDevice(), + swapchain, + q, + render_complete_semaphore, + present_complete_semaphore, + device_render_pass + ); + + return true; +} + VK_NAMESPACE_END