Skip to content
systemcoding.dev
GPU

Create a VkInstance with vkCreateInstance

Create a VkInstance for Vulkan 1.3 with vkCreateInstance, asking GLFW for the surface extensions the window system needs.

1 min read
Summarize in AI

Every Vulkan program starts with an instance. [vkCreateInstance](https://registry.khronos.org/vulkan/specs/latest/man/html/vkCreateInstance.html) takes the API version you intend to use and the instance extensions you need. For a window, GLFW 3.4 reports the surface extensions for the current platform, so you never hard-code VK_KHR_xcb_surface and friends.

instance.cc
#include <GLFW/glfw3.h>
#include <vulkan/vulkan.h>

VkInstance make_instance(void)
{
    uint32_t count = 0;
    const char **exts = glfwGetRequiredInstanceExtensions(&count);

    VkApplicationInfo app = {
        .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
        .pApplicationName = "demo",
        .apiVersion = VK_API_VERSION_1_3,
    };
    VkInstanceCreateInfo ci = {
        .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
        .pApplicationInfo = &app,
        .enabledExtensionCount = count,
        .ppEnabledExtensionNames = exts,
    };
    VkInstance instance = VK_NULL_HANDLE;
    if (vkCreateInstance(&ci, NULL, &instance) != VK_SUCCESS)
        return VK_NULL_HANDLE;
    return instance;
}

apiVersion is the highest version your code is written for, not a requirement. A loader that only supports 1.1 still creates the instance, but you then must not call 1.3 functions on it. Leaving the field at zero is legal and means 1.0, so setting it is how you unlock the newer core functions.

The pointer GLFW returns belongs to GLFW and stays valid until glfwTerminate, so you can pass it straight in. If creation returns VK_ERROR_EXTENSION_NOT_PRESENT, the driver lacks a surface extension, and VK_ERROR_INCOMPATIBLE_DRIVER means no driver supports the requested version.

Taggedcvulkan
Share

More snippets

GPU

List platforms with clGetPlatformIDs

Call clGetPlatformIDs twice, once for the count and once for the handles, then print each platform name with clGetPlatformInfo in OpenCL 3.0 C.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.