Skip to content

Commit a63e37c

Browse files
Add support for memory budget to record&replay system
1 parent b68b368 commit a63e37c

5 files changed

Lines changed: 108 additions & 13 deletions

File tree

docs/Recording file format.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ An ordered sequence of values of some type, separated by single space.
305305
PhysicalDeviceMemory,Type,3,propertyFlags,14
306306
Extension,VK_KHR_dedicated_allocation,1
307307
Extension,VK_KHR_bind_memory2,1
308+
Extension,VK_EXT_memory_budget,1
308309
Macro,VMA_DEBUG_ALWAYS_DEDICATED_MEMORY,0
309310
Macro,VMA_DEBUG_ALIGNMENT,1
310311
Macro,VMA_DEBUG_MARGIN,0

src/VmaReplay/Constants.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,8 @@ const char* VMA_ALLOCATION_CREATE_FLAG_NAMES[] = {
737737
"VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT",
738738
"VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT",
739739
"VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT",
740+
"VMA_ALLOCATION_CREATE_DONT_BIND_BIT",
741+
"VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT",
740742
"VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT",
741743
"VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT",
742744
"VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT",
@@ -749,6 +751,8 @@ const uint32_t VMA_ALLOCATION_CREATE_FLAG_VALUES[] = {
749751
VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT,
750752
VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT,
751753
VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT,
754+
VMA_ALLOCATION_CREATE_DONT_BIND_BIT,
755+
VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT,
752756
VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT,
753757
VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT,
754758
VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT,

src/VmaReplay/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ enum CMD_LINE_OPT
3636
CMD_LINE_OPT_PHYSICAL_DEVICE,
3737
CMD_LINE_OPT_USER_DATA,
3838
CMD_LINE_OPT_VK_KHR_DEDICATED_ALLOCATION,
39+
CMD_LINE_OPT_VK_EXT_MEMORY_BUDGET,
3940
CMD_LINE_OPT_VK_LAYER_LUNARG_STANDARD_VALIDATION,
4041
CMD_LINE_OPT_MEM_STATS,
4142
CMD_LINE_OPT_DUMP_STATS_AFTER_LINE,

src/VmaReplay/VmaReplay.cpp

Lines changed: 95 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ static bool g_UserDataEnabled = true;
670670
static bool g_MemStatsEnabled = false;
671671
VULKAN_EXTENSION_REQUEST g_VK_KHR_dedicated_allocation_request = VULKAN_EXTENSION_REQUEST::DEFAULT;
672672
VULKAN_EXTENSION_REQUEST g_VK_LAYER_LUNARG_standard_validation = VULKAN_EXTENSION_REQUEST::DEFAULT;
673+
VULKAN_EXTENSION_REQUEST g_VK_EXT_memory_budget_request = VULKAN_EXTENSION_REQUEST::DEFAULT;
673674

674675
struct StatsAfterLineEntry
675676
{
@@ -1063,7 +1064,8 @@ class ConfigurationParser
10631064
void Compare(
10641065
const VkPhysicalDeviceProperties& currDevProps,
10651066
const VkPhysicalDeviceMemoryProperties& currMemProps,
1066-
bool currDedicatedAllocationExtensionEnabled);
1067+
bool currDedicatedAllocationExtensionEnabled,
1068+
bool currMemoryBudgetEnabled);
10671069

10681070
private:
10691071
enum class OPTION
@@ -1078,6 +1080,8 @@ class ConfigurationParser
10781080
PhysicalDeviceLimits_bufferImageGranularity,
10791081
PhysicalDeviceLimits_nonCoherentAtomSize,
10801082
Extension_VK_KHR_dedicated_allocation,
1083+
Extension_VK_KHR_bind_memory2,
1084+
Extension_VK_EXT_memory_budget,
10811085
Macro_VMA_DEBUG_ALWAYS_DEDICATED_MEMORY,
10821086
Macro_VMA_DEBUG_ALIGNMENT,
10831087
Macro_VMA_DEBUG_MARGIN,
@@ -1202,6 +1206,10 @@ bool ConfigurationParser::Parse(LineSplit& lineSplit)
12021206
const StrRange subOptionName = csvSplit.GetRange(1);
12031207
if(StrRangeEq(subOptionName, "VK_KHR_dedicated_allocation"))
12041208
SetOption(currLineNumber, OPTION::Extension_VK_KHR_dedicated_allocation, csvSplit.GetRange(2));
1209+
else if(StrRangeEq(subOptionName, "VK_KHR_bind_memory2"))
1210+
SetOption(currLineNumber, OPTION::Extension_VK_KHR_bind_memory2, csvSplit.GetRange(2));
1211+
else if(StrRangeEq(subOptionName, "VK_EXT_memory_budget"))
1212+
SetOption(currLineNumber, OPTION::Extension_VK_EXT_memory_budget, csvSplit.GetRange(2));
12051213
else
12061214
printf("Line %zu: Unrecognized configuration option.\n", currLineNumber);
12071215
}
@@ -1297,7 +1305,8 @@ bool ConfigurationParser::Parse(LineSplit& lineSplit)
12971305
void ConfigurationParser::Compare(
12981306
const VkPhysicalDeviceProperties& currDevProps,
12991307
const VkPhysicalDeviceMemoryProperties& currMemProps,
1300-
bool currDedicatedAllocationExtensionEnabled)
1308+
bool currDedicatedAllocationExtensionEnabled,
1309+
bool currMemoryBudgetEnabled)
13011310
{
13021311
CompareOption(VERBOSITY::MAXIMUM, "PhysicalDevice apiVersion",
13031312
OPTION::PhysicalDevice_apiVersion, currDevProps.apiVersion);
@@ -1319,7 +1328,7 @@ void ConfigurationParser::Compare(
13191328
CompareOption(VERBOSITY::DEFAULT, "PhysicalDeviceLimits nonCoherentAtomSize",
13201329
OPTION::PhysicalDeviceLimits_nonCoherentAtomSize, currDevProps.limits.nonCoherentAtomSize);
13211330
CompareOption(VERBOSITY::DEFAULT, "Extension VK_KHR_dedicated_allocation",
1322-
OPTION::Extension_VK_KHR_dedicated_allocation, currDedicatedAllocationExtensionEnabled);
1331+
OPTION::Extension_VK_EXT_memory_budget, currMemoryBudgetEnabled);
13231332

13241333
CompareMemProps(currMemProps);
13251334
}
@@ -1580,6 +1589,7 @@ class Player
15801589
VkCommandPool m_CommandPool = VK_NULL_HANDLE;
15811590
VkCommandBuffer m_CommandBuffer = VK_NULL_HANDLE;
15821591
bool m_DedicatedAllocationEnabled = false;
1592+
bool m_MemoryBudgetEnabled = false;
15831593
const VkPhysicalDeviceProperties* m_DevProps = nullptr;
15841594
const VkPhysicalDeviceMemoryProperties* m_MemProps = nullptr;
15851595

