将Device::RenderPass创建代码迁移到独立的.cpp中

This commit is contained in:
hyzboy 2019-06-19 16:57:42 +08:00
parent 223f767b01
commit d7dbbea3e6
8 changed files with 240 additions and 100 deletions

View File

@ -14,3 +14,5 @@ CreateProject(6.LoadModel LoadModel.cpp TGATexture.cpp AssimpLoaderMesh.h Assimp
target_link_libraries(6.LoadModel assimp)
CreateProject(7.InlineGeometryScene InlineGeometryScene.cpp)
CreateProject(8.RenderToColor RenderToColor.cpp TGATexture.cpp)

View File

@ -116,7 +116,15 @@ public: //material相关
public: //Command Buffer 相关
CommandBuffer * CreateCommandBuffer();
RenderPass * CreateRenderPass(VkFormat color_format,VkFormat depth_format);
RenderPass * CreateRenderPass( List<VkFormat> color_format,VkFormat depth_format,
VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
RenderPass * CreateRenderPass( VkFormat color_format,VkFormat depth_format,
VkImageLayout color_final_layout=VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Fence * CreateFence();
Semaphore * CreateSem();

View File

@ -11,7 +11,9 @@ class Framebuffer
private:
friend Framebuffer *CreateFramebuffer(Device *,RenderPass *,List<ImageView *> color,ImageView *depth);
friend Framebuffer *CreateFramebuffer(Device *,RenderPass *,ImageView *color,ImageView *depth);
friend Framebuffer *CreateFramebuffer(Device *,RenderPass *,ImageView *depth);
Framebuffer(VkDevice dev,VkFramebuffer fb)
{
@ -26,6 +28,8 @@ public:
operator VkFramebuffer(){return frame_buffer;}
};//class Framebuffer
Framebuffer *CreateFramebuffer(Device *,RenderPass *,List<ImageView *> color,ImageView *depth=nullptr);
Framebuffer *CreateFramebuffer(Device *,RenderPass *,ImageView *color,ImageView *depth=nullptr);
Framebuffer *CreateFramebuffer(Device *,RenderPass *,ImageView *depth);
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_FRAMEBUFFER_INCLUDE

View File

@ -13,17 +13,26 @@ class RenderPass
VkDevice device;
VkRenderPass render_pass;
VkFormat color_format,depth_format;
List<VkFormat> color_formats;
VkFormat depth_format;
private:
friend class Device;
RenderPass(VkDevice d,VkRenderPass rp,const List<VkFormat> &cf,VkFormat df)
{
device=d;
render_pass=rp;
color_formats=cf;
depth_format=df;
}
RenderPass(VkDevice d,VkRenderPass rp,VkFormat cf,VkFormat df)
{
device=d;
render_pass=rp;
color_format=cf;
color_formats.Add(cf);
depth_format=df;
}
@ -33,7 +42,7 @@ public:
operator VkRenderPass(){return render_pass;}
const VkFormat GetColorFormat()const{return color_format;}
const List<VkFormat> & GetColorFormat()const{return color_formats;}
const VkFormat GetDepthFormat()const{return depth_format;}
};//class RenderPass
VK_NAMESPACE_END

View File

@ -23,7 +23,8 @@
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSurfaceExtensionName.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKVertexAttributeBinding.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKTexture.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSampler.h)
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSampler.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h)
SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKInstance.cpp
@ -35,6 +36,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKDevice.cpp
VKDeviceBuffer.cpp
VKDeviceTexture.cpp
VKDeviceRenderPass.cpp
VKBuffer.cpp
VKDescriptorSets.cpp
VKDescriptorSetLayoutCreater.cpp
@ -51,7 +53,9 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKMaterial.cpp
VKRenderable.cpp
VKTexture.cpp
VKSampler.cpp)
VKSampler.cpp
#VKRenderTarget.cpp
)
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp)

View File

