From 4cdd6a497a28db40d1b96f7cafd4bbf645502559 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Sat, 7 Jan 2023 05:24:58 +0000 Subject: [PATCH] Fix transfer buffer alignment (#31) Reviewed-on: https://gitea.moonside.games/MoonsideGames/Refresh/pulls/31 --- src/Refresh_Driver_Vulkan.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 02eafdb..e94b93e 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -6734,9 +6734,11 @@ static Refresh_Buffer* VULKAN_CreateBuffer( static VulkanTransferBuffer* VULKAN_INTERNAL_AcquireTransferBuffer( VulkanRenderer *renderer, VulkanCommandBuffer *commandBuffer, - VkDeviceSize requiredSize + VkDeviceSize requiredSize, + VkDeviceSize alignment ) { VkDeviceSize size; + VkDeviceSize offset; uint32_t i; VulkanTransferBuffer *transferBuffer; @@ -6745,9 +6747,11 @@ static VulkanTransferBuffer* VULKAN_INTERNAL_AcquireTransferBuffer( for (i = 0; i < commandBuffer->transferBufferCount; i += 1) { transferBuffer = commandBuffer->transferBuffers[i]; + offset = transferBuffer->offset + alignment - (transferBuffer->offset % alignment); - if (transferBuffer->offset + requiredSize <= transferBuffer->buffer->size) + if (offset + requiredSize <= transferBuffer->buffer->size) { + transferBuffer->offset = offset; return transferBuffer; } } @@ -6759,8 +6763,9 @@ static VulkanTransferBuffer* VULKAN_INTERNAL_AcquireTransferBuffer( for (i = 0; i < renderer->transferBufferPool.availableBufferCount; i += 1) { transferBuffer = renderer->transferBufferPool.availableBuffers[i]; + offset = transferBuffer->offset + alignment - (transferBuffer->offset % alignment); - if (transferBuffer->offset + requiredSize <= transferBuffer->buffer->size) + if (offset + requiredSize <= transferBuffer->buffer->size) { if (commandBuffer->transferBufferCount == commandBuffer->transferBufferCapacity) { @@ -6778,6 +6783,7 @@ static VulkanTransferBuffer* VULKAN_INTERNAL_AcquireTransferBuffer( renderer->transferBufferPool.availableBufferCount -= 1; SDL_UnlockMutex(renderer->transferBufferPool.lock); + transferBuffer->offset = offset; return transferBuffer; } } @@ -6847,7 +6853,8 @@ static void VULKAN_SetTextureData( textureSlice->rectangle.w, textureSlice->rectangle.h, vulkanTexture->format - ) + ), + VULKAN_INTERNAL_BytesPerPixel(vulkanTexture->format) ); if (transferBuffer == NULL) @@ -6944,7 +6951,7 @@ static void VULKAN_SetTextureDataYUV( uint32_t dataLength ) { VulkanRenderer *renderer = (VulkanRenderer*) driverData; - VulkanTexture *tex; + VulkanTexture *tex = (VulkanTexture*) y; VulkanCommandBuffer *vulkanCommandBuffer = (VulkanCommandBuffer*)commandBuffer; VulkanTransferBuffer *transferBuffer; @@ -6957,7 +6964,8 @@ static void VULKAN_SetTextureDataYUV( transferBuffer = VULKAN_INTERNAL_AcquireTransferBuffer( renderer, vulkanCommandBuffer, - yDataLength + uvDataLength + yDataLength + uvDataLength, + VULKAN_INTERNAL_BytesPerPixel(tex->format) ); if (transferBuffer == NULL) @@ -6983,8 +6991,6 @@ static void VULKAN_SetTextureDataYUV( /* Y */ - tex = (VulkanTexture*) y; - SDL_memcpy( stagingBufferPointer, dataPtr, @@ -7336,7 +7342,8 @@ static void VULKAN_SetBufferData( transferBuffer = VULKAN_INTERNAL_AcquireTransferBuffer( renderer, vulkanCommandBuffer, - dataLength + dataLength, + renderer->physicalDeviceProperties.properties.limits.optimalBufferCopyOffsetAlignment ); if (transferBuffer == NULL)