@@ -1700,7 +1710,9 @@ Player::~Player()
17001710

17011711
void Player::ApplyConfig(ConfigurationParser& configParser)
17021712
{
1703-
configParser.Compare(*m_DevProps, *m_MemProps, m_DedicatedAllocationEnabled);
1713+
configParser.Compare(*m_DevProps, *m_MemProps,
1714+
m_DedicatedAllocationEnabled,
1715+
m_MemoryBudgetEnabled);
17041716
}
17051717

17061718
void Player::ExecuteLine(size_t lineNumber, const StrRange& line)
@@ -2007,15 +2019,35 @@ int Player::InitVulkan()
20072019
default: assert(0);
20082020
}
20092021

2010-
std::vector<const char*> instanceExtensions;
2011-
//instanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
2012-
//instanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
2022+
uint32_t availableInstanceExtensionCount = 0;
2023+
res = vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, nullptr);
2024+
assert(res == VK_SUCCESS);
2025+
std::vector<VkExtensionProperties> availableInstanceExtensions(availableInstanceExtensionCount);
2026+
if(availableInstanceExtensionCount > 0)
2027+
{
2028+
res = vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, availableInstanceExtensions.data());
2029+
assert(res == VK_SUCCESS);
2030+
}
2031+
2032+
std::vector<const char*> enabledInstanceExtensions;
2033+
//enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
2034+
//enabledInstanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
20132035

20142036
std::vector<const char*> instanceLayers;
20152037
if(validationLayersEnabled)
20162038
{
20172039
instanceLayers.push_back(VALIDATION_LAYER_NAME);
2018-
instanceExtensions.push_back("VK_EXT_debug_report");
2040+
enabledInstanceExtensions.push_back("VK_EXT_debug_report");
2041+
}
2042+
2043+
bool VK_KHR_get_physical_device_properties2_enabled = false;
2044+
for(const auto& extensionProperties : availableInstanceExtensions)
2045+
{
2046+
if(strcmp(extensionProperties.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) == 0)
2047+
{
2048+
enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
2049+
VK_KHR_get_physical_device_properties2_enabled = true;
2050+
}
20192051
}
20202052

20212053
VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
@@ -2027,8 +2059,8 @@ int Player::InitVulkan()
20272059

