diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index db6b37c..648701e 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -432,6 +432,7 @@ struct VulkanMemoryAllocation uint32_t freeRegionCount; uint32_t freeRegionCapacity; uint8_t dedicated; + SDL_mutex *memoryLock; }; typedef struct VulkanMemoryAllocator @@ -1737,6 +1738,7 @@ static uint8_t VULKAN_INTERNAL_AllocateMemory( allocation = SDL_malloc(sizeof(VulkanMemoryAllocation)); allocation->size = allocationSize; + allocation->memoryLock = SDL_CreateMutex(); if (dedicated) { @@ -2157,6 +2159,7 @@ static void VULKAN_INTERNAL_DestroyTexture( NULL ); + SDL_DestroyMutex(texture->allocation->memoryLock); SDL_free(texture->allocation->freeRegions); SDL_free(texture->allocation); } @@ -2243,6 +2246,7 @@ static void VULKAN_INTERNAL_DestroyBuffer( NULL ); + SDL_DestroyMutex(buffer->subBuffers[i]->allocation->memoryLock); SDL_free(buffer->subBuffers[i]->allocation->freeRegions); SDL_free(buffer->subBuffers[i]->allocation); } @@ -3324,6 +3328,8 @@ static uint8_t VULKAN_INTERNAL_CreateBuffer( return 0; } + SDL_LockMutex(buffer->subBuffers[i]->allocation->memoryLock); + vulkanResult = renderer->vkBindBufferMemory( renderer->logicalDevice, buffer->subBuffers[i]->buffer, @@ -3331,6 +3337,8 @@ static uint8_t VULKAN_INTERNAL_CreateBuffer( buffer->subBuffers[i]->offset ); + SDL_UnlockMutex(buffer->subBuffers[i]->allocation->memoryLock); + if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to bind buffer memory!"); @@ -3618,6 +3626,7 @@ static void VULKAN_DestroyDevice( NULL ); + SDL_DestroyMutex(allocator->allocations[j]->memoryLock); SDL_free(allocator->allocations[j]); } @@ -5458,6 +5467,8 @@ static uint8_t VULKAN_INTERNAL_CreateTexture( return 0; } + SDL_LockMutex(texture->allocation->memoryLock); + vulkanResult = renderer->vkBindImageMemory( renderer->logicalDevice, texture->image, @@ -5465,6 +5476,8 @@ static uint8_t VULKAN_INTERNAL_CreateTexture( texture->offset ); + SDL_UnlockMutex(texture->allocation->memoryLock); + if (vulkanResult != VK_SUCCESS) { LogVulkanResult("vkBindImageMemory", vulkanResult); @@ -6001,6 +6014,8 @@ static void VULKAN_SetTextureData2D( VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLengthInBytes); VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer); + SDL_LockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + vulkanResult = renderer->vkMapMemory( renderer->logicalDevice, renderer->textureStagingBuffer->subBuffers[0]->allocation->memory, @@ -6013,6 +6028,7 @@ static void VULKAN_SetTextureData2D( if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to map buffer memory!"); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); return; } @@ -6023,6 +6039,8 @@ static void VULKAN_SetTextureData2D( renderer->textureStagingBuffer->subBuffers[0]->allocation->memory ); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + VULKAN_INTERNAL_ImageMemoryBarrier( renderer, commandBuffer, @@ -6104,6 +6122,8 @@ static void VULKAN_SetTextureData3D( VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength); VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer); + SDL_LockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + vulkanResult = renderer->vkMapMemory( renderer->logicalDevice, renderer->textureStagingBuffer->subBuffers[0]->allocation->memory, @@ -6116,6 +6136,7 @@ static void VULKAN_SetTextureData3D( if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to map buffer memory!"); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); return; } @@ -6126,6 +6147,8 @@ static void VULKAN_SetTextureData3D( renderer->textureStagingBuffer->subBuffers[0]->allocation->memory ); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + VULKAN_INTERNAL_ImageMemoryBarrier( renderer, commandBuffer, @@ -6206,6 +6229,8 @@ static void VULKAN_SetTextureDataCube( VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength); VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer); + SDL_LockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + vulkanResult = renderer->vkMapMemory( renderer->logicalDevice, renderer->textureStagingBuffer->subBuffers[0]->allocation->memory, @@ -6218,6 +6243,7 @@ static void VULKAN_SetTextureDataCube( if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to map buffer memory!"); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); return; } @@ -6228,6 +6254,8 @@ static void VULKAN_SetTextureDataCube( renderer->textureStagingBuffer->subBuffers[0]->allocation->memory ); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + VULKAN_INTERNAL_ImageMemoryBarrier( renderer, commandBuffer, @@ -6311,6 +6339,8 @@ static void VULKAN_SetTextureDataYUV( VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength); VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer); + SDL_LockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + /* Initialize values that are the same for Y, U, and V */ imageCopy.imageExtent.depth = 1; @@ -6338,6 +6368,7 @@ static void VULKAN_SetTextureDataYUV( if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to map buffer memory!"); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); return; } @@ -6436,6 +6467,8 @@ static void VULKAN_SetTextureDataYUV( renderer->textureStagingBuffer->subBuffers[0]->allocation->memory ); + SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock); + VULKAN_INTERNAL_ImageMemoryBarrier( renderer, commandBuffer, @@ -6684,6 +6717,7 @@ static void VULKAN_SetBufferData( return; } + SDL_LockMutex(SUBBUF->allocation->memoryLock); /* Map the memory and perform the copy */ vulkanResult = renderer->vkMapMemory( @@ -6698,6 +6732,7 @@ static void VULKAN_SetBufferData( if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to map buffer memory!"); + SDL_UnlockMutex(SUBBUF->allocation->memoryLock); return; } @@ -6712,6 +6747,8 @@ static void VULKAN_SetBufferData( SUBBUF->allocation->memory ); + SDL_UnlockMutex(SUBBUF->allocation->memoryLock); + #undef CURIDX #undef SUBBUF } @@ -7207,6 +7244,8 @@ static void VULKAN_GetBufferData( uint8_t *mapPointer; VkResult vulkanResult; + SDL_LockMutex(vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memoryLock); + vulkanResult = renderer->vkMapMemory( renderer->logicalDevice, vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memory, @@ -7219,6 +7258,7 @@ static void VULKAN_GetBufferData( if (vulkanResult != VK_SUCCESS) { REFRESH_LogError("Failed to map buffer memory!"); + SDL_UnlockMutex(vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memoryLock); return; } @@ -7232,6 +7272,8 @@ static void VULKAN_GetBufferData( renderer->logicalDevice, vulkanBuffer->subBuffers[0]->allocation->memory ); + + SDL_UnlockMutex(vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memoryLock); } static void VULKAN_INTERNAL_CopyTextureData(