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