From 1460840334c1c888d3091df541f9d669436250c3 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Sat, 13 Jul 2019 18:23:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86Swapchain=E5=88=9B=E5=BB=BA=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E5=87=BD=E6=95=B0=E7=8B=AC=E7=AB=8B=E5=88=B0VKSwapcha?= =?UTF-8?q?inCreater.cpp=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/Vulkan/Deferred.cpp | 2 +- inc/hgl/graph/vulkan/VKDevice.h | 4 + inc/hgl/graph/vulkan/VKSwapchain.h | 16 ++- src/RenderDevice/Vulkan/CMakeLists.txt | 1 + src/RenderDevice/Vulkan/VKDeviceCreater.cpp | 98 ----------------- src/RenderDevice/Vulkan/VKShaderParse.h | 10 +- src/RenderDevice/Vulkan/VKSwapchain.cpp | 28 ++++- .../Vulkan/VKSwapchainCreater.cpp | 104 ++++++++++++++++++ 8 files changed, 154 insertions(+), 109 deletions(-) create mode 100644 src/RenderDevice/Vulkan/VKSwapchainCreater.cpp diff --git a/example/Vulkan/Deferred.cpp b/example/Vulkan/Deferred.cpp index ee3564af..4ac4017b 100644 --- a/example/Vulkan/Deferred.cpp +++ b/example/Vulkan/Deferred.cpp @@ -26,7 +26,7 @@ using Texture2DPointer=vulkan::Texture2D *; constexpr VkFormat position_candidate_format[]={FMT_RGBA32F,FMT_RGBA16F}; constexpr VkFormat color_candidate_format []={FMT_RGBA32F, - FMT_RGBA16F,FMT_RGB16UN,FMT_RGB16SN, + FMT_RGBA16F, FMT_RGBA8UN,FMT_RGBA8SN,FMT_RGBA8U, FMT_BGRA8UN,FMT_BGRA8SN,FMT_BGRA8U, FMT_ABGR8UN,FMT_ABGR8SN,FMT_ABGR8U, diff --git a/inc/hgl/graph/vulkan/VKDevice.h b/inc/hgl/graph/vulkan/VKDevice.h index 63e42dc2..684af6f0 100644 --- a/inc/hgl/graph/vulkan/VKDevice.h +++ b/inc/hgl/graph/vulkan/VKDevice.h @@ -169,6 +169,10 @@ public: //Command Buffer 相关 Fence * CreateFence(bool); Semaphore * CreateSem(); + +public: + + bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列 };//class Device VK_NAMESPACE_END #endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE diff --git a/inc/hgl/graph/vulkan/VKSwapchain.h b/inc/hgl/graph/vulkan/VKSwapchain.h index 2aa54cac..66101ad3 100644 --- a/inc/hgl/graph/vulkan/VKSwapchain.h +++ b/inc/hgl/graph/vulkan/VKSwapchain.h @@ -20,7 +20,7 @@ protected: uint32_t swap_chain_count; ObjectList sc_texture; - Texture2D * sc_depth =nullptr; + Texture2D * sc_depth =nullptr; protected: @@ -63,8 +63,19 @@ public: /** * 请求获得下一帧的索引,并将确认信息发送到指定信号 + * @param complete_semaphore 完成后请发送至此信号 + * @return 下一帧的索引 + * @return <0 错误 */ - bool AcquireNextImage (VkSemaphore); ///<请求获得下一帧的索引 + int AcquireNextImage (VkSemaphore complete_semaphore); ///<请求获得下一帧的索引 + + /** + * 提交一个绘制指令 + * @param cmd_list 绘制指令 + * @param wait_sem 指令开始前要等待的确认的信号 + * @param complete_semaphore 绘制完成后发送的信号 + */ + bool SubmitDraw (VkCommandBuffer &cmd_list,VkSemaphore &wait_sem,VkSemaphore &complete_semaphore); ///<提交绘制指令 /** * 提交一批绘制指令 @@ -74,7 +85,6 @@ public: */ bool SubmitDraw (List &cmd_list,List &wait_sems,List &complete_semaphores); ///<提交绘制指令 - bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列 bool PresentBackbuffer (); ///<等待绘制队列完成,并将后台缓冲区呈现到前台 };//class Swapchain diff --git a/src/RenderDevice/Vulkan/CMakeLists.txt b/src/RenderDevice/Vulkan/CMakeLists.txt index fd073932..b1ab34fa 100644 --- a/src/RenderDevice/Vulkan/CMakeLists.txt +++ b/src/RenderDevice/Vulkan/CMakeLists.txt @@ -58,6 +58,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp VKTexture.cpp VKSampler.cpp VKSwapchain.cpp + VKSwapchainCreater.cpp ) SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp) diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index 6bfbefa7..9aebcbd3 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -12,31 +12,6 @@ VK_NAMESPACE_BEGIN namespace { - template T Clamp(const T &cur,const T &min_value,const T &max_value) - { - if(curmax_value)return max_value; - - return cur; - } - - VkExtent2D GetSwapchainExtent(VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) - { - if(surface_caps.currentExtent.width==UINT32_MAX) - { - VkExtent2D swapchain_extent; - - swapchain_extent.width =Clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); - swapchain_extent.height =Clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); - - return swapchain_extent; - } - else - { - return surface_caps.currentExtent; - } - } - VkDevice CreateDevice(VkInstance instance,VkPhysicalDevice physical_device,uint32_t graphics_family) { float queue_priorities[1]={0.0}; @@ -162,61 +137,6 @@ namespace return CreateImageView(device,VK_IMAGE_VIEW_TYPE_2D,format,extent,VK_IMAGE_ASPECT_DEPTH_BIT,img); } - bool CreateSwapchainTexture(DeviceAttribute *rsa) - { - uint32_t count; - - if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,nullptr)!=VK_SUCCESS) - return(false); - - VkImage *sc_images=new VkImage[count]; - - if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,sc_images)!=VK_SUCCESS) - { - delete sc_images; - return(false); - } - - VkImage *ip=sc_images; - Texture2D *tex; - - for(uint32_t i=0; idevice, - rsa->format, - rsa->swapchain_extent.width, - rsa->swapchain_extent.height, - VK_IMAGE_ASPECT_COLOR_BIT, - *ip, - VK_IMAGE_LAYOUT_UNDEFINED); - - rsa->sc_texture.Add(tex); - - ++ip; - } - - delete[] sc_images; - return(true); - } - - bool CreateDepthBuffer(DeviceAttribute *rsa) - { - const VkFormat depth_format=rsa->physical_device->GetDepthFormat(); - - const VkFormatProperties props=rsa->physical_device->GetFormatProperties(depth_format); - - rsa->sc_depth=VK_NAMESPACE::CreateTexture2D(rsa->device,rsa->physical_device, - depth_format, - rsa->swapchain_extent.width, - rsa->swapchain_extent.height, - VK_IMAGE_ASPECT_DEPTH_BIT, - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_IMAGE_LAYOUT_UNDEFINED, - (props.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR); - - return(true); - } - VkDescriptorPool CreateDescriptorPool(VkDevice device,int sets_count) { VkDescriptorPoolSize pool_size[]= @@ -259,22 +179,6 @@ namespace return cache; } - bool CreateSwapchinAndDepthBuffer(DeviceAttribute *attr) - { - attr->swap_chain=CreateSwapChain(attr); - - if(!attr->swap_chain) - return(false); - - if(!CreateSwapchainTexture(attr)) - return(false); - - if(!CreateDepthBuffer(attr)) - return(false); - - return(true); - } - void DebugOut(const VkPhysicalDeviceFeatures &features) { #define OUTPUT_PHYSICAL_DEVICE_FEATURE(name) std::cout< auto_delete(attr); - attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,width,height); - if(attr->graphics_family==ERROR_FAMILY_INDEX) return(nullptr); diff --git a/src/RenderDevice/Vulkan/VKShaderParse.h b/src/RenderDevice/Vulkan/VKShaderParse.h index f997abe5..95f73422 100644 --- a/src/RenderDevice/Vulkan/VKShaderParse.h +++ b/src/RenderDevice/Vulkan/VKShaderParse.h @@ -66,16 +66,16 @@ public: constexpr VkFormat format[][4]= { - {FMT_R8I,FMT_RG8I,FMT_RGB8I,FMT_RGBA8I}, //sbyte - {FMT_R8U,FMT_RG8U,FMT_RGB8U,FMT_RGBA8U}, //ubyte - {FMT_R16I,FMT_RG16I,FMT_RGB16I,FMT_RGBA16I},//short - {FMT_R16U,FMT_RG16U,FMT_RGB16U,FMT_RGBA16U},//ushort + {FMT_R8I,FMT_RG8I,VK_FORMAT_UNDEFINED,FMT_RGBA8I}, //sbyte + {FMT_R8U,FMT_RG8U,VK_FORMAT_UNDEFINED,FMT_RGBA8U}, //ubyte + {FMT_R16I,FMT_RG16I,VK_FORMAT_UNDEFINED,FMT_RGBA16I},//short + {FMT_R16U,FMT_RG16U,VK_FORMAT_UNDEFINED,FMT_RGBA16U},//ushort {FMT_R32I,FMT_RG32I,FMT_RGB32I,FMT_RGBA32I},//int {FMT_R32U,FMT_RG32U,FMT_RGB32U,FMT_RGBA32U},//uint {FMT_R64I,FMT_RG64I,FMT_RGB64I,FMT_RGBA64I},//int64 {FMT_R64U,FMT_RG64U,FMT_RGB64U,FMT_RGBA64U},//uint64 {}, //atomic - {FMT_R16F,FMT_RG16F,FMT_RGB16F,FMT_RGBA16F},//half + {FMT_R16F,FMT_RG16F,VK_FORMAT_UNDEFINED,FMT_RGBA16F},//half {FMT_R32F,FMT_RG32F,FMT_RGB32F,FMT_RGBA32F},//float {FMT_R64F,FMT_RG64F,FMT_RGB64F,FMT_RGBA64F} //double }; diff --git a/src/RenderDevice/Vulkan/VKSwapchain.cpp b/src/RenderDevice/Vulkan/VKSwapchain.cpp index 1b50dae0..7313007e 100644 --- a/src/RenderDevice/Vulkan/VKSwapchain.cpp +++ b/src/RenderDevice/Vulkan/VKSwapchain.cpp @@ -93,9 +93,33 @@ bool Swapchain::Wait(bool wait_all,uint64_t time_out) return(true); } -bool Swapchain::AcquireNextImage(VkSemaphore present_complete_semaphore) +int Swapchain::AcquireNextImage(VkSemaphore present_complete_semaphore) { - return(vkAcquireNextImageKHR(device->GetDevice(),swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS); + if(vkAcquireNextImageKHR(device->GetDevice(),swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,¤t_frame)==VK_SUCCESS) + return current_frame; + + return -1; +} + +bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,VkSemaphore &wait_sem,VkSemaphore &complete_sem) +{ + submit_info.waitSemaphoreCount =1; + submit_info.pWaitSemaphores =&wait_sem; + submit_info.commandBufferCount =1; + submit_info.pCommandBuffers =&cmd_list; + submit_info.signalSemaphoreCount=1; + submit_info.pSignalSemaphores =&complete_sem; + + VkFence fence=*fence_list[current_fence]; + + VkResult result=vkQueueSubmit(graphics_queue,1,&submit_info,fence); + + if(++current_fence==swap_chain_count) + current_fence=0; + + //不在这里立即等待fence完成,是因为有可能queue submit需要久一点工作时间,我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence,而且是另一帧的fence。这样有利于异步处理 + + return(result==VK_SUCCESS); } bool Swapchain::SubmitDraw(List &cmd_lists,List &wait_sems,List &complete_sems) diff --git a/src/RenderDevice/Vulkan/VKSwapchainCreater.cpp b/src/RenderDevice/Vulkan/VKSwapchainCreater.cpp new file mode 100644 index 00000000..9148406a --- /dev/null +++ b/src/RenderDevice/Vulkan/VKSwapchainCreater.cpp @@ -0,0 +1,104 @@ +#include + +VK_NAMESPACE_BEGIN +namespace +{ + template T Clamp(const T &cur,const T &min_value,const T &max_value) + { + if(curmax_value)return max_value; + + return cur; + } + + VkExtent2D GetSwapchainExtent(VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent) + { + if(surface_caps.currentExtent.width==UINT32_MAX) + { + VkExtent2D swapchain_extent; + + swapchain_extent.width =Clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width ); + swapchain_extent.height =Clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height ); + + return swapchain_extent; + } + else + { + return surface_caps.currentExtent; + } + } + + bool CreateSwapchainColorTexture(DeviceAttribute *rsa) + { + uint32_t count; + + if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,nullptr)!=VK_SUCCESS) + return(false); + + VkImage *sc_images=new VkImage[count]; + + if(vkGetSwapchainImagesKHR(rsa->device,rsa->swap_chain,&count,sc_images)!=VK_SUCCESS) + { + delete sc_images; + return(false); + } + + VkImage *ip=sc_images; + Texture2D *tex; + + for(uint32_t i=0; idevice, + rsa->format, + rsa->swapchain_extent.width, + rsa->swapchain_extent.height, + VK_IMAGE_ASPECT_COLOR_BIT, + *ip, + VK_IMAGE_LAYOUT_UNDEFINED); + + rsa->sc_texture.Add(tex); + + ++ip; + } + + delete[] sc_images; + return(true); + } + + bool CreateSwapchainDepthTexture(DeviceAttribute *rsa) + { + const VkFormat depth_format=rsa->physical_device->GetDepthFormat(); + + const VkFormatProperties props=rsa->physical_device->GetFormatProperties(depth_format); + + rsa->sc_depth=VK_NAMESPACE::CreateTexture2D(rsa->device,rsa->physical_device, + depth_format, + rsa->swapchain_extent.width, + rsa->swapchain_extent.height, + VK_IMAGE_ASPECT_DEPTH_BIT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + VK_IMAGE_LAYOUT_UNDEFINED, + (props.optimalTilingFeatures&VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)?VK_IMAGE_TILING_OPTIMAL:VK_IMAGE_TILING_LINEAR); + + return(true); + } + + bool CreateSwapchinAndDepthBuffer(DeviceAttribute *attr) + { + attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,width,height); + + attr->swap_chain=CreateSwapChain(attr); + + if(!attr->swap_chain) + return(false); + + if(!CreateSwapchainColorTexture(attr)) + return(false); + + if(!CreateSwapchainDepthTexture(attr)) + return(false); + + return(true); + } +}//namespace +VK_NAMESPACE_END