Understanding Vulkan’s memory model
Memory allocation and management are crucial in Vulkan, as almost none of the details of memory usage are managed by Vulkan. Except for deciding the exact memory address where memory should be allocated, all other details are the responsibility of the application. This means the programmer must manage memory types, their sizes, and alignments, as well as any sub-allocations. This approach gives applications more control over memory management and allows developers to optimize their programs for specific uses. This recipe will provide some fundamental information about the types of memory provided by the API as well as a summary of how to allocate and bind that memory to resources.
Getting ready
Graphics cards come in two variants, integrated and discrete. Integrated graphics cards share the same memory as the CPU, as shown in Figure 2.1:
Figure 2.1 – Typical memory architecture for discrete graphics cards
Discrete graphics cards have their own memory (device memory) separate from the main memory (host memory), as shown in Figure 2.2:
Figure 2.2 – Typical memory architecture for integrated graphics cards
Vulkan provides different types of memory:
- Device-local memory: This type of memory is optimized for use by the GPU and is local to the device. It is typically faster than host-visible memory but is not accessible from the CPU. Usually, resources such as render targets, storage images, and buffers are stored in this memory.
- Host-visible memory: This type of memory is accessible from both the GPU and the CPU. It is typically slower than device-local memory but allows for efficient data transfer between the GPU and CPU. Reads from GPU to CPU happen across Peripheral Component Interconnect Express (PCI-E) lanes in the case of non-integrated GPU. It’s typically used to set up staging buffers, where data is stored before being transferred to device-local memory, and uniform buffers, which are constantly updated from the application.
- Host-coherent memory: This type of memory is like host-visible memory but provides guaranteed memory consistency between the GPU and CPU. This type of memory is typically slower than both device-local and host-visible memory but is useful for storing data that needs to be frequently updated by both the GPU and CPU.
Figure 2.3 summarizes the three aforementioned types of memory. Device-local memory is not visible from the host, while host-coherent and host-visible are. Copying data from the CPU to the GPU can be done using mapped memory for those two types of memory allocations. For device-local memory, it’s necessary to copy the data from the CPU to host-visible memory first using mapped memory (the staging buffer), and then perform a copy of the data from the staging buffer to the destination, the device-local memory, using a Vulkan function:
Figure 2.3 – Types of memory and their visibility from the application in Vulkan
Images are usually device-local memory, as they have their own layout that isn’t readily interpretable by the application. Buffers can be of any one of the aforementioned types.
How to do it…
A typical workflow for creating and uploading data to a buffer includes the following steps:
- Create a buffer object of type
VkBuffer
by using theVkBufferCreateInfo
structure and callingvkCreateBuffer
. - Retrieve the memory requirements based on the buffer’s properties by calling
vkGetBufferMemoryRequirements
. The device may require a certain alignment, which could affect the necessary size of the allocation to accommodate the buffer’s contents. - Create a structure of type
VkMemoryAllocateInfo
, specify the size of the allocation and the type of memory, and callvkAllocateMemory
. - Call
vkBindBufferMemory
to bind the allocation with the buffer object. - If the buffer is visible from the host, map a pointer to the destination with
vkMapMemory
, copy the data, and unmap the memory withvkUnmapMemory
. - If the buffer is a device-local buffer, copy the data to a staging buffer first, then perform the final copy from the staging buffer to the device-local memory using the
vkCmdCopyBuffer
function.
As you can see, that’s a complex procedure that can be simplified by using the VMA library, an open source library that provides a convenient and efficient way to manage memory in Vulkan. It offers a high-level interface that abstracts the complex details of memory allocation, freeing you from the burden of manual memory management.