From b9249d960ccccc41d371718e76e2e2e5d4bc956d Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 21 Jan 2020 16:23:01 +0800 Subject: [PATCH] Split vulkan::Device::CreateAttachmentDescription to a new function. --- example/Vulkan/DeferredModel.cpp | 13 ++- inc/hgl/graph/vulkan/VKDevice.h | 12 +- res/shader/VertexNormal.frag | 9 ++ .../Vulkan/VKDeviceRenderPass.cpp | 106 ++++++++++-------- 4 files changed, 88 insertions(+), 52 deletions(-) create mode 100644 res/shader/VertexNormal.frag diff --git a/example/Vulkan/DeferredModel.cpp b/example/Vulkan/DeferredModel.cpp index f900b431..7826ccbe 100644 --- a/example/Vulkan/DeferredModel.cpp +++ b/example/Vulkan/DeferredModel.cpp @@ -70,7 +70,8 @@ private: struct { List desc_list; - List ref_list; + List color_ref_list; + VkAttachmentReference depth_ref; }attachment; struct @@ -186,15 +187,19 @@ private: gbuffer.image_view_list.Add(gbuffer.texture_list[i]->GetImageView()); } - if(!device->CreateAttachment( gbuffer.attachment.ref_list, - gbuffer.attachment.desc_list, + device->CreateColorAttachmentReference(gbuffer.attachment.color_ref_list,0,3); + device->CreateDepthAttachmentReference(&gbuffer.attachment.depth_ref,3); + + if(!device->CreateAttachment( gbuffer.attachment.desc_list, gbuffer.gbuffer_format_list, gbuffer.depth->GetFormat())) return(false); VkSubpassDescription desc; - device->CreateSubpassDescription(desc,gbuffer.attachment.ref_list); + device->CreateSubpassDescription(desc, + gbuffer.attachment.color_ref_list, + &gbuffer.attachment.depth_ref); gbuffer.subpass.desc.Add(desc); diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 1fa4e3d6..8a8e102b 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -176,8 +176,13 @@ public: //Command Buffer 相关 CommandBuffer * CreateCommandBuffer(const VkExtent2D &extent,const uint32_t atta_count); - bool CreateAttachment( List &ref_list, - List &desc_list, + void CreateAttachmentReference(VkAttachmentReference *ref_list,uint start,uint count,VkImageLayout layout)const; + + void CreateColorAttachmentReference(List &ref_list, uint start,uint count )const{ref_list.SetCount(count); CreateAttachmentReference(ref_list.GetData(), start,count,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);} + void CreateDepthAttachmentReference( VkAttachmentReference *depth_ref, uint index )const{ CreateAttachmentReference(depth_ref, index,1 ,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);} + void CreateInputAttachment( List &ref_list, uint start,uint count )const{ref_list.SetCount(count); CreateAttachmentReference(ref_list.GetData(), start,count,VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);} + + bool CreateAttachment( List &color_output_desc_list, const List &color_format, const VkFormat depth_format, const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, @@ -186,8 +191,9 @@ public: //Command Buffer 相关 bool CreateColorAttachment( List &ref_list,List &desc_list,const List &color_format,const VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL)const; bool CreateDepthAttachment( List &ref_list,List &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)const; + void CreateSubpassDependency(VkSubpassDependency *); void CreateSubpassDependency(List &dependency,const uint32_t count)const; - void CreateSubpassDescription(VkSubpassDescription &,const List &)const; + void CreateSubpassDescription(VkSubpassDescription &,const List &color_ref_list,VkAttachmentReference *depth_ref=nullptr)const; RenderPass * CreateRenderPass( const List &desc_list, const List &subpass, diff --git a/res/shader/VertexNormal.frag b/res/shader/VertexNormal.frag new file mode 100644 index 00000000..a516dcd6 --- /dev/null +++ b/res/shader/VertexNormal.frag @@ -0,0 +1,9 @@ +#version 450 core + +layout(location = 0) in vec4 FragmentNormal; +layout(location = 0) out vec4 FragColor; + +void main() +{ + FragColor=FragmentNormal; +} diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp index 55642c33..c707ffb5 100644 --- a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp @@ -3,6 +3,17 @@ #include VK_NAMESPACE_BEGIN +void Device::CreateSubpassDependency(VkSubpassDependency *dependency) +{ + dependency->srcSubpass = VK_SUBPASS_EXTERNAL; + dependency->dstSubpass = 0; + dependency->srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; + dependency->srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; + dependency->dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; + dependency->dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; + dependency->dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; +} + void Device::CreateSubpassDependency(List &subpass_dependency_list,const uint32_t count) const { if(count<=0)return; @@ -56,45 +67,54 @@ void Device::CreateSubpassDependency(List &subpass_dependen } } -bool Device::CreateAttachment(List &ref_list,List &desc_list,const List &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) const +void Device::CreateAttachmentReference(VkAttachmentReference *ref_list,uint start,uint count,VkImageLayout layout) const { - uint atta_count=color_format.GetCount(); + VkAttachmentReference *ref=ref_list; - desc_list.SetCount(atta_count+1); + for(uint i=start;iattachment =i; + ref->layout =layout; + + ++ref; + } +} + +bool Device::CreateAttachment(List &desc_list,const List &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout) const +{ + const uint color_count=color_format.GetCount(); + + desc_list.SetCount(color_count+1); VkAttachmentDescription *desc=desc_list.GetData(); - ref_list.SetCount(atta_count+1); - VkAttachmentReference *ref=ref_list.GetData(); - - for(uint i=0;iflags = 0; + desc->samples = VK_SAMPLE_COUNT_1_BIT; + desc->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; //LOAD_OP_CLEAR代表LOAD时清空内容 + desc->storeOp = VK_ATTACHMENT_STORE_OP_STORE; //STORE_OP_STROE代表SOTRE时储存内容 + desc->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; //DONT CARE表示不在意 + desc->stencilStoreOp= VK_ATTACHMENT_STORE_OP_DONT_CARE; + desc->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //代表不关心初始布局 + + ++desc; } + desc=desc_list.GetData(); const VkFormat *cf=color_format.GetData(); - for(uint i=0;ifinalLayout = color_final_layout; + desc->format = *cf; - ref[i].attachment = i; - ref[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + ++desc; + ++cf; } - desc[atta_count].finalLayout = depth_final_layout; - desc[atta_count].format = depth_format; - desc[atta_count].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; //深度缓冲区不用于显示也不用于下一帧,所以结束后不用保存 + desc->finalLayout = depth_final_layout; + desc->format = depth_format; + desc->storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; //深度缓冲区不用于显示也不用于下一帧,所以结束后不用保存 - ref[atta_count].attachment = atta_count; - ref[atta_count].layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - return(true); } @@ -166,26 +186,18 @@ bool Device::CreateDepthAttachment( List &ref_list,List &ref_list) const +void Device::CreateSubpassDescription(VkSubpassDescription &sd,const List &color_ref_list,VkAttachmentReference *depth_ref) const { - const VkAttachmentReference *end_ref=ref_list.GetPointer(ref_list.GetCount()-1); - - if(end_ref->layout==VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) //最后一个是深度 - { - sd.colorAttachmentCount =ref_list.GetCount()-1; - sd.pDepthStencilAttachment =end_ref; - } - else - { - sd.colorAttachmentCount =ref_list.GetCount(); - sd.pDepthStencilAttachment =nullptr; - } - sd.flags =0; sd.pipelineBindPoint =VK_PIPELINE_BIND_POINT_GRAPHICS; sd.inputAttachmentCount =0; sd.pInputAttachments =nullptr; - sd.pColorAttachments =ref_list.GetData(); + + sd.pColorAttachments =color_ref_list.GetData(); + sd.colorAttachmentCount =color_ref_list.GetCount(); + + sd.pDepthStencilAttachment =depth_ref; + sd.pResolveAttachments =nullptr; sd.preserveAttachmentCount =0; sd.pPreserveAttachments =nullptr; @@ -243,14 +255,18 @@ RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)) return(false); - List ref_list; + List color_ref; + VkAttachmentReference depth_ref; List desc_list; List color_format_list; color_format_list.Add(color_format); - CreateAttachment(ref_list,desc_list,color_format_list,depth_format,color_final_layout,depth_final_layout); + CreateColorAttachmentReference(color_ref,0,1); + CreateDepthAttachmentReference(&depth_ref,1); + + CreateAttachment(desc_list,color_format_list,depth_format,color_final_layout,depth_final_layout); List subpass_desc_list; @@ -261,9 +277,9 @@ RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format subpass.inputAttachmentCount = 0; subpass.pInputAttachments = nullptr; subpass.colorAttachmentCount = 1; - subpass.pColorAttachments = ref_list.GetData(); + subpass.pColorAttachments = color_ref.GetData(); subpass.pResolveAttachments = nullptr; - subpass.pDepthStencilAttachment = ref_list.GetData()+1; + subpass.pDepthStencilAttachment = &depth_ref; subpass.preserveAttachmentCount = 0; subpass.pPreserveAttachments = nullptr;