vkAllocateMemory
函数原型
VkResult vkAllocateMemory(
VkDevice device,
const VkMemoryAllocateInfo* pAllocateInfo,
const VkAllocationCallbacks* pAllocator,
VkDeviceMemory* pMemory);
描述
申请GPU显存。
参数
device
: 具备显存的逻辑设备。pAllocateInfo
:VkMemoryAllocateInfo
结构体指针,描述了如何申请显存。pAllocator
: host memory分配器。pMemory
:VkDeviceMemory
句柄,指向申请的显存。
补充
VkMemoryAllocateInfo
结构体定义:
typedef struct VkMemoryAllocateInfo {
VkStructureType sType;
const void* pNext;
VkDeviceSize allocationSize;
uint32_t memoryTypeIndex;
} VkMemoryAllocateInfo;
sType
: VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFOallocationSize
: 申请的显存大小。memoryTypeIndex
: 显存类型索引。
返回值
VK_SUCCESS
: 成功申请显存。- 其他 : 申请显存失败。
代码示例
VkBuffer buffer = VK_NULL_PTR;
VkBufferCreateInfo create_info = {};
create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
create_info.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
create_info.size = 1024;
VK_CHECK(vkCreateBuffer(device, &create_info, nullptr, &buffer));
VkMemoryRequirements memory_requirements;
vkGetBufferMemoryRequirements(device, buffer, &memory_requirements);
VkDeviceMemory memory;
VkMemoryAllocateInfo memory_allocation{};
memory_allocation.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
memory_allocation.allocationSize = memory_requirements.size;
memory_allocation.memoryTypeIndex = 0;
VK_CHECK(vkAllocateMemory(device, &memory_allocation, nullptr, &memory));