Memory allocation and binding image resources

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.

Gathering memory allocation requirements

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

device

This refers to the device that owns the image.

image

This refers to the VkImage object.

pMemoryRequirements

This returns the VkMemoryRequirements structure object.

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

size

This specifies the size of the image resource required in bytes.

alignment

This refers to the alignment offset in bytes that specifies the offset within the allocation required for the resource.

memoryTypeBits

This is a bitwise flag indicating the supported memory type for the image resource. If the bit i is set, it means it will support the memory type i in the VkPhysicalDeviceMemoryProperties structure of the image resource.

Allocating physical memory on the device

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.

Binding the allocated memory to an image object

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

device

This is the logical device that owns the memory and image object.

image

This refers to the VkImage object to which we need to bind the memory.

memory

This refers to the allocated VkDeviceMemory.

memoryOffset

This is the offset in bytes that specifies the starting point of the memory to which the image will be bounded.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.232