diff --git a/inc/hgl/graph/VKRenderTarget.h b/inc/hgl/graph/VKRenderTarget.h index fbd933f7..f09a9ab4 100644 --- a/inc/hgl/graph/VKRenderTarget.h +++ b/inc/hgl/graph/VKRenderTarget.h @@ -26,9 +26,9 @@ protected: protected: - uint32_t color_count; - Texture2D **color_textures; - Texture2D *depth_texture; + uint32_t color_count; ///<颜色成分数量 + Texture2D **color_textures; ///<颜色成分纹理列表 + Texture2D *depth_texture; ///<深度成分纹理 protected: @@ -45,9 +45,10 @@ public: const VkExtent2D & GetExtent ()const {return extent;} virtual RenderPass * GetRenderPass () {return render_pass;} virtual const VkRenderPass GetVkRenderPass ()const {return render_pass->GetVkRenderPass();} - virtual const uint32_t GetColorCount ()const {return fbo->GetColorCount();} - virtual Framebuffer * GetFramebuffer () {return fbo;} + virtual const uint32_t GetColorCount ()const {return fbo->GetColorCount();} + + virtual Framebuffer * GetFramebuffer () {return fbo;} virtual Texture2D * GetColorTexture (const int index=0){return color_textures[index];} virtual Texture2D * GetDepthTexture (){return depth_texture;} @@ -78,14 +79,15 @@ 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];} + const uint32_t GetColorCount ()const override {return 1;} ///Swapchain的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;} + Framebuffer * GetFramebuffer ()override {return swapchain->sc_image[current_frame].fbo;} + Framebuffer * GetFramebuffer (const int index) {return swapchain->sc_image[index].fbo;} + + 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: diff --git a/inc/hgl/graph/VKSwapchain.h b/inc/hgl/graph/VKSwapchain.h index 9a509193..451bf005 100644 --- a/inc/hgl/graph/VKSwapchain.h +++ b/inc/hgl/graph/VKSwapchain.h @@ -1,30 +1,47 @@ -#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE -#define HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE +#pragma once #include #include #include +#include VK_NAMESPACE_BEGIN + +struct SwapchainImage +{ + Texture2D * color =nullptr; + Texture2D * depth =nullptr; + + Framebuffer * fbo =nullptr; + +public: + + ~SwapchainImage() + { + delete fbo; + delete depth; + delete color; + } +};//struct SwapchainImage + struct Swapchain { public: - VkDevice device =VK_NULL_HANDLE; + VkDevice device =VK_NULL_HANDLE; - VkExtent2D extent; + VkExtent2D extent; + VkSurfaceTransformFlagBitsKHR transform; - VkSwapchainKHR swap_chain =VK_NULL_HANDLE; + VkSwapchainKHR swap_chain =VK_NULL_HANDLE; + VkSurfaceFormatKHR surface_format; + VkFormat depth_format; - uint32_t color_count =0; + uint32_t image_count =0; - Texture2D ** sc_color =nullptr; - Texture2D * sc_depth =nullptr; - - Framebuffer ** sc_fbo =nullptr; + SwapchainImage * sc_image =nullptr; public: virtual ~Swapchain(); };//struct Swapchain VK_NAMESPACE_END -#endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE diff --git a/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp b/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp index d7df9916..a71b52f4 100644 --- a/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceRenderTarget.cpp @@ -64,7 +64,7 @@ RTSwapchain *GPUDevice::CreateSwapchainRenderTarget() if(!sc) return(nullptr); - DeviceQueue *q=CreateQueue(sc->color_count,false); + DeviceQueue *q=CreateQueue(sc->image_count,false); Semaphore *render_complete_semaphore=CreateGPUSemaphore(); Semaphore *present_complete_semaphore=CreateGPUSemaphore(); diff --git a/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp b/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp index c0686a23..1f760a47 100644 --- a/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp +++ b/src/SceneGraph/Vulkan/VKDeviceSwapchain.cpp @@ -84,49 +84,41 @@ namespace bool GPUDevice::CreateSwapchainFBO(Swapchain *swapchain) { - if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS) + if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->image_count),nullptr)!=VK_SUCCESS) return(false); - AutoDeleteArray sc_images(swapchain->color_count); + AutoDeleteArray sc_images(swapchain->image_count); - if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS) + if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->image_count),sc_images)!=VK_SUCCESS) return(false); - swapchain->sc_depth =CreateTexture2D(new SwapchainDepthTextureCreateInfo(attr->physical_device->GetDepthFormat(),swapchain->extent)); + swapchain->sc_image=hgl_zero_new(swapchain->image_count); - 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++) + for(uint32_t i=0;iimage_count;i++) { - swapchain->sc_color[i]=CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,sc_images[i])); + swapchain->sc_image[i].color=CreateTexture2D(new SwapchainColorTextureCreateInfo(swapchain->surface_format.format,swapchain->extent,sc_images[i])); - if(!swapchain->sc_color[i]) + if(!swapchain->sc_image[i].color) return(false); - swapchain->sc_fbo[i]=CreateFBO( device_render_pass, - swapchain->sc_color[i]->GetImageView(), - swapchain->sc_depth->GetImageView()); + swapchain->sc_image[i].depth =CreateTexture2D(new SwapchainDepthTextureCreateInfo(swapchain->depth_format,swapchain->extent)); + + if(!swapchain->sc_image[i].depth) + return(false); + + swapchain->sc_image[i].fbo=CreateFBO( device_render_pass, + swapchain->sc_image[i].color->GetImageView(), + swapchain->sc_image[i].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)); + AnsiString num=AnsiString::numberOf(i); - attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i)); + attr->debug_utils->SetTexture(swapchain->sc_image[i].color,"SwapchainColor_"+num); + attr->debug_utils->SetTexture(swapchain->sc_image[i].depth,"SwapchainDepth_"+num); + + attr->debug_utils->SetFramebuffer(swapchain->sc_image[i].fbo->GetFramebuffer(),"SwapchainFBO_"+num); } #endif//_DEBUG } @@ -138,10 +130,13 @@ Swapchain *GPUDevice::CreateSwapchain(const VkExtent2D &acquire_extent) { Swapchain *swapchain=new Swapchain; - swapchain->device =attr->device; - swapchain->extent =acquire_extent; + swapchain->device =attr->device; + swapchain->extent =acquire_extent; + swapchain->transform =attr->surface_caps.currentTransform; + swapchain->surface_format =attr->surface_format; + swapchain->depth_format =attr->physical_device->GetDepthFormat(); - swapchain->swap_chain =CreateSwapChain(attr,acquire_extent); + swapchain->swap_chain =CreateSwapChain(attr,acquire_extent); if(swapchain->swap_chain) if(CreateSwapchainFBO(swapchain)) diff --git a/src/SceneGraph/Vulkan/VKSwapchain.cpp b/src/SceneGraph/Vulkan/VKSwapchain.cpp index f0286eba..8bdb4dbc 100644 --- a/src/SceneGraph/Vulkan/VKSwapchain.cpp +++ b/src/SceneGraph/Vulkan/VKSwapchain.cpp @@ -4,9 +4,7 @@ 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 +12,6 @@ Swapchain::~Swapchain() swap_chain=VK_NULL_HANDLE; } - color_count=0; + image_count=0; } VK_NAMESPACE_END