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
icculus@11365
     1
/*
icculus@11365
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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
slouken@11381
    26
#if SDL_VIDEO_VULKAN
icculus@11365
    27
icculus@11365
    28
const char *SDL_Vulkan_GetResultString(VkResult result)
icculus@11365
    29
{
icculus@11365
    30
    switch((int)result)
icculus@11365
    31
    {
icculus@11365
    32
    case VK_SUCCESS:
icculus@11365
    33
        return "VK_SUCCESS";
icculus@11365
    34
    case VK_NOT_READY:
icculus@11365
    35
        return "VK_NOT_READY";
icculus@11365
    36
    case VK_TIMEOUT:
icculus@11365
    37
        return "VK_TIMEOUT";
icculus@11365
    38
    case VK_EVENT_SET:
icculus@11365
    39
        return "VK_EVENT_SET";
icculus@11365
    40
    case VK_EVENT_RESET:
icculus@11365
    41
        return "VK_EVENT_RESET";
icculus@11365
    42
    case VK_INCOMPLETE:
icculus@11365
    43
        return "VK_INCOMPLETE";
icculus@11365
    44
    case VK_ERROR_OUT_OF_HOST_MEMORY:
icculus@11365
    45
        return "VK_ERROR_OUT_OF_HOST_MEMORY";
icculus@11365
    46
    case VK_ERROR_OUT_OF_DEVICE_MEMORY:
icculus@11365
    47
        return "VK_ERROR_OUT_OF_DEVICE_MEMORY";
icculus@11365
    48
    case VK_ERROR_INITIALIZATION_FAILED:
icculus@11365
    49
        return "VK_ERROR_INITIALIZATION_FAILED";
icculus@11365
    50
    case VK_ERROR_DEVICE_LOST:
icculus@11365
    51
        return "VK_ERROR_DEVICE_LOST";
icculus@11365
    52
    case VK_ERROR_MEMORY_MAP_FAILED:
icculus@11365
    53
        return "VK_ERROR_MEMORY_MAP_FAILED";
icculus@11365
    54
    case VK_ERROR_LAYER_NOT_PRESENT:
icculus@11365
    55
        return "VK_ERROR_LAYER_NOT_PRESENT";
icculus@11365
    56
    case VK_ERROR_EXTENSION_NOT_PRESENT:
icculus@11365
    57
        return "VK_ERROR_EXTENSION_NOT_PRESENT";
icculus@11365
    58
    case VK_ERROR_FEATURE_NOT_PRESENT:
icculus@11365
    59
        return "VK_ERROR_FEATURE_NOT_PRESENT";
icculus@11365
    60
    case VK_ERROR_INCOMPATIBLE_DRIVER:
icculus@11365
    61
        return "VK_ERROR_INCOMPATIBLE_DRIVER";
icculus@11365
    62
    case VK_ERROR_TOO_MANY_OBJECTS:
icculus@11365
    63
        return "VK_ERROR_TOO_MANY_OBJECTS";
icculus@11365
    64
    case VK_ERROR_FORMAT_NOT_SUPPORTED:
icculus@11365
    65
        return "VK_ERROR_FORMAT_NOT_SUPPORTED";
icculus@11365
    66
    case VK_ERROR_FRAGMENTED_POOL:
icculus@11365
    67
        return "VK_ERROR_FRAGMENTED_POOL";
icculus@11365
    68
    case VK_ERROR_SURFACE_LOST_KHR:
icculus@11365
    69
        return "VK_ERROR_SURFACE_LOST_KHR";
icculus@11365
    70
    case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
icculus@11365
    71
        return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
icculus@11365
    72
    case VK_SUBOPTIMAL_KHR:
icculus@11365
    73
        return "VK_SUBOPTIMAL_KHR";
icculus@11365
    74
    case VK_ERROR_OUT_OF_DATE_KHR:
icculus@11365
    75
        return "VK_ERROR_OUT_OF_DATE_KHR";
icculus@11365
    76
    case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
icculus@11365
    77
        return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
icculus@11365
    78
    case VK_ERROR_VALIDATION_FAILED_EXT:
icculus@11365
    79
        return "VK_ERROR_VALIDATION_FAILED_EXT";
icculus@11365
    80
    case VK_ERROR_OUT_OF_POOL_MEMORY_KHR:
icculus@11365
    81
        return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
icculus@11365
    82
    case VK_ERROR_INVALID_SHADER_NV:
icculus@11365
    83
        return "VK_ERROR_INVALID_SHADER_NV";
icculus@11365
    84
    case VK_RESULT_MAX_ENUM:
icculus@11365
    85
    case VK_RESULT_RANGE_SIZE:
icculus@11365
    86
        break;
icculus@11365
    87
    }
icculus@11365
    88
    if(result < 0)
icculus@11365
    89
        return "VK_ERROR_<Unknown>";
icculus@11365
    90
    return "VK_<Unknown>";
icculus@11365
    91
}
icculus@11365
    92
icculus@11365
    93
VkExtensionProperties *SDL_Vulkan_CreateInstanceExtensionsList(
icculus@11365
    94
    PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties,
icculus@11365
    95
    Uint32 *extensionCount)
icculus@11365
    96
{
icculus@11365
    97
    Uint32 count = 0;
icculus@11365
    98
    VkResult result = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
icculus@11365
    99
    VkExtensionProperties *retval;
icculus@11365
   100
    if(result == VK_ERROR_INCOMPATIBLE_DRIVER)
icculus@11365
   101
    {
libsdl@11518
   102
        /* Avoid the ERR_MAX_STRLEN limit by passing part of the message
libsdl@11518
   103
         * as a string argument.
libsdl@11518
   104
         */
