对于使用vulkan-hpp和Vulkan Memory Allocator的vulkan渲染器我试图使用暂存缓冲区将顶点数据传输到gpu。在不使用中间登台缓冲区的情况下,传输工作正常,并且可以毫无问题地渲染网格(仅使用VMA_MEMORY_USAGE_CPU_TO_GPU创建单个缓冲区)。但是通过调用vkCmdCopyBuffer,生成的vertexBuffer只包含0(使用NSight检查)。考虑到可能需要使用障碍进行同步,我尝试了以下代码,但仍然遇到同样的问题。
也许需要将执行复制的命令缓冲区与后面的命令缓冲区绑定同步并从顶点缓冲区中获取?虽然我没有看到这个完成或在我遇到的任何教程中提到过。
// vulkan initialization
// VmaAllocator initialization
std::vector<Vertex> vertices = { /*filled*/ };
auto vertexBufferSize = vertices.size() * sizeof(Vertex);
vk::Buffer stagingBuffer;
auto ci = vk::BufferCreateInfo().
setUsage(vk::BufferUsageFlagBits::eTransferSrc).
setSharingMode(vk::SharingMode::eExclusive).
setSize(vertexBufferSize );
VmaAllocationCreateInfo allocationCI = {};
allocationCI.usage = VMA_MEMORY_USAGE_CPU_ONLY;
vmaCreateBuffer(allocator, (VkBufferCreateInfo*)&ci,
&allocationCI, (VkBuffer*)&stagingBuffer, &allocation, nullptr);
void* pVBufMem = nullptr;
vmaMapMemory(allocator, allocation, &pVBufMem);
std::memcpy(pVBufMem, vertices.size(), vertexBufferSize );
vmaUnmapMemory(allocator, allocation);
vk::Buffer vertexBuffer;
ci = vk::BufferCreateInfo().
setUsage(vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eVertexBuffer).
setSharingMode(vk::SharingMode::eExclusive).
setSize(vertexBufferSize);
allocationCI = {};
allocationCI.usage = VMA_MEMORY_USAGE_GPU_ONLY;
vmaCreateBuffer(allocator, (VkBufferCreateInfo*)&ci,
&allocationCI, (VkBuffer*)&vertexBuffer, &allocation, nullptr);
// copy staging buffer to vertex buffer
auto allocCI = vk::CommandBufferAllocateInfo().
setCommandBufferCount(1).
setCommandPool(pool).
setLevel(vk::CommandBufferLevel::ePrimary);
auto cmd = device.allocateCommandBuffers(allocCI)[0];
cmd.begin({ vk::CommandBufferUsageFlagBits::eOneTimeSubmit });
// barrier host write -> copyBuffer read
auto bufferMemoryBarrier = vk::BufferMemoryBarrier()
.setBuffer(stagingBuffer)
.setSize(vertexBufferSize)
.setSrcAccessMask(vk::AccessFlagBits::eMemoryWrite)
.setDstAccessMask(vk::AccessFlagBits::eTransferRead)
.setSrcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED)
.setDstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED);
cmd.pipelineBarrier(
vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {},
{ }, { bufferMemoryBarrier }, { });
cmd.copyBuffer(stagingBuffer, vertexBuffer, { 0, 0, vertexBufferSize });
// copyBuffer write -> vertex read
bufferMemoryBarrier = vk::BufferMemoryBarrier()
.setBuffer(vertexBuffer)
.setSize(vertexBufferSize)
.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite)
.setDstAccessMask(vk::AccessFlagBits::eVertexAttributeRead)
.setSrcQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED)
.setDstQueueFamilyIndex(VK_QUEUE_FAMILY_IGNORED);
cmd.pipelineBarrier(
vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {},
{ }, { bufferMemoryBarrier }, { });
cmd.end();
auto submitInfo = vk::SubmitInfo().
setCommandBufferCount(1).
setPCommandBuffers(&cmdBuffer);
queue.submit({ submitInfo }, {});
queue.waitIdle();
这是固定的。 copybuffer的第三个参数未正确初始化。正确的电话应该是这样的:
cmd.copyBuffer(stagingBuffer, vertexBuffer, {{ 0, 0, vertexBufferSize }});
这条线
cmd.copyBuffer(stagingBuffer, vertexBuffer, { 0, 0, vertexBufferSize });
实际上创建三个不同的复制区域