1.拆分建立独立的SwapchainAttribute

2.Swapchain代码完全迁移到VKSwapchain
This commit is contained in:
hyzboy 2019-07-15 22:37:00 +08:00
parent 5577b07dfd
commit 4a46a6e014
12 changed files with 275 additions and 246 deletions

View File

@ -17,6 +17,7 @@ VK_NAMESPACE_BEGIN
class Instance;
class PhysicalDevice;
class Device;
struct DeviceAttribute;
class ImageView;
class Framebuffer;

View File

@ -24,7 +24,7 @@ class Device
private:
friend Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height);
friend Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,const VkExtent2D &extent);
Device(DeviceAttribute *da);
@ -33,6 +33,7 @@ public:
virtual ~Device();
operator VkDevice () {return attr->device;}
DeviceAttribute * GetDeviceAttribute () {return attr;}
VkSurfaceKHR GetSurface () {return attr->surface;}
VkDevice GetDevice () {return attr->device;}

View File

@ -30,16 +30,12 @@ private:
public:
~RenderTarget()
virtual ~RenderTarget()
{
if(fb)delete fb;
if(fb)
delete fb;
}
operator RenderPass * (){return rp;}
operator Framebuffer * (){return fb;}
operator VkRenderPass (){return rp?rp->operator VkRenderPass():nullptr;}
operator VkFramebuffer (){return fb?fb->operator VkFramebuffer():nullptr;}
const VkExtent2D & GetExtent ()const{return extent;} ///<取得画面尺寸
const uint GetColorCount ()const{colors.GetCount();} ///<取得颜色成份数量

View File

@ -6,11 +6,9 @@
#include<hgl/graph/vulkan/VKFence.h>
#include<hgl/graph/vulkan/VKSemaphore.h>
VK_NAMESPACE_BEGIN
class Swapchain
struct SwapchainAttribute
{
protected:
Device *device;
VkDevice device;
VkExtent2D extent;
@ -19,8 +17,17 @@ protected:
uint32_t swap_chain_count;
ObjectList<Texture2D> sc_texture;
ObjectList<Texture2D> sc_color;
Texture2D * sc_depth =nullptr;
};//struct SwapchainAttribute
class Swapchain
{
protected:
Device *device;
SwapchainAttribute *sc_attr;
protected:
@ -42,8 +49,8 @@ protected:
public:
const VkExtent2D & GetExtent ()const {return extent;}
const uint32_t GetImageCount ()const {return sc_texture.GetCount();}
const VkExtent2D & GetExtent ()const {return sc_attr->extent;}
const uint32_t GetImageCount ()const {return sc_attr->sc_color.GetCount();}
RenderPass * GetMainRenderPass () {return main_rp;}
Framebuffer * GetFramebuffer (int index) {return render_frame[index];}
@ -52,10 +59,9 @@ public:
public:
Swapchain(Device *);
virtual ~Swapchain();
Swapchain(Device *,SwapchainAttribute *sa);
void Clear ();
virtual ~Swapchain();
void Recreate ();

View File

@ -0,0 +1,25 @@
#ifndef HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE
#define HGL_GRAPH_VULKAN_SWAP_CHAIN_ATTRIBUTE_INCLUDE
#include<hgl/graph/Vulkan/VK.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

@ -101,7 +101,7 @@ namespace hgl
0.0f, 0.0f, zfar / (znear - zfar), (znear * zfar) / (znear - zfar),
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 某些引擎这两项会乘0.5,那是因为他们是 -1 to 1 的Z值设定而我们是0 to 1所以这里不用乘
// 同理camera的znear必须无限接近0的正数zfar为一个较大的正数默认使用0.01/1024
// 同理camera的znear为接近0的正数zfar为一个较大的正数默认使用16/256
0.0f, 0.0f, -1.0f, 0.0f);
}

View File

