forked from MoonsideGames/Refresh
Vulkan: persisent map buffers
parent
b12b785dbe
commit
bf91de783f
|
@ -921,7 +921,7 @@ void Refresh_GetTextureHandles(
|
||||||
Refresh_TextureHandles *handles
|
Refresh_TextureHandles *handles
|
||||||
) {
|
) {
|
||||||
NULL_RETURN(device);
|
NULL_RETURN(device);
|
||||||
return device->GetTextureHandles(
|
device->GetTextureHandles(
|
||||||
device->driverData,
|
device->driverData,
|
||||||
texture,
|
texture,
|
||||||
handles
|
handles
|
||||||
|
|
|
@ -432,6 +432,7 @@ struct VulkanMemoryAllocation
|
||||||
uint32_t freeRegionCount;
|
uint32_t freeRegionCount;
|
||||||
uint32_t freeRegionCapacity;
|
uint32_t freeRegionCapacity;
|
||||||
uint8_t dedicated;
|
uint8_t dedicated;
|
||||||
|
uint8_t *mapPointer;
|
||||||
SDL_mutex *memoryLock;
|
SDL_mutex *memoryLock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1797,6 +1798,28 @@ static uint8_t VULKAN_INTERNAL_AllocateMemory(
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (buffer != NULL)
|
||||||
|
{
|
||||||
|
result = renderer->vkMapMemory(
|
||||||
|
renderer->logicalDevice,
|
||||||
|
allocation->memory,
|
||||||
|
0,
|
||||||
|
allocation->size,
|
||||||
|
0,
|
||||||
|
(void**) &allocation->mapPointer
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result != VK_SUCCESS)
|
||||||
|
{
|
||||||
|
LogVulkanResult("vkMapMemory", result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
allocation->mapPointer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
VULKAN_INTERNAL_NewMemoryFreeRegion(
|
VULKAN_INTERNAL_NewMemoryFreeRegion(
|
||||||
allocation,
|
allocation,
|
||||||
0,
|
0,
|
||||||
|
@ -6030,41 +6053,24 @@ static void VULKAN_SetTextureData(
|
||||||
VkCommandBuffer commandBuffer = renderer->transferCommandBuffers[renderer->frameIndex];
|
VkCommandBuffer commandBuffer = renderer->transferCommandBuffers[renderer->frameIndex];
|
||||||
VkBufferImageCopy imageCopy;
|
VkBufferImageCopy imageCopy;
|
||||||
VkResult vulkanResult;
|
VkResult vulkanResult;
|
||||||
uint8_t *mapPointer;
|
uint8_t *stagingBufferPointer;
|
||||||
|
|
||||||
SDL_LockMutex(renderer->stagingLock);
|
SDL_LockMutex(renderer->stagingLock);
|
||||||
|
|
||||||
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);
|
stagingBufferPointer =
|
||||||
|
renderer->textureStagingBuffer->subBuffers[0]->allocation->mapPointer +
|
||||||
|
renderer->textureStagingBuffer->subBuffers[0]->offset +
|
||||||
|
renderer->textureStagingBufferOffset;
|
||||||
|
|
||||||
vulkanResult = renderer->vkMapMemory(
|
SDL_memcpy(
|
||||||
renderer->logicalDevice,
|
stagingBufferPointer,
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->allocation->memory,
|
data,
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->offset + renderer->textureStagingBufferOffset,
|
dataLengthInBytes
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->size,
|
|
||||||
0,
|
|
||||||
(void**) &mapPointer
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (vulkanResult != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
Refresh_LogError("Failed to map buffer memory!");
|
|
||||||
SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock);
|
|
||||||
SDL_UnlockMutex(renderer->stagingLock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_memcpy(mapPointer, data, dataLengthInBytes);
|
|
||||||
|
|
||||||
renderer->vkUnmapMemory(
|
|
||||||
renderer->logicalDevice,
|
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->allocation->memory
|
|
||||||
);
|
|
||||||
|
|
||||||
SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock);
|
|
||||||
|
|
||||||
VULKAN_INTERNAL_ImageMemoryBarrier(
|
VULKAN_INTERNAL_ImageMemoryBarrier(
|
||||||
renderer,
|
renderer,
|
||||||
commandBuffer,
|
commandBuffer,
|
||||||
|
@ -6144,15 +6150,17 @@ static void VULKAN_SetTextureDataYUV(
|
||||||
int32_t yDataLength = BytesPerImage(yWidth, yHeight, REFRESH_COLORFORMAT_R8);
|
int32_t yDataLength = BytesPerImage(yWidth, yHeight, REFRESH_COLORFORMAT_R8);
|
||||||
int32_t uvDataLength = BytesPerImage(uvWidth, uvHeight, REFRESH_COLORFORMAT_R8);
|
int32_t uvDataLength = BytesPerImage(uvWidth, uvHeight, REFRESH_COLORFORMAT_R8);
|
||||||
VkBufferImageCopy imageCopy;
|
VkBufferImageCopy imageCopy;
|
||||||
uint8_t *mapPointer;
|
uint8_t * stagingBufferPointer;
|
||||||
VkResult vulkanResult;
|
|
||||||
|
|
||||||
SDL_LockMutex(renderer->stagingLock);
|
SDL_LockMutex(renderer->stagingLock);
|
||||||
|
|
||||||
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);
|
stagingBufferPointer =
|
||||||
|
renderer->textureStagingBuffer->subBuffers[0]->allocation->mapPointer +
|
||||||
|
renderer->textureStagingBuffer->subBuffers[0]->offset +
|
||||||
|
renderer->textureStagingBufferOffset;
|
||||||
|
|
||||||
/* Initialize values that are the same for Y, U, and V */
|
/* Initialize values that are the same for Y, U, and V */
|
||||||
|
|
||||||
|
@ -6169,25 +6177,8 @@ static void VULKAN_SetTextureDataYUV(
|
||||||
|
|
||||||
tex = (VulkanTexture*) y;
|
tex = (VulkanTexture*) y;
|
||||||
|
|
||||||
vulkanResult = renderer->vkMapMemory(
|
|
||||||
renderer->logicalDevice,
|
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->allocation->memory,
|
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->offset + renderer->textureStagingBufferOffset,
|
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->size,
|
|
||||||
0,
|
|
||||||
(void**) &mapPointer
|
|
||||||
);
|
|
||||||
|
|
||||||
if (vulkanResult != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
Refresh_LogError("Failed to map buffer memory!");
|
|
||||||
SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock);
|
|
||||||
SDL_UnlockMutex(renderer->stagingLock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_memcpy(
|
SDL_memcpy(
|
||||||
mapPointer,
|
stagingBufferPointer,
|
||||||
dataPtr,
|
dataPtr,
|
||||||
yDataLength
|
yDataLength
|
||||||
);
|
);
|
||||||
|
@ -6236,7 +6227,7 @@ static void VULKAN_SetTextureDataYUV(
|
||||||
tex = (VulkanTexture*) u;
|
tex = (VulkanTexture*) u;
|
||||||
|
|
||||||
SDL_memcpy(
|
SDL_memcpy(
|
||||||
mapPointer + yDataLength,
|
stagingBufferPointer + yDataLength,
|
||||||
dataPtr + yDataLength,
|
dataPtr + yDataLength,
|
||||||
uvDataLength
|
uvDataLength
|
||||||
);
|
);
|
||||||
|
@ -6271,18 +6262,11 @@ static void VULKAN_SetTextureDataYUV(
|
||||||
tex = (VulkanTexture*) v;
|
tex = (VulkanTexture*) v;
|
||||||
|
|
||||||
SDL_memcpy(
|
SDL_memcpy(
|
||||||
mapPointer + yDataLength + uvDataLength,
|
stagingBufferPointer + yDataLength + uvDataLength,
|
||||||
dataPtr + yDataLength + uvDataLength,
|
dataPtr + yDataLength + uvDataLength,
|
||||||
uvDataLength
|
uvDataLength
|
||||||
);
|
);
|
||||||
|
|
||||||
renderer->vkUnmapMemory(
|
|
||||||
renderer->logicalDevice,
|
|
||||||
renderer->textureStagingBuffer->subBuffers[0]->allocation->memory
|
|
||||||
);
|
|
||||||
|
|
||||||
SDL_UnlockMutex(renderer->textureStagingBuffer->subBuffers[0]->allocation->memoryLock);
|
|
||||||
|
|
||||||
VULKAN_INTERNAL_ImageMemoryBarrier(
|
VULKAN_INTERNAL_ImageMemoryBarrier(
|
||||||
renderer,
|
renderer,
|
||||||
commandBuffer,
|
commandBuffer,
|
||||||
|
@ -6483,7 +6467,6 @@ static void VULKAN_SetBufferData(
|
||||||
) {
|
) {
|
||||||
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
|
VulkanRenderer* renderer = (VulkanRenderer*)driverData;
|
||||||
VulkanBuffer* vulkanBuffer = (VulkanBuffer*)buffer;
|
VulkanBuffer* vulkanBuffer = (VulkanBuffer*)buffer;
|
||||||
uint8_t* mapPointer;
|
|
||||||
VkResult vulkanResult;
|
VkResult vulkanResult;
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
|
||||||
|
@ -6508,38 +6491,12 @@ static void VULKAN_SetBufferData(
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LockMutex(SUBBUF->allocation->memoryLock);
|
|
||||||
|
|
||||||
/* Map the memory and perform the copy */
|
|
||||||
vulkanResult = renderer->vkMapMemory(
|
|
||||||
renderer->logicalDevice,
|
|
||||||
SUBBUF->allocation->memory,
|
|
||||||
SUBBUF->offset,
|
|
||||||
SUBBUF->size,
|
|
||||||
0,
|
|
||||||
(void**)&mapPointer
|
|
||||||
);
|
|
||||||
|
|
||||||
if (vulkanResult != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
Refresh_LogError("Failed to map buffer memory!");
|
|
||||||
SDL_UnlockMutex(SUBBUF->allocation->memoryLock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_memcpy(
|
SDL_memcpy(
|
||||||
mapPointer + offsetInBytes,
|
SUBBUF->allocation->mapPointer + SUBBUF->offset + offsetInBytes,
|
||||||
data,
|
data,
|
||||||
dataLength
|
dataLength
|
||||||
);
|
);
|
||||||
|
|
||||||
renderer->vkUnmapMemory(
|
|
||||||
renderer->logicalDevice,
|
|
||||||
SUBBUF->allocation->memory
|
|
||||||
);
|
|
||||||
|
|
||||||
SDL_UnlockMutex(SUBBUF->allocation->memoryLock);
|
|
||||||
|
|
||||||
#undef CURIDX
|
#undef CURIDX
|
||||||
#undef SUBBUF
|
#undef SUBBUF
|
||||||
}
|
}
|
||||||
|
@ -7033,38 +6990,16 @@ static void VULKAN_GetBufferData(
|
||||||
VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer;
|
VulkanBuffer *vulkanBuffer = (VulkanBuffer*) buffer;
|
||||||
uint8_t *dataPtr = (uint8_t*) data;
|
uint8_t *dataPtr = (uint8_t*) data;
|
||||||
uint8_t *mapPointer;
|
uint8_t *mapPointer;
|
||||||
VkResult vulkanResult;
|
|
||||||
|
|
||||||
SDL_LockMutex(vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memoryLock);
|
mapPointer =
|
||||||
|
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->mapPointer +
|
||||||
vulkanResult = renderer->vkMapMemory(
|
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->offset;
|
||||||
renderer->logicalDevice,
|
|
||||||
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memory,
|
|
||||||
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->offset,
|
|
||||||
vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->size,
|
|
||||||
0,
|
|
||||||
(void**) &mapPointer
|
|
||||||
);
|
|
||||||
|
|
||||||
if (vulkanResult != VK_SUCCESS)
|
|
||||||
{
|
|
||||||
Refresh_LogError("Failed to map buffer memory!");
|
|
||||||
SDL_UnlockMutex(vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memoryLock);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_memcpy(
|
SDL_memcpy(
|
||||||
dataPtr,
|
dataPtr,
|
||||||
mapPointer,
|
mapPointer,
|
||||||
dataLengthInBytes
|
dataLengthInBytes
|
||||||
);
|
);
|
||||||
|
|
||||||
renderer->vkUnmapMemory(
|
|
||||||
renderer->logicalDevice,
|
|
||||||
vulkanBuffer->subBuffers[0]->allocation->memory
|
|
||||||
);
|
|
||||||
|
|
||||||
SDL_UnlockMutex(vulkanBuffer->subBuffers[vulkanBuffer->currentSubBufferIndex]->allocation->memoryLock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void VULKAN_CopyTextureToBuffer(
|
static void VULKAN_CopyTextureToBuffer(
|
||||||
|
|
Loading…
Reference in New Issue