@ -121,96 +121,6 @@ CommandBuffer *Device::CreateCommandBuffer()
return(new CommandBuffer(attr->device,attr->swapchain_extent,attr->cmd_pool,cmd_buf));
}
RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format)
{
VkAttachmentDescription attachments[2];
VkAttachmentReference color_reference={0,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkAttachmentReference depth_reference={1,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
VkSubpassDescription subpass={};
subpass.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.flags=0;
subpass.inputAttachmentCount=0;
subpass.pInputAttachments=nullptr;
subpass.pResolveAttachments=nullptr;
subpass.preserveAttachmentCount=0;
subpass.pPreserveAttachments=nullptr;
int att_count=0;
if(color_format!=VK_FORMAT_UNDEFINED)
{
attachments[0].format=color_format;
attachments[0].samples=VK_SAMPLE_COUNT_1_BIT;
attachments[0].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[0].storeOp=VK_ATTACHMENT_STORE_OP_STORE;
attachments[0].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[0].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[0].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
attachments[0].finalLayout=VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
attachments[0].flags=0;
++att_count;
subpass.colorAttachmentCount=1;
subpass.pColorAttachments=&color_reference;
}
else
{
subpass.colorAttachmentCount=0;
subpass.pColorAttachments=nullptr;
}
if(depth_format!=VK_FORMAT_UNDEFINED)
{
attachments[att_count].format=depth_format;
attachments[att_count].samples=VK_SAMPLE_COUNT_1_BIT;
attachments[att_count].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[att_count].storeOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[att_count].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[att_count].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[att_count].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
attachments[att_count].finalLayout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachments[att_count].flags=0;
depth_reference.attachment=att_count;
++att_count;
subpass.pDepthStencilAttachment=&depth_reference;
}
else
{
subpass.pDepthStencilAttachment=nullptr;
}
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
VkRenderPassCreateInfo rp_info={};
rp_info.sType=VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
rp_info.pNext=nullptr;
rp_info.attachmentCount=att_count;
rp_info.pAttachments=attachments;
rp_info.subpassCount=1;
rp_info.pSubpasses=&subpass;
rp_info.dependencyCount=1;
rp_info.pDependencies=&dependency;
VkRenderPass render_pass;
if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS)
return(nullptr);
return(new RenderPass(attr->device,render_pass,color_format,depth_format));
}
Fence *Device::CreateFence()
{
VkFenceCreateInfo fenceInfo;

View File

@ -0,0 +1,183 @@
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
#include<hgl/graph/vulkan/VKRenderPass.h>
VK_NAMESPACE_BEGIN
RenderPass *Device::CreateRenderPass(List<VkFormat> color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout)
{
int depth=-1;
uint atta_count=color_format.GetCount();
if(attr->physical_device->CheckDepthFormat(depth_format))
{
depth=atta_count++;
}
VkAttachmentDescription *attachments=new VkAttachmentDescription[atta_count];
VkAttachmentReference *color_references=new VkAttachmentReference[color_format.GetCount()];
VkAttachmentReference depth_references;
for(uint i=0;i<atta_count;i++)
{
attachments[i].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[i].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
}
VkFormat *cf=color_format.GetData();
for(int i=0;i<color_format.GetCount();i++)
{
attachments[i].finalLayout = color_final_layout;
attachments[i].format = *cf;
++cf;
color_references[i].attachment = i;
color_references[i].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
}
VkSubpassDescription subpass;
subpass.flags = 0;
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.inputAttachmentCount = 0;
subpass.pInputAttachments = nullptr;
subpass.colorAttachmentCount = color_format.GetCount();
subpass.pColorAttachments = color_references;
subpass.pResolveAttachments = nullptr;
subpass.preserveAttachmentCount = 0;
subpass.pPreserveAttachments = nullptr;
if(depth!=-1)
{
attachments[depth].finalLayout = depth_final_layout;
attachments[depth].format = depth_format;
depth_references.attachment = depth;
depth_references.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
subpass.pDepthStencilAttachment = &depth_references;
}
else
subpass.pDepthStencilAttachment = nullptr;
VkSubpassDependency dependency;
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
VkRenderPassCreateInfo rp_info;
rp_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
rp_info.pNext = nullptr;
rp_info.attachmentCount = atta_count;
rp_info.pAttachments = attachments;
rp_info.subpassCount = 1;
rp_info.pSubpasses = &subpass;
rp_info.dependencyCount = 1;
rp_info.pDependencies = &dependency;
VkRenderPass render_pass;
if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS)
return(nullptr);
return(new RenderPass(attr->device,render_pass,color_format,depth_format));
}
RenderPass *Device::CreateRenderPass(VkFormat color_format,VkFormat depth_format,VkImageLayout color_final_layout,VkImageLayout depth_final_layout)
{
VkAttachmentDescription attachments[2];
VkAttachmentReference color_reference={0,VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL};
VkAttachmentReference depth_reference={1,VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL};
VkSubpassDescription subpass={};
subpass.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.flags=0;
subpass.inputAttachmentCount=0;
subpass.pInputAttachments=nullptr;
subpass.pResolveAttachments=nullptr;
subpass.preserveAttachmentCount=0;
subpass.pPreserveAttachments=nullptr;
int att_count=0;
if(color_format!=VK_FORMAT_UNDEFINED)
{
attachments[0].format=color_format;
attachments[0].samples=VK_SAMPLE_COUNT_1_BIT;
attachments[0].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[0].storeOp=VK_ATTACHMENT_STORE_OP_STORE;
attachments[0].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[0].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[0].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
attachments[0].finalLayout=color_final_layout;
attachments[0].flags=0;
++att_count;
subpass.colorAttachmentCount=1;
subpass.pColorAttachments=&color_reference;
}
else
{
subpass.colorAttachmentCount=0;
subpass.pColorAttachments=nullptr;
}
if(depth_format!=VK_FORMAT_UNDEFINED)
{
attachments[att_count].format=depth_format;
attachments[att_count].samples=VK_SAMPLE_COUNT_1_BIT;
attachments[att_count].loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[att_count].storeOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[att_count].stencilLoadOp=VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[att_count].stencilStoreOp=VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[att_count].initialLayout=VK_IMAGE_LAYOUT_UNDEFINED;
attachments[att_count].finalLayout=depth_final_layout;
attachments[att_count].flags=0;
depth_reference.attachment=att_count;
++att_count;
subpass.pDepthStencilAttachment=&depth_reference;
}
else
{
subpass.pDepthStencilAttachment=nullptr;
}
VkSubpassDependency dependency;
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
VkRenderPassCreateInfo rp_info;
rp_info.sType =VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
rp_info.pNext =nullptr;
rp_info.flags =0;
rp_info.attachmentCount =att_count;
rp_info.pAttachments =attachments;
rp_info.subpassCount =1;
rp_info.pSubpasses =&subpass;
rp_info.dependencyCount =1;
rp_info.pDependencies =&dependency;
VkRenderPass render_pass;
if(vkCreateRenderPass(attr->device,&rp_info,nullptr,&render_pass)!=VK_SUCCESS)
return(nullptr);
return(new RenderPass(attr->device,render_pass,color_format,depth_format));
}
VK_NAMESPACE_END

View File

@ -9,6 +9,15 @@ Framebuffer::~Framebuffer()
vkDestroyFramebuffer(device,frame_buffer,nullptr);
}
Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,List<ImageView *> color,ImageView *depth)
{
if(!dev)return(nullptr);
if(!rp)return(nullptr);
if(!color.GetCount()&&!depth)return(nullptr);
}
Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView *color,ImageView *depth)
{
if(!dev)return(nullptr);
@ -16,9 +25,18 @@ Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView *color,Image
if(!color&&!depth)return(nullptr);
if(color)
if(rp->GetColorFormat()!=color->GetFormat())
{
const auto &cf_list=rp->GetColorFormat();
VkFormat cf;
if(!cf_list.Get(0,cf))
return(nullptr);
if(cf!=color->GetFormat())
return(nullptr);
}
if(depth)
if(rp->GetDepthFormat()!=depth->GetFormat())
return(nullptr);
@ -60,4 +78,6 @@ Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,ImageView *color,Image
return(new Framebuffer(dev->GetDevice(),fb));
}
//Framebuffer *CreateFramebuffer(Device *,RenderPass *,ImageView *depth)
VK_NAMESPACE_END