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 | #ifndef PXV_NPHASE_IMPLEMENTATION_CONTEXT_H |
32 | #define PXV_NPHASE_IMPLEMENTATION_CONTEXT_H |
33 | |
34 | #include "PxSceneDesc.h" |
35 | #include "PxsContactManagerState.h" |
36 | #include "PsArray.h" |
37 | |
38 | namespace physx |
39 | { |
40 | |
41 | namespace IG |
42 | { |
43 | class SimpleIslandManager; |
44 | class IslandSim; |
45 | typedef PxU32 EdgeIndex; |
46 | } |
47 | |
48 | namespace Dy |
49 | { |
50 | class Context; |
51 | } |
52 | |
53 | class PxBaseTask; |
54 | class PxsContext; |
55 | struct PxsShapeCore; |
56 | class PxsMaterialCore; |
57 | struct PxgDynamicsMemoryConfig; |
58 | class PxsContactManager; |
59 | struct PxsContactManagerOutput; |
60 | class PxsKernelWranglerManager; |
61 | class PxsHeapMemoryAllocatorManager; |
62 | |
63 | |
64 | struct PxsContactManagerBase |
65 | { |
66 | static const PxU32 NEW_CONTACT_MANAGER_MASK = 0x80000000; |
67 | static const PxU32 GPU_NP_OFFSET = 0x4; |
68 | |
69 | static const PxU32 MaxBucketBits = 3; |
70 | |
71 | const PxU32 mBucketId; |
72 | |
73 | PxsContactManagerBase(const PxU32 bucketId) : mBucketId(bucketId) |
74 | { |
75 | PX_ASSERT(bucketId < (1<<MaxBucketBits)); |
76 | } |
77 | |
78 | |
79 | PX_FORCE_INLINE PxU32 computeId(const PxU32 index) const { PX_ASSERT(index < PxU32(1 << (32 - (MaxBucketBits-1)))); return (index << MaxBucketBits) | (mBucketId); } |
80 | static PX_FORCE_INLINE PxU32 computeIndexFromId(const PxU32 id) { return id >> MaxBucketBits; } |
81 | static PX_FORCE_INLINE PxU32 computeBucketIndexFromId(const PxU32 id) { return id & ((1<<MaxBucketBits)-1); } |
82 | |
83 | private: |
84 | PX_NOCOPY(PxsContactManagerBase) |
85 | }; |
86 | |
87 | class PxsContactManagerOutputIterator |
88 | { |
89 | PxU32 mOffsets[1<<PxsContactManagerBase::MaxBucketBits]; |
90 | PxsContactManagerOutput* mOutputs; |
91 | |
92 | public: |
93 | |
94 | PxsContactManagerOutputIterator() : mOutputs(NULL) |
95 | { |
96 | } |
97 | |
98 | PxsContactManagerOutputIterator(PxU32* offsets, PxU32 nbOffsets, PxsContactManagerOutput* outputs) : mOutputs(outputs) |
99 | { |
100 | PX_ASSERT(nbOffsets <= (1<<PxsContactManagerBase::MaxBucketBits)); |
101 | |
102 | for(PxU32 a = 0; a < nbOffsets; ++a) |
103 | { |
104 | mOffsets[a] = offsets[a]; |
105 | } |
106 | } |
107 | |
108 | PX_FORCE_INLINE PxsContactManagerOutput& getContactManager(PxU32 id) |
109 | { |
110 | PX_ASSERT((id & PxsContactManagerBase::NEW_CONTACT_MANAGER_MASK) == 0); |
111 | PxU32 bucketId = PxsContactManagerBase::computeBucketIndexFromId(id); |
112 | PxU32 cmOutId = PxsContactManagerBase::computeIndexFromId(id); |
113 | return mOutputs[mOffsets[bucketId] + cmOutId]; |
114 | } |
115 | |
116 | PxU32 getIndex(PxU32 id) |
117 | { |
118 | PX_ASSERT((id & PxsContactManagerBase::NEW_CONTACT_MANAGER_MASK) == 0); |
119 | PxU32 bucketId = PxsContactManagerBase::computeBucketIndexFromId(id); |
120 | PxU32 cmOutId = PxsContactManagerBase::computeIndexFromId(id); |
121 | return mOffsets[bucketId] + cmOutId; |
122 | } |
123 | }; |
124 | |
125 | |
126 | class PxvNphaseImplementationContext |
127 | { |
128 | private: |
129 | PX_NOCOPY(PxvNphaseImplementationContext) |
130 | public: |
131 | |
132 | PxvNphaseImplementationContext(PxsContext& context): mContext(context) {} |
133 | virtual ~PxvNphaseImplementationContext() {} |
134 | virtual void destroy() = 0; |
135 | virtual void updateContactManager(PxReal dt, bool hasBoundsArrayChanged, bool hasContactDistanceChanged, PxBaseTask* continuation, PxBaseTask* firstPassContinuation) = 0; |
136 | virtual void postBroadPhaseUpdateContactManager() = 0; |
137 | virtual void secondPassUpdateContactManager(PxReal dt, PxBaseTask* continuation) = 0; |
138 | virtual void fetchUpdateContactManager() = 0; |
139 | |
140 | virtual void registerContactManager(PxsContactManager* cm, PxI32 touching, PxU32 patchCount) = 0; |
141 | virtual void registerContactManagers(PxsContactManager** cm, PxU32 nbContactManagers, PxU32 maxContactManagerId) = 0; |
142 | virtual void unregisterContactManager(PxsContactManager* cm) = 0; |
143 | virtual void refreshContactManager(PxsContactManager* cm) = 0; |
144 | |
145 | virtual void registerShape(const PxsShapeCore& shapeCore) = 0; |
146 | virtual void unregisterShape(const PxsShapeCore& shapeCore) = 0; |
147 | |
148 | virtual void registerMaterial(const PxsMaterialCore& materialCore) = 0; |
149 | virtual void updateMaterial(const PxsMaterialCore& materialCore) = 0; |
150 | virtual void unregisterMaterial(const PxsMaterialCore& materialCore) = 0; |
151 | |
152 | virtual void updateShapeMaterial(const PxsShapeCore& shapeCore) = 0; |
153 | |
154 | virtual PxsContactManagerOutput* getGPUContactManagerOutputBase() = 0; |
155 | |
156 | virtual void startNarrowPhaseTasks() = 0; |
157 | |
158 | virtual void appendContactManagers() = 0; |
159 | |
160 | virtual PxsContactManagerOutput& getNewContactManagerOutput(PxU32 index) = 0; |
161 | |
162 | virtual PxsContactManagerOutputIterator getContactManagerOutputs() = 0; |
163 | |
164 | virtual void setContactModifyCallback(PxContactModifyCallback* callback) = 0; |
165 | |
166 | virtual void acquireContext() = 0; |
167 | virtual void releaseContext() = 0; |
168 | virtual void preallocateNewBuffers(PxU32 nbNewPairs, PxU32 maxIndex) = 0; |
169 | |
170 | virtual void lock() = 0; |
171 | virtual void unlock() = 0; |
172 | |
173 | |
174 | |
175 | |
176 | protected: |
177 | |
178 | PxsContext& mContext; |
179 | }; |
180 | |
181 | class PxvNphaseImplementationFallback |
182 | { |
183 | private: |
184 | PX_NOCOPY(PxvNphaseImplementationFallback) |
185 | public: |
186 | |
187 | PxvNphaseImplementationFallback() {} |
188 | virtual ~PxvNphaseImplementationFallback() {} |
189 | virtual void processContactManager(PxReal dt, PxsContactManagerOutput* cmOutputs, PxBaseTask* continuation) = 0; |
190 | virtual void processContactManagerSecondPass(PxReal dt, PxBaseTask* continuation) = 0; |
191 | |
192 | virtual void registerContactManager(PxsContactManager* cm, PxI32 touching, PxU32 numPatches) = 0; |
193 | virtual void unregisterContactManagerFallback(PxsContactManager* cm, PxsContactManagerOutput* cmOutputs) = 0; |
194 | |
195 | virtual void refreshContactManagerFallback(PxsContactManager* cm, PxsContactManagerOutput* cmOutputs) = 0; |
196 | |
197 | virtual PxsContactManagerOutput& getNewContactManagerOutput(PxU32 npId) = 0; |
198 | |
199 | virtual void appendContactManagersFallback(PxsContactManagerOutput* outputs) = 0; |
200 | |
201 | virtual void setContactModifyCallback(PxContactModifyCallback* callback) = 0; |
202 | |
203 | virtual void removeContactManagersFallback(PxsContactManagerOutput* cmOutputs) = 0; |
204 | |
205 | virtual void lock() = 0; |
206 | virtual void unlock() = 0; |
207 | |
208 | }; |
209 | |
210 | class PxvNphaseImplementationContextUsableAsFallback: public PxvNphaseImplementationContext, public PxvNphaseImplementationFallback |
211 | { |
212 | private: |
213 | PX_NOCOPY(PxvNphaseImplementationContextUsableAsFallback) |
214 | public: |
215 | PxvNphaseImplementationContextUsableAsFallback(PxsContext& context): PxvNphaseImplementationContext(context) {} |
216 | virtual ~PxvNphaseImplementationContextUsableAsFallback() {} |
217 | }; |
218 | |
219 | PxvNphaseImplementationContextUsableAsFallback* createNphaseImplementationContext(PxsContext& context, IG::IslandSim* islandSim); |
220 | |
221 | } |
222 | |
223 | #endif |
224 | |