废除原本的Swapchain/SwapchainAttribute双层结构,将原本的SwapchainAttribute改为Swapchain

This commit is contained in:
hyzboy 2019-07-16 20:57:17 +08:00
parent d23abfade0
commit f87747f7b3
12 changed files with 187 additions and 414 deletions

View File

@ -3,6 +3,7 @@
#include<hgl/graph/vulkan/VKInstance.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
#include<hgl/graph/vulkan/VKBuffer.h>
#include<hgl/graph/vulkan/VKShaderModule.h>
#include<hgl/graph/vulkan/VKShaderModuleManage.h>

View File

@ -20,7 +20,7 @@ class Device;
struct DeviceAttribute;
class ImageView;
class Framebuffer;
class Swapchain;
struct Swapchain;
class RenderTarget;
class SwapchainRenderTarget;

View File

@ -179,7 +179,7 @@ public: //Command Buffer 相关
const VkImageLayout depth_final_layout=VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL)const;
Fence * CreateFence(bool);
Semaphore * CreateSem();
vulkan::Semaphore * CreateSem();
public:

View File

@ -2,6 +2,7 @@
#define HGL_GRAPH_VULKAN_RENDER_TARGET_INCLUDE
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKFence.h>
#include<hgl/graph/vulkan/VKRenderPass.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKSwapchain.h>

View File

@ -1,85 +1,39 @@
#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE
#define HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE
#include<hgl/graph/vulkan/VKSwapchainAttribute.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKFence.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
#include<hgl/graph/vulkan/VK.h>
#include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/type/List.h>
VK_NAMESPACE_BEGIN
class Swapchain
struct Swapchain
{
protected:
public:
Device *device;
VkDevice device =VK_NULL_HANDLE;
SwapchainAttribute *sc_attr;
VkExtent2D extent;
protected:
VkQueue graphics_queue =VK_NULL_HANDLE;
VkSwapchainKHR swap_chain =VK_NULL_HANDLE;
// VkPipelineStageFlags pipe_stage_flags;
uint32_t swap_chain_count=0;
//uint32_t current_frame;
//ObjectList<Framebuffer> render_frame;
//uint32_t current_fence;
//ObjectList<Fence> fence_list;
//RenderPass *main_rp =nullptr;
//VkSubmitInfo submit_info;
//VkPresentInfoKHR present_info;
ObjectList<Texture2D> sc_color;
Texture2D * sc_depth =nullptr;
public:
VkSwapchainKHR GetSwapchain () {return sc_attr->swap_chain;}
const VkExtent2D & GetExtent ()const {return sc_attr->extent;}
const uint32_t GetImageCount ()const {return sc_attr->sc_color.GetCount();}
VkSwapchainKHR GetSwapchain () {return swap_chain;}
const VkExtent2D & GetExtent ()const {return extent;}
const uint32_t GetImageCount ()const {return sc_color.GetCount();}
Texture2D ** GetColorTextures () {return sc_attr->sc_color.GetData();}
Texture2D * GetColorTexture (int index) {return sc_attr->sc_color[index];}
Texture2D * GetDepthTexture () {return sc_attr->sc_depth;}
// RenderPass * GetMainRenderPass () {return main_rp;}
// Framebuffer * GetFramebuffer (int index) {return render_frame[index];}
//const uint32_t GetCurrentFrameIndices () {return current_frame;}
// Framebuffer * GetCurrentFramebuffer () {return render_frame[current_frame];}
Texture2D ** GetColorTextures () {return sc_color.GetData();}
Texture2D * GetColorTexture (int index) {return sc_color[index];}
Texture2D * GetDepthTexture () {return sc_depth;}
public:
Swapchain(Device *,SwapchainAttribute *sa);
virtual ~Swapchain();
//bool Wait (bool wait_all=VK_TRUE,uint64_t time_out=HGL_NANO_SEC_PER_SEC*0.1); ///<等待队列完成
///**
// * 请求获得下一帧的索引,并将确认信息发送到指定信号
// * @param complete_semaphore 完成后请发送至此信号
// * @return 下一帧的索引
// * @return <0 错误
// */
//int AcquireNextImage (vulkan::Semaphore *complete_semaphore); ///<请求获得下一帧的索引
//
///**
// * 提交一个绘制指令
// * @param cmd_list 绘制指令
// * @param wait_sem 指令开始前要等待的确认的信号
// * @param complete_semaphore 绘制完成后发送的信号
// */
//bool SubmitDraw (VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_semaphore); ///<提交绘制指令
///**
// * 提交一批绘制指令
// * @param cmd_list 绘制指令
// * @param wait_sems 指令开始前要等待的确认的信号
// * @param complete_semaphores 绘制完成后发送的信号
// */
//bool SubmitDraw (List<VkCommandBuffer> &cmd_list,List<VkSemaphore> &wait_sems,List<VkSemaphore> &complete_semaphores); ///<提交绘制指令
///**
// * @param render_complete_semaphore 渲染完成信号
// */
//bool PresentBackbuffer (vulkan::Semaphore *render_complete_semaphore); ///<等待绘制队列完成,并将后台缓冲区呈现到前台
};//class Swapchain
};//struct Swapchain
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_INCLUDE

