Skip to content

Commit 10ec7bd

Browse files
Merge pull request #428 from IAmNotHanni/master
Improve sample code for advanced data uploading in docs
2 parents 912319e + a2e25c6 commit 10ec7bd

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
@@ -18194,7 +18194,8 @@ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT |
1819418194
VkBuffer buf;
1819518195
VmaAllocation alloc;
1819618196
VmaAllocationInfo allocInfo;
18197-
vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
18197+
VkResult result = vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
18198+
// Check result...
1819818199

1819918200
VkMemoryPropertyFlags memPropFlags;
1820018201
vmaGetAllocationMemoryProperties(allocator, alloc, &memPropFlags);
@@ -18205,10 +18206,24 @@ if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
1820518206

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

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

0 commit comments

Comments
 (0)