@ -17,16 +17,17 @@
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKPrimivate.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderable.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderPass.h
# ${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSemaphore.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderResource.h
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKShaderModule.h
${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
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKRenderTarget.h)
${ROOT_INCLUDE_PATH}/hgl/graph/vulkan/VKSampler.h)
SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKMemory.cpp
@ -45,6 +46,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKDescriptorSetLayoutCreater.cpp
VKDescriptorSetLayoutCreater.h
VKRenderPass.cpp
# VKRenderTarget.cpp
VKShaderParse.h
VKShaderModule.cpp
VKShaderModuleManage.cpp
@ -58,7 +60,7 @@ SET(RENDER_DEVICE_VULKAN_SOURCE VKFormat.cpp
VKTexture.cpp
VKSampler.cpp
VKSwapchain.cpp
VKSwapchainCreater.cpp
VKSwapchainAttribute.cpp
)
SET(RENDER_DEVICE_VULKAN_POD_SOURCE pod/VKPipelineCreateInfo.POD.cpp)

View File

@ -10,7 +10,7 @@
#include<hgl/graph/vulkan/VKDescriptorSets.h>
VK_NAMESPACE_BEGIN
Swapchain *CreateSwapchain(DeviceAttribute *attr,const VkExtent2D &);
Swapchain *CreateSwapchain(Device *attr,const VkExtent2D &);
Device::Device(DeviceAttribute *da)
{
@ -39,7 +39,7 @@ bool Device::Resize(const VkExtent2D &extent)
if(swapchain)
delete swapchain;
swapchain=CreateSwapchain(attr,extent);
swapchain=CreateSwapchain(this,extent);
if(texture_cmd_buf)delete texture_cmd_buf;

View File

@ -3,6 +3,7 @@
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
#include<hgl/graph/vulkan/VKFramebuffer.h>
#include<hgl/graph/vulkan/VKTexture.h>
#include<hgl/graph/vulkan/VKDevice.h>
#ifdef _DEBUG
#include<iostream>
@ -10,6 +11,8 @@
#endif//_DEBUG
VK_NAMESPACE_BEGIN
SwapchainAttribute *CreateSwapchinAttribute(const DeviceAttribute *attr,const VkExtent2D &acquire_extent);
namespace
{
VkDevice CreateDevice(VkInstance instance,VkPhysicalDevice physical_device,uint32_t graphics_family)
@ -71,55 +74,6 @@ namespace
return(VK_NULL_HANDLE);
}
VkSwapchainKHR CreateSwapChain(DeviceAttribute *rsa)
{
VkSwapchainCreateInfoKHR swapchain_ci={};
swapchain_ci.sType=VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchain_ci.pNext=nullptr;
swapchain_ci.surface=rsa->surface;
swapchain_ci.minImageCount=3;//rsa->surface_caps.minImageCount;
swapchain_ci.imageFormat=rsa->format;
swapchain_ci.imageExtent=rsa->swapchain_extent;
swapchain_ci.preTransform=rsa->preTransform;
swapchain_ci.compositeAlpha=rsa->compositeAlpha;
swapchain_ci.imageArrayLayers=1;
swapchain_ci.presentMode=VK_PRESENT_MODE_FIFO_KHR;
swapchain_ci.oldSwapchain=VK_NULL_HANDLE;
swapchain_ci.clipped=true;
swapchain_ci.imageColorSpace=VK_COLORSPACE_SRGB_NONLINEAR_KHR;
swapchain_ci.imageUsage=VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
if(rsa->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_SRC_BIT)
swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
if(rsa->surface_caps.supportedUsageFlags&VK_IMAGE_USAGE_TRANSFER_DST_BIT)
swapchain_ci.imageUsage|=VK_IMAGE_USAGE_TRANSFER_DST_BIT;
uint32_t queueFamilyIndices[2]={rsa->graphics_family, rsa->present_family};
if(rsa->graphics_family!=rsa->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(rsa->device,&swapchain_ci,nullptr,&swap_chain)==VK_SUCCESS)
return(swap_chain);
return(VK_NULL_HANDLE);
}
ImageView *Create2DImageView(VkDevice device,VkFormat format,const VkExtent2D &ext,VkImage img=VK_NULL_HANDLE)
{
VkExtent3D extent;
@ -137,22 +91,23 @@ namespace
return CreateImageView(device,VK_IMAGE_VIEW_TYPE_2D,format,extent,VK_IMAGE_ASPECT_DEPTH_BIT,img);
}
VkDescriptorPool CreateDescriptorPool(VkDevice device,int sets_count)
VkDescriptorPool CreateDescriptorPool(VkDevice device,uint32_t sets_count)
{
VkDescriptorPoolSize pool_size[]=
{
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1024},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1024},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, 1024},
{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 48}
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, sets_count},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, sets_count},
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, sets_count},
{VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, sets_count}
};
VkDescriptorPoolCreateInfo dp_create_info={};
dp_create_info.sType=VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
dp_create_info.pNext=nullptr;
dp_create_info.maxSets=sets_count;
VkDescriptorPoolCreateInfo dp_create_info;
dp_create_info.sType =VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
dp_create_info.pNext =nullptr;
dp_create_info.flags =0;
dp_create_info.maxSets =sets_count;
dp_create_info.poolSizeCount=sizeof(pool_size)/sizeof(VkDescriptorPoolSize);
dp_create_info.pPoolSizes=pool_size;
dp_create_info.pPoolSizes =pool_size;
VkDescriptorPool desc_pool;
@ -165,11 +120,11 @@ namespace
VkPipelineCache CreatePipelineCache(VkDevice device)
{
VkPipelineCacheCreateInfo pipelineCache;
pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
pipelineCache.pNext = nullptr;
pipelineCache.initialDataSize = 0;
pipelineCache.pInitialData = nullptr;
pipelineCache.flags = 0;
pipelineCache.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
pipelineCache.pNext = nullptr;
pipelineCache.flags = 0;
pipelineCache.initialDataSize = 0;
pipelineCache.pInitialData = nullptr;
VkPipelineCache cache;
@ -391,7 +346,7 @@ namespace
}
}//namespace
Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,uint width,uint height)
Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device,VkSurfaceKHR surface,const VkExtent2D &extent)
{
#ifdef _DEBUG
{
@ -414,49 +369,47 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device
}
#endif//_DEBUG
DeviceAttribute *attr=new DeviceAttribute(inst,physical_device,surface);
DeviceAttribute *device_attr=new DeviceAttribute(inst,physical_device,surface);
AutoDelete<DeviceAttribute> auto_delete(attr);
AutoDelete<DeviceAttribute> auto_delete(device_attr);
if(attr->graphics_family==ERROR_FAMILY_INDEX)
if(device_attr->graphics_family==ERROR_FAMILY_INDEX)
return(nullptr);
attr->device=CreateDevice(inst,*physical_device,attr->graphics_family);
device_attr->device=CreateDevice(inst,*physical_device,device_attr->graphics_family);
if(!attr->device)
if(!device_attr->device)
return(nullptr);
GetDeviceQueue(attr);
GetDeviceQueue(device_attr);
attr->cmd_pool=CreateCommandPool(attr->device,attr->graphics_family);
device_attr->cmd_pool=CreateCommandPool(device_attr->device,device_attr->graphics_family);
if(!attr->cmd_pool)
if(!device_attr->cmd_pool)
return(nullptr);
device_attr->desc_pool=CreateDescriptorPool(device_attr->device,1024);
if(!device_attr->desc_pool)
return(nullptr);
if(!CreateSwapchinAndDepthBuffer(attr))
return(nullptr);
device_attr->pipeline_cache=CreatePipelineCache(device_attr->device);
attr->desc_pool=CreateDescriptorPool(attr->device,1024);
if(!attr->desc_pool)
return(nullptr);
attr->pipeline_cache=CreatePipelineCache(attr->device);
if(!attr->pipeline_cache)
if(!device_attr->pipeline_cache)
return(nullptr);
auto_delete.Clear();
return(new Device(attr));
return(new Device(device_attr));
}
Swapchain *CreateSwapchain(DeviceAttribute *attr,const VkExtent2D &acquire_extent)
Swapchain *CreateSwapchain(Device *device,const VkExtent2D &extent)
{
attr->Refresh();
SwapchainAttribute *sc_attr=CreateSwapchinAttribute(device->GetDeviceAttribute(),extent);
if(!sc_attr)
return(nullptr);
VkExtent2D extent=GetSwapchainExtent(attr->surface_caps,acquire_extent);
return CreateSwapchinAndDepthBuffer(attr);
return(new Swapchain(device,sc_attr));
}
VK_NAMESPACE_END

