When an image resource object (VkImage
) is created, it contains a logical allocation. The image has no physical association with the device memory at that point. The actual memory backing is provided separately at a later stage. The physical allocation is very type-dependent; the images can be categorized into sparse and non-sparse. The sparse resource is specified using sparse creation flags (VkImageCreateFlagBits
in VkImageCreateInfo
); however, if the flag is not specified, it is a non-sparse image resource. This chapter will only address non-sparse memory as a reference. For more information on sparse resource allocation, refer to the official Vulkan 1.0 specification.
The association of an image with memory is a three-step process: gathering memory allocation requirements for image allocation, allocating the physical chunk on the device memory, and binding the allocated memory to the image resource. Let's take a look at this in detail.
The non-sparse image resources memory requirement can be queried using the vkGetImageMemoryRequirements()
API. Here is the syntax of this:
void vkGetImageMemoryRequirements( VkDevice device, VkImage image, VkMemoryRequirements* pMemoryRequirements);
The following are the different fields of the vkGetImageMemoryRequirements()
API:
Parameters |
Description |
|
This refers to the device that owns the image. |
|
This refers to the |
|
This returns the |
The VkMemoryRequirements
structure object contains the memory requirements associated with the image object that we pass to vkGetImageMemoryRequirements()
. Here's the syntax of this:
typedef struct VkMemoryRequirements { VkDeviceSize size; VkDeviceSize alignment; uint32_t memoryTypeBits; } VkMemoryRequirements;
The parameters of this structure and their respective descriptions are as follows:
Parameters |
Description |
|
This specifies the size of the image resource required in bytes. |
|
This refers to the alignment offset in bytes that specifies the offset within the allocation required for the resource. |
|
This is a bitwise flag indicating the supported memory type for the image resource. If the bit |
Physical memory is allocated using the vkAllocateMemory()
API. This API was discussed in the last chapter. For a detailed description of this API, refer to the Allocating the device memory subsection under Managing memory in Vulkan in Chapter 5, Command Buffer and Memory Management in Vulkan.
Once physical memory is allocated to the device, what we need to do is bind this memory to its own image resource object (VkImage
). The image resource is bonded with the allocated device memory using the vkBindImageMemory()
API. The code for this is as follows:
VkResult vkBindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize memoryOffset);
The parameters of this structure are described as follows:
Parameters |
Description |
|
This is the logical device that owns the memory and image object. |
|
This refers to the |
|
This refers to the allocated |
|
This is the offset in bytes that specifies the starting point of the memory to which the image will be bounded. |
18.118.12.232