🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1152 lines
45 KiB

  1. /*
  2. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include "SDL_test_common.h"
  15. #if defined(__ANDROID__) && defined(__ARM_EABI__) && !defined(__ARM_ARCH_7A__)
  16. int main(int argc, char *argv[])
  17. {
  18. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Vulkan support on this system\n");
  19. return 1;
  20. }
  21. #else
  22. #define VK_NO_PROTOTYPES
  23. #ifdef HAVE_VULKAN_H
  24. #include <vulkan/vulkan.h>
  25. #else
  26. /* SDL includes a copy for building on systems without the Vulkan SDK */
  27. #include "../src/video/khronos/vulkan/vulkan.h"
  28. #endif
  29. #include "SDL_vulkan.h"
  30. #ifndef UINT64_MAX /* VS2008 */
  31. #define UINT64_MAX 18446744073709551615
  32. #endif
  33. #define VULKAN_FUNCTIONS() \
  34. VULKAN_DEVICE_FUNCTION(vkAcquireNextImageKHR) \
  35. VULKAN_DEVICE_FUNCTION(vkAllocateCommandBuffers) \
  36. VULKAN_DEVICE_FUNCTION(vkBeginCommandBuffer) \
  37. VULKAN_DEVICE_FUNCTION(vkCmdClearColorImage) \
  38. VULKAN_DEVICE_FUNCTION(vkCmdPipelineBarrier) \
  39. VULKAN_DEVICE_FUNCTION(vkCreateCommandPool) \
  40. VULKAN_DEVICE_FUNCTION(vkCreateFence) \
  41. VULKAN_DEVICE_FUNCTION(vkCreateImageView) \
  42. VULKAN_DEVICE_FUNCTION(vkCreateSemaphore) \
  43. VULKAN_DEVICE_FUNCTION(vkCreateSwapchainKHR) \
  44. VULKAN_DEVICE_FUNCTION(vkDestroyCommandPool) \
  45. VULKAN_DEVICE_FUNCTION(vkDestroyDevice) \
  46. VULKAN_DEVICE_FUNCTION(vkDestroyFence) \
  47. VULKAN_DEVICE_FUNCTION(vkDestroyImageView) \
  48. VULKAN_DEVICE_FUNCTION(vkDestroySemaphore) \
  49. VULKAN_DEVICE_FUNCTION(vkDestroySwapchainKHR) \
  50. VULKAN_DEVICE_FUNCTION(vkDeviceWaitIdle) \
  51. VULKAN_DEVICE_FUNCTION(vkEndCommandBuffer) \
  52. VULKAN_DEVICE_FUNCTION(vkFreeCommandBuffers) \
  53. VULKAN_DEVICE_FUNCTION(vkGetDeviceQueue) \
  54. VULKAN_DEVICE_FUNCTION(vkGetFenceStatus) \
  55. VULKAN_DEVICE_FUNCTION(vkGetSwapchainImagesKHR) \
  56. VULKAN_DEVICE_FUNCTION(vkQueuePresentKHR) \
  57. VULKAN_DEVICE_FUNCTION(vkQueueSubmit) \
  58. VULKAN_DEVICE_FUNCTION(vkResetCommandBuffer) \
  59. VULKAN_DEVICE_FUNCTION(vkResetFences) \
  60. VULKAN_DEVICE_FUNCTION(vkWaitForFences) \
  61. VULKAN_GLOBAL_FUNCTION(vkCreateInstance) \
  62. VULKAN_GLOBAL_FUNCTION(vkEnumerateInstanceExtensionProperties) \
  63. VULKAN_GLOBAL_FUNCTION(vkEnumerateInstanceLayerProperties) \
  64. VULKAN_INSTANCE_FUNCTION(vkCreateDevice) \
  65. VULKAN_INSTANCE_FUNCTION(vkDestroyInstance) \
  66. VULKAN_INSTANCE_FUNCTION(vkDestroySurfaceKHR) \
  67. VULKAN_INSTANCE_FUNCTION(vkEnumerateDeviceExtensionProperties) \
  68. VULKAN_INSTANCE_FUNCTION(vkEnumeratePhysicalDevices) \
  69. VULKAN_INSTANCE_FUNCTION(vkGetDeviceProcAddr) \
  70. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceFeatures) \
  71. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceProperties) \
  72. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties) \
  73. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \
  74. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceFormatsKHR) \
  75. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfacePresentModesKHR) \
  76. VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR)
  77. #define VULKAN_DEVICE_FUNCTION(name) static PFN_##name name = NULL;
  78. #define VULKAN_GLOBAL_FUNCTION(name) static PFN_##name name = NULL;
  79. #define VULKAN_INSTANCE_FUNCTION(name) static PFN_##name name = NULL;
  80. VULKAN_FUNCTIONS()
  81. #undef VULKAN_DEVICE_FUNCTION
  82. #undef VULKAN_GLOBAL_FUNCTION
  83. #undef VULKAN_INSTANCE_FUNCTION
  84. static PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
  85. /* Based on the headers found in
  86. * https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers
  87. */
  88. #if VK_HEADER_VERSION < 22
  89. enum
  90. {
  91. VK_ERROR_FRAGMENTED_POOL = -12,
  92. };
  93. #endif
  94. #if VK_HEADER_VERSION < 38
  95. enum {
  96. VK_ERROR_OUT_OF_POOL_MEMORY_KHR = -1000069000
  97. };
  98. #endif
  99. static const char *getVulkanResultString(VkResult result)
  100. {
  101. switch((int) result)
  102. {
  103. #define RESULT_CASE(x) case x: return #x
  104. RESULT_CASE(VK_SUCCESS);
  105. RESULT_CASE(VK_NOT_READY);
  106. RESULT_CASE(VK_TIMEOUT);
  107. RESULT_CASE(VK_EVENT_SET);
  108. RESULT_CASE(VK_EVENT_RESET);
  109. RESULT_CASE(VK_INCOMPLETE);
  110. RESULT_CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
  111. RESULT_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
  112. RESULT_CASE(VK_ERROR_INITIALIZATION_FAILED);
  113. RESULT_CASE(VK_ERROR_DEVICE_LOST);
  114. RESULT_CASE(VK_ERROR_MEMORY_MAP_FAILED);
  115. RESULT_CASE(VK_ERROR_LAYER_NOT_PRESENT);
  116. RESULT_CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
  117. RESULT_CASE(VK_ERROR_FEATURE_NOT_PRESENT);
  118. RESULT_CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
  119. RESULT_CASE(VK_ERROR_TOO_MANY_OBJECTS);
  120. RESULT_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
  121. RESULT_CASE(VK_ERROR_FRAGMENTED_POOL);
  122. RESULT_CASE(VK_ERROR_SURFACE_LOST_KHR);
  123. RESULT_CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
  124. RESULT_CASE(VK_SUBOPTIMAL_KHR);
  125. RESULT_CASE(VK_ERROR_OUT_OF_DATE_KHR);
  126. RESULT_CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
  127. RESULT_CASE(VK_ERROR_VALIDATION_FAILED_EXT);
  128. RESULT_CASE(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
  129. RESULT_CASE(VK_ERROR_INVALID_SHADER_NV);
  130. #undef RESULT_CASE
  131. default: break;
  132. }
  133. return (result < 0) ? "VK_ERROR_<Unknown>" : "VK_<Unknown>";
  134. }
  135. typedef struct VulkanContext
  136. {
  137. SDL_Window *window;
  138. VkInstance instance;
  139. VkDevice device;
  140. VkSurfaceKHR surface;
  141. VkSwapchainKHR swapchain;
  142. VkPhysicalDeviceProperties physicalDeviceProperties;
  143. VkPhysicalDeviceFeatures physicalDeviceFeatures;
  144. uint32_t graphicsQueueFamilyIndex;
  145. uint32_t presentQueueFamilyIndex;
  146. VkPhysicalDevice physicalDevice;
  147. VkQueue graphicsQueue;
  148. VkQueue presentQueue;
  149. VkSemaphore imageAvailableSemaphore;
  150. VkSemaphore renderingFinishedSemaphore;
  151. VkSurfaceCapabilitiesKHR surfaceCapabilities;
  152. VkSurfaceFormatKHR *surfaceFormats;
  153. uint32_t surfaceFormatsAllocatedCount;
  154. uint32_t surfaceFormatsCount;
  155. uint32_t swapchainDesiredImageCount;
  156. VkSurfaceFormatKHR surfaceFormat;
  157. VkExtent2D swapchainSize;
  158. VkCommandPool commandPool;
  159. uint32_t swapchainImageCount;
  160. VkImage *swapchainImages;
  161. VkCommandBuffer *commandBuffers;
  162. VkFence *fences;
  163. } VulkanContext;
  164. static SDLTest_CommonState *state;
  165. static VulkanContext *vulkanContexts = NULL; // an array of state->num_windows items
  166. static VulkanContext *vulkanContext = NULL; // for the currently-rendering window
  167. static void shutdownVulkan(SDL_bool doDestroySwapchain);
  168. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  169. static void quit(int rc)
  170. {
  171. shutdownVulkan(SDL_TRUE);
  172. SDLTest_CommonQuit(state);
  173. exit(rc);
  174. }
  175. static void loadGlobalFunctions(void)
  176. {
  177. vkGetInstanceProcAddr = SDL_Vulkan_GetVkGetInstanceProcAddr();
  178. if (!vkGetInstanceProcAddr) {
  179. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  180. "SDL_Vulkan_GetVkGetInstanceProcAddr(): %s\n",
  181. SDL_GetError());
  182. quit(2);
  183. }
  184. #define VULKAN_DEVICE_FUNCTION(name)
  185. #define VULKAN_GLOBAL_FUNCTION(name) \
  186. name = (PFN_##name)vkGetInstanceProcAddr(VK_NULL_HANDLE, #name); \
  187. if (!name) { \
  188. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
  189. "vkGetInstanceProcAddr(VK_NULL_HANDLE, \"" #name "\") failed\n"); \
  190. quit(2); \
  191. }
  192. #define VULKAN_INSTANCE_FUNCTION(name)
  193. VULKAN_FUNCTIONS()
  194. #undef VULKAN_DEVICE_FUNCTION
  195. #undef VULKAN_GLOBAL_FUNCTION
  196. #undef VULKAN_INSTANCE_FUNCTION
  197. }
  198. static void createInstance(void)
  199. {
  200. VkApplicationInfo appInfo = {0};
  201. VkInstanceCreateInfo instanceCreateInfo = {0};
  202. const char **extensions = NULL;
  203. unsigned extensionCount = 0;
  204. VkResult result;
  205. appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
  206. appInfo.apiVersion = VK_API_VERSION_1_0;
  207. instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
  208. instanceCreateInfo.pApplicationInfo = &appInfo;
  209. if (!SDL_Vulkan_GetInstanceExtensions(NULL, &extensionCount, NULL)) {
  210. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  211. "SDL_Vulkan_GetInstanceExtensions(): %s\n",
  212. SDL_GetError());
  213. quit(2);
  214. }
  215. extensions = (const char **) SDL_malloc(sizeof(const char *) * extensionCount);
  216. if (!extensions) {
  217. SDL_OutOfMemory();
  218. quit(2);
  219. }
  220. if (!SDL_Vulkan_GetInstanceExtensions(NULL, &extensionCount, extensions)) {
  221. SDL_free((void*)extensions);
  222. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  223. "SDL_Vulkan_GetInstanceExtensions(): %s\n",
  224. SDL_GetError());
  225. quit(2);
  226. }
  227. instanceCreateInfo.enabledExtensionCount = extensionCount;
  228. instanceCreateInfo.ppEnabledExtensionNames = extensions;
  229. result = vkCreateInstance(&instanceCreateInfo, NULL, &vulkanContext->instance);
  230. SDL_free((void*)extensions);
  231. if (result != VK_SUCCESS) {
  232. vulkanContext->instance = VK_NULL_HANDLE;
  233. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  234. "vkCreateInstance(): %s\n",
  235. getVulkanResultString(result));
  236. quit(2);
  237. }
  238. }
  239. static void loadInstanceFunctions(void)
  240. {
  241. #define VULKAN_DEVICE_FUNCTION(name)
  242. #define VULKAN_GLOBAL_FUNCTION(name)
  243. #define VULKAN_INSTANCE_FUNCTION(name) \
  244. name = (PFN_##name)vkGetInstanceProcAddr(vulkanContext->instance, #name); \
  245. if (!name) { \
  246. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
  247. "vkGetInstanceProcAddr(instance, \"" #name "\") failed\n"); \
  248. quit(2); \
  249. }
  250. VULKAN_FUNCTIONS()
  251. #undef VULKAN_DEVICE_FUNCTION
  252. #undef VULKAN_GLOBAL_FUNCTION
  253. #undef VULKAN_INSTANCE_FUNCTION
  254. }
  255. static void createSurface(void)
  256. {
  257. if (!SDL_Vulkan_CreateSurface(vulkanContext->window,
  258. vulkanContext->instance,
  259. &vulkanContext->surface)) {
  260. vulkanContext->surface = VK_NULL_HANDLE;
  261. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Vulkan_CreateSurface(): %s\n", SDL_GetError());
  262. quit(2);
  263. }
  264. }
  265. static void findPhysicalDevice(void)
  266. {
  267. uint32_t physicalDeviceCount = 0;
  268. VkPhysicalDevice *physicalDevices;
  269. VkQueueFamilyProperties *queueFamiliesProperties = NULL;
  270. uint32_t queueFamiliesPropertiesAllocatedSize = 0;
  271. VkExtensionProperties *deviceExtensions = NULL;
  272. uint32_t deviceExtensionsAllocatedSize = 0;
  273. uint32_t physicalDeviceIndex;
  274. VkResult result;
  275. result = vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, NULL);
  276. if (result != VK_SUCCESS) {
  277. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  278. "vkEnumeratePhysicalDevices(): %s\n",
  279. getVulkanResultString(result));
  280. quit(2);
  281. }
  282. if (physicalDeviceCount == 0) {
  283. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  284. "vkEnumeratePhysicalDevices(): no physical devices\n");
  285. quit(2);
  286. }
  287. physicalDevices = (VkPhysicalDevice *) SDL_malloc(sizeof(VkPhysicalDevice) * physicalDeviceCount);
  288. if (!physicalDevices) {
  289. SDL_OutOfMemory();
  290. quit(2);
  291. }
  292. result = vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, physicalDevices);
  293. if (result != VK_SUCCESS) {
  294. SDL_free(physicalDevices);
  295. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  296. "vkEnumeratePhysicalDevices(): %s\n",
  297. getVulkanResultString(result));
  298. quit(2);
  299. }
  300. vulkanContext->physicalDevice = NULL;
  301. for (physicalDeviceIndex = 0; physicalDeviceIndex < physicalDeviceCount; physicalDeviceIndex++) {
  302. uint32_t queueFamiliesCount = 0;
  303. uint32_t queueFamilyIndex;
  304. uint32_t deviceExtensionCount = 0;
  305. SDL_bool hasSwapchainExtension = SDL_FALSE;
  306. uint32_t i;
  307. VkPhysicalDevice physicalDevice = physicalDevices[physicalDeviceIndex];
  308. vkGetPhysicalDeviceProperties(physicalDevice, &vulkanContext->physicalDeviceProperties);
  309. if(VK_VERSION_MAJOR(vulkanContext->physicalDeviceProperties.apiVersion) < 1) {
  310. continue;
  311. }
  312. vkGetPhysicalDeviceFeatures(physicalDevice, &vulkanContext->physicalDeviceFeatures);
  313. vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, NULL);
  314. if (queueFamiliesCount == 0) {
  315. continue;
  316. }
  317. if (queueFamiliesPropertiesAllocatedSize < queueFamiliesCount) {
  318. SDL_free(queueFamiliesProperties);
  319. queueFamiliesPropertiesAllocatedSize = queueFamiliesCount;
  320. queueFamiliesProperties = (VkQueueFamilyProperties *) SDL_malloc(sizeof(VkQueueFamilyProperties) * queueFamiliesPropertiesAllocatedSize);
  321. if (!queueFamiliesProperties) {
  322. SDL_free(physicalDevices);
  323. SDL_free(deviceExtensions);
  324. SDL_OutOfMemory();
  325. quit(2);
  326. }
  327. }
  328. vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, queueFamiliesProperties);
  329. vulkanContext->graphicsQueueFamilyIndex = queueFamiliesCount;
  330. vulkanContext->presentQueueFamilyIndex = queueFamiliesCount;
  331. for (queueFamilyIndex = 0; queueFamilyIndex < queueFamiliesCount; queueFamilyIndex++) {
  332. VkBool32 supported = 0;
  333. if (queueFamiliesProperties[queueFamilyIndex].queueCount == 0) {
  334. continue;
  335. }
  336. if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  337. vulkanContext->graphicsQueueFamilyIndex = queueFamilyIndex;
  338. }
  339. result = vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, queueFamilyIndex, vulkanContext->surface, &supported);
  340. if (result != VK_SUCCESS) {
  341. SDL_free(physicalDevices);
  342. SDL_free(queueFamiliesProperties);
  343. SDL_free(deviceExtensions);
  344. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  345. "vkGetPhysicalDeviceSurfaceSupportKHR(): %s\n",
  346. getVulkanResultString(result));
  347. quit(2);
  348. }
  349. if (supported) {
  350. vulkanContext->presentQueueFamilyIndex = queueFamilyIndex;
  351. if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  352. break; // use this queue because it can present and do graphics
  353. }
  354. }
  355. }
  356. if (vulkanContext->graphicsQueueFamilyIndex == queueFamiliesCount) { // no good queues found
  357. continue;
  358. }
  359. if (vulkanContext->presentQueueFamilyIndex == queueFamiliesCount) { // no good queues found
  360. continue;
  361. }
  362. result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, NULL);
  363. if (result != VK_SUCCESS)
  364. {
  365. SDL_free(physicalDevices);
  366. SDL_free(queueFamiliesProperties);
  367. SDL_free(deviceExtensions);
  368. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  369. "vkEnumerateDeviceExtensionProperties(): %s\n",
  370. getVulkanResultString(result));
  371. quit(2);
  372. }
  373. if (deviceExtensionCount == 0) {
  374. continue;
  375. }
  376. if (deviceExtensionsAllocatedSize < deviceExtensionCount) {
  377. SDL_free(deviceExtensions);
  378. deviceExtensionsAllocatedSize = deviceExtensionCount;
  379. deviceExtensions = SDL_malloc(sizeof(VkExtensionProperties) * deviceExtensionsAllocatedSize);
  380. if (!deviceExtensions) {
  381. SDL_free(physicalDevices);
  382. SDL_free(queueFamiliesProperties);
  383. SDL_OutOfMemory();
  384. quit(2);
  385. }
  386. }
  387. result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, deviceExtensions);
  388. if (result != VK_SUCCESS) {
  389. SDL_free(physicalDevices);
  390. SDL_free(queueFamiliesProperties);
  391. SDL_free(deviceExtensions);
  392. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  393. "vkEnumerateDeviceExtensionProperties(): %s\n",
  394. getVulkanResultString(result));
  395. quit(2);
  396. }
  397. for (i = 0; i < deviceExtensionCount; i++) {
  398. if(SDL_strcmp(deviceExtensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
  399. hasSwapchainExtension = SDL_TRUE;
  400. break;
  401. }
  402. }
  403. if (!hasSwapchainExtension) {
  404. continue;
  405. }
  406. vulkanContext->physicalDevice = physicalDevice;
  407. break;
  408. }
  409. SDL_free(physicalDevices);
  410. SDL_free(queueFamiliesProperties);
  411. SDL_free(deviceExtensions);
  412. if (!vulkanContext->physicalDevice) {
  413. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Vulkan: no viable physical devices found");
  414. quit(2);
  415. }
  416. }
  417. static void createDevice(void)
  418. {
  419. VkDeviceQueueCreateInfo deviceQueueCreateInfo[1] = { {0} };
  420. static const float queuePriority[] = {1.0f};
  421. VkDeviceCreateInfo deviceCreateInfo = {0};
  422. static const char *const deviceExtensionNames[] = {
  423. VK_KHR_SWAPCHAIN_EXTENSION_NAME,
  424. };
  425. VkResult result;
  426. deviceQueueCreateInfo->sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
  427. deviceQueueCreateInfo->queueFamilyIndex = vulkanContext->graphicsQueueFamilyIndex;
  428. deviceQueueCreateInfo->queueCount = 1;
  429. deviceQueueCreateInfo->pQueuePriorities = &queuePriority[0];
  430. deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
  431. deviceCreateInfo.queueCreateInfoCount = 1;
  432. deviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfo;
  433. deviceCreateInfo.pEnabledFeatures = NULL;
  434. deviceCreateInfo.enabledExtensionCount = SDL_arraysize(deviceExtensionNames);
  435. deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
  436. result = vkCreateDevice(vulkanContext->physicalDevice, &deviceCreateInfo, NULL, &vulkanContext->device);
  437. if (result != VK_SUCCESS) {
  438. vulkanContext->device = VK_NULL_HANDLE;
  439. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkCreateDevice(): %s\n", getVulkanResultString(result));
  440. quit(2);
  441. }
  442. }
  443. static void loadDeviceFunctions(void)
  444. {
  445. #define VULKAN_DEVICE_FUNCTION(name) \
  446. name = (PFN_##name)vkGetDeviceProcAddr(vulkanContext->device, #name); \
  447. if (!name) { \
  448. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
  449. "vkGetDeviceProcAddr(device, \"" #name "\") failed\n"); \
  450. quit(2); \
  451. }
  452. #define VULKAN_GLOBAL_FUNCTION(name)
  453. #define VULKAN_INSTANCE_FUNCTION(name)
  454. VULKAN_FUNCTIONS()
  455. #undef VULKAN_DEVICE_FUNCTION
  456. #undef VULKAN_GLOBAL_FUNCTION
  457. #undef VULKAN_INSTANCE_FUNCTION
  458. }
  459. #undef VULKAN_FUNCTIONS
  460. static void getQueues(void)
  461. {
  462. vkGetDeviceQueue(vulkanContext->device,
  463. vulkanContext->graphicsQueueFamilyIndex,
  464. 0,
  465. &vulkanContext->graphicsQueue);
  466. if (vulkanContext->graphicsQueueFamilyIndex != vulkanContext->presentQueueFamilyIndex) {
  467. vkGetDeviceQueue(vulkanContext->device,
  468. vulkanContext->presentQueueFamilyIndex,
  469. 0,
  470. &vulkanContext->presentQueue);
  471. } else {
  472. vulkanContext->presentQueue = vulkanContext->graphicsQueue;
  473. }
  474. }
  475. static void createSemaphore(VkSemaphore *semaphore)
  476. {
  477. VkResult result;
  478. VkSemaphoreCreateInfo createInfo = {0};
  479. createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
  480. result = vkCreateSemaphore(vulkanContext->device, &createInfo, NULL, semaphore);
  481. if (result != VK_SUCCESS) {
  482. *semaphore = VK_NULL_HANDLE;
  483. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  484. "vkCreateSemaphore(): %s\n",
  485. getVulkanResultString(result));
  486. quit(2);
  487. }
  488. }
  489. static void createSemaphores(void)
  490. {
  491. createSemaphore(&vulkanContext->imageAvailableSemaphore);
  492. createSemaphore(&vulkanContext->renderingFinishedSemaphore);
  493. }
  494. static void getSurfaceCaps(void)
  495. {
  496. VkResult result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanContext->physicalDevice, vulkanContext->surface, &vulkanContext->surfaceCapabilities);
  497. if (result != VK_SUCCESS) {
  498. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  499. "vkGetPhysicalDeviceSurfaceCapabilitiesKHR(): %s\n",
  500. getVulkanResultString(result));
  501. quit(2);
  502. }
  503. // check surface usage
  504. if (!(vulkanContext->surfaceCapabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
  505. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  506. "Vulkan surface doesn't support VK_IMAGE_USAGE_TRANSFER_DST_BIT\n");
  507. quit(2);
  508. }
  509. }
  510. static void getSurfaceFormats(void)
  511. {
  512. VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(vulkanContext->physicalDevice,
  513. vulkanContext->surface,
  514. &vulkanContext->surfaceFormatsCount,
  515. NULL);
  516. if (result != VK_SUCCESS) {
  517. vulkanContext->surfaceFormatsCount = 0;
  518. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  519. "vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n",
  520. getVulkanResultString(result));
  521. quit(2);
  522. }
  523. if (vulkanContext->surfaceFormatsCount > vulkanContext->surfaceFormatsAllocatedCount) {
  524. vulkanContext->surfaceFormatsAllocatedCount = vulkanContext->surfaceFormatsCount;
  525. SDL_free(vulkanContext->surfaceFormats);
  526. vulkanContext->surfaceFormats = (VkSurfaceFormatKHR *) SDL_malloc(sizeof(VkSurfaceFormatKHR) * vulkanContext->surfaceFormatsAllocatedCount);
  527. if (!vulkanContext->surfaceFormats) {
  528. vulkanContext->surfaceFormatsCount = 0;
  529. SDL_OutOfMemory();
  530. quit(2);
  531. }
  532. }
  533. result = vkGetPhysicalDeviceSurfaceFormatsKHR(vulkanContext->physicalDevice,
  534. vulkanContext->surface,
  535. &vulkanContext->surfaceFormatsCount,
  536. vulkanContext->surfaceFormats);
  537. if (result != VK_SUCCESS) {
  538. vulkanContext->surfaceFormatsCount = 0;
  539. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  540. "vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n",
  541. getVulkanResultString(result));
  542. quit(2);
  543. }
  544. }
  545. static void getSwapchainImages(void)
  546. {
  547. VkResult result;
  548. SDL_free(vulkanContext->swapchainImages);
  549. vulkanContext->swapchainImages = NULL;
  550. result = vkGetSwapchainImagesKHR(vulkanContext->device, vulkanContext->swapchain, &vulkanContext->swapchainImageCount, NULL);
  551. if (result != VK_SUCCESS) {
  552. vulkanContext->swapchainImageCount = 0;
  553. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  554. "vkGetSwapchainImagesKHR(): %s\n",
  555. getVulkanResultString(result));
  556. quit(2);
  557. }
  558. vulkanContext->swapchainImages = SDL_malloc(sizeof(VkImage) * vulkanContext->swapchainImageCount);
  559. if (!vulkanContext->swapchainImages) {
  560. SDL_OutOfMemory();
  561. quit(2);
  562. }
  563. result = vkGetSwapchainImagesKHR(vulkanContext->device,
  564. vulkanContext->swapchain,
  565. &vulkanContext->swapchainImageCount,
  566. vulkanContext->swapchainImages);
  567. if (result != VK_SUCCESS) {
  568. SDL_free(vulkanContext->swapchainImages);
  569. vulkanContext->swapchainImages = NULL;
  570. vulkanContext->swapchainImageCount = 0;
  571. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  572. "vkGetSwapchainImagesKHR(): %s\n",
  573. getVulkanResultString(result));
  574. quit(2);
  575. }
  576. }
  577. static SDL_bool createSwapchain(void)
  578. {
  579. uint32_t i;
  580. int w, h;
  581. VkSwapchainCreateInfoKHR createInfo = {0};
  582. VkResult result;
  583. // pick an image count
  584. vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.minImageCount + 1;
  585. if ( (vulkanContext->swapchainDesiredImageCount > vulkanContext->surfaceCapabilities.maxImageCount) &&
  586. (vulkanContext->surfaceCapabilities.maxImageCount > 0) ) {
  587. vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.maxImageCount;
  588. }
  589. // pick a format
  590. if ( (vulkanContext->surfaceFormatsCount == 1) &&
  591. (vulkanContext->surfaceFormats[0].format == VK_FORMAT_UNDEFINED) ) {
  592. // aren't any preferred formats, so we pick
  593. vulkanContext->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
  594. vulkanContext->surfaceFormat.format = VK_FORMAT_R8G8B8A8_UNORM;
  595. } else {
  596. vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[0];
  597. for (i = 0; i < vulkanContext->surfaceFormatsCount; i++) {
  598. if(vulkanContext->surfaceFormats[i].format == VK_FORMAT_R8G8B8A8_UNORM) {
  599. vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[i];
  600. break;
  601. }
  602. }
  603. }
  604. // get size
  605. SDL_Vulkan_GetDrawableSize(vulkanContext->window, &w, &h);
  606. // Clamp the size to the allowable image extent.
  607. // SDL_Vulkan_GetDrawableSize()'s result it not always in this range (bug #3287)
  608. vulkanContext->swapchainSize.width = SDL_clamp((uint32_t) w,
  609. vulkanContext->surfaceCapabilities.minImageExtent.width,
  610. vulkanContext->surfaceCapabilities.maxImageExtent.width);
  611. vulkanContext->swapchainSize.height = SDL_clamp((uint32_t) h,
  612. vulkanContext->surfaceCapabilities.minImageExtent.height,
  613. vulkanContext->surfaceCapabilities.maxImageExtent.height);
  614. if (w == 0 || h == 0) {
  615. return SDL_FALSE;
  616. }
  617. getSurfaceCaps();
  618. createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
  619. createInfo.surface = vulkanContext->surface;
  620. createInfo.minImageCount = vulkanContext->swapchainDesiredImageCount;
  621. createInfo.imageFormat = vulkanContext->surfaceFormat.format;
  622. createInfo.imageColorSpace = vulkanContext->surfaceFormat.colorSpace;
  623. createInfo.imageExtent = vulkanContext->swapchainSize;
  624. createInfo.imageArrayLayers = 1;
  625. createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
  626. createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
  627. createInfo.preTransform = vulkanContext->surfaceCapabilities.currentTransform;
  628. createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
  629. createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
  630. createInfo.clipped = VK_TRUE;
  631. createInfo.oldSwapchain = vulkanContext->swapchain;
  632. result = vkCreateSwapchainKHR(vulkanContext->device, &createInfo, NULL, &vulkanContext->swapchain);
  633. if (createInfo.oldSwapchain) {
  634. vkDestroySwapchainKHR(vulkanContext->device, createInfo.oldSwapchain, NULL);
  635. }
  636. if(result != VK_SUCCESS) {
  637. vulkanContext->swapchain = VK_NULL_HANDLE;
  638. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  639. "vkCreateSwapchainKHR(): %s\n",
  640. getVulkanResultString(result));
  641. quit(2);
  642. }
  643. getSwapchainImages();
  644. return SDL_TRUE;
  645. }
  646. static void destroySwapchain(void)
  647. {
  648. if (vulkanContext->swapchain) {
  649. vkDestroySwapchainKHR(vulkanContext->device, vulkanContext->swapchain, NULL);
  650. vulkanContext->swapchain = VK_NULL_HANDLE;
  651. }
  652. SDL_free(vulkanContext->swapchainImages);
  653. vulkanContext->swapchainImages = NULL;
  654. }
  655. static void destroyCommandBuffers(void)
  656. {
  657. if (vulkanContext->commandBuffers) {
  658. vkFreeCommandBuffers(vulkanContext->device,
  659. vulkanContext->commandPool,
  660. vulkanContext->swapchainImageCount,
  661. vulkanContext->commandBuffers);
  662. SDL_free(vulkanContext->commandBuffers);
  663. vulkanContext->commandBuffers = NULL;
  664. }
  665. }
  666. static void destroyCommandPool(void)
  667. {
  668. if (vulkanContext->commandPool) {
  669. vkDestroyCommandPool(vulkanContext->device, vulkanContext->commandPool, NULL);
  670. }
  671. vulkanContext->commandPool = VK_NULL_HANDLE;
  672. }
  673. static void createCommandPool(void)
  674. {
  675. VkResult result;
  676. VkCommandPoolCreateInfo createInfo = {0};
  677. createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
  678. createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
  679. createInfo.queueFamilyIndex = vulkanContext->graphicsQueueFamilyIndex;
  680. result = vkCreateCommandPool(vulkanContext->device, &createInfo, NULL, &vulkanContext->commandPool);
  681. if (result != VK_SUCCESS) {
  682. vulkanContext->commandPool = VK_NULL_HANDLE;
  683. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  684. "vkCreateCommandPool(): %s\n",
  685. getVulkanResultString(result));
  686. quit(2);
  687. }
  688. }
  689. static void createCommandBuffers(void)
  690. {
  691. VkResult result;
  692. VkCommandBufferAllocateInfo allocateInfo = {0};
  693. allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
  694. allocateInfo.commandPool = vulkanContext->commandPool;
  695. allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
  696. allocateInfo.commandBufferCount = vulkanContext->swapchainImageCount;
  697. vulkanContext->commandBuffers = (VkCommandBuffer *) SDL_malloc(sizeof(VkCommandBuffer) * vulkanContext->swapchainImageCount);
  698. result = vkAllocateCommandBuffers(vulkanContext->device, &allocateInfo, vulkanContext->commandBuffers);
  699. if(result != VK_SUCCESS) {
  700. SDL_free(vulkanContext->commandBuffers);
  701. vulkanContext->commandBuffers = NULL;
  702. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  703. "vkAllocateCommandBuffers(): %s\n",
  704. getVulkanResultString(result));
  705. quit(2);
  706. }
  707. }
  708. static void createFences(void)
  709. {
  710. uint32_t i;
  711. vulkanContext->fences = SDL_malloc(sizeof(VkFence) * vulkanContext->swapchainImageCount);
  712. if (!vulkanContext->fences) {
  713. SDL_OutOfMemory();
  714. quit(2);
  715. }
  716. for (i = 0; i < vulkanContext->swapchainImageCount; i++) {
  717. VkResult result;
  718. VkFenceCreateInfo createInfo = {0};
  719. createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
  720. createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
  721. result = vkCreateFence(vulkanContext->device, &createInfo, NULL, &vulkanContext->fences[i]);
  722. if(result != VK_SUCCESS) {
  723. for(; i > 0; i--) {
  724. vkDestroyFence(vulkanContext->device, vulkanContext->fences[i - 1], NULL);
  725. }
  726. SDL_free(vulkanContext->fences);
  727. vulkanContext->fences = NULL;
  728. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  729. "vkCreateFence(): %s\n",
  730. getVulkanResultString(result));
  731. quit(2);
  732. }
  733. }
  734. }
  735. static void destroyFences(void)
  736. {
  737. uint32_t i;
  738. if (!vulkanContext->fences) {
  739. return;
  740. }
  741. for (i = 0; i < vulkanContext->swapchainImageCount; i++) {
  742. vkDestroyFence(vulkanContext->device, vulkanContext->fences[i], NULL);
  743. }
  744. SDL_free(vulkanContext->fences);
  745. vulkanContext->fences = NULL;
  746. }
  747. static void recordPipelineImageBarrier(VkCommandBuffer commandBuffer,
  748. VkAccessFlags sourceAccessMask,
  749. VkAccessFlags destAccessMask,
  750. VkImageLayout sourceLayout,
  751. VkImageLayout destLayout,
  752. VkImage image)
  753. {
  754. VkImageMemoryBarrier barrier = {0};
  755. barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  756. barrier.srcAccessMask = sourceAccessMask;
  757. barrier.dstAccessMask = destAccessMask;
  758. barrier.oldLayout = sourceLayout;
  759. barrier.newLayout = destLayout;
  760. barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  761. barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  762. barrier.image = image;
  763. barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  764. barrier.subresourceRange.baseMipLevel = 0;
  765. barrier.subresourceRange.levelCount = 1;
  766. barrier.subresourceRange.baseArrayLayer = 0;
  767. barrier.subresourceRange.layerCount = 1;
  768. vkCmdPipelineBarrier(commandBuffer,
  769. VK_PIPELINE_STAGE_TRANSFER_BIT,
  770. VK_PIPELINE_STAGE_TRANSFER_BIT,
  771. 0,
  772. 0,
  773. NULL,
  774. 0,
  775. NULL,
  776. 1,
  777. &barrier);
  778. }
  779. static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *clearColor)
  780. {
  781. VkCommandBuffer commandBuffer = vulkanContext->commandBuffers[frameIndex];
  782. VkImage image = vulkanContext->swapchainImages[frameIndex];
  783. VkCommandBufferBeginInfo beginInfo = {0};
  784. VkImageSubresourceRange clearRange = {0};
  785. VkResult result = vkResetCommandBuffer(commandBuffer, 0);
  786. if(result != VK_SUCCESS) {
  787. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  788. "vkResetCommandBuffer(): %s\n",
  789. getVulkanResultString(result));
  790. quit(2);
  791. }
  792. beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
  793. beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
  794. result = vkBeginCommandBuffer(commandBuffer, &beginInfo);
  795. if (result != VK_SUCCESS) {
  796. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  797. "vkBeginCommandBuffer(): %s\n",
  798. getVulkanResultString(result));
  799. quit(2);
  800. }
  801. recordPipelineImageBarrier(commandBuffer,
  802. 0,
  803. VK_ACCESS_TRANSFER_WRITE_BIT,
  804. VK_IMAGE_LAYOUT_UNDEFINED,
  805. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  806. image);
  807. clearRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  808. clearRange.baseMipLevel = 0;
  809. clearRange.levelCount = 1;
  810. clearRange.baseArrayLayer = 0;
  811. clearRange.layerCount = 1;
  812. vkCmdClearColorImage(commandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, clearColor, 1, &clearRange);
  813. recordPipelineImageBarrier(commandBuffer,
  814. VK_ACCESS_TRANSFER_WRITE_BIT,
  815. VK_ACCESS_MEMORY_READ_BIT,
  816. VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
  817. VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
  818. image);
  819. result = vkEndCommandBuffer(commandBuffer);
  820. if (result != VK_SUCCESS) {
  821. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  822. "vkEndCommandBuffer(): %s\n",
  823. getVulkanResultString(result));
  824. quit(2);
  825. }
  826. }
  827. static void destroySwapchainAndSwapchainSpecificStuff(SDL_bool doDestroySwapchain)
  828. {
  829. vkDeviceWaitIdle(vulkanContext->device);
  830. destroyFences();
  831. destroyCommandBuffers();
  832. destroyCommandPool();
  833. if (doDestroySwapchain) {
  834. destroySwapchain();
  835. }
  836. }
  837. static SDL_bool createNewSwapchainAndSwapchainSpecificStuff(void)
  838. {
  839. destroySwapchainAndSwapchainSpecificStuff(SDL_FALSE);
  840. getSurfaceCaps();
  841. getSurfaceFormats();
  842. if(!createSwapchain()) {
  843. return SDL_FALSE;
  844. }
  845. createCommandPool();
  846. createCommandBuffers();
  847. createFences();
  848. return SDL_TRUE;
  849. }
  850. static void initVulkan(void)
  851. {
  852. int i;
  853. SDL_Vulkan_LoadLibrary(NULL);
  854. vulkanContexts = (VulkanContext *) SDL_calloc(state->num_windows, sizeof (VulkanContext));
  855. if (!vulkanContexts) {
  856. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  857. quit(2);
  858. }
  859. for (i = 0; i < state->num_windows; ++i) {
  860. vulkanContext = &vulkanContexts[i];
  861. vulkanContext->window = state->windows[i];
  862. loadGlobalFunctions();
  863. createInstance();
  864. loadInstanceFunctions();
  865. createSurface();
  866. findPhysicalDevice();
  867. createDevice();
  868. loadDeviceFunctions();
  869. getQueues();
  870. createSemaphores();
  871. createNewSwapchainAndSwapchainSpecificStuff();
  872. }
  873. }
  874. static void shutdownVulkan(SDL_bool doDestroySwapchain)
  875. {
  876. if (vulkanContexts) {
  877. int i;
  878. for (i = 0; i < state->num_windows; ++i) {
  879. vulkanContext = &vulkanContexts[i];
  880. if (vulkanContext->device && vkDeviceWaitIdle) {
  881. vkDeviceWaitIdle(vulkanContext->device);
  882. }
  883. destroySwapchainAndSwapchainSpecificStuff(doDestroySwapchain);
  884. if (vulkanContext->imageAvailableSemaphore && vkDestroySemaphore) {
  885. vkDestroySemaphore(vulkanContext->device, vulkanContext->imageAvailableSemaphore, NULL);
  886. }
  887. if (vulkanContext->renderingFinishedSemaphore && vkDestroySemaphore) {
  888. vkDestroySemaphore(vulkanContext->device, vulkanContext->renderingFinishedSemaphore, NULL);
  889. }
  890. if (vulkanContext->device && vkDestroyDevice) {
  891. vkDestroyDevice(vulkanContext->device, NULL);
  892. }
  893. if (vulkanContext->surface && vkDestroySurfaceKHR) {
  894. vkDestroySurfaceKHR(vulkanContext->instance, vulkanContext->surface, NULL);
  895. }
  896. if (vulkanContext->instance && vkDestroyInstance) {
  897. vkDestroyInstance(vulkanContext->instance, NULL);
  898. }
  899. SDL_free(vulkanContext->surfaceFormats);
  900. }
  901. SDL_free(vulkanContexts);
  902. vulkanContexts = NULL;
  903. }
  904. SDL_Vulkan_UnloadLibrary();
  905. }
  906. static SDL_bool render(void)
  907. {
  908. uint32_t frameIndex;
  909. VkResult result;
  910. double currentTime;
  911. VkClearColorValue clearColor = { {0} };
  912. VkPipelineStageFlags waitDestStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT;
  913. VkSubmitInfo submitInfo = {0};
  914. VkPresentInfoKHR presentInfo = {0};
  915. int w, h;
  916. if (!vulkanContext->swapchain) {
  917. SDL_bool retval = createNewSwapchainAndSwapchainSpecificStuff();
  918. if(!retval)
  919. SDL_Delay(100);
  920. return retval;
  921. }
  922. result = vkAcquireNextImageKHR(vulkanContext->device,
  923. vulkanContext->swapchain,
  924. UINT64_MAX,
  925. vulkanContext->imageAvailableSemaphore,
  926. VK_NULL_HANDLE,
  927. &frameIndex);
  928. if (result == VK_ERROR_OUT_OF_DATE_KHR) {
  929. return createNewSwapchainAndSwapchainSpecificStuff();
  930. }
  931. if ((result != VK_SUBOPTIMAL_KHR) && (result != VK_SUCCESS)) {
  932. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  933. "vkAcquireNextImageKHR(): %s\n",
  934. getVulkanResultString(result));
  935. quit(2);
  936. }
  937. result = vkWaitForFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex], VK_FALSE, UINT64_MAX);
  938. if (result != VK_SUCCESS) {
  939. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkWaitForFences(): %s\n", getVulkanResultString(result));
  940. quit(2);
  941. }
  942. result = vkResetFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex]);
  943. if (result != VK_SUCCESS) {
  944. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkResetFences(): %s\n", getVulkanResultString(result));
  945. quit(2);
  946. }
  947. currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
  948. clearColor.float32[0] = (float)(0.5 + 0.5 * SDL_sin(currentTime));
  949. clearColor.float32[1] = (float)(0.5 + 0.5 * SDL_sin(currentTime + M_PI * 2 / 3));
  950. clearColor.float32[2] = (float)(0.5 + 0.5 * SDL_sin(currentTime + M_PI * 4 / 3));
  951. clearColor.float32[3] = 1;
  952. rerecordCommandBuffer(frameIndex, &clearColor);
  953. submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
  954. submitInfo.waitSemaphoreCount = 1;
  955. submitInfo.pWaitSemaphores = &vulkanContext->imageAvailableSemaphore;
  956. submitInfo.pWaitDstStageMask = &waitDestStageMask;
  957. submitInfo.commandBufferCount = 1;
  958. submitInfo.pCommandBuffers = &vulkanContext->commandBuffers[frameIndex];
  959. submitInfo.signalSemaphoreCount = 1;
  960. submitInfo.pSignalSemaphores = &vulkanContext->renderingFinishedSemaphore;
  961. result = vkQueueSubmit(vulkanContext->graphicsQueue, 1, &submitInfo, vulkanContext->fences[frameIndex]);
  962. if (result != VK_SUCCESS) {
  963. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkQueueSubmit(): %s\n", getVulkanResultString(result));
  964. quit(2);
  965. }
  966. presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
  967. presentInfo.waitSemaphoreCount = 1;
  968. presentInfo.pWaitSemaphores = &vulkanContext->renderingFinishedSemaphore;
  969. presentInfo.swapchainCount = 1;
  970. presentInfo.pSwapchains = &vulkanContext->swapchain;
  971. presentInfo.pImageIndices = &frameIndex;
  972. result = vkQueuePresentKHR(vulkanContext->presentQueue, &presentInfo);
  973. if ((result == VK_ERROR_OUT_OF_DATE_KHR) || (result == VK_SUBOPTIMAL_KHR)) {
  974. return createNewSwapchainAndSwapchainSpecificStuff();
  975. }
  976. if (result != VK_SUCCESS) {
  977. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
  978. "vkQueuePresentKHR(): %s\n",
  979. getVulkanResultString(result));
  980. quit(2);
  981. }
  982. SDL_Vulkan_GetDrawableSize(vulkanContext->window, &w, &h);
  983. if(w != (int)vulkanContext->swapchainSize.width || h != (int)vulkanContext->swapchainSize.height) {
  984. return createNewSwapchainAndSwapchainSpecificStuff();
  985. }
  986. return SDL_TRUE;
  987. }
  988. int main(int argc, char **argv)
  989. {
  990. int done;
  991. SDL_DisplayMode mode;
  992. SDL_Event event;
  993. Uint32 then, now, frames;
  994. int dw, dh;
  995. /* Enable standard application logging */
  996. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  997. /* Initialize test framework */
  998. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  999. if(!state) {
  1000. return 1;
  1001. }
  1002. /* Set Vulkan parameters */
  1003. state->window_flags |= SDL_WINDOW_VULKAN;
  1004. state->skip_renderer = 1;
  1005. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  1006. SDLTest_CommonQuit(state);
  1007. return 1;
  1008. }
  1009. SDL_GetCurrentDisplayMode(0, &mode);
  1010. SDL_Log("Screen BPP : %d\n", SDL_BITSPERPIXEL(mode.format));
  1011. SDL_GetWindowSize(state->windows[0], &dw, &dh);
  1012. SDL_Log("Window Size : %d,%d\n", dw, dh);
  1013. SDL_Vulkan_GetDrawableSize(state->windows[0], &dw, &dh);
  1014. SDL_Log("Draw Size : %d,%d\n", dw, dh);
  1015. SDL_Log("\n");
  1016. initVulkan();
  1017. /* Main render loop */
  1018. frames = 0;
  1019. then = SDL_GetTicks();
  1020. done = 0;
  1021. while (!done) {
  1022. /* Check for events */
  1023. frames++;
  1024. while(SDL_PollEvent(&event)) {
  1025. /* Need to destroy the swapchain before the window created
  1026. * by SDL.
  1027. */
  1028. if (event.type == SDL_WINDOWEVENT_CLOSE) {
  1029. destroySwapchainAndSwapchainSpecificStuff(SDL_TRUE);
  1030. }
  1031. SDLTest_CommonEvent(state, &event, &done);
  1032. }
  1033. if (!done) {
  1034. int i;
  1035. for (i = 0; i < state->num_windows; ++i) {
  1036. if (state->windows[i]) {
  1037. vulkanContext = &vulkanContexts[i];
  1038. render();
  1039. }
  1040. }
  1041. }
  1042. }
  1043. /* Print out some timing information */
  1044. now = SDL_GetTicks();
  1045. if (now > then) {
  1046. SDL_Log("%2.2f frames per second\n", ((double)frames * 1000) / (now - then));
  1047. }
  1048. shutdownVulkan(SDL_TRUE);
  1049. SDLTest_CommonQuit(state);
  1050. return 0;
  1051. }
  1052. #endif