ULRE/src/SceneGraph/Vulkan/VKDeviceAttribute.cpp

174 lines
5.3 KiB
C++
Raw Normal View History

#include<hgl/graph/VKDeviceAttribute.h>
#include<hgl/graph/VKPhysicalDevice.h>
#include<hgl/graph/VKImageView.h>
#include<hgl/graph/VKTexture.h>
#include<iostream>
VK_NAMESPACE_BEGIN
2020-11-05 14:02:35 +08:00
void SavePipelineCacheData(VkDevice device,VkPipelineCache cache,const VkPhysicalDeviceProperties &pdp);
2020-10-21 12:39:22 +08:00
GPUDeviceAttribute::GPUDeviceAttribute(VkInstance inst,const GPUPhysicalDevice *pd,VkSurfaceKHR s)
{
instance=inst;
physical_device=pd;
surface=s;
Refresh();
}
2020-10-21 12:39:22 +08:00
GPUDeviceAttribute::~GPUDeviceAttribute()
{
if(pipeline_cache)
2020-11-05 14:02:35 +08:00
{
SavePipelineCacheData(device,pipeline_cache,physical_device->GetProperties());
vkDestroyPipelineCache(device,pipeline_cache,nullptr);
2020-11-05 14:02:35 +08:00
}
if(desc_pool)
vkDestroyDescriptorPool(device,desc_pool,nullptr);
if(cmd_pool)
vkDestroyCommandPool(device,cmd_pool,nullptr);
if(device)
vkDestroyDevice(device,nullptr);
if(surface)
vkDestroySurfaceKHR(instance,surface,nullptr);
}
2020-10-21 12:39:22 +08:00
bool GPUDeviceAttribute::CheckMemoryType(uint32_t typeBits,VkMemoryPropertyFlags properties,uint32_t *typeIndex) const
{
return physical_device->CheckMemoryType(typeBits,properties,typeIndex);
}
2020-10-21 12:39:22 +08:00
void GPUDeviceAttribute::Refresh()
{
VkPhysicalDevice pdevice = *physical_device;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(pdevice, surface, &surface_caps);
{
if (surface_caps.supportedTransforms & VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR)
preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
2019-04-11 20:24:09 +08:00
else
preTransform = surface_caps.currentTransform;
2019-04-11 20:24:09 +08:00
}
{
constexpr VkCompositeAlphaFlagBitsKHR compositeAlphaFlags[4]={VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
2019-04-11 20:24:09 +08:00
VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR,
VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR,
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR };
2019-04-11 20:24:09 +08:00
2020-11-05 14:02:35 +08:00
for(auto flags:compositeAlphaFlags)
if (surface_caps.supportedCompositeAlpha & flags)
2019-04-11 20:24:09 +08:00
{
2020-11-05 14:02:35 +08:00
compositeAlpha = flags;
2019-04-11 20:24:09 +08:00
break;
}
}
{
uint32_t format_count;
surface_format.format = VK_FORMAT_B8G8R8A8_SRGB;
surface_format.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
if (vkGetPhysicalDeviceSurfaceFormatsKHR(pdevice, surface, &format_count, nullptr) == VK_SUCCESS)
{
surface_formats_list.SetCount(format_count);
if (vkGetPhysicalDeviceSurfaceFormatsKHR(pdevice, surface, &format_count, surface_formats_list.GetData()) != VK_SUCCESS)
2019-04-11 20:24:09 +08:00
{
surface_formats_list.Clear();
2019-04-11 20:24:09 +08:00
}
else
{
VkSurfaceFormatKHR *sf = surface_formats_list.GetData();
2019-04-11 20:24:09 +08:00
2021-09-23 15:23:09 +08:00
if (format_count == 1 && sf->format == VK_FORMAT_UNDEFINED)
{
}
else
2020-10-26 22:28:55 +08:00
{
surface_format.format=VK_FORMAT_UNDEFINED;
2020-10-26 22:28:55 +08:00
2020-11-17 14:33:30 +08:00
for(uint32_t i=0;i<format_count;i++)
2020-10-26 22:28:55 +08:00
{
if(sf->format>surface_format.format)
surface_format=*sf;
2020-10-26 22:28:55 +08:00
++sf;
}
}
2019-04-11 20:24:09 +08:00
}
}
}
{
uint32_t mode_count;
if (vkGetPhysicalDeviceSurfacePresentModesKHR(pdevice, surface, &mode_count, nullptr) == VK_SUCCESS)
{
present_modes.SetCount(mode_count);
if (vkGetPhysicalDeviceSurfacePresentModesKHR(pdevice, surface, &mode_count, present_modes.GetData()) != VK_SUCCESS)
present_modes.Clear();
}
}
{
uint32_t family_count;
vkGetPhysicalDeviceQueueFamilyProperties(pdevice, &family_count, nullptr);
family_properties.SetCount(family_count);
vkGetPhysicalDeviceQueueFamilyProperties(pdevice, &family_count, family_properties.GetData());
{
2019-04-11 20:24:09 +08:00
supports_present.SetCount(family_count);
VkBool32 *sp = supports_present.GetData();
for (uint32_t i = 0; i < family_count; i++)
2019-04-11 20:24:09 +08:00
{
vkGetPhysicalDeviceSurfaceSupportKHR(pdevice, i, surface, sp);
2019-04-11 20:24:09 +08:00
++sp;
}
}
2019-04-11 20:24:09 +08:00
{
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)
2019-04-11 20:24:09 +08:00
{
if (graphics_family == ERROR_FAMILY_INDEX)
graphics_family = i;
2019-04-11 20:24:09 +08:00
if (*sp)
2019-04-11 20:24:09 +08:00
{
graphics_family = i;
present_family = i;
2019-04-11 20:24:09 +08:00
break;
}
}
++fp;
++sp;
}
2019-04-11 20:24:09 +08:00
}
if (present_family == ERROR_FAMILY_INDEX)
2019-04-11 20:24:09 +08:00
{
VkBool32 *sp = supports_present.GetData();
for (uint32_t i = 0; i < family_count; i++)
2019-04-11 20:24:09 +08:00
{
if (*sp)
2019-04-11 20:24:09 +08:00
{
present_family = i;
2019-04-11 20:24:09 +08:00
break;
}
++sp;
}
}
}
}
VK_NAMESPACE_END