From 924df77e1ffa1b6263426d536cbb18b4ebfccc7d Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 16 Oct 2020 19:44:00 +0800 Subject: [PATCH] new CreateRenderTarget functions. --- inc/hgl/graph/vulkan/VKDevice.h | 6 + .../Vulkan/VKDeviceRenderPass.cpp | 34 ++++-- .../Vulkan/VKDeviceRenderTarget.cpp | 112 +++++++----------- 3 files changed, 71 insertions(+), 81 deletions(-) diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 7ff8a98f..da24ea62 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -288,6 +288,12 @@ public: 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 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); + Pipeline *CreatePipeline(const InlinePipeline &,const Material *,const RenderTarget *); Pipeline *CreatePipeline( PipelineData *, const Material *,const RenderTarget *); diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp index ab08965b..0887d4d5 100644 --- a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp @@ -201,9 +201,12 @@ RenderPass *Device::CreateRenderPass( const List &des return(nullptr); } - if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format) - &&!attr->physical_device->IsDepthAttachmentLinear(depth_format)) - return(nullptr); + if(depth_format!=FMT_UNDEFINED) + { + if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format) + &&!attr->physical_device->IsDepthAttachmentLinear(depth_format)) + return(nullptr); + } VkRenderPassCreateInfo rp_info; rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; @@ -230,24 +233,29 @@ RenderPass *Device::CreateRenderPass(const List &color_format_list,VkF if(!attr->physical_device->IsColorAttachmentOptimal(fmt)) return(nullptr); - if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) - return(nullptr); - List color_ref_list; VkAttachmentReference depth_ref; List atta_desc_list; + List subpass_desc_list; + List subpass_dependency_list; color_ref_list.SetCount(color_format_list.GetCount()); CreateColorAttachmentReference(color_ref_list.GetData(),0,color_format_list.GetCount()); - CreateDepthAttachmentReference(&depth_ref,color_format_list.GetCount()); - + CreateAttachmentDescription(atta_desc_list,color_format_list,depth_format,color_final_layout,depth_final_layout); - List subpass_desc_list; - - subpass_desc_list.Add(SubpassDescription(color_ref_list.GetData(),color_ref_list.GetCount(),&depth_ref)); - - List subpass_dependency_list; + if(depth_format!=FMT_UNDEFINED) + { + if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) + return(nullptr); + + CreateDepthAttachmentReference(&depth_ref,color_format_list.GetCount()); + subpass_desc_list.Add(SubpassDescription(color_ref_list.GetData(),color_ref_list.GetCount(),&depth_ref)); + } + else + { + subpass_desc_list.Add(SubpassDescription(color_ref_list.GetData(),color_ref_list.GetCount())); + } CreateSubpassDependency(subpass_dependency_list,2); diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp index 19b49e9d..1d50e4cd 100644 --- a/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceRenderTarget.cpp @@ -8,6 +8,47 @@ RenderTarget *Device::CreateRenderTarget(Framebuffer *fb) return(new RenderTarget(this,fb,cb)); } +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) +{ + 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; + + for(const VkFormat &fmt:color_format_list) + { + Texture2D *color_texture=CreateAttachmentTextureColor(fmt,w,h); + + if(!color_texture) + { + delete rp; + return(nullptr); + } + + color_texture_list.Add(color_texture); + color_texture_image_view_list.Add(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()); + + if(!fb) + { + SAFE_CLEAR(depth_texture) + delete rp; + return nullptr; + } + + return(CreateRenderTarget(fb)); +} + RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, const VkFormat color_format, const VkFormat depth_format, @@ -16,75 +57,10 @@ RenderTarget *Device::CreateRenderTarget( const uint w,const uint h, { if(w<=0||h<=0)return(nullptr); - RenderPass *rp=CreateRenderPass(color_format,depth_format,color_layout,depth_layout); //Renderpass内部会验证格式,所以不需要自己处理 + List color_format_list; - if(!CheckTextureFormatSupport(color_format))return(nullptr); - if(!CheckTextureFormatSupport(depth_format))return(nullptr); + color_format_list.Add(color_format); - Texture2D *color_texture=CreateAttachmentTextureColor(color_format,w,h); - Texture2D *depth_texture=CreateAttachmentTextureDepth(depth_format,w,h); - - Framebuffer *fb=CreateFramebuffer(rp,color_texture->GetImageView(),depth_texture->GetImageView()); - - return(CreateRenderTarget(fb)); + return CreateRenderTarget(w,h,color_format_list,depth_format,color_layout,depth_layout); } - -//RenderTarget *Device::CreateRenderTarget(const uint w,const uint h,const List &fmt_list) -//{ -// if(w<=0||h<=0||fmt_list.GetCount()<=0)return(nullptr); -// -// uint color_count=0; -// uint depth_count=0; //只能有一个 -// uint stencil_count=0; -// -// for(VkFormat fmt:fmt_list) -// { -// if(IsDepthFormat(fmt))++depth_count; -// else -// if(IsStencilFormat(fmt))++stencil_count; -// else -// ++color_count; -// -// if(CheckTextureFormatSupport(fmt)) -// return(nullptr); -// } -// -// if(depth_count>1)return(nullptr); -// if(stencil_count>1)return(nullptr); -// -// List color_format_list; -// VkFormat depth_format; -// List desc_list; -// List color_ref_list; -// VkAttachmentReference depth_ref; -// List image_view_list; -// -// for(VkFormat fmt:fmt_list) -// { -// Texture2D *tex=nullptr; -// -// if(IsDepthFormat(fmt)) -// { -// tex=CreateAttachmentTextureDepth(fmt,w,h); -// -// depth_format=fmt; -// } -// else -// { -// tex=CreateAttachmentTextureColor(fmt,w,h); -// -// image_view_list.Add(tex->GetImageView()); -// color_format_list.Add(fmt); -// } -// } -// -// if(depth_count>0)CreateDepthAttachmentReference(&depth_ref,color_count); -// if(color_count>0)CreateColorAttachmentReference(color_ref_list,0,color_count); -// -// CreateAttachmentDescription(desc_list,color_format_list,depth_format,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); -// -// VkSubpassDescription sd; -// -// CreateSubpassDescription(sd,color_ref_list,&depth_ref); -//} VK_NAMESPACE_END \ No newline at end of file