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