SDL_windowsvulkan.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's
  20. * SDL_x11vulkan.c.
  21. */
  22. #include "../../SDL_internal.h"
  23. #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_WINDOWS
  24. #include "SDL_windowsvideo.h"
  25. #include "SDL_windowswindow.h"
  26. #include "SDL_assert.h"
  27. #include "SDL_loadso.h"
  28. #include "SDL_windowsvulkan.h"
  29. #include "SDL_syswm.h"
  30. int WIN_Vulkan_LoadLibrary(_THIS, const char *path)
  31. {
  32. VkExtensionProperties *extensions = NULL;
  33. Uint32 extensionCount = 0;
  34. Uint32 i;
  35. SDL_bool hasSurfaceExtension = SDL_FALSE;
  36. SDL_bool hasWin32SurfaceExtension = SDL_FALSE;
  37. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
  38. if(_this->vulkan_config.loader_handle)
  39. return SDL_SetError("Vulkan already loaded");
  40. /* Load the Vulkan loader library */
  41. if(!path)
  42. path = SDL_getenv("SDL_VULKAN_LIBRARY");
  43. if(!path)
  44. path = "vulkan-1.dll";
  45. _this->vulkan_config.loader_handle = SDL_LoadObject(path);
  46. if(!_this->vulkan_config.loader_handle)
  47. return -1;
  48. SDL_strlcpy(_this->vulkan_config.loader_path, path,
  49. SDL_arraysize(_this->vulkan_config.loader_path));
  50. vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
  51. _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
  52. if(!vkGetInstanceProcAddr)
  53. goto fail;
  54. _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
  55. _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
  56. (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
  57. VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
  58. if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties)
  59. goto fail;
  60. extensions = SDL_Vulkan_CreateInstanceExtensionsList(
  61. (PFN_vkEnumerateInstanceExtensionProperties)
  62. _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
  63. &extensionCount);
  64. if(!extensions)
  65. goto fail;
  66. for(i = 0; i < extensionCount; i++)
  67. {
  68. if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
  69. hasSurfaceExtension = SDL_TRUE;
  70. else if(SDL_strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0)
  71. hasWin32SurfaceExtension = SDL_TRUE;
  72. }
  73. SDL_free(extensions);
  74. if(!hasSurfaceExtension)
  75. {
  76. SDL_SetError("Installed Vulkan doesn't implement the "
  77. VK_KHR_SURFACE_EXTENSION_NAME " extension");
  78. goto fail;
  79. }
  80. else if(!hasWin32SurfaceExtension)
  81. {
  82. SDL_SetError("Installed Vulkan doesn't implement the "
  83. VK_KHR_WIN32_SURFACE_EXTENSION_NAME "extension");
  84. goto fail;
  85. }
  86. return 0;
  87. fail:
  88. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  89. _this->vulkan_config.loader_handle = NULL;
  90. return -1;
  91. }
  92. void WIN_Vulkan_UnloadLibrary(_THIS)
  93. {
  94. if(_this->vulkan_config.loader_handle)
  95. {
  96. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  97. _this->vulkan_config.loader_handle = NULL;
  98. }
  99. }
  100. SDL_bool WIN_Vulkan_GetInstanceExtensions(_THIS,
  101. SDL_Window *window,
  102. unsigned *count,
  103. const char **names)
  104. {
  105. static const char *const extensionsForWin32[] = {
  106. VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME
  107. };
  108. if(!_this->vulkan_config.loader_handle)
  109. {
  110. SDL_SetError("Vulkan is not loaded");
  111. return SDL_FALSE;
  112. }
  113. return SDL_Vulkan_GetInstanceExtensions_Helper(
  114. count, names, SDL_arraysize(extensionsForWin32),
  115. extensionsForWin32);
  116. }
  117. SDL_bool WIN_Vulkan_CreateSurface(_THIS,
  118. SDL_Window *window,
  119. VkInstance instance,
  120. VkSurfaceKHR *surface)
  121. {
  122. SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
  123. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
  124. (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
  125. PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR =
  126. (PFN_vkCreateWin32SurfaceKHR)vkGetInstanceProcAddr(
  127. (VkInstance)instance,
  128. "vkCreateWin32SurfaceKHR");
  129. VkWin32SurfaceCreateInfoKHR createInfo;
  130. VkResult result;
  131. if(!_this->vulkan_config.loader_handle)
  132. {
  133. SDL_SetError("Vulkan is not loaded");
  134. return SDL_FALSE;
  135. }
  136. if(!vkCreateWin32SurfaceKHR)
  137. {
  138. SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME
  139. " extension is not enabled in the Vulkan instance.");
  140. return SDL_FALSE;
  141. }
  142. createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
  143. createInfo.pNext = NULL;
  144. createInfo.flags = 0;
  145. createInfo.hinstance = windowData->hinstance;
  146. createInfo.hwnd = windowData->hwnd;
  147. result = vkCreateWin32SurfaceKHR(instance, &createInfo,
  148. NULL, surface);
  149. if(result != VK_SUCCESS)
  150. {
  151. SDL_SetError("vkCreateWin32SurfaceKHR failed: %s",
  152. SDL_Vulkan_GetResultString(result));
  153. return SDL_FALSE;
  154. }
  155. return SDL_TRUE;
  156. }
  157. #endif
  158. /* vi: set ts=4 sw=4 expandtab: */