Skip to content

Commit a2e25c6

Browse files
committed
Improve sample code for advanced data uploading in docs
1 parent 257138b commit a2e25c6

1 file changed

Lines changed: 50 additions & 7 deletions

File tree

include/vk_mem_alloc.h

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18195,7 +18195,8 @@ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
1819518195
VkBuffer buf;
1819618196
VmaAllocation alloc;
1819718197
VmaAllocationInfo allocInfo;
18198-
vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
18198+
VkResult result = vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
18199+
// Check result...
1819918200

1820018201
VkMemoryPropertyFlags memPropFlags;
1820118202
vmaGetAllocationMemoryProperties(allocator, alloc, &memPropFlags);
@@ -18206,10 +18207,24 @@ if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
1820618207

1820718208
// [Executed in runtime]:
1820818209
memcpy(allocInfo.pMappedData, myData, myDataSize);
18210+
result = vmaFlushAllocation(allocator, alloc, 0, VK_WHOLE_SIZE);
18211+
// Check result...
18212+
18213+
VkBufferMemoryBarrier bufMemBarrier = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER };
18214+
bufMemBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
18215+
bufMemBarrier.dstAccessMask = VK_ACCESS_UNIFORM_READ_BIT;
18216+
bufMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
18217+
bufMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
18218+
bufMemBarrier.buffer = buf;
18219+
bufMemBarrier.offset = 0;
18220+
bufMemBarrier.size = VK_WHOLE_SIZE;
18221+
18222+
vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
18223+
0, 0, nullptr, 1, &bufMemBarrier, 0, nullptr);
1820918224
}
1821018225
else
1821118226
{
18212-
// Allocation ended up in a non-mappable memory - need to transfer.
18227+
// Allocation ended up in a non-mappable memory - a transfer using a staging buffer is required.
1821318228
VkBufferCreateInfo stagingBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
1821418229
stagingBufCreateInfo.size = 65536;
1821518230
stagingBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
@@ -18222,18 +18237,46 @@ else
1822218237
VkBuffer stagingBuf;
1822318238
VmaAllocation stagingAlloc;
1822418239
VmaAllocationInfo stagingAllocInfo;
18225-
vmaCreateBuffer(allocator, &stagingBufCreateInfo, &stagingAllocCreateInfo,
18226-
&stagingBuf, &stagingAlloc, stagingAllocInfo);
18240+
result = vmaCreateBuffer(allocator, &stagingBufCreateInfo, &stagingAllocCreateInfo,
18241+
&stagingBuf, &stagingAlloc, &stagingAllocInfo);
18242+
// Check result...
1822718243

1822818244
// [Executed in runtime]:
1822918245
memcpy(stagingAllocInfo.pMappedData, myData, myDataSize);
18230-
vmaFlushAllocation(allocator, stagingAlloc, 0, VK_WHOLE_SIZE);
18231-
//vkCmdPipelineBarrier: VK_ACCESS_HOST_WRITE_BIT --> VK_ACCESS_TRANSFER_READ_BIT
18246+
result = vmaFlushAllocation(allocator, stagingAlloc, 0, VK_WHOLE_SIZE);
18247+
// Check result...
18248+
18249+
VkBufferMemoryBarrier bufMemBarrier = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER };
18250+
bufMemBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
18251+
bufMemBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
18252+
bufMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
18253+
bufMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
18254+
bufMemBarrier.buffer = stagingBuf;
18255+
bufMemBarrier.offset = 0;
18256+
bufMemBarrier.size = VK_WHOLE_SIZE;
18257+
18258+
vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_HOST_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
18259+
0, 0, nullptr, 1, &bufMemBarrier, 0, nullptr);
18260+
1823218261
VkBufferCopy bufCopy = {
1823318262
0, // srcOffset
1823418263
0, // dstOffset,
18235-
myDataSize); // size
18264+
myDataSize, // size
18265+
};
18266+
1823618267
vkCmdCopyBuffer(cmdBuf, stagingBuf, buf, 1, &bufCopy);
18268+
18269+
VkBufferMemoryBarrier bufMemBarrier2 = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER };
18270+
bufMemBarrier2.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
18271+
bufMemBarrier2.dstAccessMask = VK_ACCESS_UNIFORM_READ_BIT; // We created a uniform buffer
18272+
bufMemBarrier2.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
18273+
bufMemBarrier2.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
18274+
bufMemBarrier2.buffer = buf;
18275+
bufMemBarrier2.offset = 0;
18276+
bufMemBarrier2.size = VK_WHOLE_SIZE;
18277+
18278+
vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT,
18279+
0, 0, nullptr, 1, &bufMemBarrier2, 0, nullptr);
1823718280
}
1823818281
\endcode
1823918282

0 commit comments

Comments
 (0)