aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Source/PhysX/src/NpFactory.h
blob: 626dec6aa2b838d7d76945364154c6e540f95a25 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//  * Neither the name of NVIDIA CORPORATION nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  


#ifndef PX_PHYSICS_NP_FACTORY
#define PX_PHYSICS_NP_FACTORY

#include "PsPool.h"
#include "PsMutex.h"
#include "PsHashSet.h"

#include "GuMeshFactory.h"
#include "CmPhysXCommon.h"
#include "PxPhysXConfig.h"
#include "PxShape.h"

#if PX_USE_CLOTH_API
#include "cloth/PxClothTypes.h"
#include "cloth/PxClothFabric.h"
#endif

namespace physx
{

class PxActor;

class PxRigidActor;

class PxRigidStatic;
class NpRigidStatic;

class PxRigidDynamic;
class NpRigidDynamic;

class NpConnectorArray;

struct PxConstraintShaderTable;
class PxConstraintConnector;
class PxConstraint;
class NpConstraint;

class PxArticulation;
class NpArticulation;
class PxArticulationLink;
class NpArticulationLink;
class NpArticulationJoint;

class PxParticleBase;
class PxParticleSystem;
class NpParticleSystem;
class PxParticleFluid;
class NpParticleFluid;

class PxClothFabric;
class NpClothFabric;
class PxCloth;
class NpCloth;
class PxMaterial;
class NpMaterial;

class PxGeometry;

class NpShape;

class NpScene;

class PxAggregate;
class NpAggregate;

class NpConnectorArray;
class NpPtrTableStorageManager;

namespace Cm
{
   class Collection;
}

namespace Scb
{
	class RigidObject;
}

class NpFactoryListener : public GuMeshFactoryListener
{
protected:
	virtual ~NpFactoryListener(){}
public:
#if PX_USE_CLOTH_API
	virtual void onNpFactoryBufferRelease(PxClothFabric& data) = 0;
#endif
};



class NpFactory : public GuMeshFactory
{
	PX_NOCOPY(NpFactory)
public:
												NpFactory();
private:
												~NpFactory();

public:
	static		void							createInstance();
	static		void							destroyInstance();
	static		void							registerArticulations();
	static		void							registerCloth();
	static		void							registerParticles();

				void							release();

				void							addCollection(const Cm::Collection& collection);

	PX_INLINE static NpFactory&					getInstance() { return *mInstance; }

				PxRigidDynamic*					createRigidDynamic(const PxTransform& pose);
				void							addRigidDynamic(PxRigidDynamic*, bool lock=true);
				void							releaseRigidDynamicToPool(NpRigidDynamic&);

				PxRigidStatic*					createRigidStatic(const PxTransform& pose);
				void							addRigidStatic(PxRigidStatic*, bool lock=true);
				void							releaseRigidStaticToPool(NpRigidStatic&);

				NpShape*						createShape(const PxGeometry& geometry,
															PxShapeFlags shapeFlags,
															PxMaterial*const* materials,
															PxU16 materialCount,
															bool isExclusive);

				void							addShape(PxShape*, bool lock=true);
				void							releaseShapeToPool(NpShape&);

				PxU32							getNbShapes() const;
				PxU32							getShapes(PxShape** userBuffer, PxU32 bufferSize, PxU32 startIndex)	const;

				void							addConstraint(PxConstraint*, bool lock=true);
				PxConstraint*					createConstraint(PxRigidActor* actor0, PxRigidActor* actor1, PxConstraintConnector& connector, const PxConstraintShaderTable& shaders, PxU32 dataSize);
				void							releaseConstraintToPool(NpConstraint&);

				void							addArticulation(PxArticulation*, bool lock=true);
				PxArticulation*					createArticulation();
				NpArticulation*                 createNpArticulation();
				void                            releaseArticulationToPool(NpArticulation& articulation);

				NpArticulationLink*             createNpArticulationLink(NpArticulation&root, NpArticulationLink* parent, const PxTransform& pose);
                void                            releaseArticulationLinkToPool(NpArticulationLink& articulation);
				PxArticulationLink*				createArticulationLink(NpArticulation&, NpArticulationLink* parent, const PxTransform& pose);

				NpArticulationJoint*            createNpArticulationJoint(NpArticulationLink& parent, const PxTransform& parentFrame, NpArticulationLink& child, const PxTransform& childFrame);
               	void                            releaseArticulationJointToPool(NpArticulationJoint& articulationJoint);

