| 788 | } |
| 789 | |
| 790 | void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseDistance, DrawCallsList& list, const RenderListBuffer<DrawCall>& drawCalls, DrawCallsListType listType, DrawPass pass) |
| 791 | { |
| 792 | PROFILE_CPU(); |
| 793 | PROFILE_MEM(GraphicsCommands); |
| 794 | const auto* drawCallsData = drawCalls.Get(); |
| 795 | const auto* listData = list.Indices.Get(); |
| 796 | const int32 listSize = list.Indices.Count(); |
| 797 | ZoneValue(listSize); |
| 798 | |
| 799 | // Use shared memory from renderer allocator |
| 800 | RenderListAlloc allocs[3]; |
| 801 | uint64* sortedKeys = allocs[0].Init<uint64>(this, listSize); |
| 802 | uint64* tempKeys = allocs[1].Init<uint64>(this, listSize); |
| 803 | int32* tempIndices = allocs[2].Init<int32>(this, listSize); |
| 804 | |
| 805 | // Setup sort keys |
| 806 | if (reverseDistance) |
| 807 | { |
| 808 | if (listType == DrawCallsListType::Forward) |
| 809 | { |
| 810 | // Transparency uses distance to take precedence over batching efficiency for correct draw order |
| 811 | for (int32 i = 0; i < listSize; i++) |
| 812 | { |
| 813 | const DrawCall& drawCall = drawCallsData[listData[i]]; |
| 814 | PackedSortKey key = *(PackedSortKey*)&drawCall.SortKey; |
| 815 | PackedSortKeyDistance forwardKey; |
| 816 | forwardKey.BatchKey = key.BatchKey; |
| 817 | forwardKey.DistanceKey = key.DistanceKey ^ MAX_uint32; // Reverse depth |
| 818 | forwardKey.DrawKey = key.DrawKey; |
| 819 | forwardKey.SortKey = key.SortKey ^ MAX_uint8; // Reverse sort order |
| 820 | sortedKeys[i] = *(uint64*)&forwardKey; |
| 821 | } |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | for (int32 i = 0; i < listSize; i++) |
| 826 | { |
| 827 | const DrawCall& drawCall = drawCallsData[listData[i]]; |
| 828 | PackedSortKey key = *(PackedSortKey*)&drawCall.SortKey; |
| 829 | key.DistanceKey ^= MAX_uint32; // Reverse depth |
| 830 | key.SortKey ^= MAX_uint8; // Reverse sort order |
| 831 | sortedKeys[i] = *(uint64*)&key; |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | else |
| 836 | { |
| 837 | for (int32 i = 0; i < listSize; i++) |
| 838 | sortedKeys[i] = drawCallsData[listData[i]].SortKey; |
| 839 | } |
| 840 | |
| 841 | // Sort draw calls indices |
| 842 | int32* resultIndices = list.Indices.Get(); |
| 843 | Sorting::RadixSort(sortedKeys, resultIndices, tempKeys, tempIndices, listSize); |
| 844 | if (resultIndices != list.Indices.Get()) |
| 845 | Platform::MemoryCopy(list.Indices.Get(), resultIndices, sizeof(int32) * listSize); |
| 846 | |
| 847 | // Perform draw calls batching |
no test coverage detected