From 48bebb2ab3486dcac5f8fa95c942a54a6575f46a Mon Sep 17 00:00:00 2001
From: cosmonaut <evan@moonside.games>
Date: Sun, 3 Jan 2021 13:17:51 -0800
Subject: [PATCH] staging buffer lock

---
 src/Refresh_Driver_Vulkan.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c
index 648701e..8973d82 100644
--- a/src/Refresh_Driver_Vulkan.c
+++ b/src/Refresh_Driver_Vulkan.c
@@ -1358,6 +1358,7 @@ typedef struct VulkanRenderer
 	SDL_mutex *uniformBufferLock;
 	SDL_mutex *descriptorSetLock;
 	SDL_mutex *boundBufferLock;
+	SDL_mutex *stagingLock;
 
 	/* Deferred destroy storage */
 
@@ -3641,6 +3642,7 @@ static void VULKAN_DestroyDevice(
 	SDL_DestroyMutex(renderer->uniformBufferLock);
 	SDL_DestroyMutex(renderer->descriptorSetLock);
 	SDL_DestroyMutex(renderer->boundBufferLock);
+	SDL_DestroyMutex(renderer->stagingLock);
 
 	SDL_free(renderer->buffersInUse);
 
@@ -6011,6 +6013,8 @@ static void VULKAN_SetTextureData2D(
 	VkBufferImageCopy imageCopy;
 	uint8_t *mapPointer;
 
+	SDL_LockMutex(renderer->stagingLock);
+
 	VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLengthInBytes);
 	VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer);
 
@@ -6096,6 +6100,8 @@ static void VULKAN_SetTextureData2D(
 			&vulkanTexture->resourceAccessType
 		);
 	}
+
+	SDL_UnlockMutex(renderer->stagingLock);
 }
 
 static void VULKAN_SetTextureData3D(
@@ -6119,6 +6125,8 @@ static void VULKAN_SetTextureData3D(
 	VkBufferImageCopy imageCopy;
 	uint8_t *mapPointer;
 
+	SDL_LockMutex(renderer->stagingLock);
+
 	VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength);
 	VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer);
 
@@ -6204,6 +6212,8 @@ static void VULKAN_SetTextureData3D(
 			&vulkanTexture->resourceAccessType
 		);
 	}
+
+	SDL_UnlockMutex(renderer->stagingLock);
 }
 
 static void VULKAN_SetTextureDataCube(
@@ -6226,6 +6236,8 @@ static void VULKAN_SetTextureDataCube(
 	VkBufferImageCopy imageCopy;
 	uint8_t *mapPointer;
 
+	SDL_LockMutex(renderer->stagingLock);
+
 	VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength);
 	VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer);
 
@@ -6311,6 +6323,8 @@ static void VULKAN_SetTextureDataCube(
 			&vulkanTexture->resourceAccessType
 		);
 	}
+
+	SDL_UnlockMutex(renderer->stagingLock);
 }
 
 static void VULKAN_SetTextureDataYUV(
@@ -6336,6 +6350,8 @@ static void VULKAN_SetTextureDataYUV(
 	uint8_t *mapPointer;
 	VkResult vulkanResult;
 
+	SDL_LockMutex(renderer->stagingLock);
+
 	VULKAN_INTERNAL_MaybeExpandStagingBuffer(renderer, dataLength);
 	VULKAN_INTERNAL_MaybeBeginTransferCommandBuffer(renderer);
 
@@ -6510,6 +6526,8 @@ static void VULKAN_SetTextureDataYUV(
 			&tex->resourceAccessType
 		);
 	}
+
+	SDL_UnlockMutex(renderer->stagingLock);
 }
 
 static void VULKAN_INTERNAL_BlitImage(
@@ -9484,6 +9502,7 @@ static REFRESH_Device* VULKAN_CreateDevice(
 	renderer->uniformBufferLock = SDL_CreateMutex();
 	renderer->descriptorSetLock = SDL_CreateMutex();
 	renderer->boundBufferLock = SDL_CreateMutex();
+	renderer->stagingLock = SDL_CreateMutex();
 
 	/* Transfer buffer */