src/video/SDL_vulkan_utils.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 18 May 2018 13:09:30 -0700
changeset 11983 3a50eb90e4b2
parent 11828 da1ca77eb713
child 12201 8bdc4d340419
permissions -rw-r--r--
Merged latest changes from Steam Link app
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "../SDL_internal.h"
    22 
    23 #include "SDL_vulkan_internal.h"
    24 #include "SDL_error.h"
    25 
    26 #if SDL_VIDEO_VULKAN
    27 
    28 const char *SDL_Vulkan_GetResultString(VkResult result)
    29 {
    30     switch((int)result)
    31     {
    32     case VK_SUCCESS:
    33         return "VK_SUCCESS";
    34     case VK_NOT_READY:
    35         return "VK_NOT_READY";
    36     case VK_TIMEOUT:
    37         return "VK_TIMEOUT";
    38     case VK_EVENT_SET:
    39         return "VK_EVENT_SET";
    40     case VK_EVENT_RESET:
    41         return "VK_EVENT_RESET";
    42     case VK_INCOMPLETE:
    43         return "VK_INCOMPLETE";
    44     case VK_ERROR_OUT_OF_HOST_MEMORY:
    45         return "VK_ERROR_OUT_OF_HOST_MEMORY";
    46     case VK_ERROR_OUT_OF_DEVICE_MEMORY:
    47         return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
    48     case VK_ERROR_INITIALIZATION_FAILED:
    49         return "VK_ERROR_INITIALIZATION_FAILED";
    50     case VK_ERROR_DEVICE_LOST:
    51         return "VK_ERROR_DEVICE_LOST";
    52     case VK_ERROR_MEMORY_MAP_FAILED:
    53         return "VK_ERROR_MEMORY_MAP_FAILED";
    54     case VK_ERROR_LAYER_NOT_PRESENT:
    55         return "VK_ERROR_LAYER_NOT_PRESENT";
    56     case VK_ERROR_EXTENSION_NOT_PRESENT:
    57         return "VK_ERROR_EXTENSION_NOT_PRESENT";
    58     case VK_ERROR_FEATURE_NOT_PRESENT:
    59         return "VK_ERROR_FEATURE_NOT_PRESENT";
    60     case VK_ERROR_INCOMPATIBLE_DRIVER:
    61         return "VK_ERROR_INCOMPATIBLE_DRIVER";
    62     case VK_ERROR_TOO_MANY_OBJECTS:
    63         return "VK_ERROR_TOO_MANY_OBJECTS";
    64     case VK_ERROR_FORMAT_NOT_SUPPORTED:
    65         return "VK_ERROR_FORMAT_NOT_SUPPORTED";
    66     case VK_ERROR_FRAGMENTED_POOL:
    67         return "VK_ERROR_FRAGMENTED_POOL";
    68     case VK_ERROR_SURFACE_LOST_KHR:
    69         return "VK_ERROR_SURFACE_LOST_KHR";
    70     case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
    71         return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
    72     case VK_SUBOPTIMAL_KHR:
    73         return "VK_SUBOPTIMAL_KHR";
    74     case VK_ERROR_OUT_OF_DATE_KHR:
    75         return "VK_ERROR_OUT_OF_DATE_KHR";
    76     case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
    77         return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
    78     case VK_ERROR_VALIDATION_FAILED_EXT:
    79         return "VK_ERROR_VALIDATION_FAILED_EXT";
    80     case VK_ERROR_OUT_OF_POOL_MEMORY_KHR:
    81         return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
    82     case VK_ERROR_INVALID_SHADER_NV:
    83         return "VK_ERROR_INVALID_SHADER_NV";
    84     case VK_RESULT_MAX_ENUM:
    85     case VK_RESULT_RANGE_SIZE:
    86         break;
    87     }
    88     if(result < 0)
    89         return "VK_ERROR_<Unknown>";
    90     return "VK_<Unknown>";
    91 }
    92 
    93 VkExtensionProperties *SDL_Vulkan_CreateInstanceExtensionsList(
    94     PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties,
    95     Uint32 *extensionCount)
    96 {
    97     Uint32 count = 0;
    98     VkResult result = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
    99     VkExtensionProperties *retval;
   100     if(result == VK_ERROR_INCOMPATIBLE_DRIVER)
   101     {
   102         /* Avoid the ERR_MAX_STRLEN limit by passing part of the message
   103          * as a string argument.
   104          */
   105         SDL_SetError(
   106             "You probably don't have a working Vulkan driver installed. %s %s %s(%d)",
   107             "Getting Vulkan extensions failed:",
   108             "vkEnumerateInstanceExtensionProperties returned",
   109             SDL_Vulkan_GetResultString(result),
   110             (int)result);
   111         return NULL;
   112     }
   113     else if(result != VK_SUCCESS)
   114     {
   115         SDL_SetError(
   116             "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
   117             "%s(%d)",
   118             SDL_Vulkan_GetResultString(result),
   119             (int)result);
   120         return NULL;
   121     }
   122     if(count == 0)
   123     {
   124         retval = SDL_calloc(1, sizeof(VkExtensionProperties)); // so we can return non-null
   125     }
   126 	else
   127 	{
   128 		retval = SDL_calloc(count, sizeof(VkExtensionProperties));
   129 	}
   130     if(!retval)
   131     {
   132         SDL_OutOfMemory();
   133         return NULL;
   134     }
   135     result = vkEnumerateInstanceExtensionProperties(NULL, &count, retval);
   136     if(result != VK_SUCCESS)
   137     {
   138         SDL_SetError(
   139             "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
   140             "%s(%d)",
   141             SDL_Vulkan_GetResultString(result),
   142             (int)result);
   143         SDL_free(retval);
   144         return NULL;
   145     }
   146     *extensionCount = count;
   147     return retval;
   148 }
   149 
   150 SDL_bool SDL_Vulkan_GetInstanceExtensions_Helper(unsigned *userCount,
   151                                                  const char **userNames,
   152                                                  unsigned nameCount,
   153                                                  const char *const *names)
   154 {
   155     if (userNames) {
   156         unsigned i;
   157 
   158         if (*userCount < nameCount) {
   159             SDL_SetError("Output array for SDL_Vulkan_GetInstanceExtensions needs to be at least %d big", nameCount);
   160             return SDL_FALSE;
   161         }
   162         for (i = 0; i < nameCount; i++) {
   163             userNames[i] = names[i];
   164         }
   165     }
   166     *userCount = nameCount;
   167     return SDL_TRUE;
   168 }
   169 
   170 #endif
   171 
   172 /* vi: set ts=4 sw=4 expandtab: */