Skip to content

Commit

Permalink
Support for external allocators:
Browse files Browse the repository at this point in the history
* The newly introduced API surface area matches that of VMA's advanced usage and another hand-rolled memory allocator in a content-heavy application.
* All suballocator callbacks -- allocate, bind image, bind buffer, map, unmap and free -- are expected to guard VkDeviceMemory operations within a mutex.
* Each texture now also keeps track of its VkDeviceMemory offset.
* The 64 bit allocationId is to be used as a book-keeping measure by external suballocator callbacks to keep track of and free up suballocations. The external allocator can use a hashtable (ala std::unordered_map in C++) to keep track of the page(s) alloted to this suballocation. ('Pages' here refers to potential sparse bindings).
  • Loading branch information
toomuchvoltage committed Aug 24, 2023
1 parent 7f67af7 commit 398d489
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 52 deletions.
26 changes: 26 additions & 0 deletions include/ktxvulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,22 @@ typedef struct ktxVulkanTexture
uint32_t depth; /*!< The depth of the image. */
uint32_t levelCount; /*!< The number of MIP levels in the image. */
uint32_t layerCount; /*!< The number of array layers in the image. */
uint64_t devMemoryOffset;
uint64_t allocationId;
} ktxVulkanTexture;

typedef uint64_t(*subAllocatorAllocMemFuncPtr)(VkMemoryAllocateInfo* allocInfo, VkMemoryRequirements* memReq, VkDeviceMemory* devMemory, uint64_t* devMemoryOffset);
typedef VkResult(*subAllocatorBindBufferFuncPtr)(VkBuffer buffer, uint64_t allocId);
typedef VkResult(*subAllocatorBindImageFuncPtr)(VkImage image, uint64_t allocId);
typedef VkResult(*subAllocatorMemoryMapFuncPtr)(uint64_t allocId, VkDeviceSize offsetFromOffset, VkDeviceSize len, void** dataPtr);
typedef void (*subAllocatorMemoryUnmapFuncPtr)(uint64_t allocId);
typedef void (*subAllocatorFreeMemFuncPtr)(uint64_t allocId);

KTX_API void KTX_APIENTRY
ktxVulkanTexture_Destruct_WithPotentialSuballocator(ktxVulkanTexture* This, VkDevice device,
const VkAllocationCallbacks* pAllocator,
subAllocatorFreeMemFuncPtr freeMemFuncPtr);

KTX_API void KTX_APIENTRY
ktxVulkanTexture_Destruct(ktxVulkanTexture* This, VkDevice device,
const VkAllocationCallbacks* pAllocator);
Expand Down Expand Up @@ -208,6 +222,18 @@ ktxVulkanDeviceInfo_Destruct(ktxVulkanDeviceInfo* This);
KTX_API void KTX_APIENTRY
ktxVulkanDeviceInfo_Destroy(ktxVulkanDeviceInfo* This);
KTX_API KTX_error_code KTX_APIENTRY
ktxTexture_VkUploadEx_WithPotentialSuballocator(ktxTexture* This, ktxVulkanDeviceInfo* vdi,
ktxVulkanTexture* vkTexture,
VkImageTiling tiling,
VkImageUsageFlags usageFlags,
VkImageLayout finalLayout,
subAllocatorAllocMemFuncPtr allocMemFuncPtr,
subAllocatorBindBufferFuncPtr bindBufferFuncPtr,
subAllocatorBindImageFuncPtr bindImageFuncPtr,
subAllocatorMemoryMapFuncPtr memoryMapFuncPtr,
subAllocatorMemoryUnmapFuncPtr memoryUnmapFuncPtr,
subAllocatorFreeMemFuncPtr freeMemFuncPtr);
KTX_API KTX_error_code KTX_APIENTRY
ktxTexture_VkUploadEx(ktxTexture* This, ktxVulkanDeviceInfo* vdi,
ktxVulkanTexture* vkTexture,
VkImageTiling tiling,
Expand Down
Loading

0 comments on commit 398d489

Please sign in to comment.