2020-10-21 11:43:18 +08:00
|
|
|
|
#include<hgl/graph/VKInstance.h>
|
|
|
|
|
#include<hgl/graph/VKSurfaceExtensionName.h>
|
|
|
|
|
#include<hgl/graph/VKPhysicalDevice.h>
|
|
|
|
|
#include<hgl/graph/VKDebugOut.h>
|
2019-04-12 22:14:40 +08:00
|
|
|
|
#include<iostream>
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2020-10-21 12:39:22 +08:00
|
|
|
|
GPUDevice *CreateRenderDevice(VkInstance,const GPUPhysicalDevice *,Window *);
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-11-13 20:50:34 +08:00
|
|
|
|
void CheckInstanceLayer(CharPointerList &layer_list,CreateInstanceLayerInfo *layer_info);
|
2019-11-13 19:55:13 +08:00
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
VulkanInstance *CreateInstance(const AnsiString &app_name,VKDebugOut *out,CreateInstanceLayerInfo *layer_info)
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2020-09-28 11:16:45 +08:00
|
|
|
|
ApplicationInfo app_info;
|
|
|
|
|
InstanceCreateInfo inst_info(&app_info);
|
2019-04-11 02:29:21 +08:00
|
|
|
|
CharPointerList ext_list;
|
2019-11-11 20:26:35 +08:00
|
|
|
|
CharPointerList layer_list;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-11-11 20:26:35 +08:00
|
|
|
|
app_info.pApplicationName = app_name.c_str();
|
2019-04-09 00:22:26 +08:00
|
|
|
|
app_info.applicationVersion = 1;
|
2019-11-11 20:26:35 +08:00
|
|
|
|
app_info.pEngineName = "CMGameEngine/ULRE";
|
|
|
|
|
app_info.engineVersion = 1;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-10 01:13:31 +08:00
|
|
|
|
ext_list.Add(VK_KHR_SURFACE_EXTENSION_NAME);
|
2019-04-12 22:14:40 +08:00
|
|
|
|
ext_list.Add(HGL_VK_SURFACE_EXTENSION_NAME); //此宏在VKSurfaceExtensionName.h中定义
|
2019-11-13 19:55:13 +08:00
|
|
|
|
|
2021-09-23 21:20:29 +08:00
|
|
|
|
constexpr char *require_ext_list[]=
|
|
|
|
|
{
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#ifdef _DEBUG
|
2021-09-23 21:20:29 +08:00
|
|
|
|
VK_EXT_DEBUG_REPORT_EXTENSION_NAME,
|
|
|
|
|
VK_EXT_DEBUG_UTILS_EXTENSION_NAME,
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#endif//_DEBUG
|
2021-12-16 17:43:28 +08:00
|
|
|
|
VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME,
|
2021-12-16 18:26:02 +08:00
|
|
|
|
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
|
|
|
|
|
VK_KHR_GET_SURFACE_CAPABILITIES_2_EXTENSION_NAME,
|
2021-09-23 21:20:29 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for(const char *ext_name:require_ext_list)
|
|
|
|
|
if(CheckInstanceExtensionSupport(ext_name))
|
|
|
|
|
ext_list.Add(ext_name);
|
2019-11-13 19:55:13 +08:00
|
|
|
|
|
|
|
|
|
if(layer_info)
|
2020-10-28 19:49:47 +08:00
|
|
|
|
{
|
2019-11-13 20:50:34 +08:00
|
|
|
|
CheckInstanceLayer(layer_list,layer_info);
|
2020-10-28 19:49:47 +08:00
|
|
|
|
}
|
2019-04-19 18:09:08 +08:00
|
|
|
|
|
2019-11-06 20:52:09 +08:00
|
|
|
|
inst_info.enabledExtensionCount = ext_list.GetCount();
|
|
|
|
|
inst_info.ppEnabledExtensionNames = ext_list.GetData();
|
2019-11-11 20:26:35 +08:00
|
|
|
|
inst_info.enabledLayerCount = layer_list.GetCount();
|
|
|
|
|
inst_info.ppEnabledLayerNames = layer_list.GetData();
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
VkInstance inst;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2021-12-16 17:43:28 +08:00
|
|
|
|
app_info.apiVersion = VK_API_VERSION_1_2;
|
|
|
|
|
if(vkCreateInstance(&inst_info,nullptr,&inst)!=VK_SUCCESS)
|
2019-11-06 20:52:09 +08:00
|
|
|
|
{
|
2021-12-16 17:43:28 +08:00
|
|
|
|
app_info.apiVersion = VK_API_VERSION_1_1;
|
|
|
|
|
|
|
|
|
|
if(vkCreateInstance(&inst_info,nullptr,&inst)!=VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
app_info.apiVersion = VK_API_VERSION_1_0;
|
|
|
|
|
|
|
|
|
|
if(!vkCreateInstance(&inst_info,nullptr,&inst)==VK_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#ifdef _DEBUG
|
2021-12-16 17:43:28 +08:00
|
|
|
|
if(!out)
|
|
|
|
|
out=new VKDebugOut;
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
2021-12-16 17:43:28 +08:00
|
|
|
|
if(out)
|
|
|
|
|
out->Init(inst);
|
2019-04-09 02:02:43 +08:00
|
|
|
|
|
2021-12-16 17:43:28 +08:00
|
|
|
|
return(new VulkanInstance(inst,out));
|
2019-04-09 00:22:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
VulkanInstance::VulkanInstance(VkInstance i,VKDebugOut *out)
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2019-04-11 02:29:21 +08:00
|
|
|
|
inst=i;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-11-08 01:58:51 +08:00
|
|
|
|
debug_out=out;
|
2019-04-12 22:14:40 +08:00
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
uint32_t gpu_count = 1;
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
if(vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr)==VK_SUCCESS)
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2019-04-13 21:44:26 +08:00
|
|
|
|
VkPhysicalDevice *pd_list=new VkPhysicalDevice[gpu_count];
|
|
|
|
|
vkEnumeratePhysicalDevices(inst, &gpu_count,pd_list);
|
|
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<gpu_count;i++)
|
2020-10-21 12:39:22 +08:00
|
|
|
|
physical_devices.Add(new GPUPhysicalDevice(inst,pd_list[i]));
|
2019-04-19 16:32:54 +08:00
|
|
|
|
|
|
|
|
|
delete[] pd_list;
|
2019-04-09 02:02:43 +08:00
|
|
|
|
}
|
2021-09-23 22:13:57 +08:00
|
|
|
|
|
|
|
|
|
GetDeviceProcAddr = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(inst, "vkGetDeviceProcAddr");
|
2019-04-11 02:29:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
VulkanInstance::~VulkanInstance()
|
2019-11-08 01:58:51 +08:00
|
|
|
|
{
|
2019-11-06 20:52:09 +08:00
|
|
|
|
SAFE_CLEAR(debug_out);
|
|
|
|
|
|
|
|
|
|
physical_devices.Clear();
|
2019-04-11 02:29:21 +08:00
|
|
|
|
vkDestroyInstance(inst,nullptr);
|
2019-04-09 00:22:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-21 12:39:22 +08:00
|
|
|
|
const GPUPhysicalDevice *VulkanInstance::GetDevice(VkPhysicalDeviceType type)const
|
2019-04-13 21:44:26 +08:00
|
|
|
|
{
|
2020-10-28 17:28:07 +08:00
|
|
|
|
for(GPUPhysicalDevice *pd:physical_devices)
|
|
|
|
|
if(pd->GetDeviceType()==type)
|
|
|
|
|
return(pd);
|
2019-04-13 21:44:26 +08:00
|
|
|
|
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2021-09-23 22:13:57 +08:00
|
|
|
|
|
|
|
|
|
void VulkanInstance::DestroySurface(VkSurfaceKHR surface)
|
|
|
|
|
{
|
|
|
|
|
vkDestroySurfaceKHR(inst,surface,nullptr);
|
|
|
|
|
}
|
2019-04-09 00:22:26 +08:00
|
|
|
|
VK_NAMESPACE_END
|