|
28 | 28 | #include "Common.h" |
29 | 29 | #include <atomic> |
30 | 30 | #include <Shlwapi.h> |
| 31 | +#include <unordered_set> |
31 | 32 |
|
32 | 33 | #pragma comment(lib, "shlwapi.lib") |
33 | 34 |
|
@@ -261,16 +262,30 @@ struct CommandLineParameters |
261 | 262 | } |
262 | 263 | } g_CommandLineParameters; |
263 | 264 |
|
264 | | -void SetDebugUtilsObjectName(VkObjectType type, uint64_t handle, const char* name) |
| 265 | +void SetDebugUtilsObjectName(VkObjectType type, uint64_t handle, std::string name) |
265 | 266 | { |
266 | | - if(vkSetDebugUtilsObjectNameEXT_Func == nullptr) |
| 267 | + TEST(!name.empty()); |
| 268 | + |
| 269 | + // We must make sure the std::string we pass as name is still valid memory until the program ends. |
| 270 | + // Since Vulkan is a C-Style API, it only accepts const char* as pObjectName parameter. |
| 271 | + // Naming objects with unformatted names like "g_hTemporaryCommandBuffer" is no problem because |
| 272 | + // this string ends up in the read-only data section of the program. However, if we for example want to |
| 273 | + // name every item in a dynamic array of items (swapchain images for example), we can only use a std::string, |
| 274 | + // and we must make sure the object lifetime of that string does not end before it's read as pObjectName. |
| 275 | + // Therefore, we store the strings as a static unordered_set here just to be sure. |
| 276 | + // Do not use a std::vector because it would continue to grow, eventually running out of memory. |
| 277 | + static std::unordered_set<std::string> debug_names; |
| 278 | + auto result = debug_names.insert(name); |
| 279 | + const char* pObjectName = result.first->c_str(); |
| 280 | + |
| 281 | + if (vkSetDebugUtilsObjectNameEXT_Func == nullptr) |
267 | 282 | return; |
268 | 283 |
|
269 | 284 | VkDebugUtilsObjectNameInfoEXT info = { VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT }; |
270 | 285 | info.objectType = type; |
271 | 286 | info.objectHandle = handle; |
272 | | - info.pObjectName = name; |
273 | | - vkSetDebugUtilsObjectNameEXT_Func(g_hDevice, &info); |
| 287 | + info.pObjectName = pObjectName; |
| 288 | + ERR_GUARD_VULKAN( vkSetDebugUtilsObjectNameEXT_Func(g_hDevice, &info) ); |
274 | 289 | } |
275 | 290 |
|
276 | 291 | void BeginSingleTimeCommands() |
|
0 commit comments