2019-06-25 22:26:09 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKDevice.h>
|
2019-06-19 16:57:42 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKRenderPass.h>
|
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-16 19:25:27 +08:00
|
|
|
|
//void 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;
|
|
|
|
|
//}
|
2020-01-21 16:23:01 +08:00
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
void CreateSubpassDependency(List<VkSubpassDependency> &subpass_dependency_list,const uint32_t count)
|
2019-06-19 16:57:42 +08:00
|
|
|
|
{
|
2019-07-05 19:56:15 +08:00
|
|
|
|
if(count<=0)return;
|
|
|
|
|
|
|
|
|
|
subpass_dependency_list.SetCount(count);
|
|
|
|
|
|
|
|
|
|
VkSubpassDependency *dependency=subpass_dependency_list.GetData();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2019-07-02 21:09:20 +08:00
|
|
|
|
if(count==1)
|
2019-07-05 19:56:15 +08:00
|
|
|
|
{
|
|
|
|
|
dependency->dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
|
|
|
|
dependency->dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
2019-06-19 16:57:42 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
2019-07-02 20:37:30 +08:00
|
|
|
|
{
|
2019-07-05 19:56:15 +08:00
|
|
|
|
dependency->dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
|
dependency->dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
2019-07-02 20:37:30 +08:00
|
|
|
|
|
2019-07-05 19:56:15 +08:00
|
|
|
|
++dependency;
|
|
|
|
|
|
|
|
|
|
for(uint32_t i=1;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
dependency->srcSubpass = i-1;
|
|
|
|
|
dependency->srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
|
|
|
|
|
|
if(i==count-1)
|
2019-07-02 20:37:30 +08:00
|
|
|
|
{
|
2019-07-05 19:56:15 +08:00
|
|
|
|
dependency->dstSubpass = VK_SUBPASS_EXTERNAL;
|
|
|
|
|
dependency->dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
|
|
|
|
|
dependency->srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
|
dependency->dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
|
2019-07-02 20:37:30 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-07-05 19:56:15 +08:00
|
|
|
|
dependency->dstSubpass = i;
|
|
|
|
|
dependency->dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
|
|
|
|
|
dependency->srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
|
|
|
|
|
dependency->dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
2019-07-02 20:37:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 19:56:15 +08:00
|
|
|
|
dependency->dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
|
|
|
|
|
|
|
|
|
|
++dependency;
|
2019-07-02 20:37:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-02 21:09:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 12:27:30 +08:00
|
|
|
|
void CreateAttachmentReference(VkAttachmentReference *ref_list,uint start,uint count,VkImageLayout layout)
|
2019-07-02 21:09:20 +08:00
|
|
|
|
{
|
2020-01-21 16:23:01 +08:00
|
|
|
|
VkAttachmentReference *ref=ref_list;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
|
2020-10-16 12:27:30 +08:00
|
|
|
|
for(uint i=start;i<start+count;i++)
|
2020-01-21 16:23:01 +08:00
|
|
|
|
{
|
|
|
|
|
ref->attachment =i;
|
|
|
|
|
ref->layout =layout;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
|
2020-01-21 16:23:01 +08:00
|
|
|
|
++ref;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-02 21:09:20 +08:00
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
bool CreateAttachmentDescription(List<VkAttachmentDescription> &desc_list,const List<VkFormat> &color_format,const VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout)
|
2020-01-21 16:23:01 +08:00
|
|
|
|
{
|
|
|
|
|
const uint color_count=color_format.GetCount();
|
|
|
|
|
|
|
|
|
|
desc_list.SetCount(color_count+1);
|
|
|
|
|
VkAttachmentDescription *desc=desc_list.GetData();
|
|
|
|
|
|
|
|
|
|
for(uint i=0;i<color_count+1;i++)
|
2019-07-02 21:09:20 +08:00
|
|
|
|
{
|
2020-01-21 16:23:01 +08:00
|
|
|
|
desc->flags = 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;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 16:23:01 +08:00
|
|
|
|
desc=desc_list.GetData();
|
2019-07-02 21:09:20 +08:00
|
|
|
|
const VkFormat *cf=color_format.GetData();
|
2020-01-21 16:23:01 +08:00
|
|
|
|
for(uint i=0;i<color_count;i++)
|
2019-07-02 21:09:20 +08:00
|
|
|
|
{
|
2020-01-21 16:23:01 +08:00
|
|
|
|
desc->finalLayout = color_final_layout;
|
|
|
|
|
desc->format = *cf;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
|
2020-01-21 16:23:01 +08:00
|
|
|
|
++desc;
|
|
|
|
|
++cf;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 16:23:01 +08:00
|
|
|
|
desc->finalLayout = depth_final_layout;
|
|
|
|
|
desc->format = depth_format;
|
2020-10-18 18:43:02 +08:00
|
|
|
|
desc->storeOp = VK_ATTACHMENT_STORE_OP_STORE; //swapchainRT最后"可能"是不需要保存深度的,,,,想想怎么最后弄成"DONT CARE"
|
2019-07-02 21:09:20 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
bool CreateColorAttachment( List<VkAttachmentReference> &ref_list,List<VkAttachmentDescription> &desc_list,const List<VkFormat> &color_format,const VkImageLayout color_final_layout)
|
2019-07-02 21:39:24 +08:00
|
|
|
|
{
|
2020-10-15 22:13:15 +08:00
|
|
|
|
//const VkFormat *cf=color_format_list.GetData();
|
2019-07-02 21:39:24 +08:00
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
//for(int i=0;i<color_format_list.GetCount();i++)
|
|
|
|
|
//{
|
|
|
|
|
// if(!attr->physical_device->IsColorAttachmentOptimal(*cf))
|
|
|
|
|
// return(false);
|
2019-07-02 21:39:24 +08:00
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
// ++cf;
|
|
|
|
|
//}
|
2019-07-02 21:39:24 +08:00
|
|
|
|
|
|
|
|
|
ref_list.SetCount(color_format.GetCount());
|
|
|
|
|
VkAttachmentReference *ref=ref_list.GetData();
|
|
|
|
|
|
|
|
|
|
desc_list.SetCount(color_format.GetCount());
|
|
|
|
|
VkAttachmentDescription *desc=desc_list.GetData();
|
|
|
|
|
|
2019-07-03 19:45:39 +08:00
|
|
|
|
for(int i=0;i<color_format.GetCount();i++)
|
2019-07-02 21:39:24 +08:00
|
|
|
|
{
|
|
|
|
|
desc->flags = 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->finalLayout = color_final_layout;
|
|
|
|
|
++desc;
|
|
|
|
|
|
|
|
|
|
ref->attachment = i;
|
|
|
|
|
ref->layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
|
|
|
|
++ref;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
bool CreateDepthAttachment( List<VkAttachmentReference> &ref_list,List<VkAttachmentDescription> &desc_list,const VkFormat &depth_format,const VkImageLayout depth_final_layout)
|
2019-07-02 21:39:24 +08:00
|
|
|
|
{
|
2020-10-15 22:13:15 +08:00
|
|
|
|
//if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format))
|
|
|
|
|
// return(false);
|
2019-07-02 21:39:24 +08:00
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ref_list.SetCount(1);
|
|
|
|
|
VkAttachmentReference *ref=ref_list.GetData();
|
|
|
|
|
|
|
|
|
|
ref->attachment=0;
|
|
|
|
|
ref->layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
desc_list.SetCount(1);
|
|
|
|
|
VkAttachmentDescription *desc=desc_list.GetData();
|
|
|
|
|
|
|
|
|
|
desc->flags = 0;
|
|
|
|
|
desc->samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
desc->loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; //LOAD_OP_CLEAR代表LOAD时清空内容
|
2019-07-11 22:09:22 +08:00
|
|
|
|
desc->storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; //DONT CARE表示不在意
|
|
|
|
|
desc->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
2019-07-02 21:39:24 +08:00
|
|
|
|
desc->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
|
|
|
|
desc->initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //代表不关心初始布局
|
|
|
|
|
desc->finalLayout = depth_final_layout;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-02 21:09:20 +08:00
|
|
|
|
RenderPass *Device::CreateRenderPass( const List<VkAttachmentDescription> &desc_list,
|
|
|
|
|
const List<VkSubpassDescription> &subpass,
|
|
|
|
|
const List<VkSubpassDependency> &dependency,
|
2020-10-15 22:13:15 +08:00
|
|
|
|
const List<VkFormat> &color_format_list,
|
2019-07-02 21:09:20 +08:00
|
|
|
|
const VkFormat depth_format,
|
|
|
|
|
const VkImageLayout color_final_layout,
|
2020-10-15 22:13:15 +08:00
|
|
|
|
const VkImageLayout depth_final_layout)
|
2019-07-02 21:09:20 +08:00
|
|
|
|
{
|
2020-10-15 22:13:15 +08:00
|
|
|
|
for(const VkFormat cf:color_format_list)
|
2019-07-02 21:39:24 +08:00
|
|
|
|
{
|
2020-10-15 22:13:15 +08:00
|
|
|
|
if(!attr->physical_device->IsColorAttachmentOptimal(cf)
|
|
|
|
|
&&!attr->physical_device->IsColorAttachmentLinear(cf))
|
|
|
|
|
return(nullptr);
|
2019-07-02 21:39:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 19:44:00 +08:00
|
|
|
|
if(depth_format!=FMT_UNDEFINED)
|
|
|
|
|
{
|
|
|
|
|
if(!attr->physical_device->IsDepthAttachmentOptimal(depth_format)
|
|
|
|
|
&&!attr->physical_device->IsDepthAttachmentLinear(depth_format))
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2019-06-19 16:57:42 +08:00
|
|
|
|
|
|
|
|
|
VkRenderPassCreateInfo rp_info;
|
|
|
|
|
rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
|
|
|
rp_info.pNext = nullptr;
|
2019-07-08 20:53:07 +08:00
|
|
|
|
rp_info.flags = 0;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
rp_info.attachmentCount = desc_list.GetCount();
|
|
|
|
|
rp_info.pAttachments = desc_list.GetData();
|
2019-07-02 20:37:30 +08:00
|
|
|
|
rp_info.subpassCount = subpass.GetCount();
|
|
|
|
|
rp_info.pSubpasses = subpass.GetData();
|
2019-07-02 21:09:20 +08:00
|
|
|
|
rp_info.dependencyCount = dependency.GetCount();
|
|
|
|
|
rp_info.pDependencies = dependency.GetData();
|
2019-06-19 16:57:42 +08:00
|
|
|
|
|
|
|
|
|
VkRenderPass render_pass;
|
|
|
|
|
|
|
|
|
|
if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS)
|
|
|
|
|
return(nullptr);
|
|
|
|
|
|
2020-10-15 22:13:15 +08:00
|
|
|
|
return(new RenderPass(attr->device,render_pass,color_format_list,depth_format));
|
2019-06-19 16:57:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
RenderPass *Device::CreateRenderPass(const List<VkFormat> &color_format_list,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout)
|
2019-06-19 16:57:42 +08:00
|
|
|
|
{
|
2020-10-16 19:25:27 +08:00
|
|
|
|
for(const VkFormat &fmt:color_format_list)
|
|
|
|
|
if(!attr->physical_device->IsColorAttachmentOptimal(fmt))
|
|
|
|
|
return(nullptr);
|
2019-07-02 21:39:24 +08:00
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
List<VkAttachmentReference> color_ref_list;
|
2020-01-21 16:23:01 +08:00
|
|
|
|
VkAttachmentReference depth_ref;
|
2020-10-16 19:25:27 +08:00
|
|
|
|
List<VkAttachmentDescription> atta_desc_list;
|
2020-10-16 19:44:00 +08:00
|
|
|
|
List<VkSubpassDescription> subpass_desc_list;
|
|
|
|
|
List<VkSubpassDependency> subpass_dependency_list;
|
2019-07-02 21:09:20 +08:00
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
color_ref_list.SetCount(color_format_list.GetCount());
|
|
|
|
|
CreateColorAttachmentReference(color_ref_list.GetData(),0,color_format_list.GetCount());
|
2020-10-16 19:44:00 +08:00
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
CreateAttachmentDescription(atta_desc_list,color_format_list,depth_format,color_final_layout,depth_final_layout);
|
2019-07-02 20:37:30 +08:00
|
|
|
|
|
2020-10-16 19:44:00 +08:00
|
|
|
|
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()));
|
|
|
|
|
}
|
2019-06-19 16:57:42 +08:00
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
CreateSubpassDependency(subpass_dependency_list,2);
|
|
|
|
|
|
|
|
|
|
return CreateRenderPass(atta_desc_list,subpass_desc_list,subpass_dependency_list,color_format_list,depth_format,color_final_layout,depth_final_layout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout)
|
|
|
|
|
{
|
|
|
|
|
List<VkFormat> color_format_list;
|
|
|
|
|
|
|
|
|
|
color_format_list.Add(color_format);
|
2019-06-19 16:57:42 +08:00
|
|
|
|
|
2020-10-16 19:25:27 +08:00
|
|
|
|
return CreateRenderPass(color_format_list,depth_format,color_final_layout,depth_final_layout);
|
2019-06-19 16:57:42 +08:00
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|