src/video/SDL_vulkan_utils.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Aug 2017 00:54:02 -0700
changeset 11385 1b4ef2593eca
parent 11381 2610f8f0a9c6
child 11394 910be5fe412a
permissions -rw-r--r--
Fixed analyzer warning "Call to 'calloc' has an allocation size of 0 bytes"
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2016 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 /* Based on the headers found in
    29  * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers
    30  */
    31 #if VK_HEADER_VERSION < 22
    32 enum
    33 {
    34     VK_ERROR_FRAGMENTED_POOL = -12,
    35 };
    36 #endif
    37 #if VK_HEADER_VERSION < 38
    38 enum {
    39     VK_ERROR_OUT_OF_POOL_MEMORY_KHR = -1000069000
    40 };
    41 #endif
    42 
    43 const char *SDL_Vulkan_GetResultString(VkResult result)
    44 {
    45     switch((int)result)
    46     {
    47     case VK_SUCCESS:
    48         return "VK_SUCCESS";
    49     case VK_NOT_READY:
    50         return "VK_NOT_READY";
    51     case VK_TIMEOUT:
    52         return "VK_TIMEOUT";
    53     case VK_EVENT_SET:
    54         return "VK_EVENT_SET";
    55     case VK_EVENT_RESET:
    56         return "VK_EVENT_RESET";
    57     case VK_INCOMPLETE:
    58         return "VK_INCOMPLETE";
    59     case VK_ERROR_OUT_OF_HOST_MEMORY:
    60         return "VK_ERROR_OUT_OF_HOST_MEMORY";
    61     case VK_ERROR_OUT_OF_DEVICE_MEMORY:
    62         return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
    63     case VK_ERROR_INITIALIZATION_FAILED:
    64         return "VK_ERROR_INITIALIZATION_FAILED";
    65     case VK_ERROR_DEVICE_LOST:
    66         return "VK_ERROR_DEVICE_LOST";
    67     case VK_ERROR_MEMORY_MAP_FAILED:
    68         return "VK_ERROR_MEMORY_MAP_FAILED";
    69     case VK_ERROR_LAYER_NOT_PRESENT:
    70         return "VK_ERROR_LAYER_NOT_PRESENT";
    71     case VK_ERROR_EXTENSION_NOT_PRESENT:
    72         return "VK_ERROR_EXTENSION_NOT_PRESENT";
    73     case VK_ERROR_FEATURE_NOT_PRESENT:
    74         return "VK_ERROR_FEATURE_NOT_PRESENT";
    75     case VK_ERROR_INCOMPATIBLE_DRIVER:
    76         return "VK_ERROR_INCOMPATIBLE_DRIVER";
    77     case VK_ERROR_TOO_MANY_OBJECTS:
    78         return "VK_ERROR_TOO_MANY_OBJECTS";
    79     case VK_ERROR_FORMAT_NOT_SUPPORTED:
    80         return "VK_ERROR_FORMAT_NOT_SUPPORTED";
    81     case VK_ERROR_FRAGMENTED_POOL:
    82         return "VK_ERROR_FRAGMENTED_POOL";
    83     case VK_ERROR_SURFACE_LOST_KHR:
    84         return "VK_ERROR_SURFACE_LOST_KHR";
    85     case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
    86         return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
    87     case VK_SUBOPTIMAL_KHR:
    88         return "VK_SUBOPTIMAL_KHR";
    89     case VK_ERROR_OUT_OF_DATE_KHR:
    90         return "VK_ERROR_OUT_OF_DATE_KHR";
    91     case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
    92         return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
    93     case VK_ERROR_VALIDATION_FAILED_EXT:
    94         return "VK_ERROR_VALIDATION_FAILED_EXT";
    95     case VK_ERROR_OUT_OF_POOL_MEMORY_KHR:
    96         return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
    97     case VK_ERROR_INVALID_SHADER_NV:
    98         return "VK_ERROR_INVALID_SHADER_NV";
    99     case VK_RESULT_MAX_ENUM:
   100     case VK_RESULT_RANGE_SIZE:
   101         break;
   102     }
   103     if(result < 0)
   104         return "VK_ERROR_<Unknown>";
   105     return "VK_<Unknown>";
   106 }
   107 
   108 VkExtensionProperties *SDL_Vulkan_CreateInstanceExtensionsList(
   109     PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties,
   110     Uint32 *extensionCount)
   111 {
   112     Uint32 count = 0;
   113     VkResult result = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
   114     VkExtensionProperties *retval;
   115     if(result == VK_ERROR_INCOMPATIBLE_DRIVER)
   116     {
   117         SDL_SetError(
   118             "You probably don't have a working Vulkan driver installed: getting Vulkan "
   119             "extensions failed: vkEnumerateInstanceExtensionProperties returned %s(%d)",
   120             SDL_Vulkan_GetResultString(result),
   121             (int)result);
   122         return NULL;
   123     }
   124     else if(result != VK_SUCCESS)
   125     {
   126         SDL_SetError(
   127             "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
   128             "%s(%d)",
   129             SDL_Vulkan_GetResultString(result),
   130             (int)result);
   131         return NULL;
   132     }
   133     if(count == 0)
   134     {
   135         retval = SDL_calloc(1, sizeof(VkExtensionProperties)); // so we can return non-null
   136     }
   137 	else
   138 	{
   139 		retval = SDL_calloc(count, sizeof(VkExtensionProperties));
   140 	}
   141     if(!retval)
   142     {
   143         SDL_OutOfMemory();
   144         return NULL;
   145     }
   146     result = vkEnumerateInstanceExtensionProperties(NULL, &count, retval);
   147     if(result != VK_SUCCESS)
   148     {
   149         SDL_SetError(
   150             "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
   151             "%s(%d)",
   152             SDL_Vulkan_GetResultString(result),
   153             (int)result);
   154         SDL_free(retval);
   155         return NULL;
   156     }
   157     *extensionCount = count;
   158     return retval;
   159 }
   160 
   161 SDL_bool SDL_Vulkan_GetInstanceExtensions_Helper(unsigned *userCount,
   162                                                  const char **userNames,
   163                                                  unsigned nameCount,
   164                                                  const char *const *names)
   165 {
   166     if(userNames)
   167     {
   168 		unsigned int i;
   169 
   170         if(*userCount != nameCount)
   171         {
   172             SDL_SetError(
   173                 "Count doesn't match count from previous call of SDL_Vulkan_GetInstanceExtensions");
   174             return SDL_FALSE;
   175         }
   176         for(i = 0; i < nameCount; i++)
   177         {
   178             userNames[i] = names[i];
   179         }
   180     }
   181     else
   182     {
   183         *userCount = nameCount;
   184     }
   185     return SDL_TRUE;
   186 }
   187 
   188 #endif