From 74f26e605d69a99956c6be1b4825591e5d240d55 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Fri, 16 Feb 2024 18:51:09 -0800 Subject: [PATCH] permanently map host-visible buffers --- src/Refresh_Driver_Vulkan.c | 74 ++++++++++--------------------------- 1 file changed, 20 insertions(+), 54 deletions(-) diff --git a/src/Refresh_Driver_Vulkan.c b/src/Refresh_Driver_Vulkan.c index 6c95d11..abf0f88 100644 --- a/src/Refresh_Driver_Vulkan.c +++ b/src/Refresh_Driver_Vulkan.c @@ -449,6 +449,7 @@ struct VulkanMemoryAllocation uint8_t availableForAllocation; VkDeviceSize freeSpace; VkDeviceSize usedSpace; + uint8_t *mapPointer; SDL_mutex *memoryLock; }; @@ -745,7 +746,6 @@ typedef struct VulkanUniformBufferObject VkDescriptorSet descriptorSet; VulkanBuffer *buffer; uint32_t currentOffset; - uint8_t *mapPointer; /* uniform buffers are permanently mapped */ SDL_mutex *lock; } VulkanUniformBufferObject; @@ -2552,6 +2552,24 @@ static uint8_t VULKAN_INTERNAL_AllocateMemory( return 0; } + /* Persistent mapping for host-visible memory */ + if (isHostVisible) + { + result = renderer->vkMapMemory( + renderer->logicalDevice, + allocation->memory, + 0, + VK_WHOLE_SIZE, + 0, + (void**) &allocation->mapPointer + ); + VULKAN_ERROR_CHECK(result, vkMapMemory, 0) + } + else + { + allocation->mapPointer = NULL; + } + VULKAN_INTERNAL_NewMemoryFreeRegion( renderer, allocation, @@ -4300,16 +4318,6 @@ static VulkanUniformBufferObject* VULKAN_INTERNAL_CreateUniformBufferObject( NULL ); - /* Permanently map the memory */ - renderer->vkMapMemory( - renderer->logicalDevice, - uniformBufferObject->buffer->usedRegion->allocation->memory, - 0, - VK_WHOLE_SIZE, - 0, - (void**) &uniformBufferObject->mapPointer - ); - return uniformBufferObject; } @@ -4354,11 +4362,6 @@ static void VULKAN_INTERNAL_DestroyUniformBufferObject( VulkanRenderer *renderer, VulkanUniformBufferObject *uniformBufferObject ) { - renderer->vkUnmapMemory( - renderer->logicalDevice, - uniformBufferObject->buffer->usedRegion->allocation->memory - ); - VULKAN_INTERNAL_DestroyBuffer(renderer, uniformBufferObject->buffer); SDL_DestroyMutex(uniformBufferObject->lock); @@ -6843,7 +6846,7 @@ static void VULKAN_INTERNAL_SetUniformBufferData( uint32_t dataLength ) { uint8_t *dst = - uniformBufferObject->mapPointer + + uniformBufferObject->buffer->usedRegion->allocation->mapPointer + uniformBufferObject->buffer->usedRegion->resourceOffset + uniformBufferObject->currentOffset; @@ -8336,43 +8339,6 @@ static void VULKAN_EndComputePass( vulkanCommandBuffer->currentComputePipeline = NULL; } -static void* VULKAN_MapCpuBuffer( - Refresh_Renderer *driverData, - Refresh_CpuBuffer *buffer, - uint32_t offsetInBytes, - uint32_t sizeInBytes -) { - VulkanRenderer *renderer = (VulkanRenderer*) driverData; - VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer; - VkResult result; - void* mapPointer; - - result = renderer->vkMapMemory( - renderer->logicalDevice, - vulkanBuffer->usedRegion->allocation->memory, - offsetInBytes, - sizeInBytes, - 0, - &mapPointer - ); - VULKAN_ERROR_CHECK(result, vkMapMemory, NULL) - - return mapPointer; -} - -static void VULKAN_UnmapCpuBuffer( - Refresh_Renderer *driverData, - Refresh_CpuBuffer *buffer -) { - VulkanRenderer *renderer = (VulkanRenderer*) driverData; - VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer; - - renderer->vkUnmapMemory( - renderer->logicalDevice, - vulkanBuffer->usedRegion->allocation->memory - ); -} - static void VULKAN_BeginCopyPass( Refresh_Renderer *driverData, Refresh_CommandBuffer *commandBuffer