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