vkCmdCopyImageToBuffer 产生纯色图像
vkCmdCopyImageToBuffer results in solid color image
我尝试替代 Vulkan Sascha Willems Render Headless example API 以在主机端使用缓冲区而不是图像。我将用于复制图像的代码(第 687 - 775 行)更改为以下内容:
//create buffer
VkBuffer buffer;
VkDeviceMemory memory;
createBuffer(
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&buffer,
&memory,
width * height * 4,
nullptr
);
//create cmd buf
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo(commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);
VkCommandBuffer copyCmd;
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, ©Cmd));
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VK_CHECK_RESULT(vkBeginCommandBuffer(copyCmd, &cmdBufInfo));
//build copy cmd
VkBufferImageCopy bufImgCpy{};
bufImgCpy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
bufImgCpy.imageSubresource.layerCount = 1;
bufImgCpy.imageExtent.width = width;
bufImgCpy.imageExtent.height = height;
bufImgCpy.imageExtent.depth = 1;
vkCmdCopyImageToBuffer(copyCmd,
colorAttachment.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
buffer, 1, &bufImgCpy);
VK_CHECK_RESULT(vkEndCommandBuffer(copyCmd));
submitWork(copyCmd, queue);
//map buffer
vkMapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, (void**)&imagedata);
然而,虽然原始代码有效,但我的代码只给我一张只有背景 color/clear 值的图像。我已经查阅了规范,但找不到关于我做错了什么的提示。
我 运行 使用 AMD RTX 480(驱动程序:Radeon 19.12.12),Windows 10 1909,LunarG SDK 版本 1.1.114.0。
原文在我看来不正确。它应该通过 VK_PIPELINE_STAGE_HOST_BIT
.
执行 内存域操作
您的代码还应包含 vkCmdPipelineBarrier
和 dstStage = VK_PIPELINE_STAGE_HOST_BIT
。
我尝试替代 Vulkan Sascha Willems Render Headless example API 以在主机端使用缓冲区而不是图像。我将用于复制图像的代码(第 687 - 775 行)更改为以下内容:
//create buffer
VkBuffer buffer;
VkDeviceMemory memory;
createBuffer(
VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&buffer,
&memory,
width * height * 4,
nullptr
);
//create cmd buf
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo(commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);
VkCommandBuffer copyCmd;
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, ©Cmd));
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VK_CHECK_RESULT(vkBeginCommandBuffer(copyCmd, &cmdBufInfo));
//build copy cmd
VkBufferImageCopy bufImgCpy{};
bufImgCpy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
bufImgCpy.imageSubresource.layerCount = 1;
bufImgCpy.imageExtent.width = width;
bufImgCpy.imageExtent.height = height;
bufImgCpy.imageExtent.depth = 1;
vkCmdCopyImageToBuffer(copyCmd,
colorAttachment.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
buffer, 1, &bufImgCpy);
VK_CHECK_RESULT(vkEndCommandBuffer(copyCmd));
submitWork(copyCmd, queue);
//map buffer
vkMapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, (void**)&imagedata);
然而,虽然原始代码有效,但我的代码只给我一张只有背景 color/clear 值的图像。我已经查阅了规范,但找不到关于我做错了什么的提示。
我 运行 使用 AMD RTX 480(驱动程序:Radeon 19.12.12),Windows 10 1909,LunarG SDK 版本 1.1.114.0。
原文在我看来不正确。它应该通过 VK_PIPELINE_STAGE_HOST_BIT
.
您的代码还应包含 vkCmdPipelineBarrier
和 dstStage = VK_PIPELINE_STAGE_HOST_BIT
。