View File

@ -1,26 +0,0 @@
#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE
#define HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE
#include<hgl/graph/vulkan/VK.h>
#include<hgl/type/List.h>
VK_NAMESPACE_BEGIN
struct SwapchainAttribute
{
VkDevice device =VK_NULL_HANDLE;
VkExtent2D extent;
VkQueue graphics_queue =VK_NULL_HANDLE;
VkSwapchainKHR swap_chain =VK_NULL_HANDLE;
uint32_t swap_chain_count=0;
ObjectList<Texture2D> sc_color;
Texture2D * sc_depth =nullptr;
public:
~SwapchainAttribute();
};//struct SwapchainAttribute
VK_NAMESPACE_END
#endif//HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE

View File

@ -24,7 +24,6 @@
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderModuleManage.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSurfaceExtensionName.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSwapchain.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSwapchainAttribute.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)
@ -60,7 +59,6 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKTexture.cpp
VKSampler.cpp
VKSwapchain.cpp
VKSwapchainAttribute.cpp
)
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp)

View File

@ -1,5 +1,6 @@
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/type/Pair.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
#include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/graph/vulkan/VKImageView.h>
#include<hgl/graph/vulkan/VKCommandBuffer.h>
@ -10,7 +11,7 @@
#include<hgl/graph/vulkan/VKDescriptorSets.h>
VK_NAMESPACE_BEGIN
Swapchain *CreateSwapchain(Device *attr,const VkExtent2D &);
Swapchain *CreateSwapchain(const DeviceAttribute *attr,const VkExtent2D &acquire_extent);
Device::Device(DeviceAttribute *da)
{
@ -44,7 +45,7 @@ bool Device::Resize(const VkExtent2D &extent)
SAFE_CLEAR(swapchainRT);
SAFE_CLEAR(swapchain);
swapchain=CreateSwapchain(this,extent);
swapchain=CreateSwapchain(attr,extent);
if(texture_cmd_buf)delete texture_cmd_buf;
@ -96,7 +97,7 @@ Fence *Device::CreateFence(bool create_signaled)
return(new Fence(attr->device,fence));
}
Semaphore *Device::CreateSem()
vulkan::Semaphore *Device::CreateSem()
{
VkSemaphoreCreateInfo SemaphoreCreateInfo;
SemaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
@ -107,7 +108,7 @@ Semaphore *Device::CreateSem()
if(vkCreateSemaphore(attr->device, &SemaphoreCreateInfo, nullptr, &sem)!=VK_SUCCESS)
return(nullptr);
return(new Semaphore(attr->device,sem));
return(new vulkan::Semaphore(attr->device,sem));
}
ShaderModuleManage *Device::CreateShaderModuleManage()

