Vulkan Allocation for Exporting Memory

in

Now, there are two main ways to allocate memory in Vulkan: through `vkAllocateMemory()` or by using `vkCreateBuffer()`. Let’s start with `vkAllocateMemory()`, which is like a fancy version of malloc(). We pass it the size we want and some other options, and it returns us a handle to our newly allocated memory.

Here’s an example:

“`c++
// Allocate memory in Vulkan using vkAllocateMemory() or vkCreateBuffer()
// Let’s start with vkAllocateMemory(), which is similar to malloc() in C
// We pass it the size we want and some other options, and it returns a handle to our newly allocated memory

// Example:
VkDeviceSize bufferSize = 64 * MB; // Allocate 64MB for our data
VkMemoryRequirements memReqs; // Declare a variable to store the memory requirements for our buffer
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProps); // Get the properties for the physical device
vkGetBufferMemoryRequirements(device, buffer, &memReqs); // Get the memory requirements for our buffer
VkMemoryAllocateInfo allocInfo = {}; // Create a struct to hold the allocation info
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; // Specify the type of the struct
allocInfo.allocationSize = memReqs.size; // Use the size we calculated earlier
allocInfo.memoryTypeIndex = findMemoryType(memProps, memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE | VK_MEMORY_PROPERTY_HOST_COHERENT); // Find a memory type that meets our requirements (host-visible and coherent)
vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory); // Allocate the memory using our info



Now  `vkCreateBuffer()`. This function is like a fancy version of malloc(), but specifically for creating buffers. We pass it some options and it returns us a handle to our newly created buffer.

Here's an example:

c++
// This script is used to create a buffer in Vulkan using the vkCreateBuffer() function.

// First, we need to specify the size of the buffer we want to create. Here, we allocate 64MB for our data.
VkDeviceSize bufferSize = 64 * MB;

// Next, we set up the buffer info struct. This will contain all the necessary information for creating our buffer.
VkBufferCreateInfo bufInfo = {};

// We specify the type of the struct we are using.
bufInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;

// We set the size of the buffer using the value we calculated earlier.
bufInfo.size = bufferSize;

// We specify the usage flags for our buffer. Here, we want it to be used as a transfer source and destination.
bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;

// Finally, we call the vkCreateBuffer() function to create our buffer. We pass in the device, buffer info, a pointer to a custom allocator (nullptr in this case), and a pointer to the buffer handle that will be returned to us.
vkCreateBuffer(device, &bufInfo, nullptr, &buffer);
“`

Allocating memory in Vulkan is like a fancy version of malloc(), but with more options for managing your data efficiently. And if you’re feeling adventurous, you can even use `vkAllocateMemory()` and `vkCreateBuffer()` together to create a buffer that’s already allocated on the GPU!

SICORPS