src/video/SDL_vulkan_utils.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 27 Aug 2017 22:15:57 -0400
changeset 11365 a9bd2625fa01
child 11381 2610f8f0a9c6
permissions -rw-r--r--
vulkan: Initial Vulkan support!

This work was done by Jacob Lifshay and Mark Callow; I'm just merging it
into revision control.
     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_SURFACE
    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         if(!retval)
   137         {
   138             SDL_OutOfMemory();
   139             return NULL;
   140         }
   141         *extensionCount = 0;
   142     }
   143     retval = SDL_calloc(count, sizeof(VkExtensionProperties));
   144     if(!retval)
   145     {
   146         SDL_OutOfMemory();
   147         return NULL;
   148     }
   149     result = vkEnumerateInstanceExtensionProperties(NULL, &count, retval);
   150     if(result != VK_SUCCESS)
   151     {
   152         SDL_SetError(
   153             "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
   154             "%s(%d)",
   155             SDL_Vulkan_GetResultString(result),
   156             (int)result);
   157         SDL_free(retval);
   158         return NULL;
   159     }
   160     *extensionCount = count;
   161     return retval;
   162 }
   163 
   164 SDL_bool SDL_Vulkan_GetInstanceExtensions_Helper(unsigned *userCount,
   165                                                  const char **userNames,
   166                                                  unsigned nameCount,
   167                                                  const char *const *names)
   168 {
   169     if(userNames)
   170     {
   171 		unsigned int i;
   172 
   173         if(*userCount != nameCount)
   174         {
   175             SDL_SetError(
   176                 "Count doesn't match count from previous call of SDL_Vulkan_GetInstanceExtensions");
   177             return SDL_FALSE;
   178         }
   179         for(i = 0; i < nameCount; i++)
   180         {
   181             userNames[i] = names[i];
   182         }
   183     }
   184     else
   185     {
   186         *userCount = nameCount;
   187     }
   188     return SDL_TRUE;
   189 }
   190 
   191 #endif