Skip to content

Commit 353e367

Browse files
Further development of budget management
1 parent 5f573f5 commit 353e367

3 files changed

Lines changed: 341 additions & 53 deletions

File tree

src/Tests.cpp

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3863,13 +3863,15 @@ static void TestBudget()
38633863

38643864
uint32_t memTypeIndex = UINT32_MAX;
38653865

3866-
static const VkDeviceSize BUF_SIZE = 0x10000;
3867-
static const uint32_t BUF_COUNT = 32;
3866+
static const VkDeviceSize BUF_SIZE = 100ull * 1024 * 1024;
3867+
static const uint32_t BUF_COUNT = 4;
38683868

38693869
for(uint32_t testIndex = 0; testIndex < 2; ++testIndex)
38703870
{
3871-
VmaBudget budgetBeg = {};
3872-
vmaGetBudget(g_hAllocator, &budgetBeg);
3871+
vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
3872+
3873+
VmaBudget budgetBeg[VK_MAX_MEMORY_HEAPS] = {};
3874+
vmaGetBudget(g_hAllocator, budgetBeg);
38733875

38743876
VkBufferCreateInfo bufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
38753877
bufInfo.size = BUF_SIZE;
@@ -3902,36 +3904,85 @@ static void TestBudget()
39023904
}
39033905
}
39043906

3905-
VmaBudget budgetWithBufs = {};
3906-
vmaGetBudget(g_hAllocator, &budgetWithBufs);
3907+
VmaBudget budgetWithBufs[VK_MAX_MEMORY_HEAPS] = {};
3908+
vmaGetBudget(g_hAllocator, budgetWithBufs);
39073909

39083910
// DESTROY BUFFERS
39093911
for(size_t bufIndex = BUF_COUNT; bufIndex--; )
39103912
{
39113913
vmaDestroyBuffer(g_hAllocator, bufInfos[bufIndex].Buffer, bufInfos[bufIndex].Allocation);
39123914
}
39133915

3914-
VmaBudget budgetEnd = {};
3915-
vmaGetBudget(g_hAllocator, &budgetEnd);
3916+
VmaBudget budgetEnd[VK_MAX_MEMORY_HEAPS] = {};
3917+
vmaGetBudget(g_hAllocator, budgetEnd);
39163918

39173919
// CHECK
39183920
for(uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i)
39193921
{
3920-
TEST(budgetEnd.allocationBytes[i] <= budgetEnd.blockBytes[i]);
3922+
TEST(budgetEnd[i].allocationBytes <= budgetEnd[i].blockBytes);
39213923
if(i == heapIndex)
39223924
{
3923-
TEST(budgetEnd.allocationBytes[i] == budgetBeg.allocationBytes[i]);
3924-
TEST(budgetWithBufs.allocationBytes[i] == budgetBeg.allocationBytes[i] + BUF_SIZE * BUF_COUNT);
3925-
TEST(budgetWithBufs.blockBytes[i] >= budgetEnd.blockBytes[i]);
3925+
TEST(budgetEnd[i].allocationBytes == budgetBeg[i].allocationBytes);
3926+
TEST(budgetWithBufs[i].allocationBytes == budgetBeg[i].allocationBytes + BUF_SIZE * BUF_COUNT);
3927+
TEST(budgetWithBufs[i].blockBytes >= budgetEnd[i].blockBytes);
3928+
}
3929+
else
3930+
{
3931+
TEST(budgetEnd[i].allocationBytes == budgetEnd[i].allocationBytes &&
3932+
budgetEnd[i].allocationBytes == budgetWithBufs[i].allocationBytes);
3933+
TEST(budgetEnd[i].blockBytes == budgetEnd[i].blockBytes &&
3934+
budgetEnd[i].blockBytes == budgetWithBufs[i].blockBytes);
3935+
}
3936+
}
3937+
}
3938+
3939+
// DELME
3940+
3941+
{
3942+
std::vector<BufferInfo> buffers;
3943+
3944+
for(uint32_t i = 0; i < 160; ++i)
3945+
{
3946+
//vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
3947+
3948+
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
3949+
bufCreateInfo.size = 50ull * 1024 * 1024;
3950+
bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
3951+
3952+
VmaAllocationCreateInfo allocCreateInfo = {};
3953+
//allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
3954+
allocCreateInfo.memoryTypeBits = 1;
3955+
//allocCreateInfo.flags = VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT;
3956+
3957+
BufferInfo bufInfo = {};
3958+
VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &bufInfo.Buffer, &bufInfo.Allocation, nullptr);
3959+
if(res == VK_SUCCESS)
3960+
{
3961+
buffers.push_back(std::move(bufInfo));
39263962
}
39273963
else
39283964
{
3929-
TEST(budgetEnd.allocationBytes[i] == budgetEnd.allocationBytes[i] &&
3930-
budgetEnd.allocationBytes[i] == budgetWithBufs.allocationBytes[i]);
3931-
TEST(budgetEnd.blockBytes[i] == budgetEnd.blockBytes[i] &&
3932-
budgetEnd.blockBytes[i] == budgetWithBufs.blockBytes[i]);
3965+
break;
39333966
}
39343967
}
3968+
3969+
char* statsString;
3970+
vmaBuildStatsString(g_hAllocator, &statsString, VK_TRUE);
3971+
printf("%s\n", statsString);
3972+
vmaFreeStatsString(g_hAllocator, statsString);
3973+
3974+
VmaBudget budget1[VK_MAX_MEMORY_HEAPS];
3975+
vmaGetBudget(g_hAllocator, budget1);
3976+
3977+
vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
3978+
3979+
VmaBudget budget2[VK_MAX_MEMORY_HEAPS];
3980+
vmaGetBudget(g_hAllocator, budget2);
3981+
3982+
for(size_t i = buffers.size(); i--; )
3983+
{
3984+
vmaDestroyBuffer(g_hAllocator, buffers[i].Buffer, buffers[i].Allocation);
3985+
}
39353986
}
39363987
}
39373988

