我在这里不太了解。: https://github.com/SaschaWillems/Vulkan/blob/master/examples/computeshader/computeshader.cpp
true
drawCmdBuffers [currentBuffer]在compute.commandBuffer之前运行,但是使用者drawCmdBuffers [currentBuffer]需要生产者compute.commandBuffer产生的textureComputetarget。
- 我不明白为什么在configure.commandBuffer之前调用drawCmdBuffers [currentBuffer]。
在下面的代码中,仅渲染第一帧,而右侧图片未获得textureComputetarget,因此以蓝色背景渲染。
void draw()
{
VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue,1,&submitInfo,VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
// Submit compute commands
// Use a fence to ensure that compute command buffer has finished executin before using it again
vkWaitForFences(device,&compute.fence,VK_TRUE,UINT64_MAX);
vkResetfences(device,&compute.fence);
VkSubmitInfo computeSubmitInfo = vks::initializers::submitInfo();
computeSubmitInfo.commandBufferCount = 1;
computeSubmitInfo.pCommandBuffers = &compute.commandBuffer;
VK_CHECK_RESULT(vkQueueSubmit(compute.queue,&computeSubmitInfo,compute.fence));
}
在调用void draw()
{
VulkanExampleBase::prepareFrame();
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
VK_CHECK_RESULT(vkQueueSubmit(queue,compute.fence));
sleep(1000) // <-------- Wait
}
时执行:
vkQueueSubmit(queue,VK_NULL_HANDLE)
- 等待VK_PIpelINE_STAGE_COMPUTE_SHADER_BIT,但是此阶段之前尚未执行,为什么管道没有卡住?是因为 之前没有管道,所以不需要等待吗?
第6.6节“管道壁垒” vkCmdpipelineBarrier是一个同步命令,它在提交到相同队列的命令之间或相同子传递中的命令之间插入依赖项。
VkImageMemoryBarrier imageMemoryBarrier = {};
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
// We won't be changing the layout of the image
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
imageMemoryBarrier.image = textureComputetarget.image;
imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT,1 };
imageMemoryBarrier.srcaccessMask = VK_accESS_SHADER_WRITE_BIT;
imageMemoryBarrier.dstaccessMask = VK_accESS_SHADER_READ_BIT;
vkCmdpipelineBarrier(
drawCmdBuffers[i],VK_PIpelINE_STAGE_COMPUTE_SHADER_BIT,VK_PIpelINE_STAGE_FRAGMENT_SHADER_BIT,VK_flaGS_NONE,nullptr,&imageMemoryBarrier);
vkCmdBeginRenderPass(drawCmdBuffers[i],&renderPassBeginInfo,VK_SUBPASS_CONTENTS_INLINE);
打印结果: 0x6000039c4a20、0x6000039c4a20 当前队列和compute.queue是同一队列,但是上面的代码可能会生成不同的队列。