Initialising Objects

Objects in Vulkan are created using structs which are used to specify information about the object

The next step after initialising the layers and extensions is to initialise the application. Vulkan provides a simple, albeit slightly verbose way of initialising objects.

The initialisation of objects is divided into two parts:

  • defining an info struct
  • using the info struct to create an object of the desired type

An info struct is essentially a collection of information and parameters that will determine how the object is created. Vulkan requires that all of the information in the structs be defined in advance.

Creating an instance

A quick example of creating a Vulkan instance is shown below.

VkInstanceCreateInfo instanceInfo = {};
instanceInfo.pNext = nullptr;
instanceInfo.flags = 0;
instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceInfo.pApplicationInfo = &applicationInfo;

instanceInfo.enabledLayerCount = static_cast<uint32_t>(appManager.instanceLayerNames.size());
instanceInfo.ppEnabledLayerNames = appManager.instanceLayerNames.data();
instanceInfo.enabledExtensionCount = static_cast<uint32_t>(appManager.instanceExtensionNames.size());
instanceInfo.ppEnabledExtensionNames = appManager.instanceExtensionNames.data();
        
The specific variables of different info structs vary depending on the object being initialised, as the information required is different for each object. The variables, sType, flags, and pNext are common to all structs.
  • sType is the type of info struct
  • flags are used to pass additional information. This is usually set to zero.
  • pNext can be used to point to another struct containing additional information. This is often used in cases where an extension requires more information when setting up the object. This means the original info struct does not have to be modified by the extension.

This info struct is then used to create the object by calling the function:

vkCreateInstance(&instanceInfo, nullptr, &appManager.instance)

When this function is called, a pointer to the info struct is passed along with a pointer to variable where the newly-created instance will be returned (appManager.instance).

After creating an instance, the next steps in setting up a Vulkan application are:
  • querying the available physical devices (the GPU) to find the one required for the application
  • defining a surface to present the result of the rendering operation
  • creating a swapchain to manage how rendered images are presented to the surface

Defining a surface is platform/OS dependent and therefore requires the correct extension to be loaded.