View File

@ -11,7 +11,7 @@
#endif//_DEBUG
VK_NAMESPACE_BEGIN
SwapchainAttribute *CreateSwapchinAttribute(const DeviceAttribute *attr,const VkExtent2D &acquire_extent);
Swapchain *CreateSwapchain(const DeviceAttribute *attr,const VkExtent2D &acquire_extent);
namespace
{
@ -402,14 +402,4 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
return(new Device(device_attr));
}
Swapchain *CreateSwapchain(Device *device,const VkExtent2D &extent)
{
SwapchainAttribute *sc_attr=CreateSwapchinAttribute(device->GetDeviceAttribute(),extent);
if(!sc_attr)
return(nullptr);
return(new Swapchain(device,sc_attr));
}
VK_NAMESPACE_END

View File

@ -1,6 +1,7 @@
#include<hgl/graph/vulkan/VKRenderTarget.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKSwapchain.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
VK_NAMESPACE_BEGIN
namespace

View File

@ -1,147 +1,164 @@
#include<hgl/graph/vulkan/VKSwapchain.h>
#include<hgl/graph/vulkan/VKDevice.h>
#include<hgl/graph/vulkan/VKRenderPass.h>
#include<hgl/graph/vulkan/VKSwapchain.h>
#include<hgl/graph/vulkan/VKDeviceAttribute.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
VK_NAMESPACE_BEGIN
Swapchain::Swapchain(Device *dev,SwapchainAttribute *sa)
{
device=dev;
sc_attr=sa;
//pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
//
//submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
//submit_info.pNext = nullptr;
////submit_info.waitSemaphoreCount = 1;
////submit_info.pWaitSemaphores = *present_complete_semaphore;
//submit_info.pWaitDstStageMask = &pipe_stage_flags;
////submit_info.signalSemaphoreCount = 1;
////submit_info.pSignalSemaphores = *render_complete_semaphore;
//
//present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
//present_info.pNext = nullptr;
//present_info.waitSemaphoreCount = 1;
////present_info.pWaitSemaphores = *render_complete_semaphore;
//present_info.swapchainCount = 1;
//present_info.pResults = nullptr;
//
//present_info.pSwapchains=&(sc_attr->swap_chain);
//main_rp=device->CreateRenderPass(sc_attr->sc_color[0]->GetFormat(),sc_attr->sc_depth->GetFormat());
//for(uint i=0;i<sc_attr->swap_chain_count;i++)
//{
// render_frame.Add(vulkan::CreateFramebuffer(device,main_rp,sc_attr->sc_color[i]->GetImageView(),sc_attr->sc_depth->GetImageView()));
// fence_list.Add(device->CreateFence(true));
//}
//
//current_frame=0;
//current_fence=0;
}
Swapchain::~Swapchain()
{
//fence_list.Clear();
//render_frame.Clear();
SAFE_CLEAR(sc_depth);
sc_color.Clear();
//delete main_rp;
if(swap_chain)
{
vkDestroySwapchainKHR(device,swap_chain,VK_NULL_HANDLE);
swap_chain=VK_NULL_HANDLE;
}
SAFE_CLEAR(sc_attr);
swap_chain_count=0;
}
//bool Swapchain::Wait(bool wait_all,uint64_t time_out)
//{
// VkFence fence=*fence_list[current_fence];
//
// VkResult result;
//
// result=vkWaitForFences(device->GetDevice(),1,&fence,wait_all,time_out);
// result=vkResetFences(device->GetDevice(),1,&fence);
//
// return(true);
//}
//
//int Swapchain::AcquireNextImage(vulkan::Semaphore *present_complete_semaphore)
//{
// VkSemaphore sem=*present_complete_semaphore;
//
// if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,sem,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
// return current_frame;
//
// return -1;
//}
//
//bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,vulkan::Semaphore *wait_sem,vulkan::Semaphore *complete_sem)
//{
// VkSemaphore ws=*wait_sem;
// VkSemaphore cs=*complete_sem;
//
// submit_info.waitSemaphoreCount =1;
// submit_info.pWaitSemaphores =&ws;
// submit_info.commandBufferCount =1;
// submit_info.pCommandBuffers =&cmd_list;
// submit_info.signalSemaphoreCount=1;
// submit_info.pSignalSemaphores =&cs;
//
// VkFence fence=*fence_list[current_fence];
//
// VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
//
// if(++current_fence==sc_attr->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)
//{
// if(cmd_lists.GetCount()<=0)
// return(false);
//
// submit_info.waitSemaphoreCount =wait_sems.GetCount();
// submit_info.pWaitSemaphores =wait_sems.GetData();
// submit_info.commandBufferCount =cmd_lists.GetCount();
// submit_info.pCommandBuffers =cmd_lists.GetData();
// submit_info.signalSemaphoreCount=complete_sems.GetCount();
// submit_info.pSignalSemaphores =complete_sems.GetData();
//
// VkFence fence=*fence_list[current_fence];
//
// VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
//
// if(++current_fence==sc_attr->swap_chain_count)
// current_fence=0;
//
// //不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
//
// return(result==VK_SUCCESS);
//}
//
//bool Swapchain::PresentBackbuffer(vulkan::Semaphore *render_complete_semaphore)
//{
// VkSemaphore sem=*render_complete_semaphore;
//
// present_info.pWaitSemaphores=&sem;
// present_info.pImageIndices=&current_frame;
//
// VkResult result=vkQueuePresentKHR(sc_attr->graphics_queue,&present_info);
//
// if (!((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR)))
// {
// if (result == VK_ERROR_OUT_OF_DATE_KHR) {
// // Swap chain is no longer compatible with the surface and needs to be recreated
//
// return false;
// }
// }
//
// result=vkQueueWaitIdle(sc_attr->graphics_queue);
//
// if(result!=VK_SUCCESS)
// return(false);
//
// return(true);
//}
namespace
{
VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent)
{
if(surface_caps.currentExtent.width==UINT32_MAX)
{
VkExtent2D swapchain_extent;
swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width );
swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height );
return swapchain_extent;
}
else
{
return surface_caps.currentExtent;
}
}
VkSwapchainKHR CreateSwapChain(const DeviceAttribute *dev_attr,const VkExtent2D &extent)
{
VkSwapchainCreateInfoKHR swapchain_ci;
swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchain_ci.pNext =nullptr;
swapchain_ci.flags =0;
swapchain_ci.surface =dev_attr->surface;
swapchain_ci.minImageCount =3;//rsa->surface_caps.minImageCount;
swapchain_ci.imageFormat =dev_attr->format;
swapchain_ci.imageColorSpace =VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
swapchain_ci.imageExtent =extent;
swapchain_ci.imageArrayLayers =1;
swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.queueFamilyIndexCount =0;
swapchain_ci.pQueueFamilyIndices =nullptr;
swapchain_ci.preTransform =dev_attr->preTransform;
swapchain_ci.compositeAlpha =dev_attr->compositeAlpha;
swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR;
swapchain_ci.clipped =VK_TRUE;
swapchain_ci.oldSwapchain =VK_NULL_HANDLE;
if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT)
swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT;
uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family};
if(dev_attr->graphics_family!=dev_attr->present_family)
{
// If the graphics and present queues are from different queue families,
// we either have to explicitly transfer ownership of images between
// the queues, or we have to create the swapchain with imageSharingMode
// as VK_SHARING_MODE_CONCURRENT
swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT;
swapchain_ci.queueFamilyIndexCount=2;
swapchain_ci.pQueueFamilyIndices=queueFamilyIndices;
}
else
{
swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE;
}
VkSwapchainKHR swap_chain;
if(vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain)==VK_SUCCESS)
return(swap_chain);
return(VK_NULL_HANDLE);
}
bool CreateSwapchainColorTexture(Swapchain *sa,const DeviceAttribute *dev_attr)
{
if(vkGetSwapchainImagesKHR(dev_attr->device,sa->swap_chain,&(sa->swap_chain_count),nullptr)!=VK_SUCCESS)
return(false);
AutoDeleteArray<VkImage> sc_images=new VkImage[sa->swap_chain_count];
if(vkGetSwapchainImagesKHR(dev_attr->device,sa->swap_chain,&(sa->swap_chain_count),sc_images)!=VK_SUCCESS)
{
delete sc_images;
return(false);
}
VkImage *ip=sc_images;
Texture2D *tex;
for(uint32_t i=0; i<sa->swap_chain_count; i++)
{
tex=VK_NAMESPACE::CreateTexture2D( dev_attr->device,
dev_attr->format,
sa->extent.width,
sa->extent.height,
VK_IMAGE_ASPECT_COLOR_BIT,
*ip,
VK_IMAGE_LAYOUT_UNDEFINED);
sa->sc_color.Add(tex);
++ip;
}
return(true);
}
bool CreateSwapchainDepthTexture(Swapchain *sa,const DeviceAttribute *dev_attr)
{
const VkFormat depth_format=dev_attr->physical_device->GetDepthFormat();
const VkFormatProperties props=dev_attr->physical_device->GetFormatProperties(depth_format);
sa->sc_depth=VK_NAMESPACE::CreateTexture2D( dev_attr->device,dev_attr->physical_device,
depth_format,
sa->extent.width,
sa->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 sa->sc_depth;
}
}//namespace
Swapchain *CreateSwapchain(const DeviceAttribute *attr,const VkExtent2D &acquire_extent)
{
AutoDelete<Swapchain> sc=new Swapchain;
sc->device =attr->device;
sc->extent =SwapchainExtentClamp(attr->surface_caps,acquire_extent);
sc->graphics_queue =attr->graphics_queue;
sc->swap_chain =CreateSwapChain(attr,sc->extent);
if(!sc->swap_chain)
return(nullptr);
if(!CreateSwapchainColorTexture(sc,attr))
return(nullptr);
if(!CreateSwapchainDepthTexture(sc,attr))
return(nullptr);
return sc.Finish();
}
VK_NAMESPACE_END

