增加创建Swapchain代码
This commit is contained in:
parent
fd8cd15ad8
commit
dfd4baa217
@ -13,6 +13,33 @@ RenderSurfaceAttribute::RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice
|
||||
vkGetPhysicalDeviceProperties(physical_device,&properties);
|
||||
vkGetPhysicalDeviceMemoryProperties(physical_device,&memory_properties);
|
||||
|
||||
{
|
||||
if(surface_caps.supportedTransforms&VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
|
||||
{
|
||||
preTransform=VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
||||
}
|
||||
else
|
||||
{
|
||||
preTransform=surface_caps.currentTransform;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
constexpr VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4]={VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
||||
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
|
||||
VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
|
||||
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR};
|
||||
|
||||
for(uint32_t i=0; i<sizeof(compositeAlphaFlags); i++)
|
||||
{
|
||||
if(surface_caps.supportedCompositeAlpha&compositeAlphaFlags[i])
|
||||
{
|
||||
compositeAlpha=compositeAlphaFlags[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
uint32_t format_count;
|
||||
if(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device,surface,&format_count,nullptr)==VK_SUCCESS)
|
||||
@ -20,7 +47,19 @@ RenderSurfaceAttribute::RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice
|
||||
surface_formts.SetCount(format_count);
|
||||
|
||||
if(vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device,surface,&format_count,surface_formts.GetData())!=VK_SUCCESS)
|
||||
{
|
||||
surface_formts.Clear();
|
||||
format=VK_FORMAT_B8G8R8A8_UNORM;
|
||||
}
|
||||
else
|
||||
{
|
||||
VkSurfaceFormatKHR *sf=surface_formts.GetData();
|
||||
|
||||
if(format_count==1&&sf->format==VK_FORMAT_UNDEFINED)
|
||||
format=VK_FORMAT_B8G8R8A8_UNORM;
|
||||
else
|
||||
format=sf->format;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,28 +79,60 @@ RenderSurfaceAttribute::RenderSurfaceAttribute(VkInstance inst,VkPhysicalDevice
|
||||
family_properties.SetCount(family_count);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(physical_device,&family_count,family_properties.GetData());
|
||||
|
||||
VkQueueFamilyProperties *fp=family_properties.GetData();
|
||||
|
||||
supports_present.SetCount(family_count);
|
||||
VkBool32 *sp=supports_present.GetData();
|
||||
for(uint32_t i=0; i<family_count; i++)
|
||||
{
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(physical_device,i,surface,sp);
|
||||
|
||||
if(family_index==-1)
|
||||
supports_present.SetCount(family_count);
|
||||
VkBool32 *sp=supports_present.GetData();
|
||||
for(uint32_t i=0; i<family_count; i++)
|
||||
{
|
||||
if(*sp&&(fp->queueFlags&VK_QUEUE_GRAPHICS_BIT))
|
||||
family_index=i;
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(physical_device,i,surface,sp);
|
||||
++sp;
|
||||
}
|
||||
}
|
||||
|
||||
++fp;
|
||||
++sp;
|
||||
{
|
||||
VkQueueFamilyProperties *fp=family_properties.GetData();
|
||||
VkBool32 *sp=supports_present.GetData();
|
||||
for(uint32_t i=0; i<family_count; i++)
|
||||
{
|
||||
if(fp->queueFlags&VK_QUEUE_GRAPHICS_BIT)
|
||||
{
|
||||
if(graphics_family==ERROR_FAMILY_INDEX)
|
||||
graphics_family=i;
|
||||
|
||||
if(*sp)
|
||||
{
|
||||
graphics_family=i;
|
||||
present_family=i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
++fp;
|
||||
++sp;
|
||||
}
|
||||
}
|
||||
|
||||
if(present_family==ERROR_FAMILY_INDEX)
|
||||
{
|
||||
VkBool32 *sp=supports_present.GetData();
|
||||
for(uint32_t i=0; i<family_count; i++)
|
||||
{
|
||||
if(*sp)
|
||||
{
|
||||
present_family=i;
|
||||
break;
|
||||
}
|
||||
++sp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RenderSurfaceAttribute::~RenderSurfaceAttribute()
|
||||
{
|
||||
if(swap_chain)
|
||||
vkDestroySwapchainKHR(device,swap_chain,nullptr);
|
||||
|
||||
if(cmd_pool)
|
||||
vkDestroyCommandPool(device,cmd_pool,nullptr);
|
||||
|
||||
|
@ -3,6 +3,9 @@
|
||||
#include"VK.h"
|
||||
|
||||
VK_NAMESPACE_BEGIN
|
||||
|
||||
constexpr uint32_t ERROR_FAMILY_INDEX=UINT32_MAX;
|
||||
|
||||
struct RenderSurfaceAttribute
|
||||
{
|
||||
VkInstance instance =nullptr;
|
||||
@ -11,7 +14,8 @@ struct RenderSurfaceAttribute
|
||||
VkSurfaceCapabilitiesKHR surface_caps;
|
||||
VkExtent2D swapchain_extent;
|
||||
|
||||
int family_index =-1;
|
||||
uint32_t graphics_family =ERROR_FAMILY_INDEX;
|
||||
uint32_t present_family =ERROR_FAMILY_INDEX;
|
||||
|
||||
List<VkQueueFamilyProperties> family_properties;
|
||||
List<VkBool32> supports_present;
|
||||
@ -21,10 +25,15 @@ struct RenderSurfaceAttribute
|
||||
VkPhysicalDeviceMemoryProperties memory_properties;
|
||||
|
||||
List<VkSurfaceFormatKHR> surface_formts;
|
||||
VkFormat format;
|
||||
List<VkPresentModeKHR> present_modes;
|
||||
|
||||
VkSurfaceTransformFlagBitsKHR preTransform;
|
||||
VkCompositeAlphaFlagBitsKHR compositeAlpha =VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
|
||||
VkDevice device =nullptr;
|
||||
VkCommandPool cmd_pool =nullptr;
|
||||
VkSwapchainKHR swap_chain =nullptr;
|
||||
|
||||
public:
|
||||
|
||||
|
@ -33,12 +33,12 @@ namespace
|
||||
}
|
||||
}
|
||||
|
||||
VkDevice CreateDevice(VkInstance instance,VkPhysicalDevice physical_device,int family_index)
|
||||
VkDevice CreateDevice(VkInstance instance,VkPhysicalDevice physical_device,uint32_t graphics_family)
|
||||
{
|
||||
float queue_priorities[1]={0.0};
|
||||
|
||||
VkDeviceQueueCreateInfo queue_info;
|
||||
queue_info.queueFamilyIndex=family_index;
|
||||
queue_info.queueFamilyIndex=graphics_family;
|
||||
queue_info.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||
queue_info.pNext=nullptr;
|
||||
queue_info.queueCount=1;
|
||||
@ -64,13 +64,13 @@ namespace
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
VkCommandPool CreateCommandPool(VkDevice device,int family_index)
|
||||
VkCommandPool CreateCommandPool(VkDevice device,uint32_t graphics_family)
|
||||
{
|
||||
VkCommandPoolCreateInfo cmd_pool_info={};
|
||||
|
||||
cmd_pool_info.sType=VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
||||
cmd_pool_info.pNext=nullptr;
|
||||
cmd_pool_info.queueFamilyIndex=family_index;
|
||||
cmd_pool_info.queueFamilyIndex=graphics_family;
|
||||
cmd_pool_info.flags=0;
|
||||
|
||||
VkCommandPool cmd_pool;
|
||||
@ -80,6 +80,48 @@ namespace
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
|
||||
VkSwapchainKHR CreateSwapChain(RenderSurfaceAttribute *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=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;
|
||||
swapchain_ci.imageSharingMode=VK_SHARING_MODE_EXCLUSIVE;
|
||||
swapchain_ci.queueFamilyIndexCount=0;
|
||||
swapchain_ci.pQueueFamilyIndices=nullptr;
|
||||
|
||||
uint32_t queueFamilyIndices[2]={(uint32_t)rsa->graphics_family, (uint32_t)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;
|
||||
}
|
||||
|
||||
VkSwapchainKHR swap_chain;
|
||||
|
||||
if(vkCreateSwapchainKHR(rsa->device,&swapchain_ci,nullptr,&swap_chain)==VK_SUCCESS)
|
||||
return(swap_chain);
|
||||
|
||||
return(nullptr);
|
||||
}
|
||||
}//namespace
|
||||
|
||||
RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_device,Window *win)
|
||||
@ -93,19 +135,24 @@ RenderSurface *CreateRenderSuface(VkInstance inst,VkPhysicalDevice physical_devi
|
||||
|
||||
rsa->swapchain_extent=GetSwapchainExtent(rsa->surface_caps,win->GetWidth(),win->GetHeight());
|
||||
|
||||
if(rsa->family_index==-1)
|
||||
if(rsa->graphics_family==ERROR_FAMILY_INDEX)
|
||||
return(nullptr);
|
||||
|
||||
rsa->device=CreateDevice(inst,physical_device,rsa->family_index);
|
||||
rsa->device=CreateDevice(inst,physical_device,rsa->graphics_family);
|
||||
|
||||
if(!rsa->device)
|
||||
return(nullptr);
|
||||
|
||||
rsa->cmd_pool=CreateCommandPool(rsa->device,rsa->family_index);
|
||||
rsa->cmd_pool=CreateCommandPool(rsa->device,rsa->graphics_family);
|
||||
|
||||
if(!rsa->cmd_pool)
|
||||
return(nullptr);
|
||||
|
||||
rsa->swap_chain=CreateSwapChain(rsa);
|
||||
|
||||
if(!rsa->swap_chain)
|
||||
return(nullptr);
|
||||
|
||||
return(new RenderSurface(rsa));
|
||||
}
|
||||
VK_NAMESPACE_END
|
||||
|
Loading…
x
Reference in New Issue
Block a user