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