2017-08-28 05:15:57 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#if defined(SDL_PLATFORM_ANDROID) && defined(__ARM_EABI__) && !defined(__ARM_ARCH_7A__)
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Vulkan support on this system\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define VK_NO_PROTOTYPES
|
2017-08-28 06:41:48 +03:00
|
|
|
#ifdef HAVE_VULKAN_H
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
#else
|
|
|
|
/* SDL includes a copy for building on systems without the Vulkan SDK */
|
2017-08-28 06:25:12 +03:00
|
|
|
#include "../src/video/khronos/vulkan/vulkan.h"
|
2017-08-28 06:41:48 +03:00
|
|
|
#endif
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL_vulkan.h>
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2017-08-28 08:20:17 +03:00
|
|
|
#ifndef UINT64_MAX /* VS2008 */
|
|
|
|
#define UINT64_MAX 18446744073709551615
|
|
|
|
#endif
|
|
|
|
|
2017-08-28 05:15:57 +03:00
|
|
|
#define VULKAN_FUNCTIONS() \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkAcquireNextImageKHR) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkAllocateCommandBuffers) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkBeginCommandBuffer) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCmdClearColorImage) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCmdPipelineBarrier) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCreateCommandPool) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCreateFence) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCreateImageView) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCreateSemaphore) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkCreateSwapchainKHR) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDestroyCommandPool) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDestroyDevice) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDestroyFence) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDestroyImageView) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDestroySemaphore) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDestroySwapchainKHR) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkDeviceWaitIdle) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkEndCommandBuffer) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkFreeCommandBuffers) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkGetDeviceQueue) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkGetFenceStatus) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkGetSwapchainImagesKHR) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkQueuePresentKHR) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkQueueSubmit) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkResetCommandBuffer) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkResetFences) \
|
|
|
|
VULKAN_DEVICE_FUNCTION(vkWaitForFences) \
|
|
|
|
VULKAN_GLOBAL_FUNCTION(vkCreateInstance) \
|
|
|
|
VULKAN_GLOBAL_FUNCTION(vkEnumerateInstanceExtensionProperties) \
|
|
|
|
VULKAN_GLOBAL_FUNCTION(vkEnumerateInstanceLayerProperties) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkCreateDevice) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkDestroyInstance) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkDestroySurfaceKHR) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkEnumerateDeviceExtensionProperties) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkEnumeratePhysicalDevices) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetDeviceProcAddr) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceFeatures) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceProperties) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceQueueFamilyProperties) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceCapabilitiesKHR) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceFormatsKHR) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfacePresentModesKHR) \
|
|
|
|
VULKAN_INSTANCE_FUNCTION(vkGetPhysicalDeviceSurfaceSupportKHR)
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
#define VULKAN_DEVICE_FUNCTION(name) static PFN_##name name = NULL;
|
|
|
|
#define VULKAN_GLOBAL_FUNCTION(name) static PFN_##name name = NULL;
|
2017-08-28 05:15:57 +03:00
|
|
|
#define VULKAN_INSTANCE_FUNCTION(name) static PFN_##name name = NULL;
|
|
|
|
VULKAN_FUNCTIONS()
|
|
|
|
#undef VULKAN_DEVICE_FUNCTION
|
|
|
|
#undef VULKAN_GLOBAL_FUNCTION
|
|
|
|
#undef VULKAN_INSTANCE_FUNCTION
|
|
|
|
static PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
|
|
|
|
|
|
|
|
/* Based on the headers found in
|
|
|
|
* https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers
|
|
|
|
*/
|
|
|
|
#if VK_HEADER_VERSION < 22
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
VK_ERROR_FRAGMENTED_POOL = -12,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
#if VK_HEADER_VERSION < 38
|
2022-11-30 23:51:59 +03:00
|
|
|
enum
|
|
|
|
{
|
2017-08-28 05:15:57 +03:00
|
|
|
VK_ERROR_OUT_OF_POOL_MEMORY_KHR = -1000069000
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *getVulkanResultString(VkResult result)
|
|
|
|
{
|
2022-11-30 23:51:59 +03:00
|
|
|
switch ((int)result) {
|
|
|
|
#define RESULT_CASE(x) \
|
|
|
|
case x: \
|
|
|
|
return #x
|
2021-08-24 19:41:41 +03:00
|
|
|
RESULT_CASE(VK_SUCCESS);
|
|
|
|
RESULT_CASE(VK_NOT_READY);
|
|
|
|
RESULT_CASE(VK_TIMEOUT);
|
|
|
|
RESULT_CASE(VK_EVENT_SET);
|
|
|
|
RESULT_CASE(VK_EVENT_RESET);
|
|
|
|
RESULT_CASE(VK_INCOMPLETE);
|
|
|
|
RESULT_CASE(VK_ERROR_OUT_OF_HOST_MEMORY);
|
|
|
|
RESULT_CASE(VK_ERROR_OUT_OF_DEVICE_MEMORY);
|
|
|
|
RESULT_CASE(VK_ERROR_INITIALIZATION_FAILED);
|
|
|
|
RESULT_CASE(VK_ERROR_DEVICE_LOST);
|
|
|
|
RESULT_CASE(VK_ERROR_MEMORY_MAP_FAILED);
|
|
|
|
RESULT_CASE(VK_ERROR_LAYER_NOT_PRESENT);
|
|
|
|
RESULT_CASE(VK_ERROR_EXTENSION_NOT_PRESENT);
|
|
|
|
RESULT_CASE(VK_ERROR_FEATURE_NOT_PRESENT);
|
|
|
|
RESULT_CASE(VK_ERROR_INCOMPATIBLE_DRIVER);
|
|
|
|
RESULT_CASE(VK_ERROR_TOO_MANY_OBJECTS);
|
|
|
|
RESULT_CASE(VK_ERROR_FORMAT_NOT_SUPPORTED);
|
|
|
|
RESULT_CASE(VK_ERROR_FRAGMENTED_POOL);
|
|
|
|
RESULT_CASE(VK_ERROR_SURFACE_LOST_KHR);
|
|
|
|
RESULT_CASE(VK_ERROR_NATIVE_WINDOW_IN_USE_KHR);
|
|
|
|
RESULT_CASE(VK_SUBOPTIMAL_KHR);
|
|
|
|
RESULT_CASE(VK_ERROR_OUT_OF_DATE_KHR);
|
|
|
|
RESULT_CASE(VK_ERROR_INCOMPATIBLE_DISPLAY_KHR);
|
|
|
|
RESULT_CASE(VK_ERROR_VALIDATION_FAILED_EXT);
|
|
|
|
RESULT_CASE(VK_ERROR_OUT_OF_POOL_MEMORY_KHR);
|
|
|
|
RESULT_CASE(VK_ERROR_INVALID_SHADER_NV);
|
2022-11-30 23:51:59 +03:00
|
|
|
#undef RESULT_CASE
|
|
|
|
default:
|
|
|
|
break;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
return (result < 0) ? "VK_ERROR_<Unknown>" : "VK_<Unknown>";
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct VulkanContext
|
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_Window *window;
|
2017-08-28 05:15:57 +03:00
|
|
|
VkInstance instance;
|
|
|
|
VkDevice device;
|
|
|
|
VkSurfaceKHR surface;
|
|
|
|
VkSwapchainKHR swapchain;
|
|
|
|
VkPhysicalDeviceProperties physicalDeviceProperties;
|
|
|
|
VkPhysicalDeviceFeatures physicalDeviceFeatures;
|
|
|
|
uint32_t graphicsQueueFamilyIndex;
|
|
|
|
uint32_t presentQueueFamilyIndex;
|
|
|
|
VkPhysicalDevice physicalDevice;
|
|
|
|
VkQueue graphicsQueue;
|
|
|
|
VkQueue presentQueue;
|
|
|
|
VkSemaphore imageAvailableSemaphore;
|
|
|
|
VkSemaphore renderingFinishedSemaphore;
|
|
|
|
VkSurfaceCapabilitiesKHR surfaceCapabilities;
|
|
|
|
VkSurfaceFormatKHR *surfaceFormats;
|
|
|
|
uint32_t surfaceFormatsAllocatedCount;
|
|
|
|
uint32_t surfaceFormatsCount;
|
|
|
|
uint32_t swapchainDesiredImageCount;
|
|
|
|
VkSurfaceFormatKHR surfaceFormat;
|
|
|
|
VkExtent2D swapchainSize;
|
|
|
|
VkCommandPool commandPool;
|
|
|
|
uint32_t swapchainImageCount;
|
|
|
|
VkImage *swapchainImages;
|
|
|
|
VkCommandBuffer *commandBuffers;
|
|
|
|
VkFence *fences;
|
|
|
|
} VulkanContext;
|
|
|
|
|
|
|
|
static SDLTest_CommonState *state;
|
2023-05-25 02:15:12 +03:00
|
|
|
static VulkanContext *vulkanContexts = NULL; /* an array of state->num_windows items */
|
|
|
|
static VulkanContext *vulkanContext = NULL; /* for the currently-rendering window */
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
static void shutdownVulkan(bool doDestroySwapchain);
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
|
|
|
|
static void quit(int rc)
|
|
|
|
{
|
2024-09-18 17:52:28 +03:00
|
|
|
shutdownVulkan(true);
|
2017-08-28 05:15:57 +03:00
|
|
|
SDLTest_CommonQuit(state);
|
2023-04-12 12:07:28 +03:00
|
|
|
/* Let 'main()' return normally */
|
|
|
|
if (rc != 0) {
|
|
|
|
exit(rc);
|
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void loadGlobalFunctions(void)
|
|
|
|
{
|
2023-01-10 01:55:12 +03:00
|
|
|
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vkGetInstanceProcAddr) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_Vulkan_GetVkGetInstanceProcAddr(): %s\n",
|
|
|
|
SDL_GetError());
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define VULKAN_DEVICE_FUNCTION(name)
|
|
|
|
#define VULKAN_GLOBAL_FUNCTION(name) \
|
|
|
|
name = (PFN_##name)vkGetInstanceProcAddr(VK_NULL_HANDLE, #name); \
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!name) { \
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
|
|
|
|
"vkGetInstanceProcAddr(VK_NULL_HANDLE, \"" #name "\") failed\n"); \
|
|
|
|
quit(2); \
|
|
|
|
}
|
|
|
|
#define VULKAN_INSTANCE_FUNCTION(name)
|
|
|
|
VULKAN_FUNCTIONS()
|
|
|
|
#undef VULKAN_DEVICE_FUNCTION
|
|
|
|
#undef VULKAN_GLOBAL_FUNCTION
|
|
|
|
#undef VULKAN_INSTANCE_FUNCTION
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createInstance(void)
|
|
|
|
{
|
2022-11-30 23:51:59 +03:00
|
|
|
VkApplicationInfo appInfo = { 0 };
|
|
|
|
VkInstanceCreateInfo instanceCreateInfo = { 0 };
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2020-12-24 00:28:50 +03:00
|
|
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
2017-08-28 05:15:57 +03:00
|
|
|
appInfo.apiVersion = VK_API_VERSION_1_0;
|
|
|
|
instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
|
|
|
instanceCreateInfo.pApplicationInfo = &appInfo;
|
2024-07-23 18:17:22 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
instanceCreateInfo.flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
|
|
|
|
#endif
|
2023-11-02 19:20:26 +03:00
|
|
|
|
2024-03-06 00:39:42 +03:00
|
|
|
instanceCreateInfo.ppEnabledExtensionNames = SDL_Vulkan_GetInstanceExtensions(&instanceCreateInfo.enabledExtensionCount);
|
2021-08-24 03:18:57 +03:00
|
|
|
result = vkCreateInstance(&instanceCreateInfo, NULL, &vulkanContext->instance);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->instance = VK_NULL_HANDLE;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkCreateInstance(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadInstanceFunctions(void)
|
|
|
|
{
|
|
|
|
#define VULKAN_DEVICE_FUNCTION(name)
|
|
|
|
#define VULKAN_GLOBAL_FUNCTION(name)
|
|
|
|
#define VULKAN_INSTANCE_FUNCTION(name) \
|
2021-08-24 19:41:41 +03:00
|
|
|
name = (PFN_##name)vkGetInstanceProcAddr(vulkanContext->instance, #name); \
|
|
|
|
if (!name) { \
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
|
|
|
|
"vkGetInstanceProcAddr(instance, \"" #name "\") failed\n"); \
|
|
|
|
quit(2); \
|
|
|
|
}
|
|
|
|
VULKAN_FUNCTIONS()
|
|
|
|
#undef VULKAN_DEVICE_FUNCTION
|
|
|
|
#undef VULKAN_GLOBAL_FUNCTION
|
|
|
|
#undef VULKAN_INSTANCE_FUNCTION
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createSurface(void)
|
|
|
|
{
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!SDL_Vulkan_CreateSurface(vulkanContext->window, vulkanContext->instance, NULL, &vulkanContext->surface)) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surface = VK_NULL_HANDLE;
|
2021-08-24 19:41:41 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Vulkan_CreateSurface(): %s\n", SDL_GetError());
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void findPhysicalDevice(void)
|
|
|
|
{
|
|
|
|
uint32_t physicalDeviceCount = 0;
|
2020-12-24 00:28:50 +03:00
|
|
|
VkPhysicalDevice *physicalDevices;
|
|
|
|
VkQueueFamilyProperties *queueFamiliesProperties = NULL;
|
2017-08-28 05:15:57 +03:00
|
|
|
uint32_t queueFamiliesPropertiesAllocatedSize = 0;
|
|
|
|
VkExtensionProperties *deviceExtensions = NULL;
|
|
|
|
uint32_t deviceExtensionsAllocatedSize = 0;
|
2020-12-24 00:28:50 +03:00
|
|
|
uint32_t physicalDeviceIndex;
|
2021-08-24 19:41:41 +03:00
|
|
|
VkResult result;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, NULL);
|
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkEnumeratePhysicalDevices(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
if (physicalDeviceCount == 0) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkEnumeratePhysicalDevices(): no physical devices\n");
|
|
|
|
quit(2);
|
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
physicalDevices = (VkPhysicalDevice *)SDL_malloc(sizeof(VkPhysicalDevice) * physicalDeviceCount);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!physicalDevices) {
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkEnumeratePhysicalDevices(vulkanContext->instance, &physicalDeviceCount, physicalDevices);
|
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkEnumeratePhysicalDevices(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->physicalDevice = NULL;
|
2021-08-24 19:41:41 +03:00
|
|
|
for (physicalDeviceIndex = 0; physicalDeviceIndex < physicalDeviceCount; physicalDeviceIndex++) {
|
2017-08-28 05:15:57 +03:00
|
|
|
uint32_t queueFamiliesCount = 0;
|
2020-12-24 00:28:50 +03:00
|
|
|
uint32_t queueFamilyIndex;
|
2017-08-28 05:15:57 +03:00
|
|
|
uint32_t deviceExtensionCount = 0;
|
2024-09-18 17:52:28 +03:00
|
|
|
bool hasSwapchainExtension = false;
|
|
|
|
bool supportsPresent;
|
2020-12-24 00:28:50 +03:00
|
|
|
uint32_t i;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2020-12-24 00:28:50 +03:00
|
|
|
VkPhysicalDevice physicalDevice = physicalDevices[physicalDeviceIndex];
|
2021-08-24 03:18:57 +03:00
|
|
|
vkGetPhysicalDeviceProperties(physicalDevice, &vulkanContext->physicalDeviceProperties);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (VK_VERSION_MAJOR(vulkanContext->physicalDeviceProperties.apiVersion) < 1) {
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
vkGetPhysicalDeviceFeatures(physicalDevice, &vulkanContext->physicalDeviceFeatures);
|
2017-08-28 05:15:57 +03:00
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (queueFamiliesCount == 0) {
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
if (queueFamiliesPropertiesAllocatedSize < queueFamiliesCount) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
queueFamiliesPropertiesAllocatedSize = queueFamiliesCount;
|
2022-11-30 23:51:59 +03:00
|
|
|
queueFamiliesProperties = (VkQueueFamilyProperties *)SDL_malloc(sizeof(VkQueueFamilyProperties) * queueFamiliesPropertiesAllocatedSize);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!queueFamiliesProperties) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(deviceExtensions);
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamiliesCount, queueFamiliesProperties);
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->graphicsQueueFamilyIndex = queueFamiliesCount;
|
|
|
|
vulkanContext->presentQueueFamilyIndex = queueFamiliesCount;
|
2021-08-24 19:41:41 +03:00
|
|
|
for (queueFamilyIndex = 0; queueFamilyIndex < queueFamiliesCount; queueFamilyIndex++) {
|
2017-08-28 05:15:57 +03:00
|
|
|
VkBool32 supported = 0;
|
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
if (queueFamiliesProperties[queueFamilyIndex].queueCount == 0) {
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->graphicsQueueFamilyIndex = queueFamilyIndex;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, queueFamilyIndex, vulkanContext->surface, &supported);
|
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
SDL_free(deviceExtensions);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkGetPhysicalDeviceSurfaceSupportKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
if (supported) {
|
2024-07-11 21:19:26 +03:00
|
|
|
/* This check isn't necessary if you are able to check a
|
|
|
|
* VkSurfaceKHR like above, but just as a sanity check we do
|
|
|
|
* this here as part of testing the API.
|
|
|
|
* -flibit
|
|
|
|
*/
|
|
|
|
supportsPresent = SDL_Vulkan_GetPresentationSupport(vulkanContext->instance, physicalDevice, queueFamilyIndex);
|
|
|
|
if (!supportsPresent) {
|
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
SDL_free(deviceExtensions);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"SDL_Vulkan_GetPresentationSupport(): %s\n",
|
|
|
|
SDL_GetError());
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->presentQueueFamilyIndex = queueFamilyIndex;
|
2021-08-24 19:41:41 +03:00
|
|
|
if (queueFamiliesProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) {
|
2017-08-28 05:15:57 +03:00
|
|
|
break; // use this queue because it can present and do graphics
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
if (vulkanContext->graphicsQueueFamilyIndex == queueFamiliesCount) { // no good queues found
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
if (vulkanContext->presentQueueFamilyIndex == queueFamiliesCount) { // no good queues found
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, NULL);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
SDL_free(deviceExtensions);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkEnumerateDeviceExtensionProperties(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
if (deviceExtensionCount == 0) {
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
if (deviceExtensionsAllocatedSize < deviceExtensionCount) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(deviceExtensions);
|
|
|
|
deviceExtensionsAllocatedSize = deviceExtensionCount;
|
2021-08-24 19:41:41 +03:00
|
|
|
deviceExtensions = SDL_malloc(sizeof(VkExtensionProperties) * deviceExtensionsAllocatedSize);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!deviceExtensions) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkEnumerateDeviceExtensionProperties(physicalDevice, NULL, &deviceExtensionCount, deviceExtensions);
|
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
SDL_free(deviceExtensions);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkEnumerateDeviceExtensionProperties(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
for (i = 0; i < deviceExtensionCount; i++) {
|
2022-11-27 19:38:43 +03:00
|
|
|
if (SDL_strcmp(deviceExtensions[i].extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0) {
|
2024-09-18 17:52:28 +03:00
|
|
|
hasSwapchainExtension = true;
|
2017-08-28 05:15:57 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!hasSwapchainExtension) {
|
2017-08-28 05:15:57 +03:00
|
|
|
continue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->physicalDevice = physicalDevice;
|
2017-08-28 05:15:57 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
SDL_free(physicalDevices);
|
|
|
|
SDL_free(queueFamiliesProperties);
|
|
|
|
SDL_free(deviceExtensions);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vulkanContext->physicalDevice) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Vulkan: no viable physical devices found");
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createDevice(void)
|
|
|
|
{
|
2024-02-28 19:54:54 +03:00
|
|
|
VkDeviceQueueCreateInfo deviceQueueCreateInfo[2] = { { 0 }, { 0 } };
|
2022-11-30 23:51:59 +03:00
|
|
|
static const float queuePriority[] = { 1.0f };
|
|
|
|
VkDeviceCreateInfo deviceCreateInfo = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
static const char *const deviceExtensionNames[] = {
|
|
|
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
2024-07-23 18:17:22 +03:00
|
|
|
#ifdef __APPLE__
|
|
|
|
"VK_KHR_portability_subset"
|
|
|
|
#endif
|
2017-08-28 05:15:57 +03:00
|
|
|
};
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
|
2024-02-28 19:54:54 +03:00
|
|
|
deviceCreateInfo.queueCreateInfoCount = 0;
|
2017-08-28 05:15:57 +03:00
|
|
|
deviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfo;
|
|
|
|
deviceCreateInfo.pEnabledFeatures = NULL;
|
|
|
|
deviceCreateInfo.enabledExtensionCount = SDL_arraysize(deviceExtensionNames);
|
|
|
|
deviceCreateInfo.ppEnabledExtensionNames = deviceExtensionNames;
|
2024-02-28 19:54:54 +03:00
|
|
|
|
|
|
|
deviceQueueCreateInfo[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
deviceQueueCreateInfo[0].queueFamilyIndex = vulkanContext->graphicsQueueFamilyIndex;
|
|
|
|
deviceQueueCreateInfo[0].queueCount = 1;
|
|
|
|
deviceQueueCreateInfo[0].pQueuePriorities = queuePriority;
|
|
|
|
++deviceCreateInfo.queueCreateInfoCount;
|
|
|
|
|
|
|
|
if (vulkanContext->presentQueueFamilyIndex != vulkanContext->graphicsQueueFamilyIndex) {
|
|
|
|
deviceQueueCreateInfo[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
|
|
|
deviceQueueCreateInfo[1].queueFamilyIndex = vulkanContext->presentQueueFamilyIndex;
|
|
|
|
deviceQueueCreateInfo[1].queueCount = 1;
|
|
|
|
deviceQueueCreateInfo[1].pQueuePriorities = queuePriority;
|
|
|
|
++deviceCreateInfo.queueCreateInfoCount;
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkCreateDevice(vulkanContext->physicalDevice, &deviceCreateInfo, NULL, &vulkanContext->device);
|
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->device = VK_NULL_HANDLE;
|
2021-08-24 19:41:41 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkCreateDevice(): %s\n", getVulkanResultString(result));
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void loadDeviceFunctions(void)
|
|
|
|
{
|
|
|
|
#define VULKAN_DEVICE_FUNCTION(name) \
|
2021-08-24 19:41:41 +03:00
|
|
|
name = (PFN_##name)vkGetDeviceProcAddr(vulkanContext->device, #name); \
|
|
|
|
if (!name) { \
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \
|
|
|
|
"vkGetDeviceProcAddr(device, \"" #name "\") failed\n"); \
|
|
|
|
quit(2); \
|
|
|
|
}
|
|
|
|
#define VULKAN_GLOBAL_FUNCTION(name)
|
|
|
|
#define VULKAN_INSTANCE_FUNCTION(name)
|
|
|
|
VULKAN_FUNCTIONS()
|
|
|
|
#undef VULKAN_DEVICE_FUNCTION
|
|
|
|
#undef VULKAN_GLOBAL_FUNCTION
|
|
|
|
#undef VULKAN_INSTANCE_FUNCTION
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef VULKAN_FUNCTIONS
|
|
|
|
|
|
|
|
static void getQueues(void)
|
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
vkGetDeviceQueue(vulkanContext->device,
|
|
|
|
vulkanContext->graphicsQueueFamilyIndex,
|
2017-08-28 05:15:57 +03:00
|
|
|
0,
|
2021-08-24 03:18:57 +03:00
|
|
|
&vulkanContext->graphicsQueue);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (vulkanContext->graphicsQueueFamilyIndex != vulkanContext->presentQueueFamilyIndex) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkGetDeviceQueue(vulkanContext->device,
|
|
|
|
vulkanContext->presentQueueFamilyIndex,
|
2017-08-28 05:15:57 +03:00
|
|
|
0,
|
2021-08-24 03:18:57 +03:00
|
|
|
&vulkanContext->presentQueue);
|
2021-08-24 19:41:41 +03:00
|
|
|
} else {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->presentQueue = vulkanContext->graphicsQueue;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void createSemaphore(VkSemaphore *semaphore)
|
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
VkSemaphoreCreateInfo createInfo = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
2021-08-24 03:18:57 +03:00
|
|
|
result = vkCreateSemaphore(vulkanContext->device, &createInfo, NULL, semaphore);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
*semaphore = VK_NULL_HANDLE;
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkCreateSemaphore(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createSemaphores(void)
|
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
createSemaphore(&vulkanContext->imageAvailableSemaphore);
|
|
|
|
createSemaphore(&vulkanContext->renderingFinishedSemaphore);
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void getSurfaceCaps(void)
|
|
|
|
{
|
2021-08-24 19:41:41 +03:00
|
|
|
VkResult result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkanContext->physicalDevice, vulkanContext->surface, &vulkanContext->surfaceCapabilities);
|
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkGetPhysicalDeviceSurfaceCapabilitiesKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check surface usage
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!(vulkanContext->surfaceCapabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"Vulkan surface doesn't support VK_IMAGE_USAGE_TRANSFER_DST_BIT\n");
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getSurfaceFormats(void)
|
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(vulkanContext->physicalDevice,
|
|
|
|
vulkanContext->surface,
|
|
|
|
&vulkanContext->surfaceFormatsCount,
|
2017-08-28 05:15:57 +03:00
|
|
|
NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormatsCount = 0;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
if (vulkanContext->surfaceFormatsCount > vulkanContext->surfaceFormatsAllocatedCount) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormatsAllocatedCount = vulkanContext->surfaceFormatsCount;
|
|
|
|
SDL_free(vulkanContext->surfaceFormats);
|
2022-11-30 23:51:59 +03:00
|
|
|
vulkanContext->surfaceFormats = (VkSurfaceFormatKHR *)SDL_malloc(sizeof(VkSurfaceFormatKHR) * vulkanContext->surfaceFormatsAllocatedCount);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vulkanContext->surfaceFormats) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormatsCount = 0;
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
result = vkGetPhysicalDeviceSurfaceFormatsKHR(vulkanContext->physicalDevice,
|
|
|
|
vulkanContext->surface,
|
|
|
|
&vulkanContext->surfaceFormatsCount,
|
|
|
|
vulkanContext->surfaceFormats);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormatsCount = 0;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkGetPhysicalDeviceSurfaceFormatsKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void getSwapchainImages(void)
|
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->swapchainImages);
|
|
|
|
vulkanContext->swapchainImages = NULL;
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkGetSwapchainImagesKHR(vulkanContext->device, vulkanContext->swapchain, &vulkanContext->swapchainImageCount, NULL);
|
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->swapchainImageCount = 0;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkGetSwapchainImagesKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->swapchainImages = SDL_malloc(sizeof(VkImage) * vulkanContext->swapchainImageCount);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vulkanContext->swapchainImages) {
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
result = vkGetSwapchainImagesKHR(vulkanContext->device,
|
|
|
|
vulkanContext->swapchain,
|
|
|
|
&vulkanContext->swapchainImageCount,
|
|
|
|
vulkanContext->swapchainImages);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->swapchainImages);
|
|
|
|
vulkanContext->swapchainImages = NULL;
|
|
|
|
vulkanContext->swapchainImageCount = 0;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkGetSwapchainImagesKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
static bool createSwapchain(void)
|
2017-08-28 05:15:57 +03:00
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
uint32_t i;
|
|
|
|
int w, h;
|
2022-11-30 23:51:59 +03:00
|
|
|
VkSwapchainCreateInfoKHR createInfo = { 0 };
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2024-03-03 22:30:57 +03:00
|
|
|
SDL_WindowFlags flags;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
// pick an image count
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.minImageCount + 1;
|
2022-11-30 23:51:59 +03:00
|
|
|
if ((vulkanContext->swapchainDesiredImageCount > vulkanContext->surfaceCapabilities.maxImageCount) &&
|
|
|
|
(vulkanContext->surfaceCapabilities.maxImageCount > 0)) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->swapchainDesiredImageCount = vulkanContext->surfaceCapabilities.maxImageCount;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
// pick a format
|
2022-11-30 23:51:59 +03:00
|
|
|
if ((vulkanContext->surfaceFormatsCount == 1) &&
|
|
|
|
(vulkanContext->surfaceFormats[0].format == VK_FORMAT_UNDEFINED)) {
|
2017-08-28 05:15:57 +03:00
|
|
|
// aren't any preferred formats, so we pick
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormat.colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
|
|
|
|
vulkanContext->surfaceFormat.format = VK_FORMAT_R8G8B8A8_UNORM;
|
2021-08-24 19:41:41 +03:00
|
|
|
} else {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[0];
|
2021-08-24 19:41:41 +03:00
|
|
|
for (i = 0; i < vulkanContext->surfaceFormatsCount; i++) {
|
2022-11-27 19:38:43 +03:00
|
|
|
if (vulkanContext->surfaceFormats[i].format == VK_FORMAT_R8G8B8A8_UNORM) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->surfaceFormat = vulkanContext->surfaceFormats[i];
|
2017-08-28 05:15:57 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get size
|
2023-01-29 05:43:03 +03:00
|
|
|
SDL_GetWindowSizeInPixels(vulkanContext->window, &w, &h);
|
2021-08-08 06:00:07 +03:00
|
|
|
|
2023-03-11 15:53:14 +03:00
|
|
|
// get flags
|
|
|
|
flags = SDL_GetWindowFlags(vulkanContext->window);
|
|
|
|
|
2021-08-08 06:00:07 +03:00
|
|
|
// Clamp the size to the allowable image extent.
|
2023-01-29 05:43:03 +03:00
|
|
|
// SDL_GetWindowSizeInPixels()'s result it not always in this range (bug #3287)
|
2022-11-30 23:51:59 +03:00
|
|
|
vulkanContext->swapchainSize.width = SDL_clamp((uint32_t)w,
|
|
|
|
vulkanContext->surfaceCapabilities.minImageExtent.width,
|
|
|
|
vulkanContext->surfaceCapabilities.maxImageExtent.width);
|
2021-08-08 06:00:07 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
vulkanContext->swapchainSize.height = SDL_clamp((uint32_t)h,
|
|
|
|
vulkanContext->surfaceCapabilities.minImageExtent.height,
|
|
|
|
vulkanContext->surfaceCapabilities.maxImageExtent.height);
|
2021-08-08 06:00:07 +03:00
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
if (w == 0 || h == 0) {
|
2024-09-18 17:52:28 +03:00
|
|
|
return false;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2021-08-17 16:52:49 +03:00
|
|
|
getSurfaceCaps();
|
|
|
|
|
2017-08-28 05:15:57 +03:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
2021-08-24 03:18:57 +03:00
|
|
|
createInfo.surface = vulkanContext->surface;
|
|
|
|
createInfo.minImageCount = vulkanContext->swapchainDesiredImageCount;
|
|
|
|
createInfo.imageFormat = vulkanContext->surfaceFormat.format;
|
|
|
|
createInfo.imageColorSpace = vulkanContext->surfaceFormat.colorSpace;
|
|
|
|
createInfo.imageExtent = vulkanContext->swapchainSize;
|
2017-08-28 05:15:57 +03:00
|
|
|
createInfo.imageArrayLayers = 1;
|
|
|
|
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
|
|
|
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2021-08-24 03:18:57 +03:00
|
|
|
createInfo.preTransform = vulkanContext->surfaceCapabilities.currentTransform;
|
2023-03-11 15:53:14 +03:00
|
|
|
if (flags & SDL_WINDOW_TRANSPARENT) {
|
|
|
|
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_PRE_MULTIPLIED_BIT_KHR;
|
|
|
|
} else {
|
|
|
|
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
|
|
|
createInfo.clipped = VK_TRUE;
|
2021-08-24 03:18:57 +03:00
|
|
|
createInfo.oldSwapchain = vulkanContext->swapchain;
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkCreateSwapchainKHR(vulkanContext->device, &createInfo, NULL, &vulkanContext->swapchain);
|
|
|
|
|
|
|
|
if (createInfo.oldSwapchain) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroySwapchainKHR(vulkanContext->device, createInfo.oldSwapchain, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
2022-11-27 19:38:43 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->swapchain = VK_NULL_HANDLE;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkCreateSwapchainKHR(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
|
2017-08-28 05:15:57 +03:00
|
|
|
getSwapchainImages();
|
2024-09-18 17:52:28 +03:00
|
|
|
return true;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroySwapchain(void)
|
|
|
|
{
|
2021-08-24 19:41:41 +03:00
|
|
|
if (vulkanContext->swapchain) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroySwapchainKHR(vulkanContext->device, vulkanContext->swapchain, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
vulkanContext->swapchain = VK_NULL_HANDLE;
|
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->swapchainImages);
|
|
|
|
vulkanContext->swapchainImages = NULL;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroyCommandBuffers(void)
|
|
|
|
{
|
2021-08-24 19:41:41 +03:00
|
|
|
if (vulkanContext->commandBuffers) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkFreeCommandBuffers(vulkanContext->device,
|
|
|
|
vulkanContext->commandPool,
|
|
|
|
vulkanContext->swapchainImageCount,
|
|
|
|
vulkanContext->commandBuffers);
|
2021-08-24 19:41:41 +03:00
|
|
|
SDL_free(vulkanContext->commandBuffers);
|
|
|
|
vulkanContext->commandBuffers = NULL;
|
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroyCommandPool(void)
|
|
|
|
{
|
2021-08-24 19:41:41 +03:00
|
|
|
if (vulkanContext->commandPool) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroyCommandPool(vulkanContext->device, vulkanContext->commandPool, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->commandPool = VK_NULL_HANDLE;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void createCommandPool(void)
|
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2022-11-30 23:51:59 +03:00
|
|
|
VkCommandPoolCreateInfo createInfo = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
2021-08-24 19:41:41 +03:00
|
|
|
createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
|
2021-08-24 03:18:57 +03:00
|
|
|
createInfo.queueFamilyIndex = vulkanContext->graphicsQueueFamilyIndex;
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkCreateCommandPool(vulkanContext->device, &createInfo, NULL, &vulkanContext->commandPool);
|
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->commandPool = VK_NULL_HANDLE;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkCreateCommandPool(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createCommandBuffers(void)
|
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2022-11-30 23:51:59 +03:00
|
|
|
VkCommandBufferAllocateInfo allocateInfo = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
2021-08-24 03:18:57 +03:00
|
|
|
allocateInfo.commandPool = vulkanContext->commandPool;
|
2017-08-28 05:15:57 +03:00
|
|
|
allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
2021-08-24 03:18:57 +03:00
|
|
|
allocateInfo.commandBufferCount = vulkanContext->swapchainImageCount;
|
2022-11-30 23:51:59 +03:00
|
|
|
vulkanContext->commandBuffers = (VkCommandBuffer *)SDL_malloc(sizeof(VkCommandBuffer) * vulkanContext->swapchainImageCount);
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkAllocateCommandBuffers(vulkanContext->device, &allocateInfo, vulkanContext->commandBuffers);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->commandBuffers);
|
|
|
|
vulkanContext->commandBuffers = NULL;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkAllocateCommandBuffers(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void createFences(void)
|
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
uint32_t i;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2021-08-24 03:18:57 +03:00
|
|
|
vulkanContext->fences = SDL_malloc(sizeof(VkFence) * vulkanContext->swapchainImageCount);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vulkanContext->fences) {
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
for (i = 0; i < vulkanContext->swapchainImageCount; i++) {
|
2020-12-24 00:28:50 +03:00
|
|
|
VkResult result;
|
2022-11-30 23:51:59 +03:00
|
|
|
VkFenceCreateInfo createInfo = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
createInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
|
|
|
createInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
2021-08-24 19:41:41 +03:00
|
|
|
result = vkCreateFence(vulkanContext->device, &createInfo, NULL, &vulkanContext->fences[i]);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
|
|
|
for (; i > 0; i--) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroyFence(vulkanContext->device, vulkanContext->fences[i - 1], NULL);
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->fences);
|
|
|
|
vulkanContext->fences = NULL;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkCreateFence(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroyFences(void)
|
|
|
|
{
|
2020-12-24 00:28:50 +03:00
|
|
|
uint32_t i;
|
2017-08-28 05:15:57 +03:00
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vulkanContext->fences) {
|
2017-08-28 05:15:57 +03:00
|
|
|
return;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < vulkanContext->swapchainImageCount; i++) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroyFence(vulkanContext->device, vulkanContext->fences[i], NULL);
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->fences);
|
|
|
|
vulkanContext->fences = NULL;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void recordPipelineImageBarrier(VkCommandBuffer commandBuffer,
|
|
|
|
VkAccessFlags sourceAccessMask,
|
|
|
|
VkAccessFlags destAccessMask,
|
|
|
|
VkImageLayout sourceLayout,
|
|
|
|
VkImageLayout destLayout,
|
|
|
|
VkImage image)
|
|
|
|
{
|
2022-11-30 23:51:59 +03:00
|
|
|
VkImageMemoryBarrier barrier = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
|
|
barrier.srcAccessMask = sourceAccessMask;
|
|
|
|
barrier.dstAccessMask = destAccessMask;
|
|
|
|
barrier.oldLayout = sourceLayout;
|
|
|
|
barrier.newLayout = destLayout;
|
|
|
|
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
barrier.image = image;
|
|
|
|
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
barrier.subresourceRange.baseMipLevel = 0;
|
|
|
|
barrier.subresourceRange.levelCount = 1;
|
|
|
|
barrier.subresourceRange.baseArrayLayer = 0;
|
|
|
|
barrier.subresourceRange.layerCount = 1;
|
|
|
|
vkCmdPipelineBarrier(commandBuffer,
|
|
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
1,
|
|
|
|
&barrier);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rerecordCommandBuffer(uint32_t frameIndex, const VkClearColorValue *clearColor)
|
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
VkCommandBuffer commandBuffer = vulkanContext->commandBuffers[frameIndex];
|
|
|
|
VkImage image = vulkanContext->swapchainImages[frameIndex];
|
2022-11-30 23:51:59 +03:00
|
|
|
VkCommandBufferBeginInfo beginInfo = { 0 };
|
|
|
|
VkImageSubresourceRange clearRange = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
|
|
|
|
VkResult result = vkResetCommandBuffer(commandBuffer, 0);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkResetCommandBuffer(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
|
|
|
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
|
|
|
|
result = vkBeginCommandBuffer(commandBuffer, &beginInfo);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkBeginCommandBuffer(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
recordPipelineImageBarrier(commandBuffer,
|
|
|
|
0,
|
|
|
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
image);
|
|
|
|
clearRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
clearRange.baseMipLevel = 0;
|
|
|
|
clearRange.levelCount = 1;
|
|
|
|
clearRange.baseArrayLayer = 0;
|
|
|
|
clearRange.layerCount = 1;
|
2021-08-24 19:41:41 +03:00
|
|
|
vkCmdClearColorImage(commandBuffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, clearColor, 1, &clearRange);
|
2017-08-28 05:15:57 +03:00
|
|
|
recordPipelineImageBarrier(commandBuffer,
|
|
|
|
VK_ACCESS_TRANSFER_WRITE_BIT,
|
|
|
|
VK_ACCESS_MEMORY_READ_BIT,
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
|
|
|
|
image);
|
|
|
|
result = vkEndCommandBuffer(commandBuffer);
|
2021-08-24 19:41:41 +03:00
|
|
|
if (result != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkEndCommandBuffer(): %s\n",
|
|
|
|
getVulkanResultString(result));
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
static void destroySwapchainAndSwapchainSpecificStuff(bool doDestroySwapchain)
|
2017-08-28 05:15:57 +03:00
|
|
|
{
|
2022-12-02 00:07:03 +03:00
|
|
|
if (vkDeviceWaitIdle != NULL) {
|
|
|
|
vkDeviceWaitIdle(vulkanContext->device);
|
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
destroyFences();
|
|
|
|
destroyCommandBuffers();
|
|
|
|
destroyCommandPool();
|
2021-08-24 19:41:41 +03:00
|
|
|
if (doDestroySwapchain) {
|
2017-08-28 05:15:57 +03:00
|
|
|
destroySwapchain();
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
static bool createNewSwapchainAndSwapchainSpecificStuff(void)
|
2017-08-28 05:15:57 +03:00
|
|
|
{
|
2024-09-18 17:52:28 +03:00
|
|
|
destroySwapchainAndSwapchainSpecificStuff(false);
|
2017-08-28 05:15:57 +03:00
|
|
|
getSurfaceCaps();
|
|
|
|
getSurfaceFormats();
|
2022-11-27 19:38:43 +03:00
|
|
|
if (!createSwapchain()) {
|
2024-09-18 17:52:28 +03:00
|
|
|
return false;
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
createCommandPool();
|
|
|
|
createCommandBuffers();
|
|
|
|
createFences();
|
2024-09-18 17:52:28 +03:00
|
|
|
return true;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void initVulkan(void)
|
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
int i;
|
|
|
|
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_Vulkan_LoadLibrary(NULL);
|
2021-08-24 03:18:57 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
vulkanContexts = (VulkanContext *)SDL_calloc(state->num_windows, sizeof(VulkanContext));
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!vulkanContexts) {
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
|
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
vulkanContext = &vulkanContexts[i];
|
|
|
|
vulkanContext->window = state->windows[i];
|
|
|
|
loadGlobalFunctions();
|
|
|
|
createInstance();
|
|
|
|
loadInstanceFunctions();
|
|
|
|
createSurface();
|
|
|
|
findPhysicalDevice();
|
|
|
|
createDevice();
|
|
|
|
loadDeviceFunctions();
|
|
|
|
getQueues();
|
|
|
|
createSemaphores();
|
|
|
|
createNewSwapchainAndSwapchainSpecificStuff();
|
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
static void shutdownVulkan(bool doDestroySwapchain)
|
2017-08-28 05:15:57 +03:00
|
|
|
{
|
2021-08-24 03:18:57 +03:00
|
|
|
if (vulkanContexts) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
vulkanContext = &vulkanContexts[i];
|
2021-08-24 19:41:41 +03:00
|
|
|
if (vulkanContext->device && vkDeviceWaitIdle) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDeviceWaitIdle(vulkanContext->device);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
2021-08-17 16:52:49 +03:00
|
|
|
destroySwapchainAndSwapchainSpecificStuff(doDestroySwapchain);
|
2021-08-24 19:41:41 +03:00
|
|
|
|
|
|
|
if (vulkanContext->imageAvailableSemaphore && vkDestroySemaphore) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroySemaphore(vulkanContext->device, vulkanContext->imageAvailableSemaphore, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vulkanContext->renderingFinishedSemaphore && vkDestroySemaphore) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroySemaphore(vulkanContext->device, vulkanContext->renderingFinishedSemaphore, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vulkanContext->device && vkDestroyDevice) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroyDevice(vulkanContext->device, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vulkanContext->surface && vkDestroySurfaceKHR) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroySurfaceKHR(vulkanContext->instance, vulkanContext->surface, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vulkanContext->instance && vkDestroyInstance) {
|
2021-08-24 03:18:57 +03:00
|
|
|
vkDestroyInstance(vulkanContext->instance, NULL);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 03:18:57 +03:00
|
|
|
SDL_free(vulkanContext->surfaceFormats);
|
|
|
|
}
|
|
|
|
SDL_free(vulkanContexts);
|
|
|
|
vulkanContexts = NULL;
|
|
|
|
}
|
|
|
|
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_Vulkan_UnloadLibrary();
|
|
|
|
}
|
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
static bool render(void)
|
2017-08-28 05:15:57 +03:00
|
|
|
{
|
|
|
|
uint32_t frameIndex;
|
2024-08-23 03:33:49 +03:00
|
|
|
VkResult rc;
|
2017-08-28 05:15:57 +03:00
|
|
|
double currentTime;
|
2022-11-30 23:51:59 +03:00
|
|
|
VkClearColorValue clearColor = { { 0 } };
|
2017-08-28 05:15:57 +03:00
|
|
|
VkPipelineStageFlags waitDestStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT;
|
2022-11-30 23:51:59 +03:00
|
|
|
VkSubmitInfo submitInfo = { 0 };
|
|
|
|
VkPresentInfoKHR presentInfo = { 0 };
|
2017-08-28 05:15:57 +03:00
|
|
|
int w, h;
|
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
if (!vulkanContext->swapchain) {
|
2024-09-18 17:52:28 +03:00
|
|
|
bool result = createNewSwapchainAndSwapchainSpecificStuff();
|
2024-08-23 03:33:49 +03:00
|
|
|
if (!result) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_Delay(100);
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2024-08-23 03:33:49 +03:00
|
|
|
return result;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
2024-08-23 03:33:49 +03:00
|
|
|
rc = vkAcquireNextImageKHR(vulkanContext->device,
|
|
|
|
vulkanContext->swapchain,
|
|
|
|
UINT64_MAX,
|
|
|
|
vulkanContext->imageAvailableSemaphore,
|
|
|
|
VK_NULL_HANDLE,
|
|
|
|
&frameIndex);
|
|
|
|
if (rc == VK_ERROR_OUT_OF_DATE_KHR) {
|
2017-08-28 05:15:57 +03:00
|
|
|
return createNewSwapchainAndSwapchainSpecificStuff();
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
|
|
|
|
2024-08-23 03:33:49 +03:00
|
|
|
if ((rc != VK_SUBOPTIMAL_KHR) && (rc != VK_SUCCESS)) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkAcquireNextImageKHR(): %s\n",
|
2024-08-23 03:33:49 +03:00
|
|
|
getVulkanResultString(rc));
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
2024-08-23 03:33:49 +03:00
|
|
|
rc = vkWaitForFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex], VK_FALSE, UINT64_MAX);
|
|
|
|
if (rc != VK_SUCCESS) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkWaitForFences(): %s\n", getVulkanResultString(rc));
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
2024-08-23 03:33:49 +03:00
|
|
|
rc = vkResetFences(vulkanContext->device, 1, &vulkanContext->fences[frameIndex]);
|
|
|
|
if (rc != VK_SUCCESS) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkResetFences(): %s\n", getVulkanResultString(rc));
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
|
|
|
|
clearColor.float32[0] = (float)(0.5 + 0.5 * SDL_sin(currentTime));
|
2022-11-25 22:35:24 +03:00
|
|
|
clearColor.float32[1] = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 2 / 3));
|
|
|
|
clearColor.float32[2] = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 4 / 3));
|
2023-03-11 15:53:14 +03:00
|
|
|
clearColor.float32[3] = 0.5; // for SDL_WINDOW_TRANSPARENT, ignored with VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
|
2017-08-28 05:15:57 +03:00
|
|
|
rerecordCommandBuffer(frameIndex, &clearColor);
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
|
|
|
submitInfo.waitSemaphoreCount = 1;
|
2021-08-24 03:18:57 +03:00
|
|
|
submitInfo.pWaitSemaphores = &vulkanContext->imageAvailableSemaphore;
|
2017-08-28 05:15:57 +03:00
|
|
|
submitInfo.pWaitDstStageMask = &waitDestStageMask;
|
|
|
|
submitInfo.commandBufferCount = 1;
|
2021-08-24 03:18:57 +03:00
|
|
|
submitInfo.pCommandBuffers = &vulkanContext->commandBuffers[frameIndex];
|
2017-08-28 05:15:57 +03:00
|
|
|
submitInfo.signalSemaphoreCount = 1;
|
2021-08-24 03:18:57 +03:00
|
|
|
submitInfo.pSignalSemaphores = &vulkanContext->renderingFinishedSemaphore;
|
2024-08-23 03:33:49 +03:00
|
|
|
rc = vkQueueSubmit(vulkanContext->graphicsQueue, 1, &submitInfo, vulkanContext->fences[frameIndex]);
|
2021-08-24 19:41:41 +03:00
|
|
|
|
2024-08-23 03:33:49 +03:00
|
|
|
if (rc != VK_SUCCESS) {
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "vkQueueSubmit(): %s\n", getVulkanResultString(rc));
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
|
|
|
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
|
|
|
presentInfo.waitSemaphoreCount = 1;
|
2021-08-24 03:18:57 +03:00
|
|
|
presentInfo.pWaitSemaphores = &vulkanContext->renderingFinishedSemaphore;
|
2017-08-28 05:15:57 +03:00
|
|
|
presentInfo.swapchainCount = 1;
|
2021-08-24 03:18:57 +03:00
|
|
|
presentInfo.pSwapchains = &vulkanContext->swapchain;
|
2017-08-28 05:15:57 +03:00
|
|
|
presentInfo.pImageIndices = &frameIndex;
|
2024-08-23 03:33:49 +03:00
|
|
|
rc = vkQueuePresentKHR(vulkanContext->presentQueue, &presentInfo);
|
|
|
|
if ((rc == VK_ERROR_OUT_OF_DATE_KHR) || (rc == VK_SUBOPTIMAL_KHR)) {
|
2017-08-28 05:15:57 +03:00
|
|
|
return createNewSwapchainAndSwapchainSpecificStuff();
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
|
2024-08-23 03:33:49 +03:00
|
|
|
if (rc != VK_SUCCESS) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
|
|
|
|
"vkQueuePresentKHR(): %s\n",
|
2024-08-23 03:33:49 +03:00
|
|
|
getVulkanResultString(rc));
|
2017-08-28 05:15:57 +03:00
|
|
|
quit(2);
|
|
|
|
}
|
2023-01-29 05:43:03 +03:00
|
|
|
SDL_GetWindowSizeInPixels(vulkanContext->window, &w, &h);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (w != (int)vulkanContext->swapchainSize.width || h != (int)vulkanContext->swapchainSize.height) {
|
2017-08-28 05:15:57 +03:00
|
|
|
return createNewSwapchainAndSwapchainSpecificStuff();
|
|
|
|
}
|
2024-09-18 17:52:28 +03:00
|
|
|
return true;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 19:41:41 +03:00
|
|
|
int main(int argc, char **argv)
|
2017-08-28 05:15:57 +03:00
|
|
|
{
|
2019-05-19 08:45:15 +03:00
|
|
|
int done;
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 08:23:14 +03:00
|
|
|
const SDL_DisplayMode *mode;
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_Event event;
|
2022-12-02 12:17:17 +03:00
|
|
|
Uint64 then, now;
|
|
|
|
Uint32 frames;
|
2017-08-28 05:15:57 +03:00
|
|
|
int dw, dh;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!state) {
|
2017-08-28 05:15:57 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set Vulkan parameters */
|
|
|
|
state->window_flags |= SDL_WINDOW_VULKAN;
|
|
|
|
state->skip_renderer = 1;
|
|
|
|
|
2019-05-19 08:45:15 +03:00
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
|
|
|
|
SDLTest_CommonQuit(state);
|
|
|
|
return 1;
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 08:23:14 +03:00
|
|
|
mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
|
|
|
|
if (mode) {
|
2024-03-07 16:20:20 +03:00
|
|
|
SDL_Log("Screen BPP : %d\n", SDL_BITSPERPIXEL(mode->format));
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 08:23:14 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_GetWindowSize(state->windows[0], &dw, &dh);
|
|
|
|
SDL_Log("Window Size : %d,%d\n", dw, dh);
|
2023-01-29 05:43:03 +03:00
|
|
|
SDL_GetWindowSizeInPixels(state->windows[0], &dw, &dh);
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_Log("Draw Size : %d,%d\n", dw, dh);
|
|
|
|
SDL_Log("\n");
|
|
|
|
|
|
|
|
initVulkan();
|
|
|
|
|
|
|
|
/* Main render loop */
|
|
|
|
frames = 0;
|
|
|
|
then = SDL_GetTicks();
|
|
|
|
done = 0;
|
2021-08-24 19:41:41 +03:00
|
|
|
while (!done) {
|
2017-08-28 05:15:57 +03:00
|
|
|
/* Check for events */
|
2021-08-24 19:41:41 +03:00
|
|
|
frames++;
|
2022-11-27 19:38:43 +03:00
|
|
|
while (SDL_PollEvent(&event)) {
|
2021-08-17 16:52:49 +03:00
|
|
|
/* Need to destroy the swapchain before the window created
|
|
|
|
* by SDL.
|
|
|
|
*/
|
2023-01-24 04:54:09 +03:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) {
|
2024-09-18 17:52:28 +03:00
|
|
|
destroySwapchainAndSwapchainSpecificStuff(true);
|
2021-08-24 19:41:41 +03:00
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
}
|
|
|
|
|
2021-08-24 03:18:57 +03:00
|
|
|
if (!done) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
if (state->windows[i]) {
|
|
|
|
vulkanContext = &vulkanContexts[i];
|
|
|
|
render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 05:15:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Print out some timing information */
|
|
|
|
now = SDL_GetTicks();
|
2021-08-24 19:41:41 +03:00
|
|
|
if (now > then) {
|
2017-08-28 05:15:57 +03:00
|
|
|
SDL_Log("%2.2f frames per second\n", ((double)frames * 1000) / (now - then));
|
|
|
|
}
|
2021-08-24 19:41:41 +03:00
|
|
|
|
2024-09-18 17:52:28 +03:00
|
|
|
shutdownVulkan(true);
|
2021-08-17 16:52:49 +03:00
|
|
|
SDLTest_CommonQuit(state);
|
2017-08-28 05:15:57 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|