From 8fc19e802d039cb8cc98fedfc4f86a78fa1c3938 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 17 Oct 2020 14:20:49 +0800 Subject: [PATCH] add new create function of Framebuffer --- CMCore | 2 +- example/common/VulkanAppFramework.h | 4 +- inc/hgl/graph/vulkan/VKDevice.h | 31 ++++----- inc/hgl/graph/vulkan/VKRenderTarget.h | 57 ++++++++-------- .../Vulkan/VKDeviceFramebuffer.cpp | 66 +++++++++++-------- .../Vulkan/VKDeviceRenderTarget.cpp | 66 ++++++++++++++----- 6 files changed, 136 insertions(+), 90 deletions(-) diff --git a/CMCore b/CMCore index 0c045fb8..1f1d1b80 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 0c045fb8b1ce2588876c433fbd72e457bd23d2df +Subproject commit 1f1d1b80350a40bfeaf50ab6cf8c6be8a613c374 diff --git a/example/common/VulkanAppFramework.h b/example/common/VulkanAppFramework.h index 675ff1c0..caad09a3 100644 --- a/example/common/VulkanAppFramework.h +++ b/example/common/VulkanAppFramework.h @@ -236,7 +236,7 @@ public: int AcquireNextImage() { - return sc_render_target->AcquireNextImage(present_complete_semaphore); + return sc_render_target->AcquireNextImage(*present_complete_semaphore); } virtual void SubmitDraw(int index) @@ -244,7 +244,7 @@ public: VkCommandBuffer cb=*cmd_buf[index]; sc_render_target->Submit(cb,present_complete_semaphore,render_complete_semaphore); - sc_render_target->PresentBackbuffer(render_complete_semaphore); + sc_render_target->PresentBackbuffer(*render_complete_semaphore); sc_render_target->Wait(); } diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index da24ea62..0d5d9a89 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -266,7 +266,7 @@ public: //Command Buffer 相关 public: //FrameBuffer相关 Framebuffer *CreateFramebuffer(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth); - Framebuffer *CreateFramebuffer(RenderPass *,List &color,ImageView *depth); +// Framebuffer *CreateFramebuffer(RenderPass *,List &color,ImageView *depth); Framebuffer *CreateFramebuffer(RenderPass *,ImageView *color,ImageView *depth); Framebuffer *CreateFramebuffer(RenderPass *,ImageView *); @@ -274,25 +274,26 @@ public: bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列 - RenderTarget *CreateRenderTarget(Framebuffer *); - - RenderTarget *CreateRenderTarget( const uint,const uint,const List &, - const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); - - RenderTarget *CreateRenderTarget( const uint,const uint,const VkFormat,const VkImageLayout final_layout); - - RenderTarget *CreateRenderTarget( const uint,const uint, - const VkFormat color_format, - const VkFormat depth_format, - const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + RenderTarget *CreateRenderTarget( Framebuffer *,const uint32_t fence_count=1); RenderTarget *CreateRenderTarget( const uint,const uint, const List &color_format_list, const VkFormat depth_format, const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, - const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + const uint32_t fence_count=1); + + RenderTarget *CreateRenderTarget( const uint,const uint, + const VkFormat color_format, + const VkFormat depth_format, + const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, + const uint32_t fence_count=1); + + RenderTarget *CreateRenderTarget( const uint,const uint,const VkFormat,const VkImageLayout final_layout,const uint32_t fence_count=1); + + +public: Pipeline *CreatePipeline(const InlinePipeline &,const Material *,const RenderTarget *); Pipeline *CreatePipeline( PipelineData *, const Material *,const RenderTarget *); diff --git a/inc/hgl/graph/vulkan/VKRenderTarget.h b/inc/hgl/graph/vulkan/VKRenderTarget.h index 2278dfd2..24a8929a 100644 --- a/inc/hgl/graph/vulkan/VKRenderTarget.h +++ b/inc/hgl/graph/vulkan/VKRenderTarget.h @@ -2,33 +2,11 @@ #define HGL_GRAPH_VULKAN_RENDER_TARGET_INCLUDE #include -#include #include #include #include +#include VK_NAMESPACE_BEGIN -class SubmitQueue -{ -protected: - - Device *device; - VkQueue queue; - - uint32_t current_fence; - ObjectList fence_list; - - SubmitInfo submit_info; - -public: - - SubmitQueue(Device *dev,VkQueue q,const uint32_t fence_count=1); - virtual ~SubmitQueue(); - - bool Wait(const bool wait_wall=true,const uint64_t time_out=HGL_NANO_SEC_PER_SEC); - bool Submit(const VkCommandBuffer &cmd_buf,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem); - bool Submit(const VkCommandBuffer *cmd_buf,const uint32_t count,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem); -};//class SumbitQueue - /** * 渲染目标 */ @@ -42,11 +20,17 @@ protected: CommandBuffer *command_buffer; +protected: + + ObjectList color_texture; + Texture2D *depth_texture; + protected: friend class Device; RenderTarget(Device *dev,Framebuffer *_fb,CommandBuffer *_cb,const uint32_t fence_count=1); + RenderTarget(Device *dev,Framebuffer *_fb,CommandBuffer *_cb,Texture2D **color_texture_list,const uint32_t color_count,Texture2D *depth_texture,const uint32_t fence_count=1); public: @@ -57,6 +41,9 @@ public: virtual const VkRenderPass GetRenderPass ()const {return fb->GetRenderPass();} virtual const uint32_t GetColorCount ()const {return fb->GetColorCount();} virtual const VkFramebuffer GetFramebuffer ()const {return fb->GetFramebuffer();} + + virtual Texture2D * GetColorTexture (const int index=0){return color_texture[index];} + virtual Texture2D * GetDepthTexture (){return depth_texture;} };//class RenderTarget /** @@ -80,14 +67,17 @@ public: SwapchainRenderTarget(Device *dev,Swapchain *sc); ~SwapchainRenderTarget(); - const VkRenderPass GetRenderPass()const override{return *main_rp;} - const VkFramebuffer GetFramebuffer()const override{return render_frame[current_frame]->GetFramebuffer();} - VkFramebuffer GetFramebuffer(const uint32_t index){return render_frame[index]->GetFramebuffer();} + const VkRenderPass GetRenderPass ()const override {return *main_rp;} + const VkFramebuffer GetFramebuffer ()const override {return render_frame[current_frame]->GetFramebuffer();} + VkFramebuffer GetFramebuffer (const uint32_t index) {return render_frame[index]->GetFramebuffer();} - const uint32_t GetColorCount()const override{return 1;} - const uint32_t GetImageCount()const{return swap_chain_count;} + const uint32_t GetColorCount ()const override {return 1;} + const uint32_t GetImageCount ()const {return swap_chain_count;} - const uint32_t GetCurrentFrameIndices()const{return current_frame;} + const uint32_t GetCurrentFrameIndices()const{return current_frame;} + + virtual Texture2D * GetColorTexture(const int index=0) override{return swapchain->GetColorTexture(index);} + virtual Texture2D * GetDepthTexture() override{return swapchain->GetDepthTexture();} public: @@ -95,13 +85,18 @@ public: * 请求下一帧画面的索引 * @param present_complete_semaphore 推送完成信号 */ - int AcquireNextImage(vulkan::Semaphore *present_complete_semaphore); + int AcquireNextImage(VkSemaphore present_complete_semaphore); /** * 推送后台画面到前台 * @param render_complete_semaphore 渲染完成信号 */ - bool PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore); + bool PresentBackbuffer(VkSemaphore *render_complete_semaphore,const uint32_t count); + + bool PresentBackbuffer(VkSemaphore render_complete_semaphore) + { + return PresentBackbuffer(&render_complete_semaphore,1); + } };//class SwapchainRenderTarget:public RenderTarget VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_RENDER_TARGET_INCLUDE diff --git a/src/RenderDevice/Vulkan/VKDeviceFramebuffer.cpp b/src/RenderDevice/Vulkan/VKDeviceFramebuffer.cpp index bf4bdff5..23c2345b 100644 --- a/src/RenderDevice/Vulkan/VKDeviceFramebuffer.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceFramebuffer.cpp @@ -1,13 +1,33 @@ #include VK_NAMESPACE_BEGIN +VkFramebuffer CreateVulkanFramebuffer(VkDevice device,RenderPass *rp,const VkExtent2D &extent,VkImageView *attachments,const uint attachmentCount) +{ + FramebufferCreateInfo fb_info; + + fb_info.renderPass = *rp; + fb_info.attachmentCount = attachmentCount; + fb_info.pAttachments = attachments; + fb_info.width = extent.width; + fb_info.height = extent.height; + fb_info.layers = 1; + + VkFramebuffer fb; + + if(vkCreateFramebuffer(device,&fb_info,nullptr,&fb)!=VK_SUCCESS) + return(nullptr); + + return fb; +} + Framebuffer *Device::CreateFramebuffer(RenderPass *rp,ImageView **color_list,const uint color_count,ImageView *depth) -{ +{ uint att_count=color_count; if(depth)++att_count; - VkImageView *attachments=new VkImageView[att_count]; + AutoDeleteArray attachments=new VkImageView[att_count]; + VkImageView *ap=attachments; if(color_count) { @@ -15,6 +35,7 @@ Framebuffer *Device::CreateFramebuffer(RenderPass *rp,ImageView **color_list,con const VkFormat *cf=cf_list.GetData(); ImageView **iv=color_list; + for(uint i=0;iGetFormat()) @@ -38,35 +59,28 @@ Framebuffer *Device::CreateFramebuffer(RenderPass *rp,ImageView **color_list,con attachments[color_count]=*depth; } - const VkExtent3D extent=depth->GetExtent(); + VkExtent2D extent; + extent.width=depth->GetExtent().width; + extent.height=depth->GetExtent().height; - FramebufferCreateInfo *fb_info=new FramebufferCreateInfo; + VkFramebuffer fbo=CreateVulkanFramebuffer(GetDevice(),rp,extent,attachments,att_count); - fb_info->renderPass = *rp; - fb_info->attachmentCount = att_count; - fb_info->pAttachments = attachments; - fb_info->width = extent.width; - fb_info->height = extent.height; - fb_info->layers = 1; - - VkFramebuffer fb; - - if(vkCreateFramebuffer(GetDevice(),fb_info,nullptr,&fb)!=VK_SUCCESS) + if(!fbo) return(nullptr); - return(new Framebuffer(GetDevice(),fb,fb_info,depth)); -} - -Framebuffer *Device::CreateFramebuffer(RenderPass *rp,List &color,ImageView *depth) -{ - if(!rp)return(nullptr); - - if(rp->GetColorFormat().GetCount()!=color.GetCount())return(nullptr); - - if(color.GetCount()==0&&!depth)return(nullptr); - - return CreateFramebuffer(rp,color.GetData(),color.GetCount(),depth); + return(new Framebuffer(GetDevice(),fbo,extent,*rp,color_count,depth)); } +// +//Framebuffer *Device::CreateFramebuffer(RenderPass *rp,List &color,ImageView *depth) +//{ +// if(!rp)return(nullptr); +// +// if(rp->GetColorFormat().GetCount()!=color.GetCount())return(nullptr); +// +// if(color.GetCount()==0&&!depth)return(nullptr); +// +// return CreateFramebuffer(rp,color.GetData(),color.GetCount(),depth); +//} Framebuffer *Device::CreateFramebuffer(RenderPass *rp,ImageView *color,ImageView *depth) { diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp index 1d50e4cd..f9a15049 100644 --- a/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp @@ -1,25 +1,33 @@ #include VK_NAMESPACE_BEGIN -RenderTarget *Device::CreateRenderTarget(Framebuffer *fb) +RenderTarget *Device::CreateRenderTarget(Framebuffer *fb,const uint32_t fence_count) { CommandBuffer *cb=CreateCommandBuffer(fb->GetExtent(),fb->GetAttachmentCount()); - return(new RenderTarget(this,fb,cb)); + return(new RenderTarget(this,fb,cb,fence_count)); } RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, const List &color_format_list, const VkFormat depth_format, const VkImageLayout color_layout, - const VkImageLayout depth_layout) + const VkImageLayout depth_layout, + const uint32_t fence_count) { if(w<=0||h<=0)return(nullptr); RenderPass *rp=CreateRenderPass(color_format_list,depth_format,color_layout,depth_layout); //Renderpass内部会验证格式,所以不需要自己处理 - ObjectList color_texture_list; - List color_texture_image_view_list; + if(!rp)return(nullptr); + + const uint32_t color_count=color_format_list.GetCount(); + + AutoDeleteObjectArray color_texture_list(color_count); + AutoDeleteArray color_iv_list=new ImageView *[color_count]; //iv只是从Texture2D中取出来的,无需一个个delete + + Texture2D **tp=color_texture_list; + ImageView **iv=color_iv_list; for(const VkFormat &fmt:color_format_list) { @@ -31,29 +39,37 @@ RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, return(nullptr); } - color_texture_list.Add(color_texture); - color_texture_image_view_list.Add(color_texture->GetImageView()); + *tp++=color_texture; + *iv++=color_texture->GetImageView(); } Texture2D *depth_texture=(depth_format!=FMT_UNDEFINED)?CreateAttachmentTextureDepth(depth_format,w,h):nullptr; - Framebuffer *fb=CreateFramebuffer(rp,color_texture_image_view_list,depth_texture->GetImageView()); + Framebuffer *fb=CreateFramebuffer(rp,color_iv_list,color_count,depth_texture?depth_texture->GetImageView():nullptr); - if(!fb) + if(fb) { - SAFE_CLEAR(depth_texture) - delete rp; - return nullptr; + CommandBuffer *cb=CreateCommandBuffer(fb->GetExtent(),fb->GetAttachmentCount()); + + if(cb) + { + color_texture_list.DiscardObject(); + + return(new RenderTarget(this,fb,cb,color_texture_list,color_count,depth_texture,fence_count)); + } } - return(CreateRenderTarget(fb)); + SAFE_CLEAR(depth_texture); + delete rp; + return nullptr; } RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, const VkFormat color_format, const VkFormat depth_format, const VkImageLayout color_layout, - const VkImageLayout depth_layout) + const VkImageLayout depth_layout, + const uint32_t fence_count) { if(w<=0||h<=0)return(nullptr); @@ -61,6 +77,26 @@ RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, color_format_list.Add(color_format); - return CreateRenderTarget(w,h,color_format_list,depth_format,color_layout,depth_layout); + return CreateRenderTarget(w,h,color_format_list,depth_format,color_layout,depth_layout,fence_count); +} + +RenderTarget *Device::CreateRenderTarget(const uint w,const uint h,const VkFormat format,const VkImageLayout final_layout,const uint32_t fence_count) +{ + if(w<=0||h<=0)return(nullptr); + + if(format==FMT_UNDEFINED)return(nullptr); + + List color_format_list; + + if(IsDepthFormat(format)) + { + return CreateRenderTarget(w,h,color_format_list,format,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,final_layout,fence_count); + } + else + { + color_format_list.Add(format); + + return CreateRenderTarget(w,h,color_format_list,FMT_UNDEFINED,final_layout,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,fence_count); + } } VK_NAMESPACE_END \ No newline at end of file