diff --git a/implementations/Crafter.Graphics-RenderingElement3D.cpp b/implementations/Crafter.Graphics-RenderingElement3D.cpp index 9622c94..e1754aa 100644 --- a/implementations/Crafter.Graphics-RenderingElement3D.cpp +++ b/implementations/Crafter.Graphics-RenderingElement3D.cpp @@ -295,13 +295,6 @@ void RenderingElement3D::BuildTLAS(VkCommandBuffer cmd, std::uint32_t index, RTB VkAccelerationStructureBuildRangeInfoKHR* tlasRangeInfoPP = &tlasRangeInfo; Device::vkCmdBuildAccelerationStructuresKHR(cmd, 1, &tlasBuildGeometryInfo, &tlasRangeInfoPP); - vkCmdPipelineBarrier( - cmd, - VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, - VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, - 0, - 0, nullptr, - 0, nullptr, - 0, nullptr - ); + const TlasReadBarrier readBarrier = BuildTlasReadBarrier(); + vkCmdPipelineBarrier(cmd, readBarrier.srcStage, readBarrier.dstStage, 0, 1, &readBarrier.memory, 0, nullptr, 0, nullptr); } \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-RenderingElement3D.cppm b/interfaces/Crafter.Graphics-RenderingElement3D.cppm index f350aa3..dee9ebf 100644 --- a/interfaces/Crafter.Graphics-RenderingElement3D.cppm +++ b/interfaces/Crafter.Graphics-RenderingElement3D.cppm @@ -134,6 +134,29 @@ export namespace Crafter { static void Add(RenderingElement3D* e); static void Remove(RenderingElement3D* e); }; + + // The dependency BuildTLAS records after its build, ordering it before + // the shaders that read the TLAS later in the same command buffer: + // traceRayEXT in a ray-tracing pass, rayQueryEXT in a compute pass. It + // has to carry the acceleration-structure memory dependency; ordering + // execution alone never makes the build's writes visible to those reads. + struct TlasReadBarrier { + VkPipelineStageFlags srcStage; + VkPipelineStageFlags dstStage; + VkMemoryBarrier memory; + }; + + inline TlasReadBarrier BuildTlasReadBarrier() { + return { + .srcStage = VK_PIPELINE_STAGE_ACCELERATION_STRUCTURE_BUILD_BIT_KHR, + .dstStage = VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, + .memory = { + .sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, + .srcAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_WRITE_BIT_KHR, + .dstAccessMask = VK_ACCESS_ACCELERATION_STRUCTURE_READ_BIT_KHR, + }, + }; + } } #endif // !CRAFTER_GRAPHICS_WINDOW_DOM