fix some race conditions and memory leaks
continuous-integration/drone/push Build is passing Details

pull/13/head
cosmonaut 2022-02-22 15:05:16 -08:00
parent 68f857fa5c
commit 98b6518f2b
1 changed files with 25 additions and 16 deletions

View File

@ -1342,6 +1342,7 @@ typedef struct VulkanRenderer
SDL_mutex *allocatorLock; SDL_mutex *allocatorLock;
SDL_mutex *submitLock; SDL_mutex *submitLock;
SDL_mutex *acquireFenceLock; SDL_mutex *acquireFenceLock;
SDL_mutex *acquireCommandBufferLock;
#define VULKAN_INSTANCE_FUNCTION(ext, ret, func, params) \ #define VULKAN_INSTANCE_FUNCTION(ext, ret, func, params) \
vkfntype_##func func; vkfntype_##func func;
@ -3975,7 +3976,7 @@ static void VULKAN_DestroyDevice(
GraphicsPipelineLayoutHashArray graphicsPipelineLayoutHashArray; GraphicsPipelineLayoutHashArray graphicsPipelineLayoutHashArray;
ComputePipelineLayoutHashArray computePipelineLayoutHashArray; ComputePipelineLayoutHashArray computePipelineLayoutHashArray;
VulkanMemorySubAllocator *allocator; VulkanMemorySubAllocator *allocator;
uint32_t i, j, k; int32_t i, j, k;
VULKAN_Wait(device->driverData); VULKAN_Wait(device->driverData);
@ -4123,6 +4124,8 @@ static void VULKAN_DestroyDevice(
VULKAN_INTERNAL_DestroySwapchain(renderer, renderer->swapchainDatas[i]->windowHandle); VULKAN_INTERNAL_DestroySwapchain(renderer, renderer->swapchainDatas[i]->windowHandle);
} }
SDL_free(renderer->swapchainDatas);
for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1) for (i = 0; i < VK_MAX_MEMORY_TYPES; i += 1)
{ {
allocator = &renderer->memoryAllocator->subAllocators[i]; allocator = &renderer->memoryAllocator->subAllocators[i];
@ -4155,6 +4158,7 @@ static void VULKAN_DestroyDevice(
SDL_DestroyMutex(renderer->allocatorLock); SDL_DestroyMutex(renderer->allocatorLock);
SDL_DestroyMutex(renderer->submitLock); SDL_DestroyMutex(renderer->submitLock);
SDL_DestroyMutex(renderer->acquireFenceLock); SDL_DestroyMutex(renderer->acquireFenceLock);
SDL_DestroyMutex(renderer->acquireCommandBufferLock);
renderer->vkDestroyDevice(renderer->logicalDevice, NULL); renderer->vkDestroyDevice(renderer->logicalDevice, NULL);
renderer->vkDestroyInstance(renderer->instance, NULL); renderer->vkDestroyInstance(renderer->instance, NULL);
@ -7604,9 +7608,13 @@ static Refresh_CommandBuffer* VULKAN_AcquireCommandBuffer(
SDL_threadID threadID = SDL_ThreadID(); SDL_threadID threadID = SDL_ThreadID();
SDL_LockMutex(renderer->acquireCommandBufferLock);
VulkanCommandBuffer *commandBuffer = VulkanCommandBuffer *commandBuffer =
VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(renderer, threadID); VULKAN_INTERNAL_GetInactiveCommandBufferFromPool(renderer, threadID);
SDL_UnlockMutex(renderer->acquireCommandBufferLock);
/* State tracking */ /* State tracking */
commandBuffer->currentComputePipeline = NULL; commandBuffer->currentComputePipeline = NULL;
@ -8055,11 +8063,24 @@ static void VULKAN_INTERNAL_CleanCommandBuffer(
} }
commandBuffer->renderPassesToDestroyCount = 0; commandBuffer->renderPassesToDestroyCount = 0;
SDL_LockMutex(renderer->acquireCommandBufferLock);
if (commandBuffer->commandPool->inactiveCommandBufferCount == commandBuffer->commandPool->inactiveCommandBufferCapacity)
{
commandBuffer->commandPool->inactiveCommandBufferCapacity += 1;
commandBuffer->commandPool->inactiveCommandBuffers = SDL_realloc(
commandBuffer->commandPool->inactiveCommandBuffers,
commandBuffer->commandPool->inactiveCommandBufferCapacity * sizeof(VulkanCommandBuffer*)
);
}
commandBuffer->commandPool->inactiveCommandBuffers[ commandBuffer->commandPool->inactiveCommandBuffers[
commandBuffer->commandPool->inactiveCommandBufferCount commandBuffer->commandPool->inactiveCommandBufferCount
] = commandBuffer; ] = commandBuffer;
commandBuffer->commandPool->inactiveCommandBufferCount += 1; commandBuffer->commandPool->inactiveCommandBufferCount += 1;
SDL_UnlockMutex(renderer->acquireCommandBufferLock);
renderer->vkResetFences( renderer->vkResetFences(
renderer->logicalDevice, renderer->logicalDevice,
1, 1,
@ -8084,26 +8105,13 @@ static void VULKAN_INTERNAL_CleanCommandBuffer(
commandBuffer->inFlightFence = VK_NULL_HANDLE; commandBuffer->inFlightFence = VK_NULL_HANDLE;
/* Return this command buffer to its pool */ /* Remove this command buffer from the submitted list */
for (i = 0; i < renderer->submittedCommandBufferCount; i += 1) for (i = 0; i < renderer->submittedCommandBufferCount; i += 1)
{ {
if (renderer->submittedCommandBuffers[i] == commandBuffer) if (renderer->submittedCommandBuffers[i] == commandBuffer)
{ {
renderer->submittedCommandBuffers[i] = renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount - 1]; renderer->submittedCommandBuffers[i] = renderer->submittedCommandBuffers[renderer->submittedCommandBufferCount - 1];
renderer->submittedCommandBufferCount -= 1; renderer->submittedCommandBufferCount -= 1;
if (commandBuffer->commandPool->inactiveCommandBufferCount == commandBuffer->commandPool->inactiveCommandBufferCapacity)
{
commandBuffer->commandPool->inactiveCommandBufferCapacity += 1;
commandBuffer->commandPool->inactiveCommandBuffers = SDL_realloc(
commandBuffer->commandPool->inactiveCommandBuffers,
commandBuffer->commandPool->inactiveCommandBufferCapacity * sizeof(VulkanCommandBuffer*)
);
}
commandBuffer->commandPool->inactiveCommandBuffers[commandBuffer->commandPool->inactiveCommandBufferCount] = commandBuffer;
commandBuffer->commandPool->inactiveCommandBufferCount += 1;
} }
} }
} }
@ -8114,7 +8122,7 @@ static void VULKAN_Wait(
VulkanRenderer *renderer = (VulkanRenderer*) driverData; VulkanRenderer *renderer = (VulkanRenderer*) driverData;
VulkanCommandBuffer *commandBuffer; VulkanCommandBuffer *commandBuffer;
VkResult result; VkResult result;
uint32_t i; int32_t i;
for (i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) for (i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1)
{ {
@ -9159,6 +9167,7 @@ static Refresh_Device* VULKAN_CreateDevice(
renderer->allocatorLock = SDL_CreateMutex(); renderer->allocatorLock = SDL_CreateMutex();
renderer->submitLock = SDL_CreateMutex(); renderer->submitLock = SDL_CreateMutex();
renderer->acquireFenceLock = SDL_CreateMutex(); renderer->acquireFenceLock = SDL_CreateMutex();
renderer->acquireCommandBufferLock = SDL_CreateMutex();
/* Create fence lists */ /* Create fence lists */