对于使用vulkan-hpp和Vulkan内存分配器的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();
答案 0 :(得分:0)
它是固定的。 copybuffer的第三个参数未正确初始化。正确的呼叫应如下所示:
cmd.copyBuffer(stagingBuffer, vertexBuffer, {{ 0, 0, vertexBufferSize }});
行
cmd.copyBuffer(stagingBuffer, vertexBuffer, { 0, 0, vertexBufferSize });
实际上创建了三个不同的复制区域。