1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | |
31 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
32 | #include "CmRadixSortBuffered.h" |
33 | #include "PsAllocator.h" |
34 | |
35 | using namespace physx; |
36 | using namespace Cm; |
37 | |
38 | RadixSortBuffered::RadixSortBuffered() |
39 | : RadixSort() |
40 | { |
41 | } |
42 | |
43 | RadixSortBuffered::~RadixSortBuffered() |
44 | { |
45 | reset(); |
46 | } |
47 | |
48 | void RadixSortBuffered::reset() |
49 | { |
50 | // Release everything |
51 | if(mDeleteRanks) |
52 | { |
53 | PX_FREE_AND_RESET(mRanks2); |
54 | PX_FREE_AND_RESET(mRanks); |
55 | } |
56 | mCurrentSize = 0; |
57 | INVALIDATE_RANKS; |
58 | } |
59 | |
60 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
61 | /** |
62 | * Resizes the inner lists. |
63 | * \param nb [in] new size (number of dwords) |
64 | * \return true if success |
65 | */ |
66 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
67 | bool RadixSortBuffered::Resize(PxU32 nb) |
68 | { |
69 | if(mDeleteRanks) |
70 | { |
71 | // Free previously used ram |
72 | PX_FREE_AND_RESET(mRanks2); |
73 | PX_FREE_AND_RESET(mRanks); |
74 | |
75 | // Get some fresh one |
76 | mRanks = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*nb, "RadixSortBuffered:mRanks" )); |
77 | mRanks2 = reinterpret_cast<PxU32*>(PX_ALLOC(sizeof(PxU32)*nb, "RadixSortBuffered:mRanks2" )); |
78 | } |
79 | |
80 | return true; |
81 | } |
82 | |
83 | PX_INLINE void RadixSortBuffered::CheckResize(PxU32 nb) |
84 | { |
85 | PxU32 CurSize = CURRENT_SIZE; |
86 | if(nb!=CurSize) |
87 | { |
88 | if(nb>CurSize) Resize(nb); |
89 | mCurrentSize = nb; |
90 | INVALIDATE_RANKS; |
91 | } |
92 | } |
93 | |
94 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
95 | /** |
96 | * Main sort routine. |
97 | * This one is for integer values. After the call, mRanks contains a list of indices in sorted order, i.e. in the order you may process your data. |
98 | * \param input [in] a list of integer values to sort |
99 | * \param nb [in] number of values to sort, must be < 2^31 |
100 | * \param hint [in] RADIX_SIGNED to handle negative values, RADIX_UNSIGNED if you know your input buffer only contains positive values |
101 | * \return Self-Reference |
102 | */ |
103 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
104 | RadixSortBuffered& RadixSortBuffered::Sort(const PxU32* input, PxU32 nb, RadixHint hint) |
105 | { |
106 | // Checkings |
107 | if(!input || !nb || nb&0x80000000) return *this; |
108 | |
109 | // Resize lists if needed |
110 | CheckResize(nb); |
111 | |
112 | //Set histogram buffers. |
113 | PxU32 histogram[1024]; |
114 | PxU32* links[256]; |
115 | mHistogram1024=histogram; |
116 | mLinks256=links; |
117 | |
118 | RadixSort::Sort(input,nb,hint); |
119 | return *this; |
120 | } |
121 | |
122 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
123 | /** |
124 | * Main sort routine. |
125 | * This one is for floating-point values. After the call, mRanks contains a list of indices in sorted order, i.e. in the order you may process your data. |
126 | * \param input2 [in] a list of floating-point values to sort |
127 | * \param nb [in] number of values to sort, must be < 2^31 |
128 | * \return Self-Reference |
129 | * \warning only sorts IEEE floating-point values |
130 | */ |
131 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
132 | RadixSortBuffered& RadixSortBuffered::Sort(const float* input2, PxU32 nb) |
133 | { |
134 | // Checkings |
135 | if(!input2 || !nb || nb&0x80000000) return *this; |
136 | |
137 | // Resize lists if needed |
138 | CheckResize(nb); |
139 | |
140 | //Set histogram buffers. |
141 | PxU32 histogram[1024]; |
142 | PxU32* links[256]; |
143 | mHistogram1024=histogram; |
144 | mLinks256=links; |
145 | |
146 | RadixSort::Sort(input: input2,nb); |
147 | return *this; |
148 | } |
149 | |
150 | |