@@ -18865,11 +18865,9 @@ vmaGetAllocationMemoryProperties(allocator, alloc, &memPropFlags);
18865
18865
18866
18866
if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
18867
18867
{
18868
- // Allocation ended up in a mappable memory and is already mapped - write to it directly.
18869
-
18870
- // [Executed in runtime]:
18871
- memcpy(allocInfo.pMappedData, myData, myDataSize);
18872
- result = vmaFlushAllocation(allocator, alloc, 0, VK_WHOLE_SIZE);
18868
+ // The Allocation ended up in a mappable memory.
18869
+ // Calling vmaCopyMemoryToAllocation() does vmaMapMemory(), memcpy(), vmaUnmapMemory(), and vmaFlushAllocation().
18870
+ result = vmaCopyMemoryToAllocation(allocator, myData, alloc, 0, myDataSize);
18873
18871
// Check result...
18874
18872
18875
18873
VkBufferMemoryBarrier bufMemBarrier = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER };
@@ -18881,6 +18879,7 @@ if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
18881
18879
bufMemBarrier.offset = 0;
18882
18880
bufMemBarrier.size = VK_WHOLE_SIZE;
18883
18881
18882
+ // It's important to insert a buffer memory barrier here to ensure writing to the buffer has finished.
18884
18883
vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
18885
18884
0, 0, nullptr, 1, &bufMemBarrier, 0, nullptr);
18886
18885
}
@@ -18903,9 +18902,8 @@ else
18903
18902
&stagingBuf, &stagingAlloc, &stagingAllocInfo);
18904
18903
// Check result...
18905
18904
18906
- // [Executed in runtime]:
18907
- memcpy(stagingAllocInfo.pMappedData, myData, myDataSize);
18908
- result = vmaFlushAllocation(allocator, stagingAlloc, 0, VK_WHOLE_SIZE);
18905
+ // Calling vmaCopyMemoryToAllocation() does vmaMapMemory(), memcpy(), vmaUnmapMemory(), and vmaFlushAllocation().
18906
+ result = vmaCopyMemoryToAllocation(allocator, myData, stagingAlloc, 0, myDataSize);
18909
18907
// Check result...
18910
18908
18911
18909
VkBufferMemoryBarrier bufMemBarrier = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER };
@@ -18917,6 +18915,7 @@ else
18917
18915
bufMemBarrier.offset = 0;
18918
18916
bufMemBarrier.size = VK_WHOLE_SIZE;
18919
18917
18918
+ // Insert a buffer memory barrier to make sure writing to the staging buffer has finished.
18920
18919
vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
18921
18920
0, 0, nullptr, 1, &bufMemBarrier, 0, nullptr);
18922
18921
@@ -18937,6 +18936,7 @@ else
18937
18936
bufMemBarrier2.offset = 0;
18938
18937
bufMemBarrier2.size = VK_WHOLE_SIZE;
18939
18938
18939
+ // Make sure copying from staging buffer to the actual buffer has finished by inserting a buffer memory barrier.
18940
18940
vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
18941
18941
0, 0, nullptr, 1, &bufMemBarrier2, 0, nullptr);
18942
18942
}
0 commit comments