Search code examples
vulkan

How to properly log Vulkan result type strings


Logging VkResult enum values

The VkResult enum contains a lot of values. Unfortunately though, they are C enums which aliases an integer, so I cannot easily just log their names to the console. For this purpose, I am envisioning a function which does something like this:

void graphics::log_vk_result(VkResult result)
{
    switch (result)
    {
    case VK_SUCCESS:
        log_core_debug("VK_SUCCESS"); return;

    case VK_NOT_READY:
        log_core_debug("VK_NOT_READY"); return;
    [...]
}

But some of the enum values are only supported by certain extensions, as indicated here. An example: The extension VK_EXT_debug_report introduces the following value to the enumeration: VK_ERROR_VALIDATION_FAILED_EXT. So my idea for (potentially) more portable code would be something like this:

void graphics::log_vk_result(VkResult result)
{
    switch (result)
    {
    [...]
#if defined(VK_EXT_DEBUG_REPORT_EXTENSION_NAME)
    case VK_ERROR_VALIDATION_FAILED_EXT:
        log_core_debug("VK_ERROR_VALIDATION_FAILED_EXT");
#endif
}

I found this name by looking at the extension manual page. I cannot easily see whether or not VK_EXT_DEBUG_REPORT_EXTENSION_NAME is a macro or an enum - it is a const char* but stated under the section "New Enum Constants". So checking for this particular value, for this particular extension, was my default choice.

[I do realize this extension is marked as deprecated, and I'm not using it! I only use it as an example here.]

I have two questions:

  1. Is this needed?
  2. Is this the correct way of doing this?

Thanks a lot!


Solution

  • All of this is unnecessary, since Vulkan SDK already includes the desired functionality:

    #include <vulkan/vk_enum_string_helper.h>
    
    void graphics::log_vk_result( const VkResult result ){
        log_core_debug( string_VkResult(result) );
    }