RavEngine
Loading...
Searching...
No Matches
GuExtendedBucketPruner.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 GU_EXTENDED_BUCKET_PRUNER_H
30#define GU_EXTENDED_BUCKET_PRUNER_H
31
32#include "GuPrunerTypedef.h"
33#include "GuAABBTreeUpdateMap.h"
34#include "foundation/PxHashMap.h"
35#include "GuAABBTreeBounds.h"
36#include "GuSecondaryPruner.h"
37
38namespace physx
39{
40 class PxRenderOutput;
41
42namespace Gu
43{
44 class AABBTreeMergeData;
45
46 // Extended bucket pruner data, if an object belongs to the tree of trees, we need to
47 // remember node for the sub tree, the tree it belongs to and the main tree node
49 {
50 PxU32 mTimeStamp; // timestamp
51 TreeNodeIndex mSubTreeNode; // sub tree node index
52 PxU32 mMergeIndex; // index in bounds and merged trees array
53 };
54
55 // Merged tree structure, holds tree and its timeStamp, released when no objects is in the tree
56 // or timeStamped objects are released
58 {
59 AABBTree* mTree; // AABB tree
60 size_t mTimeStamp; //
61 };
62
63 // hashing function for PrunerPayload key
64 // PT: TODO: move this to PrunerPayload?
66 {
67 PX_FORCE_INLINE uint32_t operator()(const PrunerPayload& payload) const
68 {
69#if PX_P64_FAMILY
70// const PxU32 h0 = PxHash((const void*)payload.data[0]);
71// const PxU32 h1 = PxHash((const void*)payload.data[1]);
72 const PxU32 h0 = PxU32(PX_MAX_U32 & payload.data[0]);
73 const PxU32 h1 = PxU32(PX_MAX_U32 & payload.data[1]);
74 return physx::PxComputeHash(PxU64(h0) | (PxU64(h1) << 32));
75#else
76 return physx::PxComputeHash(PxU64(payload.data[0]) | (PxU64(payload.data[1]) << 32));
77#endif
78 }
79 PX_FORCE_INLINE bool equal(const PrunerPayload& k0, const PrunerPayload& k1) const
80 {
81 return (k0.data[0] == k1.data[0]) && (k0.data[1] == k1.data[1]);
82 }
83 };
84
85 // A.B. replace, this is useless, need to be able to traverse the map and release while traversing, also eraseAt failed
87
88 // Extended bucket pruner holds single objects in a bucket pruner and AABBtrees in a tree of trees.
89 // Base usage of ExtendedBucketPruner is for dynamic AABBPruner new objects, that did not make it
90 // into new tree. Single objects go directly into a bucket pruner, while merged AABBtrees
91 // go into a tree of trees.
92 // PT: TODO: this is not a Pruner (doesn't use the Pruner API) so its name should be e.g. "ExtendedBucketPrunerCore".
93 // And it's also not always using a bucket pruner... so the whole "ExtendedBucketPruner" name everywhere is wrong.
95 {
96 public:
97 ExtendedBucketPruner(PxU64 contextID, CompanionPrunerType type, const PruningPool* pool);
99
100 // release
101 void release();
102
103 // add single object into a bucket pruner directly
104 PX_FORCE_INLINE bool addObject(const PrunerPayload& object, PrunerHandle handle, const PxBounds3& worldAABB, const PxTransform& transform, PxU32 timeStamp, const PoolIndex poolIndex)
105 {
106 return mCompanion ? mCompanion->addObject(object, handle, worldAABB, transform, timeStamp, poolIndex) : true;
107 }
108
109 // add AABB tree from pruning structure - adds new primitive into main AABB tree
110 void addTree(const AABBTreeMergeData& mergeData, PxU32 timeStamp);
111
112 // update object
113 bool updateObject(const PxBounds3& worldAABB, const PxTransform& transform, const PrunerPayload& object, PrunerHandle handle, const PoolIndex poolIndex);
114
115 // remove object, removed object is replaced in pruning pool by swapped object, indices needs to be updated
116 bool removeObject(const PrunerPayload& object, PrunerHandle handle, PxU32 objectIndex, const PrunerPayload& swapObject, PxU32 swapObjectIndex);
117
118 // swap object index, the object index can be in core pruner or tree of trees
119 void swapIndex(PxU32 objectIndex, const PrunerPayload& swapObject, PxU32 swapObjectIndex, bool corePrunerIncluded = true);
120
121 // refit marked nodes in tree of trees
122 void refitMarkedNodes(const PxBounds3* boxes);
123
124 // notify timestampChange - swap trees in incremental pruner
125 PX_FORCE_INLINE void timeStampChange()
126 {
127 if(mCompanion)
128 mCompanion->timeStampChange();
129 }
130
131 // look for objects marked with input timestamp everywhere in the structure, and remove them. This is the same
132 // as calling 'removeObject' individually for all these objects, but much more efficient. Returns number of removed objects.
133 PxU32 removeMarkedObjects(PxU32 timeStamp);
134
135 // queries against the pruner
136 bool raycast(const PxVec3& origin, const PxVec3& unitDir, PxReal& inOutDistance, PrunerRaycastCallback&) const;
137 bool overlap(const ShapeData& queryVolume, PrunerOverlapCallback&) const;
138 bool sweep(const ShapeData& queryVolume, const PxVec3& unitDir, PxReal& inOutDistance, PrunerRaycastCallback&) const;
139
140 // origin shift
141 void shiftOrigin(const PxVec3& shift);
142
143 // debug visualize
144 void visualize(PxRenderOutput& out, PxU32 color) const;
145
146 PX_FORCE_INLINE void build()
147 {
148 if(mCompanion)
149 mCompanion->build();
150 }
151
152 PX_FORCE_INLINE PxU32 getNbObjects() const
153 {
154 const PxU32 nb = mCompanion ? mCompanion->getNbObjects() : 0;
155 return nb + mExtendedBucketPrunerMap.size();
156 }
157
158 void getGlobalBounds(PxBounds3&) const;
159 private:
160 // separate call for indices invalidation, object can be either in AABBPruner or Bucket pruner, but the swapped object can be
161 // in the tree of trees
162 void invalidateObject(const ExtendedBucketPrunerData& object, PxU32 objectIndex, const PrunerPayload& swapObject, PxU32 swapObjectIndex);
163
164 void resize(PxU32 size);
165 void buildMainAABBTree();
166 void cleanTrees();
167#if PX_DEBUG
168 // Extended bucket pruner validity check
169 bool checkValidity();
170#endif
171 CompanionPruner* mCompanion; // Companion pruner for single objects
172 const PruningPool* mPruningPool; // Pruning pool from AABB pruner
173 ExtendedBucketPrunerMap mExtendedBucketPrunerMap; // Map holding objects from tree merge - objects in tree of trees
174 AABBTree* mMainTree; // Main tree holding merged trees
175 AABBTreeUpdateMap mMainTreeUpdateMap; // Main tree updated map - merged trees index to nodes
176 AABBTreeUpdateMap mMergeTreeUpdateMap; // Merged tree update map used while tree is merged
177 AABBTreeBounds mBounds; // Merged trees bounds used for main tree building
178 MergedTree* mMergedTrees; // Merged trees
179 PxU32 mCurrentTreeIndex; // Current trees index
180 PxU32 mCurrentTreeCapacity; // Current tress capacity
181 bool mTreesDirty; // Dirty marker
182 };
183
184}
185
186}
187
188#endif
Definition GuAABBTreeBounds.h:39
Contains AABB-tree merge parameters.
Definition GuAABBTree.h:212
Definition GuAABBTreeUpdateMap.h:53
AABB-tree, N primitives/leaf.
Definition GuAABBTree.h:290
Definition GuSecondaryPruner.h:44
Definition GuExtendedBucketPruner.h:95
Definition GuPruningPool.h:55
Definition GuBounds.h:115
Class representing 3D range or axis aligned bounding box.
Definition PxBounds3.h:58
Definition PxHashMap.h:78
Definition PxRenderOutput.h:50
class representing a rigid euclidean transform as a quaternion and a vector
Definition PxTransform.h:49
3 Element vector class.
Definition PxVec3.h:50
#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 GuExtendedBucketPruner.h:49
Definition GuExtendedBucketPruner.h:66
Definition GuExtendedBucketPruner.h:58
Definition GuPruner.h:55
Definition GuPrunerPayload.h:43
Definition GuPruner.h:47