Foundations
The Vulkan loader
A program never calls the GPU driver by name. Each Vulkan call goes through a small library, the loader, which finds the driver, puts optional layers such as validation in between, and passes the call on. This page explains that path, the two ways a program can get the functions, and why the version of the loader and the version of the GPU are two different numbers. The last part shows how RASM does it.
In short
- The loader (vulkan-1.dll, libvulkan) sits between the program and the driver. Layers can sit in between too.
- A function the program gets from the loader is a trampoline: it reads which driver the handle belongs to, then jumps there. One extra jump per call.
- Linked against the loader, the program cannot start when the loader lacks one of its functions. Loaded at run time, the program starts, checks, and says what is wrong.
- Functions come in three levels: global, instance and device. Device functions from vkGetDeviceProcAddr skip the trampoline, but they belong to one device: keep a table per device.
- The loader's version limits instance-level features. Device commands need the GPU's version, and come from the driver. A 1.3 loader can run a 1.4 GPU.
1From a call to the driver
Follow one draw call. It leaves the program, passes the loader and maybe some layers, and ends in the driver of the GPU that owns the command buffer.
vkCmdDraw is a trampoline: it reads that pointer and jumps to the table's entry. Handles of other kinds (buffers, images) are plain numbers.The table belongs to one device, because each device can have another driver behind it: two GPUs from two vendors, or a GPU and a software rasterizer. The trampoline is what lets one function name serve them all.
2Linked or loaded
A program can get the loader's functions in two ways. The difference shows on a machine whose loader is older than the program expects.
Linked: the OS checks first
The import table names each function. One missing name, and the OS stops the program before main, with a system error. The program never gets to explain.
Loaded: the program checks
It opens the loader in main, asks for each function by name, and gets null for one that does not exist. It can say "update your driver", or work without that feature.
Loading also means the program starts on a machine with no Vulkan at all: tools and tests that never touch the GPU run anywhere. Libraries such as volk do the loading: one call opens the loader, and the functions are then plain function pointers with the usual names.
3Three levels of functions
Which function you can get, and from where, depends on what its first parameter is.
Global
No handle yet: vkCreateInstance, vkEnumerateInstanceVersion, the instance layers and extensions.
Instance
The first parameter is an instance or a physical device: vkEnumeratePhysicalDevices, the properties and features of a GPU, surface queries. Trampolines.
Device
Almost every call of a frame: vkCmdDraw, vkQueueSubmit2, vkCreateBuffer. From vkGetDeviceProcAddr they skip the trampoline.
| You ask | For a device function you get | Valid for |
|---|---|---|
| the import library (linked) | the loader's trampoline | every device |
vkGetInstanceProcAddr(instance, ...) | the loader's trampoline | every device of that instance |
| vkGetDeviceProcAddr(device, ...) | the first layer's function, or the driver's | that one device only |
4A table per device
A function from vkGetDeviceProcAddr saves a jump on every call, but it is tied to the device it came from. Where to keep it?
Global trampolines
Right for every device, one extra jump per call. What linking gives you.
correct, a little slower
Globals from one device
Fast for device A. A second device calls A's functions, which may belong to another driver.
wrong with two devices
A table per device
Each device object holds a struct of pointers and calls through it: table.vkCmdDraw(cmd, ...).
correct and direct
How much does the jump cost? A few nanoseconds: a load, an indirect jump, and a cache line that is usually warm. A frame that records ten thousand commands pays tens of microseconds. A GPU-driven renderer records few commands per frame (one indirect draw draws thousands of objects), so there the saving is small. The table is worth having mostly for correctness with several devices.
With a table per device, also remove the global device functions from the build, so a call that skips the table does not compile. volk does this with VOLK_NO_DEVICE_PROTOTYPES; Vulkan's own header does it with VK_NO_PROTOTYPES.
5Whose version?
"Vulkan 1.4" can mean two things. The loader's version (vkEnumerateInstanceVersion) says which instance-level features the instance has. The GPU's version (VkPhysicalDeviceProperties::apiVersion) says which device features and commands its driver has.
instance features ≤ min(loader version, apiVersion)
device features ≤ min(GPU version, apiVersion)
- apiVersion
- the highest version the program asks for when it creates the instance
- loader version
- comes with the GPU driver installer, or with another program that put its own loader next to it
- GPU version
- what the driver of that one GPU supports; two GPUs in one machine can differ
Vulkan 1.4 added only device commands (host image copy, push descriptors, vkCmdBindIndexBuffer2, ...), and no new instance or physical-device commands. A device command from vkGetDeviceProcAddr comes from the driver, which knows it, even when the loader is older. So a program that loads its functions needs only the loader version of its instance features, and a 1.4 GPU. A linked program needs every name in the loader. Try both:
main. Load it instead: it runs. Then pick a 1.3 GPU or a 1.2 loader and read what it says.Windows looks for a DLL in the program's own folder before the system folder. A browser, a game launcher or a tool may ship its own vulkan-1.dll next to its executable, and an old one there wins for that program. The system's loader comes with the GPU driver and is usually new.
6Pitfalls
- The program does not start, with a system error about an entry point
- It is linked against the loader and uses a function this loader does not have. Load the functions at run time instead, and check the version yourself.
- A link error for every
vk...function - The header declared prototypes (no
VK_NO_PROTOTYPES) and the code called them, but nothing links the loader. Call through the loaded pointers or the device table. - A function pointer is null
- Its extension or version was not enabled when the instance or device was made, or the loader or driver does not have it. Enable it first, and check for null.
- The window's surface belongs to another loader
- A windowing library (SDL, GLFW) may open the loader itself. If the program opens another copy, by another name or path, the surface and the instance come from two libraries. Take the windowing library's
vkGetInstanceProcAddr, or give it yours. - The loader is not found on macOS
- libvulkan.dylib lives in /usr/local/lib after the SDK's system install, and the dynamic linker may not search there for a plain name. Try that path too, or set DYLD_LIBRARY_PATH.
- Calls are slow only with validation
- With layers on, a device pointer leads to the first layer, which checks the call. That is expected; the table still skips the loader's trampoline.
7In RASM
Nothing in RASM links the loader. volk opens it when the device starts, with a window or without one, and each VulkanDevice keeps its own table of device functions. SDL never opens a loader: a window reaches the device as native handles, and RASM makes the surface itself with its own instance.
VolkDeviceTable.| This page | RASM |
|---|---|
| Link or load | Load, with volk (vcpkg port volk). No executable imports vulkan-1.dll or libvulkan; CI checks every executable and DLL of the build with llvm-objdump and otool. |
| Which loader | volkInitialize, with a window or headless; on macOS it tries /usr/local/lib/libvulkan.dylib too. The surface comes from the window's NativeWindow through that loader (createVulkanSurface), so no windowing library opens a second copy. |
| Versions | The loader must be 1.3 or newer and the GPU 1.4. Each check logs the version it found. A 1.3 loader runs RASM, as the image tests show on CI's runner. |
| Global and instance functions | volk's globals, one set per process, loaded with volkLoadInstanceOnly. They are trampolines, so a second instance does not break them. |
| A table per device | VulkanDevice::vk, a VolkDeviceTable. VOLK_NO_DEVICE_PROTOTYPES leaves no global device function, so a call outside the table does not compile. vkSetDebugUtilsObjectNameEXT belongs to an instance extension and stays global. |
| Other libraries | vk-bootstrap gets volk's vkGetInstanceProcAddr; VMA gets its pointers from vmaImportVulkanFunctionsFromVolk. The Vulkan headers come from vcpkg at volk's version, so all of them see one header. |
| Programs with no GPU | rasm_tests never creates a device, so it starts with no Vulkan on the machine (the macOS CI job installs none). |
In examples/05_city (headless, Release, RTX 3060), the CPU time to record a frame's commands (the self time of RenderGraph::execute) went down a little when the device calls left the trampoline, within the run-to-run noise of 5 to 8%:
Where it lives in code
| What | Where |
|---|---|
| Open the loader, check versions | loadVulkan, logOldDevices in src/rhi/vulkan/vulkanDevice.cpp |
| The device table, VMA | VulkanDevice::initialize in the same file |
| Calls through the table | src/rhi/vulkan/vulkanCommandBuffer.cpp, src/rhi/vulkan/vulkanQueue.cpp, src/rhi/vulkan/vulkanSwapchain.cpp |
| The surface | createVulkanSurface in src/rhi/vulkan/vulkanSurface.cpp |
| Build | src/CMakeLists.txt, vcpkg.json, cmake/rasmConfig.cmake.in |
| CI check | .github/workflows/ci.yml |
Call device functions through the device's table (vk.vkCmdDraw, getVkTable()), never through a global. Do not add Vulkan::Vulkan or any other loader link; CI fails on it. A library that calls Vulkan itself gets volk's pointers, not its own loader.
Further reading
- Khronos, "Architecture of the Vulkan Loader Interfaces" (LoaderInterfaceArchitecture.md in the Vulkan-Loader repository).
- volk, "Meta loader for Vulkan API" (its README: tables,
VOLK_NO_DEVICE_PROTOTYPES). - Graphics APIs and the RHI, Headless rendering and readback, Image tests.