View File

@ -1,164 +0,0 @@
#include<hgl/graph/vulkan/VKSwapchainAttribute.h>
#include<hgl/graph/vulkan/VKDeviceAttribute.h>
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
VK_NAMESPACE_BEGIN
SwapchainAttribute::~SwapchainAttribute()
{
SAFE_CLEAR(sc_depth);
sc_color.Clear();
if(swap_chain)
{
vkDestroySwapchainKHR(device,swap_chain,VK_NULL_HANDLE);
swap_chain=VK_NULL_HANDLE;
}
swap_chain_count=0;
}
namespace
{
VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent)
{
if(surface_caps.currentExtent.width==UINT32_MAX)
{
VkExtent2D swapchain_extent;
swapchain_extent.width =hgl_clamp(acquire_extent.width, surface_caps.minImageExtent.width, surface_caps.maxImageExtent.width );
swapchain_extent.height =hgl_clamp(acquire_extent.height, surface_caps.minImageExtent.height, surface_caps.maxImageExtent.height );
return swapchain_extent;
}
else
{
return surface_caps.currentExtent;
}
}
VkSwapchainKHR CreateSwapChain(const DeviceAttribute *dev_attr,const VkExtent2D &extent)
{
VkSwapchainCreateInfoKHR swapchain_ci;
swapchain_ci.sType =VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchain_ci.pNext =nullptr;
swapchain_ci.flags =0;
swapchain_ci.surface =dev_attr->surface;
swapchain_ci.minImageCount =3;//rsa->surface_caps.minImageCount;
swapchain_ci.imageFormat =dev_attr->format;
swapchain_ci.imageColorSpace =VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
swapchain_ci.imageExtent =extent;
swapchain_ci.imageArrayLayers =1;
swapchain_ci.imageUsage =VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_ci.queueFamilyIndexCount =0;
swapchain_ci.pQueueFamilyIndices =nullptr;
swapchain_ci.preTransform =dev_attr->preTransform;
swapchain_ci.compositeAlpha =dev_attr->compositeAlpha;
swapchain_ci.presentMode =VK_PRESENT_MODE_FIFO_KHR;
swapchain_ci.clipped =VK_TRUE;
swapchain_ci.oldSwapchain =VK_NULL_HANDLE;
if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
if(dev_attr->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT)
swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT;
uint32_t queueFamilyIndices[2]={dev_attr->graphics_family, dev_attr->present_family};
if(dev_attr->graphics_family!=dev_attr->present_family)
{
// If the graphics and present queues are from different queue families,
// we either have to explicitly transfer ownership of images between
// the queues, or we have to create the swapchain with imageSharingMode
// as VK_SHARING_MODE_CONCURRENT
swapchain_ci.imageSharingMode=VK_SHARING_MODE_CONCURRENT;
swapchain_ci.queueFamilyIndexCount=2;
swapchain_ci.pQueueFamilyIndices=queueFamilyIndices;
}
else
{
swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE;
}
VkSwapchainKHR swap_chain;
if(vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain)==VK_SUCCESS)
return(swap_chain);
return(VK_NULL_HANDLE);
}
bool CreateSwapchainColorTexture(SwapchainAttribute *sa,const DeviceAttribute *dev_attr)
{
if(vkGetSwapchainImagesKHR(dev_attr->device,sa->swap_chain,&(sa->swap_chain_count),nullptr)!=VK_SUCCESS)
return(false);
AutoDeleteArray<VkImage> sc_images=new VkImage[sa->swap_chain_count];
if(vkGetSwapchainImagesKHR(dev_attr->device,sa->swap_chain,&(sa->swap_chain_count),sc_images)!=VK_SUCCESS)
{
delete sc_images;
return(false);
}
VkImage *ip=sc_images;
Texture2D *tex;
for(uint32_t i=0; i<sa->swap_chain_count; i++)
{
tex=VK_NAMESPACE::CreateTexture2D( dev_attr->device,
dev_attr->format,
sa->extent.width,
sa->extent.height,
VK_IMAGE_ASPECT_COLOR_BIT,
*ip,
VK_IMAGE_LAYOUT_UNDEFINED);
sa->sc_color.Add(tex);
++ip;
}
return(true);
}
bool CreateSwapchainDepthTexture(SwapchainAttribute *sa,const DeviceAttribute *dev_attr)
{
const VkFormat depth_format=dev_attr->physical_device->GetDepthFormat();
const VkFormatProperties props=dev_attr->physical_device->GetFormatProperties(depth_format);
sa->sc_depth=VK_NAMESPACE::CreateTexture2D( dev_attr->device,dev_attr->physical_device,
depth_format,
sa->extent.width,
sa->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 sa->sc_depth;
}
}//namespace
SwapchainAttribute *CreateSwapchinAttribute(const DeviceAttribute *attr,const VkExtent2D &acquire_extent)
{
AutoDelete<SwapchainAttribute> sa=new SwapchainAttribute;
sa->device =attr->device;
sa->extent =SwapchainExtentClamp(attr->surface_caps,acquire_extent);
sa->graphics_queue =attr->graphics_queue;
sa->swap_chain =CreateSwapChain(attr,sa->extent);
if(!sa->swap_chain)
return(nullptr);
if(!CreateSwapchainColorTexture(sa,attr))
return(nullptr);
if(!CreateSwapchainDepthTexture(sa,attr))
return(nullptr);
return sa.Finish();
}
VK_NAMESPACE_END