2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKDevice.h>
|
|
|
|
|
#include<hgl/graph/VKDeviceAttribute.h>
|
|
|
|
|
#include<hgl/graph/VKPhysicalDevice.h>
|
2023-07-28 20:17:46 +08:00
|
|
|
|
#include<iostream>
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
|
|
|
|
namespace
|
|
|
|
|
{
|
2021-12-15 19:55:50 +08:00
|
|
|
|
//VkExtent2D SwapchainExtentClamp(const VkSurfaceCapabilitiesKHR &surface_caps,const VkExtent2D &acquire_extent)
|
|
|
|
|
//{
|
|
|
|
|
// VkExtent2D swapchain_extent;
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2021-12-15 19:55:50 +08:00
|
|
|
|
// 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 );
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2021-12-15 19:55:50 +08:00
|
|
|
|
// return swapchain_extent;
|
|
|
|
|
//}
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
VkSwapchainKHR CreateSwapChain(const GPUDeviceAttribute *dev_attr,const VkExtent2D &extent)
|
2019-11-26 00:22:54 +08:00
|
|
|
|
{
|
|
|
|
|
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;
|
2021-09-23 15:19:09 +08:00
|
|
|
|
swapchain_ci.imageFormat =dev_attr->surface_format.format;
|
|
|
|
|
swapchain_ci.imageColorSpace =dev_attr->surface_format.colorSpace;
|
2019-11-26 00:22:54 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
2020-09-02 18:27:19 +08:00
|
|
|
|
swapchain_ci.imageSharingMode = VkSharingMode(SharingMode::Exclusive);
|
2019-11-26 00:22:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VkSwapchainKHR swap_chain;
|
2023-05-13 03:14:38 +08:00
|
|
|
|
VkResult result;
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2023-05-13 03:14:38 +08:00
|
|
|
|
result=vkCreateSwapchainKHR(dev_attr->device,&swapchain_ci,nullptr,&swap_chain);
|
|
|
|
|
|
2023-10-13 01:48:07 +08:00
|
|
|
|
if(result!=VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
//LOG_ERROR(OS_TEXT("vkCreateSwapchainKHR failed, result = ")+OSString(result));
|
|
|
|
|
os_err<<"vkCreateSwapchainKHR failed, result="<<result<<std::endl;
|
|
|
|
|
|
|
|
|
|
return(VK_NULL_HANDLE);
|
|
|
|
|
}
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2023-10-13 01:48:07 +08:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if(dev_attr->debug_utils)
|
2023-10-13 02:01:57 +08:00
|
|
|
|
dev_attr->debug_utils->SetSwapchainKHR(swap_chain,"SwapChain");
|
2023-10-13 01:48:07 +08:00
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
|
|
|
|
return(swap_chain);
|
2019-11-26 00:22:54 +08:00
|
|
|
|
}
|
|
|
|
|
}//namespace
|
|
|
|
|
|
2021-12-15 20:52:52 +08:00
|
|
|
|
bool GPUDevice::CreateSwapchainFBO(Swapchain *swapchain)
|
2019-11-26 00:22:54 +08:00
|
|
|
|
{
|
2021-12-15 20:46:33 +08:00
|
|
|
|
if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),nullptr)!=VK_SUCCESS)
|
2019-11-26 00:22:54 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2021-12-15 20:46:33 +08:00
|
|
|
|
AutoDeleteArray<VkImage> sc_images(swapchain->color_count);
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2021-12-15 20:46:33 +08:00
|
|
|
|
if(vkGetSwapchainImagesKHR(attr->device,swapchain->swap_chain,&(swapchain->color_count),sc_images)!=VK_SUCCESS)
|
2019-11-26 00:22:54 +08:00
|
|
|
|
return(false);
|
|
|
|
|
|
2023-05-11 00:55:44 +08:00
|
|
|
|
swapchain->sc_depth =CreateTexture2D(new SwapchainDepthTextureCreateInfo(attr->physical_device->GetDepthFormat(),swapchain->extent));
|
2022-01-07 11:57:34 +08:00
|
|
|
|
|
|
|
|
|
if(!swapchain->sc_depth)
|
|
|
|
|
return(false);
|
|
|
|
|
|
2023-10-13 01:48:07 +08:00
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if(attr->debug_utils)
|
|
|
|
|
{
|
2023-10-13 02:01:57 +08:00
|
|
|
|
attr->debug_utils->SetImage(swapchain->sc_depth->GetImage(),"SwapchainDepthImage");
|
|
|
|
|
attr->debug_utils->SetImageView(swapchain->sc_depth->GetVulkanImageView(),"SwapchainDepthImageView");
|
|
|
|
|
attr->debug_utils->SetDeviceMemory(swapchain->sc_depth->GetDeviceMemory(),"SwapchainDepthMemory");
|
2023-10-13 01:48:07 +08:00
|
|
|
|
}
|
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
2023-05-11 00:55:44 +08:00
|
|
|
|
swapchain->sc_color =hgl_zero_new<Texture2D *>(swapchain->color_count);
|
|
|
|
|
swapchain->sc_fbo =hgl_zero_new<Framebuffer *>(swapchain->color_count);
|
2021-12-15 20:46:33 +08:00
|
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<swapchain->color_count;i++)
|
2021-12-15 20:52:52 +08:00
|
|
|
|
{
|
2021-12-15 20:46:33 +08:00
|
|
|
|
swapchain->sc_color[i]=CreateTexture2D(new SwapchainColorTextureCreateInfo(attr->surface_format.format,swapchain->extent,sc_images[i]));
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2022-01-07 11:57:34 +08:00
|
|
|
|
if(!swapchain->sc_color[i])
|
|
|
|
|
return(false);
|
|
|
|
|
|
2023-05-11 00:55:44 +08:00
|
|
|
|
swapchain->sc_fbo[i]=CreateFBO( device_render_pass,
|
|
|
|
|
swapchain->sc_color[i]->GetImageView(),
|
|
|
|
|
swapchain->sc_depth->GetImageView());
|
2023-10-13 01:48:07 +08:00
|
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
|
if(attr->debug_utils)
|
|
|
|
|
{
|
2023-10-13 02:01:57 +08:00
|
|
|
|
attr->debug_utils->SetImage(swapchain->sc_color[i]->GetImage(),"SwapchainColorImage_"+AnsiString::numberOf(i));
|
|
|
|
|
attr->debug_utils->SetImageView(swapchain->sc_color[i]->GetVulkanImageView(),"SwapchainColorImageView_"+AnsiString::numberOf(i));
|
2023-10-13 01:48:07 +08:00
|
|
|
|
|
2023-10-13 02:01:57 +08:00
|
|
|
|
attr->debug_utils->SetFramebuffer(swapchain->sc_fbo[i]->GetFramebuffer(),"SwapchainFBO_"+AnsiString::numberOf(i));
|
2023-10-13 01:48:07 +08:00
|
|
|
|
}
|
|
|
|
|
#endif//_DEBUG
|
2021-12-15 20:46:33 +08:00
|
|
|
|
}
|
2021-12-15 20:52:52 +08:00
|
|
|
|
|
|
|
|
|
return(true);
|
2021-12-15 20:46:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Swapchain *GPUDevice::CreateSwapchain(const VkExtent2D &acquire_extent)
|
2019-11-26 00:22:54 +08:00
|
|
|
|
{
|
2021-12-15 20:46:33 +08:00
|
|
|
|
Swapchain *swapchain=new Swapchain;
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2019-11-26 00:33:24 +08:00
|
|
|
|
swapchain->device =attr->device;
|
2021-12-15 19:55:50 +08:00
|
|
|
|
swapchain->extent =acquire_extent;
|
2021-12-15 20:46:33 +08:00
|
|
|
|
|
|
|
|
|
swapchain->swap_chain =CreateSwapChain(attr,acquire_extent);
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2019-11-26 00:33:24 +08:00
|
|
|
|
if(swapchain->swap_chain)
|
2021-12-15 20:52:52 +08:00
|
|
|
|
if(CreateSwapchainFBO(swapchain))
|
2021-12-15 20:46:33 +08:00
|
|
|
|
return(swapchain);
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2019-11-26 00:33:24 +08:00
|
|
|
|
delete swapchain;
|
|
|
|
|
swapchain=nullptr;
|
2019-11-26 00:22:54 +08:00
|
|
|
|
|
2021-12-15 20:46:33 +08:00
|
|
|
|
return(nullptr);
|
2019-11-26 00:22:54 +08:00
|
|
|
|
}
|
|
|
|
|
VK_NAMESPACE_END
|