20282060
VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
20292061
instInfo.pApplicationInfo = &appInfo;
2030-
instInfo.enabledExtensionCount = (uint32_t)instanceExtensions.size();
2031-
instInfo.ppEnabledExtensionNames = instanceExtensions.data();
2062+
instInfo.enabledExtensionCount = (uint32_t)enabledInstanceExtensions.size();
2063+
instInfo.ppEnabledExtensionNames = enabledInstanceExtensions.data();
20322064
instInfo.enabledLayerCount = (uint32_t)instanceLayers.size();
20332065
instInfo.ppEnabledLayerNames = instanceLayers.data();
20342066

@@ -2136,6 +2168,7 @@ int Player::InitVulkan()
21362168
// Determine list of device extensions to enable.
21372169
std::vector<const char*> enabledDeviceExtensions;
21382170
//enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
2171+
bool memoryBudgetAvailable = false;
21392172
{
21402173
uint32_t propertyCount = 0;
21412174
res = vkEnumerateDeviceExtensionProperties(m_PhysicalDevice, nullptr, &propertyCount, nullptr);
@@ -2157,6 +2190,13 @@ int Player::InitVulkan()
21572190
{
21582191
VK_KHR_dedicated_allocation_available = true;
21592192
}
2193+
else if(strcmp(properties[i].extensionName, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME) == 0)
2194+
{
2195+
if(VK_KHR_get_physical_device_properties2_enabled)
2196+
{
2197+
memoryBudgetAvailable = true;
2198+
}
2199+
}
21602200
}
21612201
}
21622202
}
@@ -2181,11 +2221,32 @@ int Player::InitVulkan()
21812221
default: assert(0);
21822222
}
21832223

2224+
switch(g_VK_EXT_memory_budget_request)
2225+
{
2226+
case VULKAN_EXTENSION_REQUEST::DISABLED:
2227+
break;
2228+
case VULKAN_EXTENSION_REQUEST::DEFAULT:
2229+
m_MemoryBudgetEnabled = memoryBudgetAvailable;
2230+
break;
2231+
case VULKAN_EXTENSION_REQUEST::ENABLED:
2232+
m_MemoryBudgetEnabled = memoryBudgetAvailable;
2233+
if(!memoryBudgetAvailable)
2234+
{
2235+
printf("WARNING: VK_EXT_memory_budget extension cannot be enabled.\n");
2236+
}
2237+
break;
2238+
default: assert(0);
2239+
}
2240+
21842241
if(m_DedicatedAllocationEnabled)
21852242
{
21862243
enabledDeviceExtensions.push_back(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME);
21872244
enabledDeviceExtensions.push_back(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
21882245
}
2246+
if(m_MemoryBudgetEnabled)
2247+
{
2248+
enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
2249+
}
21892250

21902251
VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
21912252
deviceCreateInfo.enabledExtensionCount = (uint32_t)enabledDeviceExtensions.size();
@@ -2212,6 +2273,7 @@ int Player::InitVulkan()
22122273
deviceMemoryCallbacks.pfnFree = FreeDeviceMemoryCallback;
22132274

22142275
VmaAllocatorCreateInfo allocatorInfo = {};
2276+
allocatorInfo.instance = m_VulkanInstance;
22152277
allocatorInfo.physicalDevice = m_PhysicalDevice;
22162278
allocatorInfo.device = m_Device;
22172279
allocatorInfo.flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
@@ -2221,6 +2283,10 @@ int Player::InitVulkan()
22212283
{
22222284
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
22232285
}
2286+
if(m_MemoryBudgetEnabled)
2287+
{
2288+
allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
2289+
}
22242290

22252291
res = vmaCreateAllocator(&allocatorInfo, &m_Allocator);
22262292
if(res != VK_SUCCESS)
@@ -3941,6 +4007,8 @@ static void PrintCommandLineSyntax()
39414007
" By default the layers are silently enabled if available.\n"
39424008
" --VK_KHR_dedicated_allocation <Value> - 0 to disable or 1 to enable this extension.\n"
39434009
" By default the extension is silently enabled if available.\n"
4010+
" --VK_EXT_memory_budget <Value> - 0 to disable or 1 to enable this extension.\n"
4011+
" By default the extension is silently enabled if available.\n"
39444012
);
39454013
}
39464014

