Compare commits

..

No commits in common. "e62b5b919ce4a46bddda1fd106a7f91d4b4bed99" and "fabd31448bf642f7a4800b34c212cdf453ec10f8" have entirely different histories.

3 changed files with 107 additions and 171 deletions

View File

@ -9,7 +9,7 @@ option(BUILD_SHARED_LIBS "Build shared library" ON)
# Version
SET(LIB_MAJOR_VERSION "1")
SET(LIB_MINOR_VERSION "8")
SET(LIB_REVISION "2")
SET(LIB_REVISION "0")
SET(LIB_VERSION "${LIB_MAJOR_VERSION}.${LIB_MINOR_VERSION}.${LIB_REVISION}")
# Build Type

View File

@ -56,7 +56,7 @@ extern "C" {
#define REFRESH_MAJOR_VERSION 1
#define REFRESH_MINOR_VERSION 8
#define REFRESH_PATCH_VERSION 2
#define REFRESH_PATCH_VERSION 0
#define REFRESH_COMPILED_VERSION ( \
(REFRESH_MAJOR_VERSION * 100 * 100) + \

View File

@ -77,7 +77,7 @@ static uint32_t deviceExtensionCount = SDL_arraysize(deviceExtensionNames);
#define UBO_BUFFER_SIZE 16000 /* 16KB */
#define DESCRIPTOR_POOL_STARTING_SIZE 128
#define DESCRIPTOR_SET_DEACTIVATE_FRAMES 10
#define WINDOW_DATA "Refresh_VulkanWindowData"
#define WINDOW_SWAPCHAIN_DATA "Refresh_VulkanSwapchain"
#define IDENTITY_SWIZZLE \
{ \
@ -789,6 +789,7 @@ typedef struct VulkanSwapchainData
/* Window surface */
VkSurfaceKHR surface;
VkSurfaceFormatKHR surfaceFormat;
void *windowHandle;
/* Swapchain for window surface */
VkSwapchainKHR swapchain;
@ -806,13 +807,6 @@ typedef struct VulkanSwapchainData
VkSemaphore renderFinishedSemaphore;
} VulkanSwapchainData;
typedef struct WindowData
{
void *windowHandle;
VkPresentModeKHR preferredPresentMode;
VulkanSwapchainData *swapchainData;
} WindowData;
typedef struct SwapChainSupportDetails
{
VkSurfaceCapabilitiesKHR capabilities;
@ -824,7 +818,7 @@ typedef struct SwapChainSupportDetails
typedef struct VulkanPresentData
{
WindowData *windowData;
VulkanSwapchainData *swapchainData;
uint32_t swapchainImageIndex;
} VulkanPresentData;
@ -1687,9 +1681,9 @@ typedef struct VulkanRenderer
VulkanMemoryAllocator *memoryAllocator;
VkPhysicalDeviceMemoryProperties memoryProperties;
WindowData **claimedWindows;
uint32_t claimedWindowCount;
uint32_t claimedWindowCapacity;
VulkanSwapchainData **swapchainDatas;
uint32_t swapchainDataCount;
uint32_t swapchainDataCapacity;
QueueFamilyIndices queueFamilyIndices;
VkQueue graphicsQueue;
@ -1786,7 +1780,6 @@ typedef struct VulkanRenderer
/* Forward declarations */
static void VULKAN_INTERNAL_BeginCommandBuffer(VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer);
static void VULKAN_UnclaimWindow(Refresh_Renderer *driverData, void *windowHandle);
static void VULKAN_Wait(Refresh_Renderer *driverData);
static void VULKAN_Submit(Refresh_Renderer *driverData, uint32_t commandBufferCount, Refresh_CommandBuffer **pCommandBuffers);
static void VULKAN_INTERNAL_DestroyRenderTarget(VulkanRenderer *renderer, VulkanRenderTarget *renderTarget);
@ -3230,17 +3223,12 @@ static void VULKAN_INTERNAL_DestroySampler(
static void VULKAN_INTERNAL_DestroySwapchain(
VulkanRenderer* renderer,
WindowData *windowData
void *windowHandle
) {
uint32_t i;
VulkanSwapchainData *swapchainData;
if (windowData == NULL)
{
return;
}
swapchainData = windowData->swapchainData;
swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA);
if (swapchainData == NULL)
{
@ -3287,7 +3275,17 @@ static void VULKAN_INTERNAL_DestroySwapchain(
NULL
);
windowData->swapchainData = NULL;
for (i = 0; i < renderer->swapchainDataCount; i += 1)
{
if (windowHandle == renderer->swapchainDatas[i]->windowHandle)
{
renderer->swapchainDatas[i] = renderer->swapchainDatas[renderer->swapchainDataCount - 1];
renderer->swapchainDataCount -= 1;
break;
}
}
SDL_SetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA, NULL);
SDL_free(swapchainData);
}
@ -4252,7 +4250,8 @@ static uint8_t VULKAN_INTERNAL_ChooseSwapPresentMode(
static uint8_t VULKAN_INTERNAL_CreateSwapchain(
VulkanRenderer *renderer,
WindowData *windowData
void *windowHandle,
Refresh_PresentMode presentMode
) {
VkResult vulkanResult;
VulkanSwapchainData *swapchainData;
@ -4265,11 +4264,12 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain(
uint32_t i;
swapchainData = SDL_malloc(sizeof(VulkanSwapchainData));
swapchainData->windowHandle = windowHandle;
/* Each swapchain must have its own surface. */
if (!SDL_Vulkan_CreateSurface(
(SDL_Window*) windowData->windowHandle,
(SDL_Window*) windowHandle,
renderer->instance,
&swapchainData->surface
)) {
@ -4306,27 +4306,6 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain(
return 0;
}
if ( swapchainSupportDetails.capabilities.currentExtent.width == 0 ||
swapchainSupportDetails.capabilities.currentExtent.height == 0)
{
/* Not an error, just minimize behavior! */
renderer->vkDestroySurfaceKHR(
renderer->instance,
swapchainData->surface,
NULL
);
if (swapchainSupportDetails.formatsLength > 0)
{
SDL_free(swapchainSupportDetails.formats);
}
if (swapchainSupportDetails.presentModesLength > 0)
{
SDL_free(swapchainSupportDetails.presentModes);
}
SDL_free(swapchainData);
return 0;
}
swapchainData->swapchainFormat = VK_FORMAT_R8G8B8A8_UNORM;
swapchainData->swapchainSwizzle.r = VK_COMPONENT_SWIZZLE_IDENTITY;
swapchainData->swapchainSwizzle.g = VK_COMPONENT_SWIZZLE_IDENTITY;
@ -4371,7 +4350,7 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain(
}
if (!VULKAN_INTERNAL_ChooseSwapPresentMode(
windowData->preferredPresentMode,
presentMode,
swapchainSupportDetails.presentModes,
swapchainSupportDetails.presentModesLength,
&swapchainData->presentMode
@ -4395,7 +4374,7 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain(
}
SDL_Vulkan_GetDrawableSize(
(SDL_Window*) windowData->windowHandle,
(SDL_Window*) windowHandle,
&drawableWidth,
&drawableHeight
);
@ -4405,6 +4384,27 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain(
drawableHeight < swapchainSupportDetails.capabilities.minImageExtent.height ||
drawableHeight > swapchainSupportDetails.capabilities.maxImageExtent.height )
{
if ( swapchainSupportDetails.capabilities.currentExtent.width == 0 ||
swapchainSupportDetails.capabilities.currentExtent.height == 0)
{
renderer->vkDestroySurfaceKHR(
renderer->instance,
swapchainData->surface,
NULL
);
if (swapchainSupportDetails.formatsLength > 0)
{
SDL_free(swapchainSupportDetails.formats);
}
if (swapchainSupportDetails.presentModesLength > 0)
{
SDL_free(swapchainSupportDetails.presentModes);
}
SDL_free(swapchainData);
/* Not an error, just Windows minimize behavior! */
return 0;
}
if (swapchainSupportDetails.capabilities.currentExtent.width != UINT32_MAX)
{
drawableWidth = VULKAN_INTERNAL_clamp(
@ -4622,17 +4622,31 @@ static uint8_t VULKAN_INTERNAL_CreateSwapchain(
&swapchainData->renderFinishedSemaphore
);
windowData->swapchainData = swapchainData;
SDL_SetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA, swapchainData);
if (renderer->swapchainDataCount >= renderer->swapchainDataCapacity)
{
renderer->swapchainDataCapacity *= 2;
renderer->swapchainDatas = SDL_realloc(
renderer->swapchainDatas,
renderer->swapchainDataCapacity * sizeof(VulkanSwapchainData*)
);
}
renderer->swapchainDatas[renderer->swapchainDataCount] = swapchainData;
renderer->swapchainDataCount += 1;
return 1;
}
static void VULKAN_INTERNAL_RecreateSwapchain(
VulkanRenderer* renderer,
WindowData *windowData
void *windowHandle,
Refresh_PresentMode presentMode
) {
VULKAN_Wait((Refresh_Renderer*) renderer);
VULKAN_INTERNAL_DestroySwapchain(renderer, windowData);
VULKAN_INTERNAL_CreateSwapchain(renderer, windowData);
VULKAN_INTERNAL_DestroySwapchain(renderer, windowHandle);
VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle, presentMode);
}
/* Command Buffers */
@ -4705,12 +4719,12 @@ static void VULKAN_DestroyDevice(
VULKAN_Wait(device->driverData);
for (i = renderer->claimedWindowCount - 1; i >= 0; i -= 1)
for (i = renderer->swapchainDataCount - 1; i >= 0; i -= 1)
{
VULKAN_UnclaimWindow(device->driverData, renderer->claimedWindows[i]->windowHandle);
VULKAN_INTERNAL_DestroySwapchain(renderer, renderer->swapchainDatas[i]->windowHandle);
}
SDL_free(renderer->claimedWindows);
SDL_free(renderer->swapchainDatas);
VULKAN_Wait(device->driverData);
@ -9250,10 +9264,10 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer(
return (Refresh_CommandBuffer*) commandBuffer;
}
static WindowData* VULKAN_INTERNAL_FetchWindowData(
static VulkanSwapchainData* VULKAN_INTERNAL_FetchSwapchainData(
void *windowHandle
) {
return (WindowData*) SDL_GetWindowData(windowHandle, WINDOW_DATA);
return (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA);
}
static uint8_t VULKAN_ClaimWindow(
@ -9262,38 +9276,11 @@ static uint8_t VULKAN_ClaimWindow(
Refresh_PresentMode presentMode
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(windowHandle);
VulkanSwapchainData *swapchainData = VULKAN_INTERNAL_FetchSwapchainData(windowHandle);
if (windowData == NULL)
if (swapchainData == NULL)
{
windowData = SDL_malloc(sizeof(WindowData));
windowData->windowHandle = windowHandle;
windowData->preferredPresentMode = presentMode;
if (VULKAN_INTERNAL_CreateSwapchain(renderer, windowData))
{
SDL_SetWindowData((SDL_Window*) windowHandle, WINDOW_DATA, windowData);
if (renderer->claimedWindowCount >= renderer->claimedWindowCapacity)
{
renderer->claimedWindowCapacity *= 2;
renderer->claimedWindows = SDL_realloc(
renderer->claimedWindows,
renderer->claimedWindowCapacity * sizeof(WindowData*)
);
}
renderer->claimedWindows[renderer->claimedWindowCount] = windowData;
renderer->claimedWindowCount += 1;
return 1;
}
else
{
Refresh_LogError("Could not create swapchain, failed to claim window!");
SDL_free(windowData);
return 0;
}
return VULKAN_INTERNAL_CreateSwapchain(renderer, windowHandle, presentMode);
}
else
{
@ -9306,36 +9293,11 @@ static void VULKAN_UnclaimWindow(
Refresh_Renderer *driverData,
void *windowHandle
) {
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(windowHandle);
uint32_t i;
if (windowData == NULL)
{
return;
}
if (windowData->swapchainData != NULL)
{
VULKAN_Wait(driverData);
VULKAN_INTERNAL_DestroySwapchain(
(VulkanRenderer*) driverData,
windowData
);
}
for (i = 0; i < renderer->claimedWindowCount; i += 1)
{
if (renderer->claimedWindows[i]->windowHandle == windowHandle)
{
renderer->claimedWindows[i] = renderer->claimedWindows[renderer->claimedWindowCount - 1];
renderer->claimedWindowCount -= 1;
break;
}
}
SDL_free(windowData);
SDL_SetWindowData((SDL_Window*) windowHandle, WINDOW_DATA, NULL);
VULKAN_Wait(driverData);
VULKAN_INTERNAL_DestroySwapchain(
(VulkanRenderer*) driverData,
windowHandle
);
}
static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
@ -9348,42 +9310,19 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*) commandBuffer;
uint32_t swapchainImageIndex;
WindowData *windowData;
VulkanSwapchainData *swapchainData;
VkResult acquireResult = VK_SUCCESS;
VulkanTexture *swapchainTexture = NULL;
VulkanPresentData *presentData;
windowData = VULKAN_INTERNAL_FetchWindowData(windowHandle);
swapchainData = VULKAN_INTERNAL_FetchSwapchainData(windowHandle);
if (windowData == NULL)
if (swapchainData == NULL)
{
Refresh_LogError("Cannot acquire swapchain texture, window has not been claimed!");
return NULL;
}
swapchainData = windowData->swapchainData;
/* Window is claimed but swapchain is invalid! */
if (swapchainData == NULL)
{
if (SDL_GetWindowFlags(windowHandle) & SDL_WINDOW_MINIMIZED)
{
/* Window is minimized, don't bother */
return NULL;
}
/* Let's try to recreate */
VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
swapchainData = windowData->swapchainData;
if (swapchainData == NULL)
{
Refresh_LogWarn("Failed to recreate swapchain!");
return NULL;
}
}
acquireResult = renderer->vkAcquireNextImageKHR(
renderer->logicalDevice,
swapchainData->swapchain,
@ -9393,11 +9332,14 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
&swapchainImageIndex
);
/* Acquisition is invalid, let's try to recreate */
/* Swapchain is invalid, let's try to recreate */
if (acquireResult != VK_SUCCESS && acquireResult != VK_SUBOPTIMAL_KHR)
{
VULKAN_INTERNAL_RecreateSwapchain(renderer, windowData);
swapchainData = windowData->swapchainData;
swapchainData = VULKAN_INTERNAL_FetchSwapchainData(windowHandle);
VULKAN_INTERNAL_RecreateSwapchain(renderer, windowHandle, swapchainData->presentMode);
swapchainData = VULKAN_INTERNAL_FetchSwapchainData(windowHandle);
if (swapchainData == NULL)
{
@ -9416,7 +9358,6 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
if (acquireResult != VK_SUCCESS && acquireResult != VK_SUBOPTIMAL_KHR)
{
Refresh_LogWarn("Failed to acquire swapchain texture!");
return NULL;
}
}
@ -9451,7 +9392,7 @@ static Refresh_Texture* VULKAN_AcquireSwapchainTexture(
presentData = &vulkanCommandBuffer->presentDatas[vulkanCommandBuffer->presentDataCount];
vulkanCommandBuffer->presentDataCount += 1;
presentData->windowData = windowData;
presentData->swapchainData = swapchainData;
presentData->swapchainImageIndex = swapchainImageIndex;
/* Set up present semaphores */
@ -9490,25 +9431,19 @@ static Refresh_TextureFormat VULKAN_GetSwapchainFormat(
Refresh_Renderer *driverData,
void *windowHandle
) {
WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(windowHandle);
VulkanSwapchainData *swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA);
if (windowData == NULL)
if (swapchainData == NULL)
{
Refresh_LogWarn("Cannot get swapchain format, window has not been claimed!");
Refresh_LogWarn("No swapchain for this window!");
return 0;
}
if (windowData->swapchainData == NULL)
{
Refresh_LogWarn("Cannot get swapchain format, swapchain is currently invalid!");
return 0;
}
if (windowData->swapchainData->swapchainFormat == VK_FORMAT_R8G8B8A8_UNORM)
if (swapchainData->swapchainFormat == VK_FORMAT_R8G8B8A8_UNORM)
{
return REFRESH_TEXTUREFORMAT_R8G8B8A8;
}
else if (windowData->swapchainData->swapchainFormat == VK_FORMAT_B8G8R8A8_UNORM)
else if (swapchainData->swapchainFormat == VK_FORMAT_B8G8R8A8_UNORM)
{
return REFRESH_TEXTUREFORMAT_B8G8R8A8;
}
@ -9524,9 +9459,9 @@ static void VULKAN_SetSwapchainPresentMode(
void *windowHandle,
Refresh_PresentMode presentMode
) {
WindowData *windowData = VULKAN_INTERNAL_FetchWindowData(windowHandle);
VulkanSwapchainData *swapchainData = (VulkanSwapchainData*) SDL_GetWindowData(windowHandle, WINDOW_SWAPCHAIN_DATA);
if (windowData == NULL)
if (swapchainData == NULL)
{
Refresh_LogWarn("Cannot set present mode, window has not been claimed!");
return;
@ -9534,7 +9469,8 @@ static void VULKAN_SetSwapchainPresentMode(
VULKAN_INTERNAL_RecreateSwapchain(
(VulkanRenderer *)driverData,
windowData
windowHandle,
presentMode
);
}
@ -9879,8 +9815,8 @@ static void VULKAN_Submit(
0,
1,
0,
currentCommandBuffer->presentDatas[j].windowData->swapchainData->textures[swapchainImageIndex].image,
&currentCommandBuffer->presentDatas[j].windowData->swapchainData->textures[swapchainImageIndex].resourceAccessType
currentCommandBuffer->presentDatas[j].swapchainData->textures[swapchainImageIndex].image,
&currentCommandBuffer->presentDatas[j].swapchainData->textures[swapchainImageIndex].resourceAccessType
);
}
@ -9933,9 +9869,9 @@ static void VULKAN_Submit(
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.pNext = NULL;
presentInfo.pWaitSemaphores = &presentData->windowData->swapchainData->renderFinishedSemaphore;
presentInfo.pWaitSemaphores = &presentData->swapchainData->renderFinishedSemaphore;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pSwapchains = &presentData->windowData->swapchainData->swapchain;
presentInfo.pSwapchains = &presentData->swapchainData->swapchain;
presentInfo.swapchainCount = 1;
presentInfo.pImageIndices = &presentData->swapchainImageIndex;
presentInfo.pResults = NULL;
@ -9949,7 +9885,8 @@ static void VULKAN_Submit(
{
VULKAN_INTERNAL_RecreateSwapchain(
renderer,
presentData->windowData
presentData->swapchainData->windowHandle,
presentData->swapchainData->presentMode
);
}
}
@ -10549,7 +10486,6 @@ static uint8_t VULKAN_INTERNAL_CreateLogicalDevice(
deviceFeatures.occlusionQueryPrecise = VK_TRUE;
deviceFeatures.fillModeNonSolid = VK_TRUE;
deviceFeatures.depthClamp = VK_TRUE;
deviceFeatures.samplerAnisotropy = VK_TRUE;
/* creating the logical device */
@ -10818,10 +10754,10 @@ static Refresh_Device* VULKAN_CreateDevice(
* Create initial swapchain array
*/
renderer->claimedWindowCapacity = 1;
renderer->claimedWindowCount = 0;
renderer->claimedWindows = SDL_malloc(
renderer->claimedWindowCapacity * sizeof(WindowData*)
renderer->swapchainDataCapacity = 1;
renderer->swapchainDataCount = 0;
renderer->swapchainDatas = SDL_malloc(
renderer->swapchainDataCapacity * sizeof(VulkanSwapchainData*)
);
/* Threading */