Vulkan 中绘制调用之间的同步

Synchronization between drawcalls in Vulkan

据我所知,如果我有一个 drawcall 以某种方式使用任何先前 drawcall 的结果或写入相同的渲染目标(帧缓冲区),那么我需要确保后面的 drawcall 看到所有先前绘制调用的记忆效应。

但是,当我渲染一个包含一堆对象的场景时,每个这样的对象都是一个绘制调用,所有这些绘制调用都写入同一个帧缓冲区。

是否需要在每次绘制调用后发出内存屏障?

例如 Sascha Willem 的 pbrbasic 示例有此代码(有点简化),我没有看到任何管道障碍:

VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));

vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);

VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);

VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);

VkDeviceSize offsets[1] = { 0 };

vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &models.objects[models.objectIndex].vertices.buffer, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], models.objects[models.objectIndex].indices.buffer, 0, VK_INDEX_TYPE_UINT32);

Material mat = materials[materialIndex];

for (uint32_t y = 0; y < GRID_DIM; y++) {
    for (uint32_t x = 0; x < GRID_DIM; x++) {
        glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f);
        vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
        mat.params.metallic = glm::clamp((float)x / (float)(GRID_DIM - 1), 0.1f, 1.0f);
        mat.params.roughness = glm::clamp((float)y / (float)(GRID_DIM - 1), 0.05f, 1.0f);
        vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material::PushBlock), &mat);
        vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
    }
}

drawUI(drawCmdBuffers[i]);

vkCmdEndRenderPass(drawCmdBuffers[i]);

VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));

是否可以保证逻辑上紧随其后的绘制调用将看到逻辑上位于其之前的绘制调用的记忆效应?

所以问题是,我什么时候需要在不同的绘制调用之间进行同步?

uses the results of any previous drawcall or writes to the same render target

它们不是同一件事。 side-effects 渲染操作(通过图像 load/store 或 SSBO 修改的内容)和写入帧缓冲区的内容在同步方面有非常不同的需求。

导致将值写入帧缓冲区的固定函数操作(剪刀测试、深度测试、混合等)受制于rasterization order: they must happen atomically and in that particular order, within a given primitive and for a particular sample potentially being written to the frame buffer. And the execution of such operations must also respect primitive order:绘图命令生成的图元顺序,但是还有图元的顺序 绘图命令中。

因此,就渲染而言,绘制调用将尊重同一子通道内先前绘制调用的结果(子通道之间由显式子通道依赖性控制)。您不需要做任何特别的事情来进行混合。如果你有两个重叠的三角形,一个按原始顺序发生在另一个之后,则第二次绘制的混合将使用第一次绘制的结果。它只是工作。

您的着色器的任何其他写入都需要显式同步。