Reader small image

You're reading from  The Modern Vulkan Cookbook

Product typeBook
Published inApr 2024
Reading LevelN/a
PublisherPackt
ISBN-139781803239989
Edition1st Edition
Languages
Concepts
Right arrow
Authors (2):
Preetish Kakkar
Preetish Kakkar
author image
Preetish Kakkar

Preetish Kakkar is a senior computer graphics engineer with Adobe and works on the rendering engine that powers products such as Aero, Stager, and After Effects. He has worked at Microsoft, MathWorks, and Stryker, where he co-authored various rendering engines as well as using other engines such as Unreal and Unity. He has more than 15 years of software development experience along with 10+ years in 3D graphics, scientific visualization, and medical imaging.
Read more about Preetish Kakkar

Mauricio Maurer
Mauricio Maurer
author image
Mauricio Maurer

Mauricio Maurer is a computer graphics engineer with nearly 20 years of experience working with 2D and 3D rendering, computational geometry, and rasterization in the fields of scientific visualization, CAD/CAM, and social networking. He is currently a graphics software engineer at Meta, helping to develop the next generation of AR/VR devices. Mauricio holds two master's degrees in computer science with a specialization in computer graphics from SUNY Stony Brook, NY, and the Federal University of Parana, Brazil.
Read more about Mauricio Maurer

View More author details
Right arrow

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

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

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

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:

  1. Create a buffer object of type VkBuffer by using the VkBufferCreateInfo structure and calling vkCreateBuffer.
  2. 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.
  3. Create a structure of type VkMemoryAllocateInfo, specify the size of the allocation and the type of memory, and call vkAllocateMemory.
  4. Call vkBindBufferMemory to bind the allocation with the buffer object.
  5. If the buffer is visible from the host, map a pointer to the destination with vkMapMemory, copy the data, and unmap the memory with vkUnmapMemory.
  6. 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.

Previous PageNext Page
You have been reading a chapter from
The Modern Vulkan Cookbook
Published in: Apr 2024Publisher: PacktISBN-13: 9781803239989
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Preetish Kakkar

Preetish Kakkar is a senior computer graphics engineer with Adobe and works on the rendering engine that powers products such as Aero, Stager, and After Effects. He has worked at Microsoft, MathWorks, and Stryker, where he co-authored various rendering engines as well as using other engines such as Unreal and Unity. He has more than 15 years of software development experience along with 10+ years in 3D graphics, scientific visualization, and medical imaging.
Read more about Preetish Kakkar

author image
Mauricio Maurer

Mauricio Maurer is a computer graphics engineer with nearly 20 years of experience working with 2D and 3D rendering, computational geometry, and rasterization in the fields of scientific visualization, CAD/CAM, and social networking. He is currently a graphics software engineer at Meta, helping to develop the next generation of AR/VR devices. Mauricio holds two master's degrees in computer science with a specialization in computer graphics from SUNY Stony Brook, NY, and the Federal University of Parana, Brazil.
Read more about Mauricio Maurer