Vulkan教程-Vulkan实例(代码示例)

时间:2022-05-14 20:51:18
索引:http://blog.csdn.net/gth2001/article/details/51316447
bool initVulkan()
{
//在创建Vulkan实例前,我们需要知道我们要启用的扩展,一般来说可以都启用,这里我们只启用两个

//下面的代码为获取实例扩展的代码,我们首先获取了扩展的总数,存在了InstanceExtensionCount中,然后创建了
//一个VkExtensionProperties数组,这里负责储存获取的实例扩展以便于后续操作
uint32_t InstanceExtensionCount = 0;

vkEnumerateInstanceExtensionProperties(NULL, &InstanceExtensionCount, NULL);

VkExtensionProperties* ExtensionProperties = new VkExtensionProperties[InstanceExtensionCount];

vkEnumerateInstanceExtensionProperties(NULL, &InstanceExtensionCount, ExtensionProperties);

//下面我们需要把需要启用的实例扩展保存到一个char*数组中,在windows下我们可能会需要VK_KHR_SURFACE_EXTENSION_NAME
//和VK_KHR_WIN32_SURFACE_EXTENSION_NAME,这两个扩展与渲染窗口有关
char* EnableExtensionNameArray[2];

for (uint32_t i = 0; i < InstanceExtensionCount; i++)
{
//surface扩展
if (strcmp(ExtensionProperties[i].extensionName, VK_KHR_SURFACE_EXTENSION_NAME) == 0)
{
EnableExtensionNameArray[0] = ExtensionProperties[i].extensionName;
}
//win32扩展
else if (strcmp(ExtensionProperties[i].extensionName, VK_KHR_WIN32_SURFACE_EXTENSION_NAME) == 0)
{
EnableExtensionNameArray[1] = ExtensionProperties[i].extensionName;
}
}

//在处理完扩展后,我们需要创建一个VkApplicationInfo,这里储存了应用程序的基本信息
VkApplicationInfo ApplicationInfo;

ApplicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
ApplicationInfo.pNext = NULL;
ApplicationInfo.apiVersion = 0;
ApplicationInfo.applicationVersion = 1;
ApplicationInfo.engineVersion = 0;
ApplicationInfo.pApplicationName = "测试工程";
ApplicationInfo.pEngineName = "GScience";

//下边是记录所创建的实例的信息
VkInstanceCreateInfo InstanceCreateInfo;

InstanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
InstanceCreateInfo.pNext = NULL;
InstanceCreateInfo.pApplicationInfo = &ApplicationInfo;
InstanceCreateInfo.enabledLayerCount = 0;
InstanceCreateInfo.enabledExtensionCount = 2;
InstanceCreateInfo.ppEnabledExtensionNames = EnableExtensionNameArray;

//创建实例
VkResult CreateResult = vkCreateInstance(&InstanceCreateInfo, NULL, &Instance);


//回收资源
delete[](ExtensionProperties);

//返回

return CreateResult == VK_SUCCESS;
}
int main()
{
if (!initVulkan())
return -1;

return 0;
}