Skip to content

Commit a68c01c

Browse files
Added optional usage of VmaAllocatorCreateInfo::pAllocationCallbacks (disabled by default).
1 parent 6cc5e85 commit a68c01c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

bin/VulkanSample_Release_vs2015.exe

1 KB
Binary file not shown.

src/VulkanSample.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ static const wchar_t* const APP_TITLE_W = L"Vulkan Memory Allocator Sample 2.0";
3535

3636
static const bool VSYNC = true;
3737
static const uint32_t COMMAND_BUFFER_COUNT = 2;
38-
39-
static bool g_EnableValidationLayer = true;
38+
static void* const CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA = (void*)(intptr_t)43564544;
39+
static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = false;
4040

4141
VkPhysicalDevice g_hPhysicalDevice;
4242
VkDevice g_hDevice;
4343
VmaAllocator g_hAllocator;
4444
bool g_MemoryAliasingWarningEnabled = true;
4545

46+
static bool g_EnableValidationLayer = true;
4647
static bool VK_KHR_get_memory_requirements2_enabled = false;
4748
static bool VK_KHR_dedicated_allocation_enabled = false;
4849

@@ -102,6 +103,28 @@ static VkImage g_hTextureImage;
102103
static VmaAllocation g_hTextureImageAlloc;
103104
static VkImageView g_hTextureImageView;
104105

106+
static void* CustomCpuAllocation(
107+
void* pUserData, size_t size, size_t alignment,
108+
VkSystemAllocationScope allocationScope)
109+
{
110+
assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
111+
return _aligned_malloc(size, alignment);
112+
}
113+
114+
static void* CustomCpuReallocation(
115+
void* pUserData, void* pOriginal, size_t size, size_t alignment,
116+
VkSystemAllocationScope allocationScope)
117+
{
118+
assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
119+
return _aligned_realloc(pOriginal, size, alignment);
120+
}
121+
122+
static void CustomCpuFree(void* pUserData, void* pMemory)
123+
{
124+
assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
125+
_aligned_free(pMemory);
126+
}
127+
105128
static void BeginSingleTimeCommands()
106129
{
107130
VkCommandBufferBeginInfo cmdBufBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
@@ -1255,10 +1278,22 @@ static void InitializeApplication()
12551278
VmaAllocatorCreateInfo allocatorInfo = {};
12561279
allocatorInfo.physicalDevice = g_hPhysicalDevice;
12571280
allocatorInfo.device = g_hDevice;
1281+
12581282
if(VK_KHR_dedicated_allocation_enabled)
12591283
{
12601284
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
12611285
}
1286+
1287+
VkAllocationCallbacks cpuAllocationCallbacks = {};
1288+
if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS)
1289+
{
1290+
cpuAllocationCallbacks.pUserData = CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA;
1291+
cpuAllocationCallbacks.pfnAllocation = &CustomCpuAllocation;
1292+
cpuAllocationCallbacks.pfnReallocation = &CustomCpuReallocation;
1293+
cpuAllocationCallbacks.pfnFree = &CustomCpuFree;
1294+
allocatorInfo.pAllocationCallbacks = &cpuAllocationCallbacks;
1295+
}
1296+
12621297
ERR_GUARD_VULKAN( vmaCreateAllocator(&allocatorInfo, &g_hAllocator) );
12631298

12641299
// Retrieve queue (doesn't need to be destroyed)

0 commit comments

Comments
 (0)