diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index 2fa1b98ca..679e2317b 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -565,7 +565,7 @@ namespace } } -void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer& drawCalls) +void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer& drawCalls, bool stable) { PROFILE_CPU(); const auto* drawCallsData = drawCalls.Get(); @@ -638,8 +638,12 @@ void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseD i += batchSize; } - // Sort draw calls batches by depth - Sorting::MergeSort(list.Batches, &SortingBatches); + // When using depth buffer draw calls are already almost ideally sorted by Radix Sort but transparency needs more stability to prevent flickering + if (stable) + { + // Sort draw calls batches by depth + Sorting::MergeSort(list.Batches, &SortingBatches); + } } FORCE_INLINE bool CanUseInstancing(DrawPass pass) diff --git a/Source/Engine/Renderer/RenderList.h b/Source/Engine/Renderer/RenderList.h index 854aca986..ef2c9d390 100644 --- a/Source/Engine/Renderer/RenderList.h +++ b/Source/Engine/Renderer/RenderList.h @@ -519,7 +519,8 @@ public: /// The collected draw calls list type. API_FUNCTION() FORCE_INLINE void SortDrawCalls(API_PARAM(Ref) const RenderContext& renderContext, bool reverseDistance, DrawCallsListType listType) { - SortDrawCalls(renderContext, reverseDistance, DrawCallsLists[(int32)listType], DrawCalls); + const bool stable = listType == DrawCallsListType::Forward; + SortDrawCalls(renderContext, reverseDistance, DrawCallsLists[(int32)listType], DrawCalls, stable); } /// @@ -529,7 +530,8 @@ public: /// If set to true reverse draw call distance to the view. Results in back to front sorting. /// The collected draw calls indices list. /// The collected draw calls list. - void SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer& drawCalls); + /// If set to true draw batches will be additionally sorted to prevent any flickering, otherwise Depth Buffer will smooth out any non-stability in sorting. + void SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer& drawCalls, bool stable = false); /// /// Executes the collected draw calls.