icculus@11365
   105
        SDL_SetError(
libsdl@11518
   106
            "You probably don't have a working Vulkan driver installed. %s %s %s(%d)",
libsdl@11518
   107
            "Getting Vulkan extensions failed:",
libsdl@11518
   108
            "vkEnumerateInstanceExtensionProperties returned",
icculus@11365
   109
            SDL_Vulkan_GetResultString(result),
icculus@11365
   110
            (int)result);
icculus@11365
   111
        return NULL;
icculus@11365
   112
    }
icculus@11365
   113
    else if(result != VK_SUCCESS)
icculus@11365
   114
    {
icculus@11365
   115
        SDL_SetError(
icculus@11365
   116
            "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
icculus@11365
   117
            "%s(%d)",
icculus@11365
   118
            SDL_Vulkan_GetResultString(result),
icculus@11365
   119
            (int)result);
icculus@11365
   120
        return NULL;
icculus@11365
   121
    }
icculus@11365
   122
    if(count == 0)
icculus@11365
   123
    {
icculus@11365
   124
        retval = SDL_calloc(1, sizeof(VkExtensionProperties)); // so we can return non-null
icculus@11365
   125
    }
slouken@11385
   126
	else
slouken@11385
   127
	{
slouken@11385
   128
		retval = SDL_calloc(count, sizeof(VkExtensionProperties));
slouken@11385
   129
	}
icculus@11365
   130
    if(!retval)
icculus@11365
   131
    {
icculus@11365
   132
        SDL_OutOfMemory();
icculus@11365
   133
        return NULL;
icculus@11365
   134
    }
icculus@11365
   135
    result = vkEnumerateInstanceExtensionProperties(NULL, &count, retval);
icculus@11365
   136
    if(result != VK_SUCCESS)
icculus@11365
   137
    {
icculus@11365
   138
        SDL_SetError(
icculus@11365
   139
            "Getting Vulkan extensions failed: vkEnumerateInstanceExtensionProperties returned "
icculus@11365
   140
            "%s(%d)",
icculus@11365
   141
            SDL_Vulkan_GetResultString(result),
icculus@11365
   142
            (int)result);
icculus@11365
   143
        SDL_free(retval);
icculus@11365
   144
        return NULL;
icculus@11365
   145
    }
icculus@11365
   146
    *extensionCount = count;
icculus@11365
   147
    return retval;
icculus@11365
   148
}
icculus@11365
   149
icculus@11365
   150
SDL_bool SDL_Vulkan_GetInstanceExtensions_Helper(unsigned *userCount,
icculus@11365
   151
                                                 const char **userNames,
icculus@11365
   152
                                                 unsigned nameCount,
icculus@11365
   153
                                                 const char *const *names)
icculus@11365
   154
{
slouken@11828
   155
    if (userNames) {
slouken@11828
   156
        unsigned i;
icculus@11365
   157
slouken@11828
   158
        if (*userCount < nameCount) {
slouken@11828
   159
            SDL_SetError("Output array for SDL_Vulkan_GetInstanceExtensions needs to be at least %d big", nameCount);
icculus@11365
   160
            return SDL_FALSE;
icculus@11365
   161
        }
slouken@11828
   162
        for (i = 0; i < nameCount; i++) {
icculus@11365
   163
            userNames[i] = names[i];
icculus@11365
   164
        }
icculus@11365
   165
    }
jpa@11827
   166
    *userCount = nameCount;
icculus@11365
   167
    return SDL_TRUE;
icculus@11365
   168
}
icculus@11365
   169
icculus@11365
   170
#endif
slouken@11828
   171
slouken@11828
   172
/* vi: set ts=4 sw=4 expandtab: */