2019-04-30 16:42:59 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKInstance.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKSurfaceExtensionName.h>
|
|
|
|
|
#include<hgl/graph/vulkan/VKPhysicalDevice.h>
|
2019-11-08 01:58:51 +08:00
|
|
|
|
#include<hgl/graph/vulkan/VKDebugOut.h>
|
2019-04-12 22:14:40 +08:00
|
|
|
|
#include<iostream>
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
|
|
|
|
VK_NAMESPACE_BEGIN
|
2019-04-18 16:06:44 +08:00
|
|
|
|
Device *CreateRenderDevice(VkInstance,const PhysicalDevice *,Window *);
|
2019-04-09 00:22:26 +08:00
|
|
|
|
|
2019-11-13 19:55:13 +08:00
|
|
|
|
using CharPointerList=hgl::List<const char *>;
|
|
|
|
|
|
|
|
|
|
Instance *CreateInstance(const UTF8String &app_name,VKDebugOut *out,CreateInstanceLayerInfo *layer_info)
|
2019-04-09 00:22:26 +08:00
|
|
|
|
{
|
2019-04-11 02:29:21 +08:00
|
|
|
|
VkApplicationInfo app_info;
|
|
|
|
|
VkInstanceCreateInfo inst_info;
|
|
|
|
|
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.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
|
|
|
|
app_info.pNext = nullptr;
|
|
|
|
|
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;
|
|
|
|
|
app_info.apiVersion = VK_API_VERSION_1_0;
|
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
|
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
2019-04-12 22:14:40 +08:00
|
|
|
|
ext_list.Add(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
|
2019-04-19 18:09:08 +08:00
|
|
|
|
ext_list.Add(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
|
|
|
|
if(layer_info)
|
|
|
|
|
{
|
|
|
|
|
#define VK_LAYER_LUNARG_ADD(name) if(layer_info->lunarg.name)layer_list.Add("VK_LAYER_LUNARG_" #name);
|
|
|
|
|
|
|
|
|
|
VK_LAYER_LUNARG_ADD(api_dump)
|
|
|
|
|
VK_LAYER_LUNARG_ADD(device_simulation)
|
|
|
|
|
VK_LAYER_LUNARG_ADD(monitor)
|
|
|
|
|
VK_LAYER_LUNARG_ADD(screenshot)
|
|
|
|
|
VK_LAYER_LUNARG_ADD(standard_validation)
|
|
|
|
|
VK_LAYER_LUNARG_ADD(vktrace)
|
|
|
|
|
|
|
|
|
|
#define VK_LAYER_KHRONOS_ADD(name) if(layer_info->khronos.name)layer_list.Add("VK_LAYER_KHRONOS_" #name);
|
|
|
|
|
|
|
|
|
|
VK_LAYER_KHRONOS_ADD(validation)
|
|
|
|
|
|
|
|
|
|
#define VK_LAYER_NV_ADD(name) if(layer_info->nv.name)layer_list.Add("VK_LAYER_NV_" #name);
|
|
|
|
|
|
|
|
|
|
VK_LAYER_NV_ADD(optimus)
|
2019-04-20 00:51:03 +08:00
|
|
|
|
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#define VK_LAYER_VALVE_ADD(name) if(layer_info->valve.name)layer_list.Add("VK_LAYER_VALVE_" #name);
|
|
|
|
|
|
|
|
|
|
VK_LAYER_VALVE_ADD(steam_overlay)
|
|
|
|
|
VK_LAYER_VALVE_ADD(steam_fossilize)
|
|
|
|
|
|
|
|
|
|
#define VK_LAYER_RENDERDOC_ADD(name) if(layer_info->RenderDoc.name)layer_list.Add("VK_LAYER_RENDERDOC_" #name);
|
|
|
|
|
|
|
|
|
|
VK_LAYER_RENDERDOC_ADD(Capture)
|
|
|
|
|
|
|
|
|
|
#define VK_LAYER_BANDICAM_ADD(name) if(layer_info->bandicam.name)layer_list.Add("VK_LAYER_bandicam_" #name);
|
|
|
|
|
|
|
|
|
|
VK_LAYER_BANDICAM_ADD(helper)
|
|
|
|
|
}
|
2019-04-19 18:09:08 +08:00
|
|
|
|
|
2019-11-06 20:52:09 +08:00
|
|
|
|
inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
|
inst_info.pNext = nullptr;
|
|
|
|
|
inst_info.flags = 0;
|
|
|
|
|
inst_info.pApplicationInfo = &app_info;
|
|
|
|
|
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
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
if(vkCreateInstance(&inst_info,nullptr,&inst)==VK_SUCCESS)
|
2019-11-06 20:52:09 +08:00
|
|
|
|
{
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#ifdef _DEBUG
|
2019-11-08 01:58:51 +08:00
|
|
|
|
if(!out)
|
|
|
|
|
out=new VKDebugOut;
|
2019-11-13 19:55:13 +08:00
|
|
|
|
#endif//_DEBUG
|
|
|
|
|
|
|
|
|
|
if(out)
|
|
|
|
|
out->Init(inst);
|
|
|
|
|
|
2019-11-13 16:10:42 +08:00
|
|
|
|
return(new Instance(inst,out));
|
2019-11-06 20:52:09 +08:00
|
|
|
|
}
|
2019-04-09 02:02:43 +08:00
|
|
|
|
|
2019-04-11 02:29:21 +08:00
|
|
|
|
return(nullptr);
|
2019-04-09 00:22:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-13 16:10:42 +08:00
|
|
|
|
Instance::Instance(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++)
|
|
|
|
|
physical_devices.Add(new PhysicalDevice(inst,pd_list[i]));
|
2019-04-19 16:32:54 +08:00
|
|
|
|
|
|
|
|
|
delete[] pd_list;
|
2019-04-09 02:02:43 +08:00
|
|
|
|
}
|
2019-04-11 02:29:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Instance::~Instance()
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-04-13 21:44:26 +08:00
|
|
|
|
const PhysicalDevice *Instance::GetDevice(VkPhysicalDeviceType type)const
|
|
|
|
|
{
|
|
|
|
|
const uint32_t count=physical_devices.GetCount();
|
|
|
|
|
PhysicalDevice **pd=physical_devices.GetData();
|
|
|
|
|
|
|
|
|
|
for(uint32_t i=0;i<count;i++)
|
|
|
|
|
{
|
|
|
|
|
if((*pd)->GetDeviceType()==type)
|
|
|
|
|
return(*pd);
|
|
|
|
|
|
|
|
|
|
++pd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return(nullptr);
|
|
|
|
|
}
|
2019-04-09 00:22:26 +08:00
|
|
|
|
VK_NAMESPACE_END
|