vkCreateInstance
函数原型
VkResult vkCreateInstance(
const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance);
描述
Vulkan中没有全局状态,与应用程序相关的状态都存储在 VkInstance
对象中。应用创建 VkInstance
对象时会初始化Vulkan库,并将自身的状态信息存储在 VkInstance
对象中。
参数
pCreateInfo
: 指向控制实例创建的VkInstanceCreateInfo
结构体的指针。pAllocator
: host memory分配器的指针。pInstance
: 创建的Instance对象句柄。
返回值
VK_SUCCESS
: 成功创建Instance对象。- 其他 : 创建失败。
代码示例
VkInstance instance = VK_NULL_HANDLE;
VkInstanceCreateInfo instanceCreateInfo = {};
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.pNext = NULL;
// app info
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "test";
appInfo.pEngineName = "test";
appInfo.apiVersion = VK_MAKE_API_VERSION(0, 1, 3, 0);
instanceCreateInfo.pApplicationInfo = &appInfo;
// instance extension
std::vector<const char*> instanceExtensions = { VK_KHR_SURFACE_EXTENSION_NAME };
instanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
instanceCreateInfo.enabledExtensionCount = (uint32_t)instanceExtensions.size();
instanceCreateInfo.ppEnabledExtensionNames = instanceExtensions.data();
// enable validation
const char* validationLayerName = "VK_LAYER_KHRONOS_validation";
instanceCreateInfo.ppEnabledLayerNames = &validationLayerName;
instanceCreateInfo.enabledLayerCount = 1;
// create instance
VkResult result = vkCreateInstance(&instanceCreateInfo, nullptr, &instance);