Connecting with a Vulkan Loader library
Support for the Vulkan API is implemented by the graphics-hardware vendor and provided through graphics drivers. Each vendor can implement it in any dynamic library they choose, and can even change it with the driver update.
That's why, along with the drivers, Vulkan Loader is also installed. We can also install it from the folder in which the SDK was installed. It allows developers to access Vulkan API entry points, through a vulkan-1.dll
library on Windows OS or libvulkan.so.1
library on Linux OS, no matter what driver, from what vendor, is installed.
Vulkan Loader is responsible for transmitting Vulkan API calls to an appropriate graphics driver. On a given computer, there may be more hardware components that support Vulkan, but with Vulkan Loader, we don't need to wonder which driver we should use, or which library we should connect with to be able to use Vulkan. Developers just need to know the name of a Vulkan library: vulkan-1.dll
on Windows or libvulkan.so.1
on Linux. When we want to use Vulkan in our application, we just need to connect with it in our code (load it).
Note
On Windows OS, Vulkan Loader library is called vulkan-1.dll
. On Linux OS, Vulkan Loader library is called libvulkan.so.1
.
How to do it...
On the Windows operating system family:
- Prepare a variable of type
HMODULE
namedvulkan_library
. - Call
LoadLibrary( "vulkan-1.dll" )
and store the result of this operation in avulkan_library
variable. - Confirm that this operation has been successful by checking if a value of a
vulkan_library
variable is different thannullptr
.
On the Linux operating system family:
- Prepare a variable of type
void*
namedvulkan_library
. - Call
dlopen( "libvulkan.so.1", RTLD_NOW )
and store the result of this operation in avulkan_library
variable. - Confirm that this operation has been successful by checking if a value of a
vulkan_library
variable is different thannullptr
.
How it works...
LoadLibrary()
is a function available on Windows operating systems. dlopen()
is a function available on Linux operating systems. They both load (open) a specified dynamic-link library into a memory space of our application. This way we can load (acquire pointers of) functions implemented and exported from a given library and use them in our application.
In the case of a function exported from a Vulkan API, in which we are, of course, most interested, we load a vulkan-1.dll
library on Windows or libvulkan.so.1
library on Linux as follows:
#if defined _WIN32 vulkan_library = LoadLibrary( "vulkan-1.dll" ); #elif defined __linux vulkan_library = dlopen( "libvulkan.so.1", RTLD_NOW ); #endif if( vulkan_library == nullptr ) { std::cout << "Could not connect with a Vulkan Runtime library." << std::endl; return false; } return true;
After a successful call, we can load a Vulkan-specific function for acquiring the addresses of all other Vulkan API procedures.
See also
The following recipes in this chapter:
- Downloading Vulkan SDK
- Enabling validation layers
- Releasing a Vulkan Loader library