src/video/windows/SDL_windowsvulkan.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 23 Oct 2018 01:34:03 -0400
branchSDL-ryan-batching-renderer
changeset 12352 2b1707cc57fc
parent 11872 e917e911dab6
child 12201 8bdc4d340419
permissions -rw-r--r--
render: Add floating point versions of various draw APIs.
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
icculus@11365
    22
/*
icculus@11365
    23
 * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's
icculus@11365
    24
 * SDL_x11vulkan.c.
icculus@11365
    25
 */
icculus@11365
    26
icculus@11365
    27
#include "../../SDL_internal.h"
icculus@11365
    28
slouken@11381
    29
#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_WINDOWS
icculus@11365
    30
icculus@11365
    31
#include "SDL_windowsvideo.h"
icculus@11365
    32
#include "SDL_windowswindow.h"
icculus@11365
    33
#include "SDL_assert.h"
icculus@11365
    34
icculus@11365
    35
#include "SDL_loadso.h"
icculus@11365
    36
#include "SDL_windowsvulkan.h"
icculus@11365
    37
#include "SDL_syswm.h"
icculus@11365
    38
icculus@11365
    39
int WIN_Vulkan_LoadLibrary(_THIS, const char *path)
icculus@11365
    40
{
icculus@11365
    41
    VkExtensionProperties *extensions = NULL;
icculus@11365
    42
    Uint32 extensionCount = 0;
icculus@11365
    43
	Uint32 i;
icculus@11365
    44
    SDL_bool hasSurfaceExtension = SDL_FALSE;
icculus@11365
    45
    SDL_bool hasWin32SurfaceExtension = SDL_FALSE;
icculus@11365
    46
    PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
icculus@11365
    47
    if(_this->vulkan_config.loader_handle)
icculus@11365
    48
        return SDL_SetError("Vulkan already loaded");
icculus@11365
    49
icculus@11365
    50
    /* Load the Vulkan loader library */
icculus@11365
    51
    if(!path)
icculus@11365
    52
        path = SDL_getenv("SDL_VULKAN_LIBRARY");
icculus@11365
    53
    if(!path)
icculus@11365
    54
        path = "vulkan-1.dll";
icculus@11365
    55
    _this->vulkan_config.loader_handle = SDL_LoadObject(path);
icculus@11365
    56
    if(!_this->vulkan_config.loader_handle)
icculus@11365
    57
        return -1;
icculus@11365
    58
    SDL_strlcpy(_this->vulkan_config.loader_path, path,
icculus@11365
    59
                SDL_arraysize(_this->vulkan_config.loader_path));
sezero@11872
    60
    vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
icculus@11365
    61
        _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
icculus@11365
    62
    if(!vkGetInstanceProcAddr)
icculus@11365
    63
        goto fail;
sezero@11872
    64
    _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
icculus@11365
    65
    _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
sezero@11872
    66
        (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
icculus@11365
    67
            VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
icculus@11365
    68
    if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties)
icculus@11365
    69
        goto fail;
icculus@11365
    70
    extensions = SDL_Vulkan_CreateInstanceExtensionsList(
icculus@11365
    71
        (PFN_vkEnumerateInstanceExtensionProperties)
icculus@11365
    72
            _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
icculus@11365
    73
        &extensionCount);
icculus@11365
    74
    if(!extensions)
icculus@11365
    75
        goto fail;
icculus@11365
    76
    for(i = 0; i < extensionCount; i++)
icculus@11365
    77
    {
icculus@11365
    78
        if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
icculus@11365
    79
            hasSurfaceExtension = SDL_TRUE;
icculus@11365
    80
        else if(SDL_strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
icculus@11365
    81
            hasWin32SurfaceExtension = SDL_TRUE;
icculus@11365
    82
    }
icculus@11365
    83
    SDL_free(extensions);
icculus@11365
    84
    if(!hasSurfaceExtension)
icculus@11365
    85
    {
icculus@11365
    86
        SDL_SetError("Installed Vulkan doesn't implement the "
icculus@11365
    87
                     VK_KHR_SURFACE_EXTENSION_NAME " extension");
icculus@11365
    88
        goto fail;
icculus@11365
    89
    }
icculus@11365
    90
    else if(!hasWin32SurfaceExtension)
icculus@11365
    91
    {
icculus@11365
    92
        SDL_SetError("Installed Vulkan doesn't implement the "
icculus@11365
    93
                     VK_KHR_WIN32_SURFACE_EXTENSION_NAME "extension");
icculus@11365
    94
        goto fail;
icculus@11365
    95
    }
icculus@11365
    96
    return 0;
icculus@11365
    97
icculus@11365
    98
fail:
icculus@11365
    99
    SDL_UnloadObject(_this->vulkan_config.loader_handle);
icculus@11365
   100
    _this->vulkan_config.loader_handle = NULL;
icculus@11365
   101
    return -1;
icculus@11365
   102
}
icculus@11365
   103
icculus@11365
   104
void WIN_Vulkan_UnloadLibrary(_THIS)
icculus@11365
   105
{
icculus@11365
   106
    if(_this->vulkan_config.loader_handle)
icculus@11365
   107
    {
icculus@11365
   108
        SDL_UnloadObject(_this->vulkan_config.loader_handle);
icculus@11365
   109
        _this->vulkan_config.loader_handle = NULL;
icculus@11365
   110
    }
icculus@11365
   111
}
icculus@11365
   112
icculus@11365
   113
SDL_bool WIN_Vulkan_GetInstanceExtensions(_THIS,
icculus@11365
   114
                                          SDL_Window *window,
icculus@11365
   115
                                          unsigned *count,
icculus@11365
   116
                                          const char **names)
icculus@11365
   117
{
icculus@11365
   118
    static const char *const extensionsForWin32[] = {
icculus@11365
   119
        VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME
icculus@11365
   120
    };
icculus@11365
   121
    if(!_this->vulkan_config.loader_handle)
icculus@11365
   122
    {
icculus@11365
   123
        SDL_SetError("Vulkan is not loaded");
icculus@11365
   124
        return SDL_FALSE;
icculus@11365
   125
    }
icculus@11365
   126
    return SDL_Vulkan_GetInstanceExtensions_Helper(
icculus@11365
   127
            count, names, SDL_arraysize(extensionsForWin32),
icculus@11365
   128
            extensionsForWin32);
icculus@11365
   129
}
icculus@11365
   130
icculus@11365
   131
SDL_bool WIN_Vulkan_CreateSurface(_THIS,
icculus@11365
   132
                                  SDL_Window *window,
icculus@11365
   133
                                  VkInstance instance,
icculus@11365
   134
                                  VkSurfaceKHR *surface)
icculus@11365
   135
{
icculus@11365
   136
    SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
icculus@11365
   137
    PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
icculus@11365
   138
        (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
icculus@11365
   139
    PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR =
icculus@11365
   140
        (PFN_vkCreateWin32SurfaceKHR)vkGetInstanceProcAddr(
icculus@11365
   141
                                            (VkInstance)instance,
icculus@11365
   142
                                            "vkCreateWin32SurfaceKHR");
icculus@11365
   143
    VkWin32SurfaceCreateInfoKHR createInfo;
icculus@11365
   144
    VkResult result;
icculus@11365
   145
icculus@11365
   146
    if(!_this->vulkan_config.loader_handle)
icculus@11365
   147
    {
icculus@11365
   148
        SDL_SetError("Vulkan is not loaded");
icculus@11365
   149
        return SDL_FALSE;
icculus@11365
   150
    }
icculus@11365
   151
icculus@11365
   152
    if(!vkCreateWin32SurfaceKHR)
icculus@11365
   153
    {
icculus@11365
   154
        SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME
icculus@11365
   155
                     " extension is not enabled in the Vulkan instance.");
icculus@11365
   156
        return SDL_FALSE;
icculus@11365
   157
    }
icculus@11365
   158
    createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
icculus@11365
   159
    createInfo.pNext = NULL;
icculus@11365
   160
    createInfo.flags = 0;
icculus@11365
   161
    createInfo.hinstance = windowData->hinstance;
icculus@11365
   162
    createInfo.hwnd = windowData->hwnd;
icculus@11365
   163
    result = vkCreateWin32SurfaceKHR(instance, &createInfo,
icculus@11365
   164
                                       NULL, surface);
icculus@11365
   165
    if(result != VK_SUCCESS)
icculus@11365
   166
    {
icculus@11365
   167
        SDL_SetError("vkCreateWin32SurfaceKHR failed: %s",
icculus@11365
   168
                     SDL_Vulkan_GetResultString(result));
icculus@11365
   169
        return SDL_FALSE;
icculus@11365
   170
    }
icculus@11365
   171
    return SDL_TRUE;
icculus@11365
   172
}
icculus@11365
   173
icculus@11365
   174
#endif
icculus@11365
   175
icculus@11365
   176
/* vi: set ts=4 sw=4 expandtab: */