testvulkan: reformatted to more-closely match SDL coding conventions.

This commit is contained in:
Ryan C. Gordon 2021-08-24 12:41:41 -04:00
parent b073d2753d
commit 79c7a1713f
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 207 additions and 285 deletions

View File

@ -112,64 +112,37 @@ static const char *getVulkanResultString(VkResult result)
{ {
switch((int) result) switch((int) result)
{ {
case VK_SUCCESS: #define RESULT_CASE(x) case x: return #x
return "VK_SUCCESS"; RESULT_CASE(VK_SUCCESS);
case VK_NOT_READY: RESULT_CASE(VK_NOT_READY);
return "VK_NOT_READY"; RESULT_CASE(VK_TIMEOUT);
case VK_TIMEOUT: RESULT_CASE(VK_EVENT_SET);
return "VK_TIMEOUT"; RESULT_CASE(VK_EVENT_RESET);
case VK_EVENT_SET: RESULT_CASE(VK_INCOMPLETE);
return "VK_EVENT_SET"; RESULT_CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
case VK_EVENT_RESET: RESULT_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
return "VK_EVENT_RESET"; RESULT_CASE(VK_ERROR_INITIALIZATION_FAILED);
case VK_INCOMPLETE: RESULT_CASE(VK_ERROR_DEVICE_LOST);
return "VK_INCOMPLETE"; RESULT_CASE(VK_ERROR_MEMORY_MAP_FAILED);
case VK_ERROR_OUT_OF_HOST_MEMORY: RESULT_CASE(VK_ERROR_LAYER_NOT_PRESENT);
return "VK_ERROR_OUT_OF_HOST_MEMORY"; RESULT_CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
case VK_ERROR_OUT_OF_DEVICE_MEMORY: RESULT_CASE(VK_ERROR_FEATURE_NOT_PRESENT);
return "VK_ERROR_OUT_OF_DEVICE_MEMORY"; RESULT_CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
case VK_ERROR_INITIALIZATION_FAILED: RESULT_CASE(VK_ERROR_TOO_MANY_OBJECTS);
return "VK_ERROR_INITIALIZATION_FAILED"; RESULT_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
case VK_ERROR_DEVICE_LOST: RESULT_CASE(VK_ERROR_FRAGMENTED_POOL);
return "VK_ERROR_DEVICE_LOST"; RESULT_CASE(VK_ERROR_SURFACE_LOST_KHR);
case VK_ERROR_MEMORY_MAP_FAILED: RESULT_CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
return "VK_ERROR_MEMORY_MAP_FAILED"; RESULT_CASE(VK_SUBOPTIMAL_KHR);
case VK_ERROR_LAYER_NOT_PRESENT: RESULT_CASE(VK_ERROR_OUT_OF_DATE_KHR);
return "VK_ERROR_LAYER_NOT_PRESENT"; RESULT_CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
case VK_ERROR_EXTENSION_NOT_PRESENT: RESULT_CASE(VK_ERROR_VALIDATION_FAILED_EXT);
return "VK_ERROR_EXTENSION_NOT_PRESENT"; RESULT_CASE(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
case VK_ERROR_FEATURE_NOT_PRESENT: RESULT_CASE(VK_ERROR_INVALID_SHADER_NV);
return "VK_ERROR_FEATURE_NOT_PRESENT"; #undef RESULT_CASE
case VK_ERROR_INCOMPATIBLE_DRIVER: default: break;
return "VK_ERROR_INCOMPATIBLE_DRIVER";
case VK_ERROR_TOO_MANY_OBJECTS:
return "VK_ERROR_TOO_MANY_OBJECTS";
case VK_ERROR_FORMAT_NOT_SUPPORTED:
return "VK_ERROR_FORMAT_NOT_SUPPORTED";
case VK_ERROR_FRAGMENTED_POOL:
return "VK_ERROR_FRAGMENTED_POOL";
case VK_ERROR_SURFACE_LOST_KHR:
return "VK_ERROR_SURFACE_LOST_KHR";
case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR:
return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR";
case VK_SUBOPTIMAL_KHR:
return "VK_SUBOPTIMAL_KHR";
case VK_ERROR_OUT_OF_DATE_KHR:
return "VK_ERROR_OUT_OF_DATE_KHR";
case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR:
return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR";
case VK_ERROR_VALIDATION_FAILED_EXT:
return "VK_ERROR_VALIDATION_FAILED_EXT";
case VK_ERROR_OUT_OF_POOL_MEMORY_KHR:
return "VK_ERROR_OUT_OF_POOL_MEMORY_KHR";
case VK_ERROR_INVALID_SHADER_NV:
return "VK_ERROR_INVALID_SHADER_NV";
default:
break;
} }
if(result < 0) return (result < 0) ? "VK_ERROR_<Unknown>" : "VK_<Unknown>";
return "VK_ERROR_<Unknown>";
return "VK_<Unknown>";
} }
typedef struct VulkanContext typedef struct VulkanContext
@ -219,8 +192,7 @@ static void quit(int rc)
static void loadGlobalFunctions(void) static void loadGlobalFunctions(void)
{ {
vkGetInstanceProcAddr = SDL_Vulkan_GetVkGetInstanceProcAddr(); vkGetInstanceProcAddr = SDL_Vulkan_GetVkGetInstanceProcAddr();
if(!vkGetInstanceProcAddr) if (!vkGetInstanceProcAddr) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"SDL_Vulkan_GetVkGetInstanceProcAddr(): %s\n", "SDL_Vulkan_GetVkGetInstanceProcAddr(): %s\n",
SDL_GetError()); SDL_GetError());
@ -230,8 +202,7 @@ static void loadGlobalFunctions(void)
#define VULKAN_DEVICE_FUNCTION(name) #define VULKAN_DEVICE_FUNCTION(name)
#define VULKAN_GLOBAL_FUNCTION(name) \ #define VULKAN_GLOBAL_FUNCTION(name) \
name = (PFN_##name)vkGetInstanceProcAddr(VK_NULL_HANDLE, #name); \ name = (PFN_##name)vkGetInstanceProcAddr(VK_NULL_HANDLE, #name); \
if(!name) \ if (!name) { \
{ \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"vkGetInstanceProcAddr(VK_NULL_HANDLE, \"" #name "\") failed\n"); \ "vkGetInstanceProcAddr(VK_NULL_HANDLE, \"" #name "\") failed\n"); \
quit(2); \ quit(2); \
@ -251,26 +222,22 @@ static void createInstance(void)
unsigned extensionCount = 0; unsigned extensionCount = 0;
VkResult result; VkResult result;
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.apiVersion = VK_API_VERSION_1_0; appInfo.apiVersion = VK_API_VERSION_1_0;
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceCreateInfo.pApplicationInfo = &appInfo; instanceCreateInfo.pApplicationInfo = &appInfo;
if(!SDL_Vulkan_GetInstanceExtensions(NULL, &extensionCount, NULL)) if (!SDL_Vulkan_GetInstanceExtensions(NULL, &extensionCount, NULL)) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"SDL_Vulkan_GetInstanceExtensions(): %s\n", "SDL_Vulkan_GetInstanceExtensions(): %s\n",
SDL_GetError()); SDL_GetError());
quit(2); quit(2);
} }
extensions = SDL_malloc(sizeof(const char *) * extensionCount); extensions = (const char **) SDL_malloc(sizeof(const char *) * extensionCount);
if(!extensions) if (!extensions) {
{
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
} }
if(!SDL_Vulkan_GetInstanceExtensions(NULL, &extensionCount, extensions)) if (!SDL_Vulkan_GetInstanceExtensions(NULL, &extensionCount, extensions)) {
{
SDL_free((void*)extensions); SDL_free((void*)extensions);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"SDL_Vulkan_GetInstanceExtensions(): %s\n", "SDL_Vulkan_GetInstanceExtensions(): %s\n",
@ -281,8 +248,7 @@ static void createInstance(void)
instanceCreateInfo.ppEnabledExtensionNames = extensions; instanceCreateInfo.ppEnabledExtensionNames = extensions;
result = vkCreateInstance(&instanceCreateInfo, NULL, &vulkanContext->instance); result = vkCreateInstance(&instanceCreateInfo, NULL, &vulkanContext->instance);
SDL_free((void*)extensions); SDL_free((void*)extensions);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
vulkanContext->instance = VK_NULL_HANDLE; vulkanContext->instance = VK_NULL_HANDLE;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkCreateInstance(): %s\n", "vkCreateInstance(): %s\n",
@ -297,8 +263,7 @@ static void loadInstanceFunctions(void)
#define VULKAN_GLOBAL_FUNCTION(name) #define VULKAN_GLOBAL_FUNCTION(name)
#define VULKAN_INSTANCE_FUNCTION(name) \ #define VULKAN_INSTANCE_FUNCTION(name) \
name = (PFN_##name)vkGetInstanceProcAddr(vulkanContext->instance, #name); \ name = (PFN_##name)vkGetInstanceProcAddr(vulkanContext->instance, #name); \
if(!name) \ if (!name) { \
{ \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"vkGetInstanceProcAddr(instance, \"" #name "\") failed\n"); \ "vkGetInstanceProcAddr(instance, \"" #name "\") failed\n"); \
quit(2); \ quit(2); \
@ -313,11 +278,9 @@ static void createSurface(void)
{ {
if (!SDL_Vulkan_CreateSurface(vulkanContext->window, if (!SDL_Vulkan_CreateSurface(vulkanContext->window,
vulkanContext->instance, vulkanContext->instance,
&vulkanContext->surface)) &vulkanContext->surface)) {
{
vulkanContext->surface = VK_NULL_HANDLE; vulkanContext->surface = VK_NULL_HANDLE;
SDL_LogError( SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Vulkan_CreateSurface(): %s\n", SDL_GetError());
SDL_LOG_CATEGORY_APPLICATION, "SDL_Vulkan_CreateSurface(): %s\n", SDL_GetError());
quit(2); quit(2);
} }
} }
@ -331,32 +294,27 @@ static void findPhysicalDevice(void)
VkExtensionProperties *deviceExtensions = NULL; VkExtensionProperties *deviceExtensions = NULL;
uint32_t deviceExtensionsAllocatedSize = 0; uint32_t deviceExtensionsAllocatedSize = 0;
uint32_t physicalDeviceIndex; uint32_t physicalDeviceIndex;
VkResult result;
VkResult result = result = vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, NULL);
vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, NULL); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkEnumeratePhysicalDevices(): %s\n", "vkEnumeratePhysicalDevices(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
if(physicalDeviceCount == 0) if (physicalDeviceCount == 0) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkEnumeratePhysicalDevices(): no physical devices\n"); "vkEnumeratePhysicalDevices(): no physical devices\n");
quit(2); quit(2);
} }
physicalDevices = SDL_malloc(sizeof(VkPhysicalDevice) * physicalDeviceCount); physicalDevices = (VkPhysicalDevice *) SDL_malloc(sizeof(VkPhysicalDevice) * physicalDeviceCount);
if(!physicalDevices) if (!physicalDevices) {
{
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
} }
result = result = vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, physicalDevices);
vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, physicalDevices); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
SDL_free(physicalDevices); SDL_free(physicalDevices);
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkEnumeratePhysicalDevices(): %s\n", "vkEnumeratePhysicalDevices(): %s\n",
@ -364,55 +322,50 @@ static void findPhysicalDevice(void)
quit(2); quit(2);
} }
vulkanContext->physicalDevice = NULL; vulkanContext->physicalDevice = NULL;
for(physicalDeviceIndex = 0; physicalDeviceIndex < physicalDeviceCount; for (physicalDeviceIndex = 0; physicalDeviceIndex < physicalDeviceCount; physicalDeviceIndex++) {
physicalDeviceIndex++)
{
uint32_t queueFamiliesCount = 0; uint32_t queueFamiliesCount = 0;
uint32_t queueFamilyIndex; uint32_t queueFamilyIndex;
uint32_t deviceExtensionCount = 0; uint32_t deviceExtensionCount = 0;
SDL_bool hasSwapchainExtension = SDL_FALSE; SDL_bool hasSwapchainExtension = SDL_FALSE;
uint32_t i; uint32_t i;
VkPhysicalDevice physicalDevice = physicalDevices[physicalDeviceIndex]; VkPhysicalDevice physicalDevice = physicalDevices[physicalDeviceIndex];
vkGetPhysicalDeviceProperties(physicalDevice, &vulkanContext->physicalDeviceProperties); vkGetPhysicalDeviceProperties(physicalDevice, &vulkanContext->physicalDeviceProperties);
if(VK_VERSION_MAJOR(vulkanContext->physicalDeviceProperties.apiVersion) < 1) if(VK_VERSION_MAJOR(vulkanContext->physicalDeviceProperties.apiVersion) < 1) {
continue; continue;
}
vkGetPhysicalDeviceFeatures(physicalDevice, &vulkanContext->physicalDeviceFeatures); vkGetPhysicalDeviceFeatures(physicalDevice, &vulkanContext->physicalDeviceFeatures);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, NULL); vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, NULL);
if(queueFamiliesCount == 0) if (queueFamiliesCount == 0) {
continue; continue;
if(queueFamiliesPropertiesAllocatedSize < queueFamiliesCount) }
{ if (queueFamiliesPropertiesAllocatedSize < queueFamiliesCount) {
SDL_free(queueFamiliesProperties); SDL_free(queueFamiliesProperties);
queueFamiliesPropertiesAllocatedSize = queueFamiliesCount; queueFamiliesPropertiesAllocatedSize = queueFamiliesCount;
queueFamiliesProperties = queueFamiliesProperties = (VkQueueFamilyProperties *) SDL_malloc(sizeof(VkQueueFamilyProperties) * queueFamiliesPropertiesAllocatedSize);
SDL_malloc(sizeof(VkQueueFamilyProperties) * queueFamiliesPropertiesAllocatedSize); if (!queueFamiliesProperties) {
if(!queueFamiliesProperties)
{
SDL_free(physicalDevices); SDL_free(physicalDevices);
SDL_free(deviceExtensions); SDL_free(deviceExtensions);
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
} }
} }
vkGetPhysicalDeviceQueueFamilyProperties( vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, queueFamiliesProperties);
physicalDevice, &queueFamiliesCount, queueFamiliesProperties);
vulkanContext->graphicsQueueFamilyIndex = queueFamiliesCount; vulkanContext->graphicsQueueFamilyIndex = queueFamiliesCount;
vulkanContext->presentQueueFamilyIndex = queueFamiliesCount; vulkanContext->presentQueueFamilyIndex = queueFamiliesCount;
for(queueFamilyIndex = 0; queueFamilyIndex < queueFamiliesCount; for (queueFamilyIndex = 0; queueFamilyIndex < queueFamiliesCount; queueFamilyIndex++) {
queueFamilyIndex++)
{
VkBool32 supported = 0; VkBool32 supported = 0;
if(queueFamiliesProperties[queueFamilyIndex].queueCount == 0) if (queueFamiliesProperties[queueFamilyIndex].queueCount == 0) {
continue; continue;
if(queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) }
if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
vulkanContext->graphicsQueueFamilyIndex = queueFamilyIndex; vulkanContext->graphicsQueueFamilyIndex = queueFamilyIndex;
result = vkGetPhysicalDeviceSurfaceSupportKHR( }
physicalDevice, queueFamilyIndex, vulkanContext->surface, &supported);
if(result != VK_SUCCESS) result = vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, queueFamilyIndex, vulkanContext->surface, &supported);
{ if (result != VK_SUCCESS) {
SDL_free(physicalDevices); SDL_free(physicalDevices);
SDL_free(queueFamiliesProperties); SDL_free(queueFamiliesProperties);
SDL_free(deviceExtensions); SDL_free(deviceExtensions);
@ -421,19 +374,21 @@ static void findPhysicalDevice(void)
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
if(supported) if (supported) {
{
vulkanContext->presentQueueFamilyIndex = queueFamilyIndex; vulkanContext->presentQueueFamilyIndex = queueFamilyIndex;
if(queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
break; // use this queue because it can present and do graphics break; // use this queue because it can present and do graphics
} }
} }
if(vulkanContext->graphicsQueueFamilyIndex == queueFamiliesCount) // no good queues found }
if (vulkanContext->graphicsQueueFamilyIndex == queueFamiliesCount) { // no good queues found
continue; continue;
if(vulkanContext->presentQueueFamilyIndex == queueFamiliesCount) // no good queues found }
if (vulkanContext->presentQueueFamilyIndex == queueFamiliesCount) { // no good queues found
continue; continue;
result = }
vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, NULL); result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, NULL);
if (result != VK_SUCCESS) if (result != VK_SUCCESS)
{ {
SDL_free(physicalDevices); SDL_free(physicalDevices);
@ -444,26 +399,22 @@ static void findPhysicalDevice(void)
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
if(deviceExtensionCount == 0) if (deviceExtensionCount == 0) {
continue; continue;
if(deviceExtensionsAllocatedSize < deviceExtensionCount) }
{ if (deviceExtensionsAllocatedSize < deviceExtensionCount) {
SDL_free(deviceExtensions); SDL_free(deviceExtensions);
deviceExtensionsAllocatedSize = deviceExtensionCount; deviceExtensionsAllocatedSize = deviceExtensionCount;
deviceExtensions = deviceExtensions = SDL_malloc(sizeof(VkExtensionProperties) * deviceExtensionsAllocatedSize);
SDL_malloc(sizeof(VkExtensionProperties) * deviceExtensionsAllocatedSize); if (!deviceExtensions) {
if(!deviceExtensions)
{
SDL_free(physicalDevices); SDL_free(physicalDevices);
SDL_free(queueFamiliesProperties); SDL_free(queueFamiliesProperties);
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
} }
} }
result = vkEnumerateDeviceExtensionProperties( result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, deviceExtensions);
physicalDevice, NULL, &deviceExtensionCount, deviceExtensions); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
SDL_free(physicalDevices); SDL_free(physicalDevices);
SDL_free(queueFamiliesProperties); SDL_free(queueFamiliesProperties);
SDL_free(deviceExtensions); SDL_free(deviceExtensions);
@ -472,24 +423,22 @@ static void findPhysicalDevice(void)
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
for(i = 0; i < deviceExtensionCount; i++) for (i = 0; i < deviceExtensionCount; i++) {
{ if(SDL_strcmp(deviceExtensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
if(0 == SDL_strcmp(deviceExtensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME))
{
hasSwapchainExtension = SDL_TRUE; hasSwapchainExtension = SDL_TRUE;
break; break;
} }
} }
if(!hasSwapchainExtension) if (!hasSwapchainExtension) {
continue; continue;
}
vulkanContext->physicalDevice = physicalDevice; vulkanContext->physicalDevice = physicalDevice;
break; break;
} }
SDL_free(physicalDevices); SDL_free(physicalDevices);
SDL_free(queueFamiliesProperties); SDL_free(queueFamiliesProperties);
SDL_free(deviceExtensions); SDL_free(deviceExtensions);
if(!vulkanContext->physicalDevice) if (!vulkanContext->physicalDevice) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Vulkan: no viable physical devices found"); SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Vulkan: no viable physical devices found");
quit(2); quit(2);
} }
@ -516,13 +465,10 @@ static void createDevice(void)
deviceCreateInfo.pEnabledFeatures = NULL; deviceCreateInfo.pEnabledFeatures = NULL;
deviceCreateInfo.enabledExtensionCount = SDL_arraysize(deviceExtensionNames); deviceCreateInfo.enabledExtensionCount = SDL_arraysize(deviceExtensionNames);
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames; deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
result = vkCreateDevice( result = vkCreateDevice(vulkanContext->physicalDevice, &deviceCreateInfo, NULL, &vulkanContext->device);
vulkanContext->physicalDevice, &deviceCreateInfo, NULL, &vulkanContext->device); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
vulkanContext->device = VK_NULL_HANDLE; vulkanContext->device = VK_NULL_HANDLE;
SDL_LogError( SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkCreateDevice(): %s\n", getVulkanResultString(result));
SDL_LOG_CATEGORY_APPLICATION, "vkCreateDevice(): %s\n", getVulkanResultString(result));
quit(2); quit(2);
} }
} }
@ -531,8 +477,7 @@ static void loadDeviceFunctions(void)
{ {
#define VULKAN_DEVICE_FUNCTION(name) \ #define VULKAN_DEVICE_FUNCTION(name) \
name = (PFN_##name)vkGetDeviceProcAddr(vulkanContext->device, #name); \ name = (PFN_##name)vkGetDeviceProcAddr(vulkanContext->device, #name); \
if(!name) \ if (!name) { \
{ \
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
"vkGetDeviceProcAddr(device, \"" #name "\") failed\n"); \ "vkGetDeviceProcAddr(device, \"" #name "\") failed\n"); \
quit(2); \ quit(2); \
@ -553,14 +498,15 @@ static void getQueues(void)
vulkanContext->graphicsQueueFamilyIndex, vulkanContext->graphicsQueueFamilyIndex,
0, 0,
&vulkanContext->graphicsQueue); &vulkanContext->graphicsQueue);
if(vulkanContext->graphicsQueueFamilyIndex != vulkanContext->presentQueueFamilyIndex) if (vulkanContext->graphicsQueueFamilyIndex != vulkanContext->presentQueueFamilyIndex) {
vkGetDeviceQueue(vulkanContext->device, vkGetDeviceQueue(vulkanContext->device,
vulkanContext->presentQueueFamilyIndex, vulkanContext->presentQueueFamilyIndex,
0, 0,
&vulkanContext->presentQueue); &vulkanContext->presentQueue);
else } else {
vulkanContext->presentQueue = vulkanContext->graphicsQueue; vulkanContext->presentQueue = vulkanContext->graphicsQueue;
} }
}
static void createSemaphore(VkSemaphore *semaphore) static void createSemaphore(VkSemaphore *semaphore)
{ {
@ -569,8 +515,7 @@ static void createSemaphore(VkSemaphore *semaphore)
VkSemaphoreCreateInfo createInfo = {0}; VkSemaphoreCreateInfo createInfo = {0};
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
result = vkCreateSemaphore(vulkanContext->device, &createInfo, NULL, semaphore); result = vkCreateSemaphore(vulkanContext->device, &createInfo, NULL, semaphore);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
*semaphore = VK_NULL_HANDLE; *semaphore = VK_NULL_HANDLE;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkCreateSemaphore(): %s\n", "vkCreateSemaphore(): %s\n",
@ -587,10 +532,8 @@ static void createSemaphores(void)
static void getSurfaceCaps(void) static void getSurfaceCaps(void)
{ {
VkResult result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR( VkResult result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanContext->physicalDevice, vulkanContext->surface, &vulkanContext->surfaceCapabilities);
vulkanContext->physicalDevice, vulkanContext->surface, &vulkanContext->surfaceCapabilities); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR(): %s\n", "vkGetPhysicalDeviceSurfaceCapabilitiesKHR(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
@ -598,8 +541,7 @@ static void getSurfaceCaps(void)
} }
// check surface usage // check surface usage
if(!(vulkanContext->surfaceCapabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) if (!(vulkanContext->surfaceCapabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Vulkan surface doesn't support VK_IMAGE_USAGE_TRANSFER_DST_BIT\n"); "Vulkan surface doesn't support VK_IMAGE_USAGE_TRANSFER_DST_BIT\n");
quit(2); quit(2);
@ -612,22 +554,18 @@ static void getSurfaceFormats(void)
vulkanContext->surface, vulkanContext->surface,
&vulkanContext->surfaceFormatsCount, &vulkanContext->surfaceFormatsCount,
NULL); NULL);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
vulkanContext->surfaceFormatsCount = 0; vulkanContext->surfaceFormatsCount = 0;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n", "vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
if(vulkanContext->surfaceFormatsCount > vulkanContext->surfaceFormatsAllocatedCount) if (vulkanContext->surfaceFormatsCount > vulkanContext->surfaceFormatsAllocatedCount) {
{
vulkanContext->surfaceFormatsAllocatedCount = vulkanContext->surfaceFormatsCount; vulkanContext->surfaceFormatsAllocatedCount = vulkanContext->surfaceFormatsCount;
SDL_free(vulkanContext->surfaceFormats); SDL_free(vulkanContext->surfaceFormats);
vulkanContext->surfaceFormats = vulkanContext->surfaceFormats = (VkSurfaceFormatKHR *) SDL_malloc(sizeof(VkSurfaceFormatKHR) * vulkanContext->surfaceFormatsAllocatedCount);
SDL_malloc(sizeof(VkSurfaceFormatKHR) * vulkanContext->surfaceFormatsAllocatedCount); if (!vulkanContext->surfaceFormats) {
if(!vulkanContext->surfaceFormats)
{
vulkanContext->surfaceFormatsCount = 0; vulkanContext->surfaceFormatsCount = 0;
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
@ -637,8 +575,7 @@ static void getSurfaceFormats(void)
vulkanContext->surface, vulkanContext->surface,
&vulkanContext->surfaceFormatsCount, &vulkanContext->surfaceFormatsCount,
vulkanContext->surfaceFormats); vulkanContext->surfaceFormats);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
vulkanContext->surfaceFormatsCount = 0; vulkanContext->surfaceFormatsCount = 0;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n", "vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n",
@ -653,10 +590,8 @@ static void getSwapchainImages(void)
SDL_free(vulkanContext->swapchainImages); SDL_free(vulkanContext->swapchainImages);
vulkanContext->swapchainImages = NULL; vulkanContext->swapchainImages = NULL;
result = vkGetSwapchainImagesKHR( result = vkGetSwapchainImagesKHR(vulkanContext->device, vulkanContext->swapchain, &vulkanContext->swapchainImageCount, NULL);
vulkanContext->device, vulkanContext->swapchain, &vulkanContext->swapchainImageCount, NULL); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
vulkanContext->swapchainImageCount = 0; vulkanContext->swapchainImageCount = 0;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkGetSwapchainImagesKHR(): %s\n", "vkGetSwapchainImagesKHR(): %s\n",
@ -664,8 +599,7 @@ static void getSwapchainImages(void)
quit(2); quit(2);
} }
vulkanContext->swapchainImages = SDL_malloc(sizeof(VkImage) * vulkanContext->swapchainImageCount); vulkanContext->swapchainImages = SDL_malloc(sizeof(VkImage) * vulkanContext->swapchainImageCount);
if(!vulkanContext->swapchainImages) if (!vulkanContext->swapchainImages) {
{
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
} }
@ -673,8 +607,7 @@ static void getSwapchainImages(void)
vulkanContext->swapchain, vulkanContext->swapchain,
&vulkanContext->swapchainImageCount, &vulkanContext->swapchainImageCount,
vulkanContext->swapchainImages); vulkanContext->swapchainImages);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
SDL_free(vulkanContext->swapchainImages); SDL_free(vulkanContext->swapchainImages);
vulkanContext->swapchainImages = NULL; vulkanContext->swapchainImages = NULL;
vulkanContext->swapchainImageCount = 0; vulkanContext->swapchainImageCount = 0;
@ -694,25 +627,21 @@ static SDL_bool createSwapchain(void)
// pick an image count // pick an image count
vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.minImageCount + 1; vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.minImageCount + 1;
if(vulkanContext->swapchainDesiredImageCount > vulkanContext->surfaceCapabilities.maxImageCount if ( (vulkanContext->swapchainDesiredImageCount > vulkanContext->surfaceCapabilities.maxImageCount) &&
&& vulkanContext->surfaceCapabilities.maxImageCount > 0) (vulkanContext->surfaceCapabilities.maxImageCount > 0) ) {
vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.maxImageCount; vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.maxImageCount;
}
// pick a format // pick a format
if(vulkanContext->surfaceFormatsCount == 1 if ( (vulkanContext->surfaceFormatsCount == 1) &&
&& vulkanContext->surfaceFormats[0].format == VK_FORMAT_UNDEFINED) (vulkanContext->surfaceFormats[0].format == VK_FORMAT_UNDEFINED) ) {
{
// aren't any preferred formats, so we pick // aren't any preferred formats, so we pick
vulkanContext->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; vulkanContext->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
vulkanContext->surfaceFormat.format = VK_FORMAT_R8G8B8A8_UNORM; vulkanContext->surfaceFormat.format = VK_FORMAT_R8G8B8A8_UNORM;
} } else {
else
{
vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[0]; vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[0];
for(i = 0; i < vulkanContext->surfaceFormatsCount; i++) for (i = 0; i < vulkanContext->surfaceFormatsCount; i++) {
{ if(vulkanContext->surfaceFormats[i].format == VK_FORMAT_R8G8B8A8_UNORM) {
if(vulkanContext->surfaceFormats[i].format == VK_FORMAT_R8G8B8A8_UNORM)
{
vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[i]; vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[i];
break; break;
} }
@ -732,8 +661,9 @@ static SDL_bool createSwapchain(void)
vulkanContext->surfaceCapabilities.minImageExtent.height, vulkanContext->surfaceCapabilities.minImageExtent.height,
vulkanContext->surfaceCapabilities.maxImageExtent.height); vulkanContext->surfaceCapabilities.maxImageExtent.height);
if(w == 0 || h == 0) if (w == 0 || h == 0) {
return SDL_FALSE; return SDL_FALSE;
}
getSurfaceCaps(); getSurfaceCaps();
@ -751,34 +681,37 @@ static SDL_bool createSwapchain(void)
createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR; createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
createInfo.clipped = VK_TRUE; createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = vulkanContext->swapchain; createInfo.oldSwapchain = vulkanContext->swapchain;
result = result = vkCreateSwapchainKHR(vulkanContext->device, &createInfo, NULL, &vulkanContext->swapchain);
vkCreateSwapchainKHR(vulkanContext->device, &createInfo, NULL, &vulkanContext->swapchain);
if(createInfo.oldSwapchain) if (createInfo.oldSwapchain) {
vkDestroySwapchainKHR(vulkanContext->device, createInfo.oldSwapchain, NULL); vkDestroySwapchainKHR(vulkanContext->device, createInfo.oldSwapchain, NULL);
if(result != VK_SUCCESS) }
{
if(result != VK_SUCCESS) {
vulkanContext->swapchain = VK_NULL_HANDLE; vulkanContext->swapchain = VK_NULL_HANDLE;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkCreateSwapchainKHR(): %s\n", "vkCreateSwapchainKHR(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
getSwapchainImages(); getSwapchainImages();
return SDL_TRUE; return SDL_TRUE;
} }
static void destroySwapchain(void) static void destroySwapchain(void)
{ {
if(vulkanContext->swapchain) if (vulkanContext->swapchain) {
vkDestroySwapchainKHR(vulkanContext->device, vulkanContext->swapchain, NULL); vkDestroySwapchainKHR(vulkanContext->device, vulkanContext->swapchain, NULL);
vulkanContext->swapchain = VK_NULL_HANDLE; vulkanContext->swapchain = VK_NULL_HANDLE;
}
SDL_free(vulkanContext->swapchainImages); SDL_free(vulkanContext->swapchainImages);
vulkanContext->swapchainImages = NULL; vulkanContext->swapchainImages = NULL;
} }
static void destroyCommandBuffers(void) static void destroyCommandBuffers(void)
{ {
if(vulkanContext->commandBuffers) if (vulkanContext->commandBuffers) {
vkFreeCommandBuffers(vulkanContext->device, vkFreeCommandBuffers(vulkanContext->device,
vulkanContext->commandPool, vulkanContext->commandPool,
vulkanContext->swapchainImageCount, vulkanContext->swapchainImageCount,
@ -786,27 +719,25 @@ static void destroyCommandBuffers(void)
SDL_free(vulkanContext->commandBuffers); SDL_free(vulkanContext->commandBuffers);
vulkanContext->commandBuffers = NULL; vulkanContext->commandBuffers = NULL;
} }
}
static void destroyCommandPool(void) static void destroyCommandPool(void)
{ {
if(vulkanContext->commandPool) if (vulkanContext->commandPool) {
vkDestroyCommandPool(vulkanContext->device, vulkanContext->commandPool, NULL); vkDestroyCommandPool(vulkanContext->device, vulkanContext->commandPool, NULL);
}
vulkanContext->commandPool = VK_NULL_HANDLE; vulkanContext->commandPool = VK_NULL_HANDLE;
} }
static void createCommandPool(void) static void createCommandPool(void)
{ {
VkResult result; VkResult result;
VkCommandPoolCreateInfo createInfo = {0}; VkCommandPoolCreateInfo createInfo = {0};
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
createInfo.flags = createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
createInfo.queueFamilyIndex = vulkanContext->graphicsQueueFamilyIndex; createInfo.queueFamilyIndex = vulkanContext->graphicsQueueFamilyIndex;
result = result = vkCreateCommandPool(vulkanContext->device, &createInfo, NULL, &vulkanContext->commandPool);
vkCreateCommandPool(vulkanContext->device, &createInfo, NULL, &vulkanContext->commandPool); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS)
{
vulkanContext->commandPool = VK_NULL_HANDLE; vulkanContext->commandPool = VK_NULL_HANDLE;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkCreateCommandPool(): %s\n", "vkCreateCommandPool(): %s\n",
@ -818,18 +749,14 @@ static void createCommandPool(void)
static void createCommandBuffers(void) static void createCommandBuffers(void)
{ {
VkResult result; VkResult result;
VkCommandBufferAllocateInfo allocateInfo = {0}; VkCommandBufferAllocateInfo allocateInfo = {0};
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocateInfo.commandPool = vulkanContext->commandPool; allocateInfo.commandPool = vulkanContext->commandPool;
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocateInfo.commandBufferCount = vulkanContext->swapchainImageCount; allocateInfo.commandBufferCount = vulkanContext->swapchainImageCount;
vulkanContext->commandBuffers = vulkanContext->commandBuffers = (VkCommandBuffer *) SDL_malloc(sizeof(VkCommandBuffer) * vulkanContext->swapchainImageCount);
SDL_malloc(sizeof(VkCommandBuffer) * vulkanContext->swapchainImageCount); result = vkAllocateCommandBuffers(vulkanContext->device, &allocateInfo, vulkanContext->commandBuffers);
result = if(result != VK_SUCCESS) {
vkAllocateCommandBuffers(vulkanContext->device, &allocateInfo, vulkanContext->commandBuffers);
if(result != VK_SUCCESS)
{
SDL_free(vulkanContext->commandBuffers); SDL_free(vulkanContext->commandBuffers);
vulkanContext->commandBuffers = NULL; vulkanContext->commandBuffers = NULL;
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
@ -844,24 +771,18 @@ static void createFences(void)
uint32_t i; uint32_t i;
vulkanContext->fences = SDL_malloc(sizeof(VkFence) * vulkanContext->swapchainImageCount); vulkanContext->fences = SDL_malloc(sizeof(VkFence) * vulkanContext->swapchainImageCount);
if(!vulkanContext->fences) if (!vulkanContext->fences) {
{
SDL_OutOfMemory(); SDL_OutOfMemory();
quit(2); quit(2);
} }
for(i = 0; i < vulkanContext->swapchainImageCount; i++) for (i = 0; i < vulkanContext->swapchainImageCount; i++) {
{
VkResult result; VkResult result;
VkFenceCreateInfo createInfo = {0}; VkFenceCreateInfo createInfo = {0};
createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
result = result = vkCreateFence(vulkanContext->device, &createInfo, NULL, &vulkanContext->fences[i]);
vkCreateFence(vulkanContext->device, &createInfo, NULL, &vulkanContext->fences[i]); if(result != VK_SUCCESS) {
if(result != VK_SUCCESS) for(; i > 0; i--) {
{
for(; i > 0; i--)
{
vkDestroyFence(vulkanContext->device, vulkanContext->fences[i - 1], NULL); vkDestroyFence(vulkanContext->device, vulkanContext->fences[i - 1], NULL);
} }
SDL_free(vulkanContext->fences); SDL_free(vulkanContext->fences);
@ -878,10 +799,11 @@ static void destroyFences(void)
{ {
uint32_t i; uint32_t i;
if(!vulkanContext->fences) if (!vulkanContext->fences) {
return; return;
for(i = 0; i < vulkanContext->swapchainImageCount; i++) }
{
for (i = 0; i < vulkanContext->swapchainImageCount; i++) {
vkDestroyFence(vulkanContext->device, vulkanContext->fences[i], NULL); vkDestroyFence(vulkanContext->device, vulkanContext->fences[i], NULL);
} }
SDL_free(vulkanContext->fences); SDL_free(vulkanContext->fences);
@ -929,8 +851,7 @@ static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *
VkImageSubresourceRange clearRange = {0}; VkImageSubresourceRange clearRange = {0};
VkResult result = vkResetCommandBuffer(commandBuffer, 0); VkResult result = vkResetCommandBuffer(commandBuffer, 0);
if(result != VK_SUCCESS) if(result != VK_SUCCESS) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkResetCommandBuffer(): %s\n", "vkResetCommandBuffer(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
@ -939,8 +860,7 @@ static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
result = vkBeginCommandBuffer(commandBuffer, &beginInfo); result = vkBeginCommandBuffer(commandBuffer, &beginInfo);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkBeginCommandBuffer(): %s\n", "vkBeginCommandBuffer(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
@ -957,8 +877,7 @@ static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *
clearRange.levelCount = 1; clearRange.levelCount = 1;
clearRange.baseArrayLayer = 0; clearRange.baseArrayLayer = 0;
clearRange.layerCount = 1; clearRange.layerCount = 1;
vkCmdClearColorImage( vkCmdClearColorImage(commandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, clearColor, 1, &clearRange);
commandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, clearColor, 1, &clearRange);
recordPipelineImageBarrier(commandBuffer, recordPipelineImageBarrier(commandBuffer,
VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_WRITE_BIT,
VK_ACCESS_MEMORY_READ_BIT, VK_ACCESS_MEMORY_READ_BIT,
@ -966,8 +885,7 @@ static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
image); image);
result = vkEndCommandBuffer(commandBuffer); result = vkEndCommandBuffer(commandBuffer);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkEndCommandBuffer(): %s\n", "vkEndCommandBuffer(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
@ -981,17 +899,19 @@ static void destroySwapchainAndSwapchainSpecificStuff(SDL_bool doDestroySwapchai
destroyFences(); destroyFences();
destroyCommandBuffers(); destroyCommandBuffers();
destroyCommandPool(); destroyCommandPool();
if(doDestroySwapchain) if (doDestroySwapchain) {
destroySwapchain(); destroySwapchain();
} }
}
static SDL_bool createNewSwapchainAndSwapchainSpecificStuff(void) static SDL_bool createNewSwapchainAndSwapchainSpecificStuff(void)
{ {
destroySwapchainAndSwapchainSpecificStuff(SDL_FALSE); destroySwapchainAndSwapchainSpecificStuff(SDL_FALSE);
getSurfaceCaps(); getSurfaceCaps();
getSurfaceFormats(); getSurfaceFormats();
if(!createSwapchain()) if(!createSwapchain()) {
return SDL_FALSE; return SDL_FALSE;
}
createCommandPool(); createCommandPool();
createCommandBuffers(); createCommandBuffers();
createFences(); createFences();
@ -1032,19 +952,32 @@ static void shutdownVulkan(SDL_bool doDestroySwapchain)
int i; int i;
for (i = 0; i < state->num_windows; ++i) { for (i = 0; i < state->num_windows; ++i) {
vulkanContext = &vulkanContexts[i]; vulkanContext = &vulkanContexts[i];
if(vulkanContext->device && vkDeviceWaitIdle) if (vulkanContext->device && vkDeviceWaitIdle) {
vkDeviceWaitIdle(vulkanContext->device); vkDeviceWaitIdle(vulkanContext->device);
}
destroySwapchainAndSwapchainSpecificStuff(doDestroySwapchain); destroySwapchainAndSwapchainSpecificStuff(doDestroySwapchain);
if(vulkanContext->imageAvailableSemaphore && vkDestroySemaphore)
if (vulkanContext->imageAvailableSemaphore && vkDestroySemaphore) {
vkDestroySemaphore(vulkanContext->device, vulkanContext->imageAvailableSemaphore, NULL); vkDestroySemaphore(vulkanContext->device, vulkanContext->imageAvailableSemaphore, NULL);
if(vulkanContext->renderingFinishedSemaphore && vkDestroySemaphore) }
if (vulkanContext->renderingFinishedSemaphore && vkDestroySemaphore) {
vkDestroySemaphore(vulkanContext->device, vulkanContext->renderingFinishedSemaphore, NULL); vkDestroySemaphore(vulkanContext->device, vulkanContext->renderingFinishedSemaphore, NULL);
if(vulkanContext->device && vkDestroyDevice) }
if (vulkanContext->device && vkDestroyDevice) {
vkDestroyDevice(vulkanContext->device, NULL); vkDestroyDevice(vulkanContext->device, NULL);
if(vulkanContext->surface && vkDestroySurfaceKHR) }
if (vulkanContext->surface && vkDestroySurfaceKHR) {
vkDestroySurfaceKHR(vulkanContext->instance, vulkanContext->surface, NULL); vkDestroySurfaceKHR(vulkanContext->instance, vulkanContext->surface, NULL);
if(vulkanContext->instance && vkDestroyInstance) }
if (vulkanContext->instance && vkDestroyInstance) {
vkDestroyInstance(vulkanContext->instance, NULL); vkDestroyInstance(vulkanContext->instance, NULL);
}
SDL_free(vulkanContext->surfaceFormats); SDL_free(vulkanContext->surfaceFormats);
} }
SDL_free(vulkanContexts); SDL_free(vulkanContexts);
@ -1065,8 +998,7 @@ static SDL_bool render(void)
VkPresentInfoKHR presentInfo = {0}; VkPresentInfoKHR presentInfo = {0};
int w, h; int w, h;
if(!vulkanContext->swapchain) if (!vulkanContext->swapchain) {
{
SDL_bool retval = createNewSwapchainAndSwapchainSpecificStuff(); SDL_bool retval = createNewSwapchainAndSwapchainSpecificStuff();
if(!retval) if(!retval)
SDL_Delay(100); SDL_Delay(100);
@ -1078,28 +1010,24 @@ static SDL_bool render(void)
vulkanContext->imageAvailableSemaphore, vulkanContext->imageAvailableSemaphore,
VK_NULL_HANDLE, VK_NULL_HANDLE,
&frameIndex); &frameIndex);
if(result == VK_ERROR_OUT_OF_DATE_KHR) if (result == VK_ERROR_OUT_OF_DATE_KHR) {
return createNewSwapchainAndSwapchainSpecificStuff(); return createNewSwapchainAndSwapchainSpecificStuff();
if(result != VK_SUBOPTIMAL_KHR && result != VK_SUCCESS) }
{
if ((result != VK_SUBOPTIMAL_KHR) && (result != VK_SUCCESS)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkAcquireNextImageKHR(): %s\n", "vkAcquireNextImageKHR(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
result = vkWaitForFences( result = vkWaitForFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex], VK_FALSE, UINT64_MAX);
vulkanContext->device, 1, &vulkanContext->fences[frameIndex], VK_FALSE, UINT64_MAX); if (result != VK_SUCCESS) {
if(result != VK_SUCCESS) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkWaitForFences(): %s\n", getVulkanResultString(result));
{
SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION, "vkWaitForFences(): %s\n", getVulkanResultString(result));
quit(2); quit(2);
} }
result = vkResetFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex]); result = vkResetFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex]);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkResetFences(): %s\n", getVulkanResultString(result));
SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION, "vkResetFences(): %s\n", getVulkanResultString(result));
quit(2); quit(2);
} }
currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency(); currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
@ -1116,12 +1044,10 @@ static SDL_bool render(void)
submitInfo.pCommandBuffers = &vulkanContext->commandBuffers[frameIndex]; submitInfo.pCommandBuffers = &vulkanContext->commandBuffers[frameIndex];
submitInfo.signalSemaphoreCount = 1; submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &vulkanContext->renderingFinishedSemaphore; submitInfo.pSignalSemaphores = &vulkanContext->renderingFinishedSemaphore;
result = vkQueueSubmit( result = vkQueueSubmit(vulkanContext->graphicsQueue, 1, &submitInfo, vulkanContext->fences[frameIndex]);
vulkanContext->graphicsQueue, 1, &submitInfo, vulkanContext->fences[frameIndex]);
if(result != VK_SUCCESS) if (result != VK_SUCCESS) {
{ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkQueueSubmit(): %s\n", getVulkanResultString(result));
SDL_LogError(
SDL_LOG_CATEGORY_APPLICATION, "vkQueueSubmit(): %s\n", getVulkanResultString(result));
quit(2); quit(2);
} }
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
@ -1131,26 +1057,24 @@ static SDL_bool render(void)
presentInfo.pSwapchains = &vulkanContext->swapchain; presentInfo.pSwapchains = &vulkanContext->swapchain;
presentInfo.pImageIndices = &frameIndex; presentInfo.pImageIndices = &frameIndex;
result = vkQueuePresentKHR(vulkanContext->presentQueue, &presentInfo); result = vkQueuePresentKHR(vulkanContext->presentQueue, &presentInfo);
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) if ((result == VK_ERROR_OUT_OF_DATE_KHR) || (result == VK_SUBOPTIMAL_KHR)) {
{
return createNewSwapchainAndSwapchainSpecificStuff(); return createNewSwapchainAndSwapchainSpecificStuff();
} }
if(result != VK_SUCCESS)
{ if (result != VK_SUCCESS) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"vkQueuePresentKHR(): %s\n", "vkQueuePresentKHR(): %s\n",
getVulkanResultString(result)); getVulkanResultString(result));
quit(2); quit(2);
} }
SDL_Vulkan_GetDrawableSize(vulkanContext->window, &w, &h); SDL_Vulkan_GetDrawableSize(vulkanContext->window, &w, &h);
if(w != (int)vulkanContext->swapchainSize.width || h != (int)vulkanContext->swapchainSize.height) if(w != (int)vulkanContext->swapchainSize.width || h != (int)vulkanContext->swapchainSize.height) {
{
return createNewSwapchainAndSwapchainSpecificStuff(); return createNewSwapchainAndSwapchainSpecificStuff();
} }
return SDL_TRUE; return SDL_TRUE;
} }
int main(int argc, char *argv[]) int main(int argc, char **argv)
{ {
int done; int done;
SDL_DisplayMode mode; SDL_DisplayMode mode;
@ -1163,8 +1087,7 @@ int main(int argc, char *argv[])
/* Initialize test framework */ /* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if(!state) if(!state) {
{
return 1; return 1;
} }
@ -1191,17 +1114,16 @@ int main(int argc, char *argv[])
frames = 0; frames = 0;
then = SDL_GetTicks(); then = SDL_GetTicks();
done = 0; done = 0;
while(!done) while (!done) {
{
/* Check for events */ /* Check for events */
++frames; frames++;
while(SDL_PollEvent(&event)) /* !!! FIXME: fix coding conventions with braces and spaces */ while(SDL_PollEvent(&event)) {
{
/* Need to destroy the swapchain before the window created /* Need to destroy the swapchain before the window created
* by SDL. * by SDL.
*/ */
if (event.type == SDL_WINDOWEVENT_CLOSE) if (event.type == SDL_WINDOWEVENT_CLOSE) {
destroySwapchainAndSwapchainSpecificStuff(SDL_TRUE); destroySwapchainAndSwapchainSpecificStuff(SDL_TRUE);
}
SDLTest_CommonEvent(state, &event, &done); SDLTest_CommonEvent(state, &event, &done);
} }
@ -1218,10 +1140,10 @@ int main(int argc, char *argv[])
/* Print out some timing information */ /* Print out some timing information */
now = SDL_GetTicks(); now = SDL_GetTicks();
if(now > then) if (now > then) {
{
SDL_Log("%2.2f frames per second\n", ((double)frames * 1000) / (now - then)); SDL_Log("%2.2f frames per second\n", ((double)frames * 1000) / (now - then));
} }
shutdownVulkan(SDL_TRUE); shutdownVulkan(SDL_TRUE);
SDLTest_CommonQuit(state); SDLTest_CommonQuit(state);
return 0; return 0;