将Swapchain创建相关函数独立到VKSwapchainCreater.cpp中
This commit is contained in:
parent
a5ff474793
commit
1460840334
@ -26,7 +26,7 @@ using Texture2DPointer=vulkan::Texture2D *;
|
|||||||
|
|
||||||
constexpr VkFormat position_candidate_format[]={FMT_RGBA32F,FMT_RGBA16F};
|
constexpr VkFormat position_candidate_format[]={FMT_RGBA32F,FMT_RGBA16F};
|
||||||
constexpr VkFormat color_candidate_format []={FMT_RGBA32F,
|
constexpr VkFormat color_candidate_format []={FMT_RGBA32F,
|
||||||
FMT_RGBA16F,FMT_RGB16UN,FMT_RGB16SN,
|
FMT_RGBA16F,
|
||||||
FMT_RGBA8UN,FMT_RGBA8SN,FMT_RGBA8U,
|
FMT_RGBA8UN,FMT_RGBA8SN,FMT_RGBA8U,
|
||||||
FMT_BGRA8UN,FMT_BGRA8SN,FMT_BGRA8U,
|
FMT_BGRA8UN,FMT_BGRA8SN,FMT_BGRA8U,
|
||||||
FMT_ABGR8UN,FMT_ABGR8SN,FMT_ABGR8U,
|
FMT_ABGR8UN,FMT_ABGR8SN,FMT_ABGR8U,
|
||||||
|
@ -169,6 +169,10 @@ public: //Command Buffer 相关
|
|||||||
|
|
||||||
Fence * CreateFence(bool);
|
Fence * CreateFence(bool);
|
||||||
Semaphore * CreateSem();
|
Semaphore * CreateSem();
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列
|
||||||
};//class Device
|
};//class Device
|
||||||
VK_NAMESPACE_END
|
VK_NAMESPACE_END
|
||||||
#endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE
|
#endif//HGL_GRAPH_RENDER_SURFACE_INCLUDE
|
||||||
|
@ -20,7 +20,7 @@ protected:
|
|||||||
uint32_t swap_chain_count;
|
uint32_t swap_chain_count;
|
||||||
|
|
||||||
ObjectList<Texture2D> sc_texture;
|
ObjectList<Texture2D> sc_texture;
|
||||||
Texture2D * sc_depth =nullptr;
|
Texture2D * sc_depth =nullptr;
|
||||||
|
|
||||||
protected:
|
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<VkCommandBuffer> &cmd_list,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_semaphores); ///<提交绘制指令
|
bool SubmitDraw (List<VkCommandBuffer> &cmd_list,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_semaphores); ///<提交绘制指令
|
||||||
|
|
||||||
bool SubmitTexture (const VkCommandBuffer *cmd_bufs,const uint32_t count=1); ///<提交纹理处理到队列
|
|
||||||
|
|
||||||
bool PresentBackbuffer (); ///<等待绘制队列完成,并将后台缓冲区呈现到前台
|
bool PresentBackbuffer (); ///<等待绘制队列完成,并将后台缓冲区呈现到前台
|
||||||
};//class Swapchain
|
};//class Swapchain
|
||||||
|
@ -58,6 +58,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
|
|||||||
VKTexture.cpp
|
VKTexture.cpp
|
||||||
VKSampler.cpp
|
VKSampler.cpp
|
||||||
VKSwapchain.cpp
|
VKSwapchain.cpp
|
||||||
|
VKSwapchainCreater.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp)
|
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp)
|
||||||
|
@ -12,31 +12,6 @@
|
|||||||
VK_NAMESPACE_BEGIN
|
VK_NAMESPACE_BEGIN
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
template<typename T> T Clamp(const T &cur,const T &min_value,const T &max_value)
|
|
||||||
{
|
|
||||||
if(cur<min_value)return min_value;
|
|
||||||
if(cur>max_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)
|
VkDevice CreateDevice(VkInstance instance,VkPhysicalDevice physical_device,uint32_t graphics_family)
|
||||||
{
|
{
|
||||||
float queue_priorities[1]={0.0};
|
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);
|
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; i<count; i++)
|
|
||||||
{
|
|
||||||
tex=VK_NAMESPACE::CreateTexture2D( rsa->device,
|
|
||||||
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)
|
VkDescriptorPool CreateDescriptorPool(VkDevice device,int sets_count)
|
||||||
{
|
{
|
||||||
VkDescriptorPoolSize pool_size[]=
|
VkDescriptorPoolSize pool_size[]=
|
||||||
@ -259,22 +179,6 @@ namespace
|
|||||||
return cache;
|
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)
|
void DebugOut(const VkPhysicalDeviceFeatures &features)
|
||||||
{
|
{
|
||||||
#define OUTPUT_PHYSICAL_DEVICE_FEATURE(name) std::cout<<std::setw(40)<<std::right<<#name<<": "<<(features.name?"true":"false")<<std::endl;
|
#define OUTPUT_PHYSICAL_DEVICE_FEATURE(name) std::cout<<std::setw(40)<<std::right<<#name<<": "<<(features.name?"true":"false")<<std::endl;
|
||||||
@ -514,8 +418,6 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
|
|||||||
|
|
||||||
AutoDelete<DeviceAttribute> auto_delete(attr);
|
AutoDelete<DeviceAttribute> auto_delete(attr);
|
||||||
|
|
||||||
attr->swapchain_extent=GetSwapchainExtent(attr->surface_caps,width,height);
|
|
||||||
|
|
||||||
if(attr->graphics_family==ERROR_FAMILY_INDEX)
|
if(attr->graphics_family==ERROR_FAMILY_INDEX)
|
||||||
return(nullptr);
|
return(nullptr);
|
||||||
|
|
||||||
|
@ -66,16 +66,16 @@ public:
|
|||||||
|
|
||||||
constexpr VkFormat format[][4]=
|
constexpr VkFormat format[][4]=
|
||||||
{
|
{
|
||||||
{FMT_R8I,FMT_RG8I,FMT_RGB8I,FMT_RGBA8I}, //sbyte
|
{FMT_R8I,FMT_RG8I,VK_FORMAT_UNDEFINED,FMT_RGBA8I}, //sbyte
|
||||||
{FMT_R8U,FMT_RG8U,FMT_RGB8U,FMT_RGBA8U}, //ubyte
|
{FMT_R8U,FMT_RG8U,VK_FORMAT_UNDEFINED,FMT_RGBA8U}, //ubyte
|
||||||
{FMT_R16I,FMT_RG16I,FMT_RGB16I,FMT_RGBA16I},//short
|
{FMT_R16I,FMT_RG16I,VK_FORMAT_UNDEFINED,FMT_RGBA16I},//short
|
||||||
{FMT_R16U,FMT_RG16U,FMT_RGB16U,FMT_RGBA16U},//ushort
|
{FMT_R16U,FMT_RG16U,VK_FORMAT_UNDEFINED,FMT_RGBA16U},//ushort
|
||||||
{FMT_R32I,FMT_RG32I,FMT_RGB32I,FMT_RGBA32I},//int
|
{FMT_R32I,FMT_RG32I,FMT_RGB32I,FMT_RGBA32I},//int
|
||||||
{FMT_R32U,FMT_RG32U,FMT_RGB32U,FMT_RGBA32U},//uint
|
{FMT_R32U,FMT_RG32U,FMT_RGB32U,FMT_RGBA32U},//uint
|
||||||
{FMT_R64I,FMT_RG64I,FMT_RGB64I,FMT_RGBA64I},//int64
|
{FMT_R64I,FMT_RG64I,FMT_RGB64I,FMT_RGBA64I},//int64
|
||||||
{FMT_R64U,FMT_RG64U,FMT_RGB64U,FMT_RGBA64U},//uint64
|
{FMT_R64U,FMT_RG64U,FMT_RGB64U,FMT_RGBA64U},//uint64
|
||||||
{}, //atomic
|
{}, //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_R32F,FMT_RG32F,FMT_RGB32F,FMT_RGBA32F},//float
|
||||||
{FMT_R64F,FMT_RG64F,FMT_RGB64F,FMT_RGBA64F} //double
|
{FMT_R64F,FMT_RG64F,FMT_RGB64F,FMT_RGBA64F} //double
|
||||||
};
|
};
|
||||||
|
@ -93,9 +93,33 @@ bool Swapchain::Wait(bool wait_all,uint64_t time_out)
|
|||||||
return(true);
|
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<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_sems)
|
bool Swapchain::SubmitDraw(List<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_sems)
|
||||||
|
104
src/RenderDevice/Vulkan/VKSwapchainCreater.cpp
Normal file
104
src/RenderDevice/Vulkan/VKSwapchainCreater.cpp
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#include<hgl/graph/vulkan/VKSwapchain.h>
|
||||||
|
|
||||||
|
VK_NAMESPACE_BEGIN
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
template<typename T> T Clamp(const T &cur,const T &min_value,const T &max_value)
|
||||||
|
{
|
||||||
|
if(cur<min_value)return min_value;
|
||||||
|
if(cur>max_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; i<count; i++)
|
||||||
|
{
|
||||||
|
tex=VK_NAMESPACE::CreateTexture2D( rsa->device,
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user