c++ 如何在Vulkan中渲染到纹理?

nmpmafwu  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(286)

我需要在我的项目中渲染texttrue。
Pass 1:绘制颜色为(0.5,0.5,0.5,1.0)的颜色正方形,渲染目标为纹理。
第2遍:使用第1遍的纹理绘制一个有纹理的正方形,渲染目标是一个表面。
预期结果:

但我得到了奇怪的结果如下:

使用以下命令创建映像:

  1. VkImage hImageTexture = VK_NULL_HANDLE;
  2. VkImageCreateInfo imageCreateInfo = {0};
  3. imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
  4. imageCreateInfo.pNext = nullptr;
  5. imageCreateInfo.flags = 0;
  6. imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
  7. imageCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
  8. imageCreateInfo.extent.width = width;
  9. imageCreateInfo.extent.height = height;
  10. imageCreateInfo.extent.depth = 1;
  11. imageCreateInfo.mipLevels = 1;
  12. imageCreateInfo.arrayLayers = 1;
  13. imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
  14. imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
  15. imageCreateInfo.usage =
  16. VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
  17. VK_IMAGE_USAGE_TRANSFER_DST_BIT |
  18. VK_IMAGE_USAGE_SAMPLED_BIT;
  19. imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  20. imageCreateInfo.queueFamilyIndexCount = 0;
  21. imageCreateInfo.pQueueFamilyIndices = nullptr;
  22. imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  23. //
  24. VkResult res = vkCreateImage(hDevice , &imageCreateInfo , nullptr , &hImageTexture);

使用以下命令创建图像视图:

  1. VkImageView hImageViewTexture = VK_NULL_HANDLE;
  2. VkImageViewCreateInfo imageViewCreateInfo = {0};
  3. imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
  4. imageViewCreateInfo.pNext = nullptr;
  5. imageViewCreateInfo.flags = 0;
  6. imageViewCreateInfo.image = hImageTexture;
  7. imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
  8. imageViewCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
  9. imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_R;
  10. imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_G;
  11. imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_B;
  12. imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_A;
  13. imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  14. imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
  15. imageViewCreateInfo.subresourceRange.levelCount = 1;
  16. imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
  17. imageViewCreateInfo.subresourceRange.layerCount = 1;
  18. VkResult res=vkCreateImageView(
  19. hDevice,
  20. &imageViewCreateInfo,
  21. NULL,
  22. &hImageViewTexture);