				void							addAggregate(PxAggregate*, bool lock=true);
				PxAggregate*					createAggregate(PxU32 maxActors, bool selfCollisions);
				void							releaseAggregateToPool(NpAggregate&);


#if PX_USE_PARTICLE_SYSTEM_API
				NpParticleSystem*               createNpParticleSystem(PxU32 maxParticles, bool perParticleRestOffset);
				void                            releaseParticleSystemToPool(NpParticleSystem& ps);
				NpParticleFluid*                createNpParticleFluid(PxU32 maxParticles, bool perParticleRestOffset);
				void                            releaseParticleFluidToPool(NpParticleFluid& pf);				
				void							addParticleSystem(PxParticleSystem*, bool lock=true);
				PxParticleSystem*				createParticleSystem(PxU32 maxParticles, bool perParticleRestOffset);
				void							addParticleFluid(PxParticleFluid*, bool lock=true);
				PxParticleFluid*				createParticleFluid(PxU32 maxParticles, bool perParticleRestOffset);
#endif

#if PX_USE_CLOTH_API
				NpCloth*                        createNpCloth(const PxTransform& globalPose, PxClothFabric& fabric, const PxClothParticle* particles, PxClothFlags flags);
				void                            releaseClothToPool(NpCloth& cloth);
				void							addCloth(PxCloth* cloth, bool lock=true);
				void							addClothFabric(NpClothFabric* cf, bool lock=true);
				PxClothFabric*					createClothFabric(PxInputStream&);
				PxClothFabric*					createClothFabric(const PxClothFabricDesc& desc);
				NpClothFabric*                  createNpClothFabric();
				void                            releaseClothFabricToPool(NpClothFabric& clothFabric);
				bool							removeClothFabric(PxClothFabric&);
				PxU32							getNbClothFabrics()	const;
				PxU32							getClothFabrics(PxClothFabric** userBuffer, PxU32 bufferSize) const;

				PxCloth*						createCloth(const PxTransform& globalPose, PxClothFabric& fabric, const PxClothParticle* particles, PxClothFlags flags);
#endif

				PxMaterial*						createMaterial(PxReal staticFriction, PxReal dynamicFriction, PxReal restitution);
				void                            releaseMaterialToPool(NpMaterial& material);

				// It's easiest to track these uninvasively, so it's OK to use the Px pointers

				void							onActorRelease(PxActor*);
				void							onConstraintRelease(PxConstraint*);
				void							onAggregateRelease(PxAggregate*);
				void							onArticulationRelease(PxArticulation*);
				void							onShapeRelease(PxShape*);

				NpConnectorArray*				acquireConnectorArray();
				void							releaseConnectorArray(NpConnectorArray*);
				
				NpPtrTableStorageManager&		getPtrTableStorageManager()	{ return *mPtrTableStorageManager; }

#if PX_SUPPORT_PVD
				void							setNpFactoryListener( NpFactoryListener& );
#endif


private:

						void					releaseExclusiveShapeUserReferences();
#if PX_SUPPORT_GPU_PHYSX
				virtual void					notifyReleaseTriangleMesh(const PxTriangleMesh& tm);
				virtual	void					notifyReleaseHeightField(const PxHeightField& hf);
				virtual	void					notifyReleaseConvexMesh(const PxConvexMesh& cm);
#endif

				Ps::Pool<NpConnectorArray>		mConnectorArrayPool;
				Ps::Mutex						mConnectorArrayPoolLock;

				NpPtrTableStorageManager*		mPtrTableStorageManager;

				Ps::HashSet<PxAggregate*>		mAggregateTracking;
				Ps::HashSet<PxArticulation*>	mArticulationTracking;
				Ps::HashSet<PxConstraint*>		mConstraintTracking;
				Ps::HashSet<PxActor*>			mActorTracking;				
				Ps::CoalescedHashSet<PxShape*>	mShapeTracking;

				Ps::Pool2<NpRigidDynamic, 4096>	mRigidDynamicPool;
				Ps::Mutex						mRigidDynamicPoolLock;

				Ps::Pool2<NpRigidStatic, 4096>	mRigidStaticPool;
				Ps::Mutex						mRigidStaticPoolLock;

				Ps::Pool2<NpShape, 4096>		mShapePool;
				Ps::Mutex						mShapePoolLock;

				Ps::Pool2<NpAggregate, 4096>	mAggregatePool;
				Ps::Mutex						mAggregatePoolLock;

				Ps::Pool2<NpConstraint, 4096>	mConstraintPool;
				Ps::Mutex						mConstraintPoolLock;

				Ps::Pool2<NpMaterial, 4096>		mMaterialPool;
				Ps::Mutex						mMaterialPoolLock;

				Ps::Pool2<NpArticulation, 4096>	mArticulationPool;
				Ps::Mutex						mArticulationPoolLock;

				Ps::Pool2<NpArticulationLink, 4096>	mArticulationLinkPool;
				Ps::Mutex						mArticulationLinkPoolLock;

				Ps::Pool2<NpArticulationJoint, 4096> mArticulationJointPool;
				Ps::Mutex						mArticulationJointPoolLock;	

#if PX_USE_PARTICLE_SYSTEM_API
				Ps::Pool2<NpParticleSystem, 4096>	mParticleSystemPool;
				Ps::Mutex						mParticleSystemPoolLock;
				Ps::Pool2<NpParticleFluid, 4096>	mParticleFluidPool;
				Ps::Mutex						mParticleFluidPoolLock;
#endif

#if PX_USE_CLOTH_API
				Ps::Array<NpClothFabric*>		mClothFabricArray;
				Ps::Pool2<NpCloth, 4096>	    mClothPool;
				Ps::Mutex						mClothPoolLock;
				Ps::Pool2<NpClothFabric, 4096>  mClothFabricPool;
				Ps::Mutex						mClothFabricPoolLock;
#endif

	static		NpFactory*						mInstance;

#if PX_SUPPORT_PVD
				NpFactoryListener*				mNpFactoryListener;
#endif

};


}

#endif