RavEngine
Loading...
Searching...
No Matches
BpBroadPhaseSapAux.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 BP_BROADPHASE_SAP_AUX_H
30#define BP_BROADPHASE_SAP_AUX_H
31
32#include "foundation/PxAssert.h"
33#include "foundation/PxIntrinsics.h"
34#include "foundation/PxUserAllocated.h"
35#include "BpBroadPhase.h"
36#include "BpBroadPhaseIntegerAABB.h"
37#include "foundation/PxBitMap.h"
38
39namespace physx
40{
41class PxcScratchAllocator;
42namespace Bp
43{
44#define ALIGN_SIZE_16(size) ((unsigned(size)+15)&(unsigned(~15)))
45
46#define NUM_SENTINELS 2
47
48#define BP_SAP_USE_PREFETCH 1//prefetch in batchUpdate
49
50#define BP_SAP_USE_OVERLAP_TEST_ON_REMOVES 1// "Useless" but faster overall because seriously reduces number of calls (from ~10000 to ~3 sometimes!)
51
52//Set 1 to test for group ids in batchCreate/batchUpdate so we can avoid group id test in ComputeCreatedDeletedPairsLists
53//Set 0 to neglect group id test in batchCreate/batchUpdate and delay test until ComputeCreatedDeletedPairsLists
54#define BP_SAP_TEST_GROUP_ID_CREATEUPDATE 1
55
56#define MAX_BP_HANDLE 0x3fffffff
57#define PX_REMOVED_BP_HANDLE 0x3ffffffd
58
59PX_FORCE_INLINE void setMinSentinel(ValType& v, BpHandle& d)
60{
61 v = 0x00000000;//0x00800000; //0x00800000 is -FLT_MAX but setting it to 0 means we don't crash when we get a value outside the float range.
62 d = (BP_INVALID_BP_HANDLE & ~1);
63}
64
65PX_FORCE_INLINE void setMaxSentinel(ValType& v, BpHandle& d)
66{
67 v = 0xffffffff;//0xff7fffff; //0xff7fffff is +FLT_MAX but setting it to 0xffffffff means we don't crash when we get a value outside the float range.
68 d = BP_INVALID_BP_HANDLE;
69}
70
71PX_FORCE_INLINE BpHandle setData(PxU32 owner_box_id, const bool is_max)
72{
73 BpHandle d = BpHandle(owner_box_id<<1);
74 if(is_max) d |= 1;
75 return d;
76}
77
78PX_FORCE_INLINE bool isSentinel(const BpHandle& d)
79{
80 return (d&~1)==(BP_INVALID_BP_HANDLE & ~1);
81}
82
83PX_FORCE_INLINE BpHandle isMax(const BpHandle& d)
84{
85 return BpHandle(d & 1);
86}
87
88PX_FORCE_INLINE BpHandle getOwner(const BpHandle& d)
89{
90 return BpHandle(d>>1);
91}
92
94{
95public:
96
99
100 BpHandle mMinMax[2];//mMinMax[0]=min, mMinMax[1]=max
101};
102
104{
105public:
108
109 void init(const PxU32 size);
110 void release();
111
112 void shrinkMemory();
113
114 const BroadPhasePair* AddPair (BpHandle id0, BpHandle id1, const PxU8 state);
115 bool RemovePair (BpHandle id0, BpHandle id1);
116 bool RemovePairs (const PxBitMap& removedAABBs);
117 const BroadPhasePair* FindPair (BpHandle id0, BpHandle id1) const;
118
119 PX_FORCE_INLINE PxU32 GetPairIndex(const BroadPhasePair* PX_RESTRICT pair) const
120 {
121 return (PxU32((size_t(pair) - size_t(mActivePairs)))/sizeof(BroadPhasePair));
122 }
123
124 BpHandle* mHashTable;
125 BpHandle* mNext;
126 PxU32 mHashSize;
127 PxU32 mHashCapacity;
128 PxU32 mMinAllowedHashCapacity;
129 BroadPhasePair* mActivePairs;
130 PxU8* mActivePairStates;
131 PxU32 mNbActivePairs;
132 PxU32 mActivePairsCapacity;
133 PxU32 mMask;
134
135 BroadPhasePair* FindPair (BpHandle id0, BpHandle id1, PxU32 hash_value) const;
136 void RemovePair (BpHandle id0, BpHandle id1, PxU32 hash_value, PxU32 pair_index);
137 void reallocPairs(const bool allocRequired);
138
139 enum
140 {
141 PAIR_INARRAY=1,
142 PAIR_REMOVED=2,
143 PAIR_NEW=4,
144 PAIR_UNKNOWN=8
145 };
146
147 PX_FORCE_INLINE bool IsInArray(const BroadPhasePair* PX_RESTRICT pair) const
148 {
149 const PxU8 state=mActivePairStates[pair-mActivePairs];
150 return state & PAIR_INARRAY ? true : false;
151 }
152 PX_FORCE_INLINE bool IsRemoved(const BroadPhasePair* PX_RESTRICT pair) const
153 {
154 const PxU8 state=mActivePairStates[pair-mActivePairs];
155 return state & PAIR_REMOVED ? true : false;
156 }
157 PX_FORCE_INLINE bool IsNew(const BroadPhasePair* PX_RESTRICT pair) const
158 {
159 const PxU8 state=mActivePairStates[pair-mActivePairs];
160 return state & PAIR_NEW ? true : false;
161 }
162 PX_FORCE_INLINE bool IsUnknown(const BroadPhasePair* PX_RESTRICT pair) const
163 {
164 const PxU8 state=mActivePairStates[pair-mActivePairs];
165 return state & PAIR_UNKNOWN ? true : false;
166 }
167
168 PX_FORCE_INLINE void ClearState(const BroadPhasePair* PX_RESTRICT pair)
169 {
170 mActivePairStates[pair-mActivePairs]=0;
171 }
172
173 PX_FORCE_INLINE void SetInArray(const BroadPhasePair* PX_RESTRICT pair)
174 {
175 mActivePairStates[pair-mActivePairs] |= PAIR_INARRAY;
176 }
177 PX_FORCE_INLINE void SetRemoved(const BroadPhasePair* PX_RESTRICT pair)
178 {
179 mActivePairStates[pair-mActivePairs] |= PAIR_REMOVED;
180 }
181 PX_FORCE_INLINE void SetNew(const BroadPhasePair* PX_RESTRICT pair)
182 {
183 mActivePairStates[pair-mActivePairs] |= PAIR_NEW;
184 }
185 PX_FORCE_INLINE void ClearInArray(const BroadPhasePair* PX_RESTRICT pair)
186 {
187 mActivePairStates[pair-mActivePairs] &= ~PAIR_INARRAY;
188 }
189 PX_FORCE_INLINE void ClearRemoved(const BroadPhasePair* PX_RESTRICT pair)
190 {
191 mActivePairStates[pair-mActivePairs] &= ~PAIR_REMOVED;
192 }
193 PX_FORCE_INLINE void ClearNew(const BroadPhasePair* PX_RESTRICT pair)
194 {
195 mActivePairStates[pair-mActivePairs] &= ~PAIR_NEW;
196 }
197};
198
200{
201 DataArray(BpHandle* data, PxU32 size, PxU32 capacity) : mData(data), mSize(size), mCapacity(capacity) {}
202
203 BpHandle* mData;
204 PxU32 mSize;
205 PxU32 mCapacity;
206
207 PX_NOINLINE void Resize(PxcScratchAllocator* scratchAllocator);
208
209 PX_FORCE_INLINE void AddData(const PxU32 data, PxcScratchAllocator* scratchAllocator)
210 {
211 if(mSize==mCapacity)
212 Resize(scratchAllocator);
213
214 PX_ASSERT(mSize<mCapacity);
215 mData[mSize++] = BpHandle(data);
216 }
217};
218
219void addPair(const BpHandle id0, const BpHandle id1, PxcScratchAllocator* scratchAllocator, SapPairManager& pairManager, DataArray& dataArray);
220void removePair(BpHandle id0, BpHandle id1, PxcScratchAllocator* scratchAllocator, SapPairManager& pairManager, DataArray& dataArray);
221
222void ComputeCreatedDeletedPairsLists
223(const Bp::FilterGroup::Enum* PX_RESTRICT boxGroups,
224 const BpHandle* PX_RESTRICT dataArray, const PxU32 dataArraySize,
225 PxcScratchAllocator* scratchAllocator,
226 BroadPhasePair* & createdPairsList, PxU32& numCreatedPairs, PxU32& maxNumCreatdPairs,
227 BroadPhasePair* & deletedPairsList, PxU32& numDeletedPairs, PxU32& maxNumDeletedPairs,
228 PxU32&numActualDeletedPairs,
229 SapPairManager& pairManager);
230
231 struct BoxX
232 {
233 PxU32 mMinX;
234 PxU32 mMaxX;
235 };
236
237 struct BoxYZ
238 {
239 PxU32 mMinY;
240 PxU32 mMinZ;
241 PxU32 mMaxY;
242 PxU32 mMaxZ;
243 };
244
245 struct AuxData
246 {
247 AuxData(PxU32 nb, const SapBox1D*const* PX_RESTRICT boxes, const BpHandle* PX_RESTRICT indicesSorted, const Bp::FilterGroup::Enum* PX_RESTRICT groupIds);
248 ~AuxData();
249
250 BoxX* mBoxX;
251 BoxYZ* mBoxYZ;
252 Bp::FilterGroup::Enum* mGroups;
253 PxU32* mRemap;
254 PxU32 mNb;
255 };
256
257void performBoxPruningNewNew( const AuxData* PX_RESTRICT auxData, PxcScratchAllocator* scratchAllocator,
258 const bool* lut, SapPairManager& pairManager, BpHandle*& dataArray, PxU32& dataArraySize, PxU32& dataArrayCapacity);
259
260void performBoxPruningNewOld( const AuxData* PX_RESTRICT auxData0, const AuxData* PX_RESTRICT auxData1, PxcScratchAllocator* scratchAllocator,
261 const bool* lut, SapPairManager& pairManager, BpHandle*& dataArray, PxU32& dataArraySize, PxU32& dataArrayCapacity);
262
263PX_FORCE_INLINE bool Intersect2D_Handle
264(const BpHandle bDir1Min, const BpHandle bDir1Max, const BpHandle bDir2Min, const BpHandle bDir2Max,
265 const BpHandle cDir1Min, const BpHandle cDir1Max, const BpHandle cDir2Min, const BpHandle cDir2Max)
266{
267 return (bDir1Max > cDir1Min && cDir1Max > bDir1Min &&
268 bDir2Max > cDir2Min && cDir2Max > bDir2Min);
269}
270
271} //namespace Bp
272
273} //namespace physx
274
275#endif //BP_BROADPHASE_SAP_AUX_H
Definition BpBroadPhaseSapAux.h:94
Definition BpBroadPhaseSapAux.h:104
Definition PxBitMap.h:52
Definition PxcScratchAllocator.h:42
#define PX_RESTRICT
Definition PxPreprocessor.h:355
#define PX_NOINLINE
Definition PxPreprocessor.h:346
#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
Definition BpBroadPhaseSapAux.h:246
Definition BpBroadPhaseSapAux.h:232
Definition BpBroadPhaseSapAux.h:238
Definition BpBroadPhase.h:82
Definition BpBroadPhaseSapAux.h:200