src/VulkanSample.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ bool g_MemoryAliasingWarningEnabled = true;
4646

4747
static bool g_EnableValidationLayer = true;
4848
static bool VK_KHR_get_memory_requirements2_enabled = false;
49+
static bool VK_KHR_get_physical_device_properties2_enabled = false;
4950
static bool VK_KHR_dedicated_allocation_enabled = false;
5051
static bool VK_KHR_bind_memory2_enabled = false;
52+
static bool VK_EXT_memory_budget_enabled = false;
5153
bool g_SparseBindingEnabled = false;
5254

5355
static HINSTANCE g_hAppInstance;
@@ -1115,15 +1117,32 @@ static void InitializeApplication()
11151117
}
11161118
}
11171119

1118-
std::vector<const char*> instanceExtensions;
1119-
instanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
1120-
instanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
1120+
uint32_t availableInstanceExtensionCount = 0;
1121+
ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, nullptr) );
1122+
std::vector<VkExtensionProperties> availableInstanceExtensions(availableInstanceExtensionCount);
1123+
if(availableInstanceExtensionCount > 0)
1124+
{
1125+
ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, availableInstanceExtensions.data()) );
1126+
}
1127+
1128+
std::vector<const char*> enabledInstanceExtensions;
1129+
enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
1130+
enabledInstanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
11211131

11221132
std::vector<const char*> instanceLayers;
11231133
if(g_EnableValidationLayer == true)
11241134
{
11251135
instanceLayers.push_back(VALIDATION_LAYER_NAME);
1126-
instanceExtensions.push_back("VK_EXT_debug_report");
1136+
enabledInstanceExtensions.push_back("VK_EXT_debug_report");
1137+
}
1138+
1139+
for(const auto& extensionProperties : availableInstanceExtensions)
1140+
{
1141+
if(strcmp(extensionProperties.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) == 0)
1142+
{
1143+
enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
1144+
VK_KHR_get_physical_device_properties2_enabled = true;
1145+
}
11271146
}
11281147

11291148
VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
@@ -1135,8 +1154,8 @@ static void InitializeApplication()
11351154

11361155
VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
11371156
instInfo.pApplicationInfo = &appInfo;
1138-
instInfo.enabledExtensionCount = static_cast<uint32_t>(instanceExtensions.size());
1139-
instInfo.ppEnabledExtensionNames = instanceExtensions.data();
1157+
instInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
1158+
instInfo.ppEnabledExtensionNames = enabledInstanceExtensions.data();
11401159
instInfo.enabledLayerCount = static_cast<uint32_t>(instanceLayers.size());
11411160
instInfo.ppEnabledLayerNames = instanceLayers.data();
11421161

@@ -1282,6 +1301,11 @@ static void InitializeApplication()
12821301
enabledDeviceExtensions.push_back(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME);
12831302
VK_KHR_bind_memory2_enabled = true;
12841303
}
1304+
else if(strcmp(properties[i].extensionName, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME) == 0)
1305+
{
1306+
enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
1307+
VK_EXT_memory_budget_enabled = true;
1308+
}
12851309
}
12861310
}
12871311
}
@@ -1302,6 +1326,7 @@ static void InitializeApplication()
13021326
VmaAllocatorCreateInfo allocatorInfo = {};
13031327
allocatorInfo.physicalDevice = g_hPhysicalDevice;
13041328
allocatorInfo.device = g_hDevice;
1329+
allocatorInfo.instance = g_hVulkanInstance;
13051330

13061331
if(VK_KHR_dedicated_allocation_enabled)
13071332
{
@@ -1322,6 +1347,10 @@ static void InitializeApplication()
13221347
{
13231348
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT;
13241349
}
1350+
if(VK_EXT_memory_budget_enabled && VK_KHR_get_physical_device_properties2_enabled)
1351+
{
1352+
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
1353+
}
13251354

13261355
if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS)
13271356
{
@@ -1341,7 +1370,7 @@ static void InitializeApplication()
13411370
/*
13421371
std::array<VkDeviceSize, VK_MAX_MEMORY_HEAPS> heapSizeLimit;
13431372
std::fill(heapSizeLimit.begin(), heapSizeLimit.end(), VK_WHOLE_SIZE);
1344-
heapSizeLimit[0] = 100ull * 1024 * 1024;
1373+
heapSizeLimit[0] = 512ull * 1024 * 1024;
13451374
allocatorInfo.pHeapSizeLimit = heapSizeLimit.data();
13461375
*/
13471376

0 commit comments

Comments
 (0)