渲染循环如下:

  1. //Pass1
  2. //Clear texture color
  3. vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
  4. vkCmdClearColorImage();//Clear color(0.0 , 0.0 ,0.0 , 1.0)
  5. vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
  6. //Change texture's barrier
  7. VkImageMemoryBarrier imageMemoryBarrierForOutput = {0};
  8. imageMemoryBarrierForOutput.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  9. imageMemoryBarrierForOutput.pNext = nullptr;
  10. imageMemoryBarrierForOutput.srcAccessMask = 0;
  11. imageMemoryBarrierForOutput.dstAccessMask =
  12. VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  13. imageMemoryBarrierForOutput.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  14. imageMemoryBarrierForOutput.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
  15. imageMemoryBarrierForOutput.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  16. imageMemoryBarrierForOutput.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  17. imageMemoryBarrierForOutput.image = hImageTexture;
  18. imageMemoryBarrierForOutput.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  19. imageMemoryBarrierForOutput.subresourceRange.baseMipLevel = 0;
  20. imageMemoryBarrierForOutput.subresourceRange.levelCount = 1;
  21. imageMemoryBarrierForOutput.subresourceRange.baseArrayLayer = 0;
  22. imageMemoryBarrierForOutput.subresourceRange.layerCount = 1;
  23. vkCmdPipelineBarrier(
  24. hCommandBuffer,
  25. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  26. VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
  27. 0,
  28. 0,
  29. nullptr,
  30. 0,
  31. nullptr,
  32. 1,
  33. &imageMemoryBarrierForOutput);
  34. //draw
  35. vkCmdBeginRenderPass();
  36. vkCmdSetViewport();
  37. vkCmdSetScissor();
  38. vkCmdBindPipeline();
  39. vkCmdBindDescriptorSets();
  40. vkCmdBindVertexBuffers();
  41. vkCmdBindIndexBuffer();
  42. vkCmdDrawIndexed();
  43. vkCmdEndRenderPass();
  44. //
  45. //Pass2
  46. //Clear surface color
  47. vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
  48. vkCmdClearColorImage();//Clear color(0.5 , 0.5 ,1.0 , 1.0)
  49. vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
  50. //Change texture's barrier
  51. VkImageMemoryBarrier imageMemoryBarrierForInput = {0};
  52. imageMemoryBarrierForInput.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
  53. imageMemoryBarrierForInput.pNext = nullptr;
  54. imageMemoryBarrierForInput.srcAccessMask = 0;
  55. imageMemoryBarrierForInput.dstAccessMask =
  56. VK_ACCESS_SHADER_READ_BIT |
  57. VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
  58. imageMemoryBarrierForInput.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
  59. imageMemoryBarrierForInput.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
  60. imageMemoryBarrierForInput.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  61. imageMemoryBarrierForInput.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
  62. imageMemoryBarrierForInput.image = hImageTexture;
  63. imageMemoryBarrierForInput.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
  64. imageMemoryBarrierForInput.subresourceRange.baseMipLevel = 0;
  65. imageMemoryBarrierForInput.subresourceRange.levelCount = 1;
  66. imageMemoryBarrierForInput.subresourceRange.baseArrayLayer = 0;
  67. imageMemoryBarrierForInput.subresourceRange.layerCount = 1;
  68. vkCmdPipelineBarrier(
  69. hCommandBuffer,
  70. VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
  71. VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
  72. 0,
  73. 0,
  74. nullptr,
  75. 0,
  76. nullptr,
  77. 1,
  78. &imageMemoryBarrierForInput);
  79. //draw
  80. vkCmdBeginRenderPass();
  81. vkCmdSetViewport();
  82. vkCmdSetScissor();
  83. vkCmdBindPipeline();
  84. vkCmdBindDescriptorSets();
  85. vkCmdBindVertexBuffers();
  86. vkCmdBindIndexBuffer();
  87. vkCmdDrawIndexed();
  88. vkCmdEndRenderPass();

Pass 1顶点着色器:

  1. #version 450
  2. layout (location=0) in vec4 inPos;
  3. layout (location=0) out vec4 outPos;
  4. void main(void)
  5. {
  6. outPos = float4(inPos.xy , 0.0 , 1.0);
  7. gl_Position = outPos;
  8. }

Pass 1片段着色器:

  1. #version 450
  2. layout (location=0) in vec4 inPos;
  3. layout (location=0) out vec4 outColor;
  4. void main(void)
  5. {
  6. outColor = float4(0.5 , 0.5 , 0.5 , 1.0);
  7. }

Pass 2顶点着色器:

  1. #version 450
  2. layout (location=0) in vec4 inPos;
  3. layout (location=1) in vec2 inUV;
  4. //
  5. layout (location=0) out vec4 outPos;
  6. layout (location=1) out vec2 outUV;
  7. //
  8. void main(void)
  9. {
  10. outPos = inPos;
  11. outUV = inUV;
  12. //
  13. gl_Position=outPos;
  14. }

Pass 2片段着色器:

  1. #version 450
  2. //
  3. layout (location=0) in vec4 inPos;
  4. layout (location=1) in vec2 inUV;
  5. //
  6. layout (binding=1) uniform sampler2D inTex;
  7. layout (location=0) out vec4 outColor;
  8. void main(void)
  9. {
  10. outColor = texture(inTex , inUV);
  11. }

如果我将图像平铺更改为VK_IMAGE_TILING_LINEAR,则会得到以下结果:

如果将图像平铺更改为VK_IMAGE_TILING_LINEAR而不透明(pass 1为透明),则结果正确!
我犯了什么错?

**编辑:**我添加了一些代码,我如何改变纹理的障碍,在渲染循环。
**编辑:**我在创建渲染过程时设置依赖项如下

  1. VkSubpassDependency subpassDependency[2];
  2. subpassDependency[0].srcSubpass = VK_SUBPASS_EXTERNAL;
  3. subpassDependency[0].dstSubpass = 0;
  4. subpassDependency[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  5. subpassDependency[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  6. subpassDependency[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
  7. subpassDependency[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  8. subpassDependency[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
  9. //
  10. subpassDependency[1].srcSubpass = 0;
  11. subpassDependency[1].dstSubpass = VK_SUBPASS_EXTERNAL;
  12. subpassDependency[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  13. subpassDependency[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  14. subpassDependency[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  15. subpassDependency[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
  16. subpassDependency[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;

但没什么可改变的

oxf4rvwz

oxf4rvwz1#

这里是一个图形-图形依赖于第一遍输出颜色附件。
第二子通道着色器读取操作必须等待第一子通道颜色附件写入操作完成。

  1. VkSubpassDependency deps[...];
  2. //other values to handle external dependencies
  3. ...
  4. // the src operations will refer to the first subpass
  5. deps[0].srcSubpass = 0;
  6. // the dst operations will refer to the second subpass
  7. deps[0].dstSubpass = 1;
  8. // the operations from the first subpass that will have to be completed
  9. // the writes to the color attachment in the color attachment output stage
  10. deps[0].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
  11. deps[0].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
  12. // this operations will wait on those specified in the previous lines
  13. // the reads issued during the fragment shader will have to wait
  14. // until all the writes have been completed
  15. deps[0].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
  16. deps[0].dstAccessMask = VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
  17. // do not wait for every write to have finished
  18. // if all writes in a region have been completed
  19. // then the reads can start (only in that region)
  20. deps[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;

这将负责子过程之间的同步以及布局 * 在一个帧 * 中的更改,因为子过程指定附件的布局。
然后,您需要向管道发出信号,表示您打算移动到下一个渲染过程。

  1. //draw
  2. vkCmdBeginRenderPass();
  3. vkCmdSetViewport();
  4. vkCmdSetScissor();
  5. vkCmdBindPipeline();
  6. vkCmdBindDescriptorSets();
  7. vkCmdBindVertexBuffers();
  8. vkCmdBindIndexBuffer();
  9. vkCmdDrawIndexed();
  10. vkNextSubpass();
  11. vkCmdSetViewport();
  12. vkCmdSetScissor();
  13. vkCmdBindPipeline();
  14. vkCmdBindDescriptorSets();
  15. vkCmdBindVertexBuffers();
  16. vkCmdBindIndexBuffer();
  17. vkCmdDrawIndexed();
  18. vkCmdEndRenderPass();

要了解常见的同步操作示例,可以查看vulkan synchronization examples page
您的问题是 * 第一次绘制写入颜色附件。第二次绘制将其作为片段着色器中的输入附件进行读取 *,因为您正在通过将其划分为更多的子过程来在一个渲染过程中完成所有操作。

展开查看全部
u5i3ibmn

u5i3ibmn2#

这里的评论建议使用管道屏障,但我不能立即看到您的使用错误。有两种方法可以帮助您诊断此问题:
1.添加debug utils层。如果您没有启用此图层,那么您部分处于黑暗中,因为您将无法获得所需的错误报告。调试层对于诊断错误至关重要:https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_EXT_debug_utils.html
1.安装Nsight Graphics并捕获您的应用程序。您可以单步执行每个绘制调用,查看图形状态,并检查渲染目标。您甚至可能会得到一个错误,它将您指向问题。在开发Vulkan代码时,使用这样的调试器将为您节省大量时间。https://developer.nvidia.com/nsight-graphics

相关问题