diff --git a/example/Vulkan/CMakeLists.txt b/example/Vulkan/CMakeLists.txt index 6bcb5d26..92c08904 100644 --- a/example/Vulkan/CMakeLists.txt +++ b/example/Vulkan/CMakeLists.txt @@ -13,4 +13,6 @@ CreateProject(5.SceneTree SceneTree.cpp) CreateProject(6.LoadModel LoadModel.cpp TGATexture.cpp AssimpLoaderMesh.h AssimpLoaderMesh.cpp ViewModelFramework.h) target_link_libraries(6.LoadModel assimp) -CreateProject(7.InlineGeometryScene InlineGeometryScene.cpp) \ No newline at end of file +CreateProject(7.InlineGeometryScene InlineGeometryScene.cpp) + +CreateProject(8.RenderToColor RenderToColor.cpp TGATexture.cpp) \ No newline at end of file diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index af3a893a..a066cac9 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -116,7 +116,15 @@ public: //material相关 public: //Command Buffer 相关 CommandBuffer * CreateCommandBuffer(); - RenderPass * CreateRenderPass(VkFormat color_format,VkFormat depth_format); + + RenderPass * CreateRenderPass( List 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(); diff --git a/inc/hgl/graph/vulkan/VKFramebuffer.h b/inc/hgl/graph/vulkan/VKFramebuffer.h index 75711674..c9f5284b 100644 --- a/inc/hgl/graph/vulkan/VKFramebuffer.h +++ b/inc/hgl/graph/vulkan/VKFramebuffer.h @@ -11,7 +11,9 @@ class Framebuffer private: - friend Framebuffer *CreateFramebuffer(Device *,RenderPass *,ImageView *color,ImageView *depth); + friend Framebuffer *CreateFramebuffer(Device *,RenderPass *,List 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 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 diff --git a/inc/hgl/graph/vulkan/VKRenderPass.h b/inc/hgl/graph/vulkan/VKRenderPass.h index d5e79f1e..9dc9e23f 100644 --- a/inc/hgl/graph/vulkan/VKRenderPass.h +++ b/inc/hgl/graph/vulkan/VKRenderPass.h @@ -13,17 +13,26 @@ class RenderPass VkDevice device; VkRenderPass render_pass; - VkFormat color_format,depth_format; + List color_formats; + VkFormat depth_format; private: friend class Device; + RenderPass(VkDevice d,VkRenderPass rp,const List &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,8 +42,8 @@ public: operator VkRenderPass(){return render_pass;} - const VkFormat GetColorFormat()const{return color_format;} - const VkFormat GetDepthFormat()const{return depth_format;} + const List & GetColorFormat()const{return color_formats;} + const VkFormat GetDepthFormat()const{return depth_format;} };//class RenderPass VK_NAMESPACE_END #endif//HGL_GRAPH_VULKAN_RENDER_PASS_INCLUDE diff --git a/src/RenderDevice/Vulkan/CMakeLists.txt b/src/RenderDevice/Vulkan/CMakeLists.txt index 5ef3d7a3..bf6069d6 100644 --- a/src/RenderDevice/Vulkan/CMakeLists.txt +++ b/src/RenderDevice/Vulkan/CMakeLists.txt @@ -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) diff --git a/src/RenderDevice/Vulkan/VKDevice.cpp b/src/RenderDevice/Vulkan/VKDevice.cpp index a17d4844..d4130a3c 100644 --- a/src/RenderDevice/Vulkan/VKDevice.cpp +++ b/src/RenderDevice/Vulkan/VKDevice.cpp @@ -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; diff --git a/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp new file mode 100644 index 00000000..e056bd7f --- /dev/null +++ b/src/RenderDevice/Vulkan/VKDeviceRenderPass.cpp @@ -0,0 +1,183 @@ +#include +#include +#include + +VK_NAMESPACE_BEGIN +RenderPass *Device::CreateRenderPass(List 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;idevice,&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 diff --git a/src/RenderDevice/Vulkan/VKFramebuffer.cpp b/src/RenderDevice/Vulkan/VKFramebuffer.cpp index e88c8f4e..1e600047 100644 --- a/src/RenderDevice/Vulkan/VKFramebuffer.cpp +++ b/src/RenderDevice/Vulkan/VKFramebuffer.cpp @@ -9,6 +9,15 @@ Framebuffer::~Framebuffer() vkDestroyFramebuffer(device,frame_buffer,nullptr); } +Framebuffer *CreateFramebuffer(Device *dev,RenderPass *rp,List 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