memory thread safety

submit_rewrite
cosmonaut 2021-01-03 13:12:12 -08:00 committed by thatcosmonaut
parent e82bce10dc
commit e91a83f357
1 changed files with 42 additions and 0 deletions

View File

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