Optimize draw calls sorting in opaque passes

This commit is contained in:
Wojtek Figat
2024-04-16 17:18:18 +02:00
parent 26c2b33fc0
commit 794b007170
2 changed files with 11 additions and 5 deletions

View File

@@ -565,7 +565,7 @@ namespace
}
}
void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer<DrawCall>& drawCalls)
void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer<DrawCall>& 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)

View File

@@ -519,7 +519,8 @@ public:
/// <param name="listType">The collected draw calls list type.</param>
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);
}
/// <summary>
@@ -529,7 +530,8 @@ public:
/// <param name="reverseDistance">If set to <c>true</c> reverse draw call distance to the view. Results in back to front sorting.</param>
/// <param name="list">The collected draw calls indices list.</param>
/// <param name="drawCalls">The collected draw calls list.</param>
void SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer<DrawCall>& drawCalls);
/// <param name="stable">If set to <c>true</c> draw batches will be additionally sorted to prevent any flickering, otherwise Depth Buffer will smooth out any non-stability in sorting.</param>
void SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer<DrawCall>& drawCalls, bool stable = false);
/// <summary>
/// Executes the collected draw calls.