RavEngine
Loading...
Searching...
No Matches
CmRadixSort.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef CM_RADIX_SORT_H
30#define CM_RADIX_SORT_H
31
32#include "common/PxPhysXCommonConfig.h"
33
34namespace physx
35{
36namespace Cm
37{
38 enum RadixHint
39 {
40 RADIX_SIGNED,
41 RADIX_UNSIGNED,
42
43 RADIX_FORCE_DWORD = 0x7fffffff
44 };
45
46#define INVALIDATE_RANKS mCurrentSize|=0x80000000
47#define VALIDATE_RANKS mCurrentSize&=0x7fffffff
48#define CURRENT_SIZE (mCurrentSize&0x7fffffff)
49#define INVALID_RANKS (mCurrentSize&0x80000000)
50
51 class PX_PHYSX_COMMON_API RadixSort
52 {
53 PX_NOCOPY(RadixSort)
54 public:
55 RadixSort();
56 virtual ~RadixSort();
57 // Sorting methods
58 RadixSort& Sort(const PxU32* input, PxU32 nb, RadixHint hint=RADIX_SIGNED);
59 RadixSort& Sort(const float* input, PxU32 nb);
60
62 PX_FORCE_INLINE const PxU32* GetRanks() const { return mRanks; }
63
65 PX_FORCE_INLINE PxU32* GetRecyclable() const { return mRanks2; }
66
68 PX_FORCE_INLINE PxU32 GetNbTotalCalls() const { return mTotalCalls; }
70 PX_FORCE_INLINE PxU32 GetNbHits() const { return mNbHits; }
71
72 PX_FORCE_INLINE void invalidateRanks() { INVALIDATE_RANKS; }
73
74 bool SetBuffers(PxU32* ranks0, PxU32* ranks1, PxU32* histogram1024, PxU32** links256);
75 protected:
77 PxU32* mRanks;
78 PxU32* mRanks2;
79 PxU32* mHistogram1024;
80 PxU32** mLinks256;
81 // Stats
83 PxU32 mNbHits;
84
85 // Stack-radix
86 bool mDeleteRanks;
87 };
88
89 #define StackRadixSort(name, ranks0, ranks1) \
90 RadixSort name; \
91 PxU32 histogramBuffer[1024]; \
92 PxU32* linksBuffer[256]; \
93 name.SetBuffers(ranks0, ranks1, histogramBuffer, linksBuffer);
94
95 class PX_PHYSX_COMMON_API RadixSortBuffered : public RadixSort
96 {
97 public:
100
101 void reset();
102
103 RadixSortBuffered& Sort(const PxU32* input, PxU32 nb, RadixHint hint=RADIX_SIGNED);
104 RadixSortBuffered& Sort(const float* input, PxU32 nb);
105
106 private:
108 RadixSortBuffered& operator=(const RadixSortBuffered& object);
109
110 // Internal methods
111 void CheckResize(PxU32 nb);
112 bool Resize(PxU32 nb);
113 };
114}
115}
116
117#endif // CM_RADIX_SORT_H
Definition CmRadixSort.h:96
Definition CmRadixSort.h:52
PX_FORCE_INLINE PxU32 GetNbHits() const
Returns the number of eraly exits due to temporal coherence.
Definition CmRadixSort.h:70
PxU32 mCurrentSize
Current size of the indices list.
Definition CmRadixSort.h:76
PxU32 * mRanks
Two lists, swapped each pass.
Definition CmRadixSort.h:77
PX_FORCE_INLINE PxU32 * GetRecyclable() const
mIndices2 gets trashed on calling the sort routine, but otherwise you can recycle it the way you want...
Definition CmRadixSort.h:65
PxU32 mTotalCalls
Total number of calls to the sort routine.
Definition CmRadixSort.h:82
PxU32 mNbHits
Number of early exits due to coherence.
Definition CmRadixSort.h:83
PX_FORCE_INLINE PxU32 GetNbTotalCalls() const
Returns the total number of calls to the radix sorter.
Definition CmRadixSort.h:68
PX_FORCE_INLINE const PxU32 * GetRanks() const
Access to results. mRanks is a list of indices in sorted order, i.e. in the order you may further pro...
Definition CmRadixSort.h:62
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39