DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Log | Files | Refs | README

vulkan.c (11854B)


      1 //========================================================================
      2 // GLFW 3.3 - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2002-2006 Marcus Geelnard
      5 // Copyright (c) 2006-2018 Camilla Löwy <elmindreda@glfw.org>
      6 //
      7 // This software is provided 'as-is', without any express or implied
      8 // warranty. In no event will the authors be held liable for any damages
      9 // arising from the use of this software.
     10 //
     11 // Permission is granted to anyone to use this software for any purpose,
     12 // including commercial applications, and to alter it and redistribute it
     13 // freely, subject to the following restrictions:
     14 //
     15 // 1. The origin of this software must not be misrepresented; you must not
     16 //    claim that you wrote the original software. If you use this software
     17 //    in a product, an acknowledgment in the product documentation would
     18 //    be appreciated but is not required.
     19 //
     20 // 2. Altered source versions must be plainly marked as such, and must not
     21 //    be misrepresented as being the original software.
     22 //
     23 // 3. This notice may not be removed or altered from any source
     24 //    distribution.
     25 //
     26 //========================================================================
     27 // Please use C89 style variable declarations in this file because VS 2010
     28 //========================================================================
     29 
     30 #include "internal.h"
     31 
     32 #include <assert.h>
     33 #include <string.h>
     34 #include <stdlib.h>
     35 
     36 #define _GLFW_FIND_LOADER    1
     37 #define _GLFW_REQUIRE_LOADER 2
     38 
     39 
     40 //////////////////////////////////////////////////////////////////////////
     41 //////                       GLFW internal API                      //////
     42 //////////////////////////////////////////////////////////////////////////
     43 
     44 GLFWbool _glfwInitVulkan(int mode)
     45 {
     46     VkResult err;
     47     VkExtensionProperties* ep;
     48     uint32_t i, count;
     49 
     50     if (_glfw.vk.available)
     51         return GLFW_TRUE;
     52 
     53 #if !defined(_GLFW_VULKAN_STATIC)
     54 #if defined(_GLFW_VULKAN_LIBRARY)
     55     _glfw.vk.handle = _glfw_dlopen(_GLFW_VULKAN_LIBRARY);
     56 #elif defined(_GLFW_WIN32)
     57     _glfw.vk.handle = _glfw_dlopen("vulkan-1.dll");
     58 #elif defined(_GLFW_COCOA)
     59     _glfw.vk.handle = _glfw_dlopen("libvulkan.1.dylib");
     60     if (!_glfw.vk.handle)
     61         _glfw.vk.handle = _glfwLoadLocalVulkanLoaderNS();
     62 #elif defined(__OpenBSD__) || defined(__NetBSD__)
     63     _glfw.vk.handle = _glfw_dlopen("libvulkan.so");
     64 #else
     65     _glfw.vk.handle = _glfw_dlopen("libvulkan.so.1");
     66 #endif
     67     if (!_glfw.vk.handle)
     68     {
     69         if (mode == _GLFW_REQUIRE_LOADER)
     70             _glfwInputError(GLFW_API_UNAVAILABLE, "Vulkan: Loader not found");
     71 
     72         return GLFW_FALSE;
     73     }
     74 
     75     _glfw.vk.GetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)
     76         _glfw_dlsym(_glfw.vk.handle, "vkGetInstanceProcAddr");
     77     if (!_glfw.vk.GetInstanceProcAddr)
     78     {
     79         _glfwInputError(GLFW_API_UNAVAILABLE,
     80                         "Vulkan: Loader does not export vkGetInstanceProcAddr");
     81 
     82         _glfwTerminateVulkan();
     83         return GLFW_FALSE;
     84     }
     85 
     86     _glfw.vk.EnumerateInstanceExtensionProperties = (PFN_vkEnumerateInstanceExtensionProperties)
     87         vkGetInstanceProcAddr(NULL, "vkEnumerateInstanceExtensionProperties");
     88     if (!_glfw.vk.EnumerateInstanceExtensionProperties)
     89     {
     90         _glfwInputError(GLFW_API_UNAVAILABLE,
     91                         "Vulkan: Failed to retrieve vkEnumerateInstanceExtensionProperties");
     92 
     93         _glfwTerminateVulkan();
     94         return GLFW_FALSE;
     95     }
     96 #endif // _GLFW_VULKAN_STATIC
     97 
     98     err = vkEnumerateInstanceExtensionProperties(NULL, &count, NULL);
     99     if (err)
    100     {
    101         // NOTE: This happens on systems with a loader but without any Vulkan ICD
    102         if (mode == _GLFW_REQUIRE_LOADER)
    103         {
    104             _glfwInputError(GLFW_API_UNAVAILABLE,
    105                             "Vulkan: Failed to query instance extension count: %s",
    106                             _glfwGetVulkanResultString(err));
    107         }
    108 
    109         _glfwTerminateVulkan();
    110         return GLFW_FALSE;
    111     }
    112 
    113     ep = calloc(count, sizeof(VkExtensionProperties));
    114 
    115     err = vkEnumerateInstanceExtensionProperties(NULL, &count, ep);
    116     if (err)
    117     {
    118         _glfwInputError(GLFW_API_UNAVAILABLE,
    119                         "Vulkan: Failed to query instance extensions: %s",
    120                         _glfwGetVulkanResultString(err));
    121 
    122         free(ep);
    123         _glfwTerminateVulkan();
    124         return GLFW_FALSE;
    125     }
    126 
    127     for (i = 0;  i < count;  i++)
    128     {
    129         if (strcmp(ep[i].extensionName, "VK_KHR_surface") == 0)
    130             _glfw.vk.KHR_surface = GLFW_TRUE;
    131 #if defined(_GLFW_WIN32)
    132         else if (strcmp(ep[i].extensionName, "VK_KHR_win32_surface") == 0)
    133             _glfw.vk.KHR_win32_surface = GLFW_TRUE;
    134 #elif defined(_GLFW_COCOA)
    135         else if (strcmp(ep[i].extensionName, "VK_MVK_macos_surface") == 0)
    136             _glfw.vk.MVK_macos_surface = GLFW_TRUE;
    137         else if (strcmp(ep[i].extensionName, "VK_EXT_metal_surface") == 0)
    138             _glfw.vk.EXT_metal_surface = GLFW_TRUE;
    139 #elif defined(_GLFW_X11)
    140         else if (strcmp(ep[i].extensionName, "VK_KHR_xlib_surface") == 0)
    141             _glfw.vk.KHR_xlib_surface = GLFW_TRUE;
    142         else if (strcmp(ep[i].extensionName, "VK_KHR_xcb_surface") == 0)
    143             _glfw.vk.KHR_xcb_surface = GLFW_TRUE;
    144 #elif defined(_GLFW_WAYLAND)
    145         else if (strcmp(ep[i].extensionName, "VK_KHR_wayland_surface") == 0)
    146             _glfw.vk.KHR_wayland_surface = GLFW_TRUE;
    147 #endif
    148     }
    149 
    150     free(ep);
    151 
    152     _glfw.vk.available = GLFW_TRUE;
    153 
    154     _glfwPlatformGetRequiredInstanceExtensions(_glfw.vk.extensions);
    155 
    156     return GLFW_TRUE;
    157 }
    158 
    159 void _glfwTerminateVulkan(void)
    160 {
    161 #if !defined(_GLFW_VULKAN_STATIC)
    162     if (_glfw.vk.handle)
    163         _glfw_dlclose(_glfw.vk.handle);
    164 #endif
    165 }
    166 
    167 const char* _glfwGetVulkanResultString(VkResult result)
    168 {
    169     switch (result)
    170     {
    171         case VK_SUCCESS:
    172             return "Success";
    173         case VK_NOT_READY:
    174             return "A fence or query has not yet completed";
    175         case VK_TIMEOUT:
    176             return "A wait operation has not completed in the specified time";
    177         case VK_EVENT_SET:
    178             return "An event is signaled";
    179         case VK_EVENT_RESET:
    180             return "An event is unsignaled";
    181         case VK_INCOMPLETE:
    182             return "A return array was too small for the result";
    183         case VK_ERROR_OUT_OF_HOST_MEMORY:
    184             return "A host memory allocation has failed";
    185         case VK_ERROR_OUT_OF_DEVICE_MEMORY:
    186             return "A device memory allocation has failed";
    187         case VK_ERROR_INITIALIZATION_FAILED:
    188             return "Initialization of an object could not be completed for implementation-specific reasons";
    189         case VK_ERROR_DEVICE_LOST:
    190             return "The logical or physical device has been lost";
    191         case VK_ERROR_MEMORY_MAP_FAILED:
    192             return "Mapping of a memory object has failed";
    193         case VK_ERROR_LAYER_NOT_PRESENT:
    194             return "A requested layer is not present or could not be loaded";
    195         case VK_ERROR_EXTENSION_NOT_PRESENT:
    196             return "A requested extension is not supported";
    197         case VK_ERROR_FEATURE_NOT_PRESENT:
    198             return "A requested feature is not supported";
    199         case VK_ERROR_INCOMPATIBLE_DRIVER:
    200             return "The requested version of Vulkan is not supported by the driver or is otherwise incompatible";
    201         case VK_ERROR_TOO_MANY_OBJECTS:
    202             return "Too many objects of the type have already been created";
    203         case VK_ERROR_FORMAT_NOT_SUPPORTED:
    204             return "A requested format is not supported on this device";
    205         case VK_ERROR_SURFACE_LOST_KHR:
    206             return "A surface is no longer available";
    207         case VK_SUBOPTIMAL_KHR:
    208             return "A swapchain no longer matches the surface properties exactly, but can still be used";
    209         case VK_ERROR_OUT_OF_DATE_KHR:
    210             return "A surface has changed in such a way that it is no longer compatible with the swapchain";
    211         case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
    212             return "The display used by a swapchain does not use the same presentable image layout";
    213         case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
    214             return "The requested window is already connected to a VkSurfaceKHR, or to some other non-Vulkan API";
    215         case VK_ERROR_VALIDATION_FAILED_EXT:
    216             return "A validation layer found an error";
    217         default:
    218             return "ERROR: UNKNOWN VULKAN ERROR";
    219     }
    220 }
    221 
    222 
    223 //////////////////////////////////////////////////////////////////////////
    224 //////                        GLFW public API                       //////
    225 //////////////////////////////////////////////////////////////////////////
    226 
    227 GLFWAPI int glfwVulkanSupported(void)
    228 {
    229     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
    230     return _glfwInitVulkan(_GLFW_FIND_LOADER);
    231 }
    232 
    233 GLFWAPI const char** glfwGetRequiredInstanceExtensions(uint32_t* count)
    234 {
    235     assert(count != NULL);
    236 
    237     *count = 0;
    238 
    239     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    240 
    241     if (!_glfwInitVulkan(_GLFW_REQUIRE_LOADER))
    242         return NULL;
    243 
    244     if (!_glfw.vk.extensions[0])
    245         return NULL;
    246 
    247     *count = 2;
    248     return (const char**) _glfw.vk.extensions;
    249 }
    250 
    251 GLFWAPI GLFWvkproc glfwGetInstanceProcAddress(VkInstance instance,
    252                                               const char* procname)
    253 {
    254     GLFWvkproc proc;
    255     assert(procname != NULL);
    256 
    257     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    258 
    259     if (!_glfwInitVulkan(_GLFW_REQUIRE_LOADER))
    260         return NULL;
    261 
    262     proc = (GLFWvkproc) vkGetInstanceProcAddr(instance, procname);
    263 #if defined(_GLFW_VULKAN_STATIC)
    264     if (!proc)
    265     {
    266         if (strcmp(procname, "vkGetInstanceProcAddr") == 0)
    267             return (GLFWvkproc) vkGetInstanceProcAddr;
    268     }
    269 #else
    270     if (!proc)
    271         proc = (GLFWvkproc) _glfw_dlsym(_glfw.vk.handle, procname);
    272 #endif
    273 
    274     return proc;
    275 }
    276 
    277 GLFWAPI int glfwGetPhysicalDevicePresentationSupport(VkInstance instance,
    278                                                      VkPhysicalDevice device,
    279                                                      uint32_t queuefamily)
    280 {
    281     assert(instance != VK_NULL_HANDLE);
    282     assert(device != VK_NULL_HANDLE);
    283 
    284     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
    285 
    286     if (!_glfwInitVulkan(_GLFW_REQUIRE_LOADER))
    287         return GLFW_FALSE;
    288 
    289     if (!_glfw.vk.extensions[0])
    290     {
    291         _glfwInputError(GLFW_API_UNAVAILABLE,
    292                         "Vulkan: Window surface creation extensions not found");
    293         return GLFW_FALSE;
    294     }
    295 
    296     return _glfwPlatformGetPhysicalDevicePresentationSupport(instance,
    297                                                              device,
    298                                                              queuefamily);
    299 }
    300 
    301 GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance,
    302                                          GLFWwindow* handle,
    303                                          const VkAllocationCallbacks* allocator,
    304                                          VkSurfaceKHR* surface)
    305 {
    306     _GLFWwindow* window = (_GLFWwindow*) handle;
    307     assert(instance != VK_NULL_HANDLE);
    308     assert(window != NULL);
    309     assert(surface != NULL);
    310 
    311     *surface = VK_NULL_HANDLE;
    312 
    313     _GLFW_REQUIRE_INIT_OR_RETURN(VK_ERROR_INITIALIZATION_FAILED);
    314 
    315     if (!_glfwInitVulkan(_GLFW_REQUIRE_LOADER))
    316         return VK_ERROR_INITIALIZATION_FAILED;
    317 
    318     if (!_glfw.vk.extensions[0])
    319     {
    320         _glfwInputError(GLFW_API_UNAVAILABLE,
    321                         "Vulkan: Window surface creation extensions not found");
    322         return VK_ERROR_EXTENSION_NOT_PRESENT;
    323     }
    324 
    325     if (window->context.client != GLFW_NO_API)
    326     {
    327         _glfwInputError(GLFW_INVALID_VALUE,
    328                         "Vulkan: Window surface creation requires the window to have the client API set to GLFW_NO_API");
    329         return VK_ERROR_NATIVE_WINDOW_IN_USE_KHR;
    330     }
    331 
    332     return _glfwPlatformCreateWindowSurface(instance, window, allocator, surface);
    333 }
    334