@@ -4160,6 +4228,7 @@ static int main2(int argc, char** argv)
41604228
cmdLineParser.RegisterOpt(CMD_LINE_OPT_PHYSICAL_DEVICE, "PhysicalDevice", true);
41614229
cmdLineParser.RegisterOpt(CMD_LINE_OPT_USER_DATA, "UserData", true);
41624230
cmdLineParser.RegisterOpt(CMD_LINE_OPT_VK_KHR_DEDICATED_ALLOCATION, "VK_KHR_dedicated_allocation", true);
4231+
cmdLineParser.RegisterOpt(CMD_LINE_OPT_VK_EXT_MEMORY_BUDGET, "VK_EXT_memory_budget", true);
41634232
cmdLineParser.RegisterOpt(CMD_LINE_OPT_VK_LAYER_LUNARG_STANDARD_VALIDATION, VALIDATION_LAYER_NAME, true);
41644233
cmdLineParser.RegisterOpt(CMD_LINE_OPT_MEM_STATS, "MemStats", true);
41654234
cmdLineParser.RegisterOpt(CMD_LINE_OPT_DUMP_STATS_AFTER_LINE, "DumpStatsAfterLine", true);
@@ -4234,6 +4303,22 @@ static int main2(int argc, char** argv)
42344303
}
42354304
}
42364305
break;
4306+
case CMD_LINE_OPT_VK_EXT_MEMORY_BUDGET:
4307+
{
4308+
bool newValue;
4309+
if(StrRangeToBool(StrRange(cmdLineParser.GetParameter()), newValue))
4310+
{
4311+
g_VK_EXT_memory_budget_request = newValue ?
4312+
VULKAN_EXTENSION_REQUEST::ENABLED :
4313+
VULKAN_EXTENSION_REQUEST::DISABLED;
4314+
}
4315+
else
4316+
{
4317+
PrintCommandLineSyntax();
4318+
return RESULT_ERROR_COMMAND_LINE;
4319+
}
4320+
}
4321+
break;
42374322
case CMD_LINE_OPT_VK_LAYER_LUNARG_STANDARD_VALIDATION:
42384323
{
42394324
bool newValue;

src/vk_mem_alloc.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6796,7 +6796,8 @@ class VmaRecorder
67966796
const VkPhysicalDeviceProperties& devProps,
67976797
const VkPhysicalDeviceMemoryProperties& memProps,
67986798
bool dedicatedAllocationExtensionEnabled,
6799-
bool bindMemory2ExtensionEnabled);
6799+
bool bindMemory2ExtensionEnabled,
6800+
bool memoryBudgetExtensionEnabled);
68006801
~VmaRecorder();
68016802

68026803
void RecordCreateAllocator(uint32_t frameIndex);
@@ -14363,7 +14364,8 @@ void VmaRecorder::WriteConfiguration(
1436314364
const VkPhysicalDeviceProperties& devProps,
1436414365
const VkPhysicalDeviceMemoryProperties& memProps,
1436514366
bool dedicatedAllocationExtensionEnabled,
14366-
bool bindMemory2ExtensionEnabled)
14367+
bool bindMemory2ExtensionEnabled,
14368+
bool memoryBudgetExtensionEnabled)
1436714369
{
1436814370
fprintf(m_File, "Config,Begin\n");
1436914371

@@ -14393,6 +14395,7 @@ void VmaRecorder::WriteConfiguration(
1439314395

1439414396
fprintf(m_File, "Extension,VK_KHR_dedicated_allocation,%u\n", dedicatedAllocationExtensionEnabled ? 1 : 0);
1439514397
fprintf(m_File, "Extension,VK_KHR_bind_memory2,%u\n", bindMemory2ExtensionEnabled ? 1 : 0);
14398+
fprintf(m_File, "Extension,VK_EXT_memory_budget,%u\n", memoryBudgetExtensionEnabled ? 1 : 0);
1439614399

1439714400
fprintf(m_File, "Macro,VMA_DEBUG_ALWAYS_DEDICATED_MEMORY,%u\n", VMA_DEBUG_ALWAYS_DEDICATED_MEMORY ? 1 : 0);
1439814401
fprintf(m_File, "Macro,VMA_DEBUG_ALIGNMENT,%llu\n", (VkDeviceSize)VMA_DEBUG_ALIGNMENT);
@@ -14593,7 +14596,8 @@ VkResult VmaAllocator_T::Init(const VmaAllocatorCreateInfo* pCreateInfo)
1459314596
m_PhysicalDeviceProperties,
1459414597
m_MemProps,
1459514598
m_UseKhrDedicatedAllocation,
14596-
m_UseKhrBindMemory2);
14599+
m_UseKhrBindMemory2,
14600+
m_UseExtMemoryBudget);
1459714601
m_pRecorder->RecordCreateAllocator(GetCurrentFrameIndex());
1459814602
#else
1459914603
VMA_ASSERT(0 && "VmaAllocatorCreateInfo::pRecordSettings used, but not supported due to VMA_RECORDING_ENABLED not defined to 1.");

0 commit comments

Comments
 (0)