View File

@ -3,9 +3,10 @@
#include<hgl/graph/vulkan/VKRenderPass.h>
VK_NAMESPACE_BEGIN
Swapchain::Swapchain(Device *dev)
Swapchain::Swapchain(Device *dev,SwapchainAttribute *sa)
{
device=dev;
sc_attr=sa;
pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
@ -29,8 +30,6 @@ Swapchain::Swapchain(Device *dev)
present_info.pWaitSemaphores = *render_complete_semaphore;
present_info.swapchainCount = 1;
present_info.pResults = nullptr;
graphics_queue=device->GetGraphicsQueue();
}
Swapchain::~Swapchain()
@ -43,19 +42,7 @@ Swapchain::~Swapchain()
delete main_rp;
Clear();
}
void Swapchain::Clear()
{
SAFE_CLEAR(sc_depth);
sc_texture.Clear();
if(swap_chain)
{
vkDestroySwapchainKHR(device->GetDevice(),swap_chain,nullptr);
swap_chain=VK_NULL_HANDLE;
}
SAFE_CLEAR(sc_attr);
}
void Swapchain::Recreate()
@ -65,15 +52,13 @@ void Swapchain::Recreate()
if(main_rp)delete main_rp;
present_info.pSwapchains=&swap_chain;
present_info.pSwapchains=&(sc_attr->swap_chain);
main_rp=device->CreateRenderPass(device->GetSurfaceFormat(),sc_depth->GetFormat());
main_rp=device->CreateRenderPass(sc_attr->sc_color[0]->GetFormat(),sc_attr->sc_depth->GetFormat());
swap_chain_count=sc_texture.GetCount();
for(uint i=0;i<swap_chain_count;i++)
for(uint i=0;i<sc_attr->swap_chain_count;i++)
{
render_frame.Add(vulkan::CreateFramebuffer(device,main_rp,sc_texture[i]->GetImageView(),sc_depth->GetImageView()));
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));
}
@ -95,7 +80,7 @@ bool Swapchain::Wait(bool wait_all,uint64_t time_out)
int Swapchain::AcquireNextImage(VkSemaphore present_complete_semaphore)
{
if(vkAcquireNextImageKHR(device->GetDevice(),swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
if(vkAcquireNextImageKHR(device->GetDevice(),sc_attr->swap_chain,UINT64_MAX,present_complete_semaphore,VK_NULL_HANDLE,&current_frame)==VK_SUCCESS)
return current_frame;
return -1;
@ -112,9 +97,9 @@ bool Swapchain::SubmitDraw(VkCommandBuffer &cmd_list,VkSemaphore &wait_sem,VkSem
VkFence fence=*fence_list[current_fence];
VkResult result=vkQueueSubmit(graphics_queue,1,&submit_info,fence);
VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
if(++current_fence==swap_chain_count)
if(++current_fence==sc_attr->swap_chain_count)
current_fence=0;
//不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
@ -136,9 +121,9 @@ bool Swapchain::SubmitDraw(List<VkCommandBuffer> &cmd_lists,List<VkSemaphore> &w
VkFence fence=*fence_list[current_fence];
VkResult result=vkQueueSubmit(graphics_queue,1,&submit_info,fence);
VkResult result=vkQueueSubmit(sc_attr->graphics_queue,1,&submit_info,fence);
if(++current_fence==swap_chain_count)
if(++current_fence==sc_attr->swap_chain_count)
current_fence=0;
//不在这里立即等待fence完成是因为有可能queue submit需要久一点工作时间我们这个时间可以去干别的。等在AcquireNextImage时再去等待fence而且是另一帧的fence。这样有利于异步处理
@ -150,7 +135,7 @@ bool Swapchain::PresentBackbuffer()
{
present_info.pImageIndices=&current_frame;
VkResult result=vkQueuePresentKHR(graphics_queue,&present_info);
VkResult result=vkQueuePresentKHR(sc_attr->graphics_queue,&present_info);
if (!((result == VK_SUCCESS) || (result == VK_SUBOPTIMAL_KHR)))
{
@ -161,7 +146,7 @@ bool Swapchain::PresentBackbuffer()
}
}
result=vkQueueWaitIdle(graphics_queue);
result=vkQueueWaitIdle(sc_attr->graphics_queue);
if(result!=VK_SUCCESS)
return(false);

View File

@ -0,0 +1,164 @@
#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

View File

@ -1,104 +0,0 @@
#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