Vulkan的内存分为两类,host memory和device memory
Host memory被vulkan用于non-device-visible存储使用。该memory被用于存储implementation相关的资源以及vulkan object的state
Vulkan允许应用程序可以根据vulkan的implementation分配host memory。然而如果不使用该feature的话,implementation会设计它独有的memory accocation。
然而由于大部分内存分配都不通过这里,所以这并非算是一个性能feature。而针对一些嵌入式系统,当调试(比如再所有主机分配之后放置保护页)或者内存分配日志记录的时候会非常有用
device memory为对device可见的memory,比如device直接可以使用的image或者buffer。physical device对应的memory properties描述了可用的memory heaps和memory types。
typedef struct VkAllocationCallbacks { void* pUserData; PFN_vkAllocationFunction pfnAllocation; PFN_vkReallocationFunction pfnReallocation; PFN_vkFreeFunction pfnFree; PFN_vkInternalAllocationNotification pfnInternalAllocation; PFN_vkInternalFreeNotification pfnInternalFree; } VkAllocationCallbacks;应用程序中具体的分配方式,是一个指向上述结构体的指针
void vkGetPhysicalDeviceMemoryProperties( VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties* pMemoryProperties);void FDeviceMemoryManager::UpdateMemoryProperties()
该API用于获取physical device对应的memory properties
第一个输入参数为被查询的 physicalDevice。
第二个输入参数为一个指向 VkPhysicalDeviceMemoryProperties 的指针,用于获取被查询出来的数据
typedef struct VkPhysicalDeviceMemoryProperties { uint32_t memoryTypeCount; //为memoryTypes数组中有效元素的数量 VkMemoryType memoryTypes[VK_MAX_MEMORY_TYPES]; uint32_t memoryHeapCount; VkMemoryHeap memoryHeaps[VK_MAX_MEMORY_HEAPS]; } VkPhysicalDeviceMemoryProperties;
void vkGetPhysicalDeviceMemoryProperties2( VkPhysicalDevice physicalDevice, VkPhysicalDeviceMemoryProperties2* pMemoryProperties);void FDeviceMemoryManager::UpdateMemoryProperties()
该API用于获取physical device对应的memory properties
第一个输入参数为被查询的 physicalDevice。
第二个输入参数为一个指向 VkPhysicalDeviceMemoryProperties 的指针,用于获取被查询出来的数据
typedef struct VkPhysicalDeviceMemoryProperties2 { VkStructureType sType; //当前结构体的类型,必须是 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2 void* pNext; //NULL,或者扩展该结构体的另外一个结构体,必须为 NULL VkPhysicalDeviceMemoryProperties memoryProperties; //一个指向 VkPhysicalDeviceMemoryProperties 的指针,用于获取被查询出来的数据,与 vkGetPhysicalDeviceMemoryProperties 相同。 } VkPhysicalDeviceMemoryProperties2;
VkResult vkAllocateMemory( VkDevice device, const VkMemoryAllocateInfo* pAllocateInfo, const VkAllocationCallbacks* pAllocator, VkDeviceMemory* pMemory);FDeviceMemoryAllocation* FDeviceMemoryManager::Alloc(bool bCanFail, VkDeviceSize AllocationSize, uint32 MemoryTypeIndex, void* DedicatedAllocateInfo, float Priority, const char* File, uint32 Line)
void vkFreeMemory( VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks* pAllocator);void FDeviceMemoryManager::FreeInternal(FDeviceMemoryAllocation* Allocation)
VkResult vkMapMemory( VkDevice device, VkDeviceMemory memory, VkDeviceSize offset, VkDeviceSize size, VkMemoryMapFlags flags, void** ppData);void* FDeviceMemoryAllocation::Map(VkDeviceSize InSize, VkDeviceSize Offset)
VkResult vkFlushMappedMemoryRanges( VkDevice device, uint32_t memoryRangeCount, const VkMappedMemoryRange* pMemoryRanges);void FDeviceMemoryAllocation::FlushMappedMemory(VkDeviceSize InOffset, VkDeviceSize InSize)
VkResult vkInvalidateMappedMemoryRanges( VkDevice device, uint32_t memoryRangeCount, const VkMappedMemoryRange* pMemoryRanges);void FDeviceMemoryAllocation::InvalidateMappedMemory(VkDeviceSize InOffset, VkDeviceSize InSize)
void vkUnmapMemory( VkDevice device, VkDeviceMemory memory);void FDeviceMemoryAllocation::Unmap()
void vkGetDeviceMemoryCommitment( VkDevice device, VkDeviceMemory memory, VkDeviceSize* pCommittedMemoryInBytes); void vkGetDeviceGroupPeerMemoryFeatures( VkDevice device, uint32_t heapIndex, uint32_t localDeviceIndex, uint32_t remoteDeviceIndex, VkPeerMemoryFeatureFlags* pPeerMemoryFeatures); uint64_t vkGetDeviceMemoryOpaqueCaptureAddress( VkDevice device, const VkDeviceMemoryOpaqueCaptureAddressInfo* pInfo);本节教程就到此结束,希望大家继续阅读我之后的教程。
谢谢大家,再见!
原创技术文章,撰写不易,转载请注明出处:电子设备中的画家|王烁 于 2021 年 5 月 10 日发表,原文链接(http://geekfaner.com/shineengine/blog20_Vulkanv1.2_6.html)