aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/framework/include/ApexScene.h
blob: 67174f65e98300e9c196509a6996a371631ccd34 (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//
// 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) 2018 NVIDIA Corporation. All rights reserved.



#ifndef APEX_SCENE_H
#define APEX_SCENE_H

#include "Apex.h"
#include "ApexResource.h"
#include "PsUserAllocated.h"
#include "ApexSDKImpl.h"
#include "SceneIntl.h"
#include "ModuleIntl.h"
#include "ApexContext.h"

#include "PsMutex.h"
#include "PsThread.h"
#include "PairFilter.h"

#if PX_PHYSICS_VERSION_MAJOR == 3
#include "PxScene.h"
#include "PxRenderBuffer.h"
#endif

#include "MirrorSceneImpl.h"
#include "ApexSceneUserNotify.h"

#include "PsSync.h"
#include "PxTask.h"
#include "PxTaskManager.h"

#include "ApexGroupsFiltering.h"
#include "ApexRWLockable.h"
#include "ReadCheck.h"
#include "WriteCheck.h"

class PxDefaultSpuDispatcher;


namespace nvidia
{
namespace apex
{

class ApexCudaTestManager;
class ApexCudaProfileManager;

// Tasks forward declarations
class PhysXSimulateTask;
class PhysXBetweenStepsTask;

#if APEX_DURING_TICK_TIMING_FIX
class DuringTickCompleteTask;
#endif

class CheckResultsTask;
class FetchResultsTask;


class ApexScene : public SceneIntl, public ApexContext, public ApexRWLockable, public UserAllocated
{
public:
	APEX_RW_LOCKABLE_BOILERPLATE

	/* == Public Scene interface == */
	void					simulate(float elapsedTime, 
									 bool finalStep = true, 
									 PxBaseTask *completionTask = NULL,
									 void* scratchMemBlock = 0, 
									 uint32_t scratchMemBlockSize = 0);

	bool					fetchResults(bool block, uint32_t* errorState);
	void					fetchPhysXStats();
	void					fetchApexStats();
	bool					checkResults(bool block) const;

	void					initDebugColorParams();
	void					updateDebugColorParams(const char* color, uint32_t val);
	NvParameterized::Interface* getDebugRenderParams() const;
	NvParameterized::Interface* getModuleDebugRenderParams(const char* name) const;

	uint32_t				allocViewMatrix(ViewMatrixType::Enum);
	uint32_t				allocProjMatrix(ProjMatrixType::Enum);
	uint32_t				getNumViewMatrices() const;
	uint32_t				getNumProjMatrices() const;

	void					setViewMatrix(const PxMat44& viewTransform, const uint32_t viewID = 0);
	PxMat44					getViewMatrix(const uint32_t viewID = 0) const;
	void					setProjMatrix(const PxMat44& projTransform, const uint32_t projID = 0);
	PxMat44					getProjMatrix(const uint32_t projID = 0) const;

	void					setUseViewProjMatrix(const uint32_t viewID = 0, const uint32_t projID = 0);
#if 0 //lionel: work in progress
	const PxMat44& 	getViewProjMatrix() const;
#endif
	void					setViewParams(const PxVec3& eyePosition, 
										  const PxVec3& eyeDirection, 
										  const PxVec3& worldUpDirection, 
										  const uint32_t viewID = 0);

	void					setProjParams(float nearPlaneDistance, 
										  float farPlaneDistance, 
										  float fieldOfViewDegree, 
										  uint32_t viewportWidth, 
										  uint32_t viewportHeight, 
										  const uint32_t projID = 0);

	PxVec3					getEyePosition(const uint32_t viewID = 0) const;
	PxVec3					getEyeDirection(const uint32_t viewID = 0) const;


#if 0 //lionel: work in progress
	const PxMat44& 	buildViewMatrix(const uint32_t viewID = 0);
	const PxMat44& 	buildProjMatrix(const uint32_t projID = 0);
	//viewportToWorld?, worldToViewport? (and screenspace)
	const SceneCalculator* const calculate()
	{
		return mSceneCalculator;
	}
#endif

	float					getElapsedTime() const
	{
		return mElapsedTime;
	}

	const SceneStats*		getStats(void) const;
	void					createApexStats(void);
	void					destroyApexStats(void);
	void					setApexStatValue(int32_t index, StatValue dataVal);


	bool					isSimulating() const
	{
		return mSimulating;
	}
	bool					physXElapsedTime(float& dt) const
	{
		dt = mPxLastElapsedTime;
		return mPxStepWasValid;
	}
	float					getPhysXSimulateTime() const
	{
		return mPhysXSimulateTime;
	}

	PxVec3					getGravity() const
	{
#if PX_PHYSICS_VERSION_MAJOR == 3
		if (mPhysXScene)
		{
			mPhysXScene->lockRead();
			PxVec3 ret = mPhysXScene->getGravity();
			mPhysXScene->unlockRead();
			return ret;
		}
#endif
		return mGravity;
	}

	void					setGravity(const PxVec3& gravity)
	{
		mGravity = gravity;
#if PX_PHYSICS_VERSION_MAJOR == 3
		if (mPhysXScene)
		{
			mPhysXScene->lockWrite();
			mPhysXScene->setGravity(gravity);
			mPhysXScene->unlockWrite();
		}
#endif
	}

	void					release()
	{
		mApexSDK->releaseScene(this);
	}

	/* == Public Context interface == */
	ApexContext* 			getApexContext()
	{
		return DYNAMIC_CAST(ApexContext*)(this);
	}
	void					removeAllActors();
	RenderableIterator* createRenderableIterator()
	{
		return ApexContext::createRenderableIterator();
	}
	void releaseRenderableIterator(RenderableIterator& iter)
	{
		ApexContext::releaseRenderableIterator(iter);
	}
	uint32_t				addActor(ApexActor& actor, ApexActor* actorPtr = NULL);

	/* == Renderable interface == */
	void					lockRenderResources();
	void					unlockRenderResources();
	void					updateRenderResources(bool rewriteBuffers = false, void* userRenderData = 0);
	void					dispatchRenderResources(UserRenderer& renderer);
	PxBounds3				getBounds() const;

	void					visualize();

	/* == SceneIntl interface == */
	void					moduleReleased(ModuleSceneIntl&);
	virtual PxTaskManager*	getTaskManager() const
	{
		READ_ZONE();
		return mTaskManager;
	};

	uint32_t				getTotalElapsedMS() const
	{
		return mTotalElapsedMS;
	}

	bool					isFinalStep() const
	{
		return mFinalStep;
	}

#if PX_PHYSICS_VERSION_MAJOR == 3
	virtual void lockRead(const char *fileName,uint32_t lineno)
	{
		if (mPhysXScene != NULL)
		{
			mPhysXScene->lockRead(fileName,lineno);
		}
	}

	virtual void lockWrite(const char *fileName,uint32_t lineno)
	{
		if (mPhysXScene != NULL)
		{
			mPhysXScene->lockWrite(fileName,lineno);
		}
	}


	virtual void unlockRead ()
	{
		if (mPhysXScene != NULL)
		{
			mPhysXScene->unlockRead();
		}
	}

	virtual void unlockWrite ()
	{
		if (mPhysXScene != NULL)
		{
			mPhysXScene->unlockWrite();
		}
	}

	virtual void	addActorPair(PxActor *actor0,PxActor *actor1);
	virtual void	removeActorPair(PxActor *actor0,PxActor *actor1);
	virtual bool	findActorPair(PxActor *actor0,PxActor *actor1) const;
#endif

	virtual void	addBoundingBox(const PxBounds3& bounds, UserBoundingBoxFlags::Enum flags) 
	{
		WRITE_ZONE();
		mBBs.pushBack(UserDefinedBoundingBox(bounds, flags));
	}

	virtual const PxBounds3 getBoundingBox(const uint32_t index) const
	{
		READ_ZONE();
		PX_ASSERT(index < mBBs.size());
		if(index < mBBs.size()) 
		{
			return mBBs[index].bb;
		}
		return PxBounds3(PxVec3(0.0f), PxVec3(0.0f));
	}

	virtual UserBoundingBoxFlags::Enum getBoundingBoxFlags(const uint32_t index) const
	{
		READ_ZONE();
		PX_ASSERT(index < mBBs.size());
		if(index < mBBs.size()) 
		{
			return mBBs[index].flags;
		}
		return UserBoundingBoxFlags::NONE;
	}

	virtual uint32_t getBoundingBoxCount() const
	{
		READ_ZONE();
		return mBBs.size();
	}

	virtual void  removeBoundingBox(const uint32_t index)
	{
		WRITE_ZONE();
		PX_ASSERT(index < mBBs.size());
		if(index < mBBs.size()) 
		{
			mBBs.remove(index);
		}
	}

	virtual void  removeAllBoundingBoxes()
	{
		WRITE_ZONE();
		mBBs.clear();
	}

	void					allocateTasks();
	void					freeTasks();
	void					setUseDebugRenderable(bool state);

	ApexScene(const SceneDesc& desc, ApexSDKImpl* sdk);
	~ApexScene();

	void					moduleCreated(ModuleIntl&);
	void					destroy();

	const PxRenderBuffer*	getRenderBuffer() const;
	const PxRenderBuffer*	getRenderBufferScreenSpace() const;

#if PX_PHYSICS_VERSION_MAJOR == 3

	virtual MirrorScene *createMirrorScene(nvidia::apex::Scene &mirrorScene,
		MirrorScene::MirrorFilter &mirrorFilter,
		float mirrorStaticDistance,
		float mirrorDynamicDistance,
		float mirrorDistanceThreshold);


	void						setPhysXScene(PxScene* s);
	PxScene*					getPhysXScene() const
	{
		READ_ZONE();
		return mPhysXScene;
	}

	PxScene*					mPhysXScene;
	
	mutable PhysXRenderBuffer	mRenderBuffer;
	mutable PhysXRenderBuffer	mRenderBufferScreenSpace;

	void					addModuleUserNotifier(physx::PxSimulationEventCallback& notify)
	{
		mUserNotify.addModuleNotifier(notify);
	}
	void					removeModuleUserNotifier(physx::PxSimulationEventCallback& notify)
	{
		mUserNotify.removeModuleNotifier(notify);
	}
	void					addModuleUserContactModify(physx::PxContactModifyCallback& contactModify)
	{
		mUserContactModify.addModuleContactModify(contactModify);
	}
	void					removeModuleUserContactModify(physx::PxContactModifyCallback& contactModify)
	{
		mUserContactModify.removeModuleContactModify(contactModify);
	}

	ApexSceneUserNotify			mUserNotify;
	ApexSceneUserContactModify	mUserContactModify;

	PhysX3Interface* getApexPhysX3Interface() const
	{
		return mPhysX3Interface;
	}

	PhysX3Interface*		mPhysX3Interface;
#endif

	ModuleSceneIntl* getInternalModuleScene(const char* moduleName);
	
	PX_INLINE void* getCudaTestManager() const
	{
		return mCudaTestManager;
	}
	PX_INLINE ApexCudaTestManager& getApexCudaTestManager()
	{
		return *mCudaTestManager;
	}
	PX_INLINE void* getCudaProfileManager() const
	{
		return mCudaProfileManager;
	}
	bool					isUsingCuda() const 
	{
		return mUseCuda;
	}

	virtual void setCudaKernelCheckEnabled(bool enabled)
	{
		mCudaKernelCheckEnabled = enabled;
	}
	virtual bool getCudaKernelCheckEnabled() const
	{
		return mCudaKernelCheckEnabled;
	}

	float						mElapsedTime;
	ApexSDKImpl*				mApexSDK;
	Array<ModuleSceneIntl*>		mModuleScenes;
	RenderDebugInterface* 		mSceneRenderDebug;

	uint32_t					mOrigSceneMaxIter;
	float						mOrigSceneSubstepSize;

	PxTaskManager*				mTaskManager;

	Mutex						mPhysXLock;

	bool						mSimulating;
	bool						mUseDebugRenderable;
	float						mUsedResource;
	float						mSumBenefit;
	mutable Sync				mFetchResultsReady;
	Sync						mSimulationComplete;

	PhysXSimulateTask*			mPhysXSimulate;
	PhysXBetweenStepsTask*		mBetweenstepTasks;
#if APEX_DURING_TICK_TIMING_FIX
	DuringTickCompleteTask*		mDuringTickComplete;
#endif
	CheckResultsTask*			mCheckResults;
	FetchResultsTask*			mFetchResults;

	uint32_t					mTotalElapsedMS;
	float						mTimeRemainder;
	float						mPhysXRemainder;
	float						mPhysXSimulateTime;

	float						mPxLastElapsedTime;
	float						mPxAccumElapsedTime;
	bool						mPxStepWasValid;
	bool						mFinalStep;

	bool						mUseCuda;


	static double				mQPC2MilliSeconds;
	static PX_INLINE float	ticksToMilliseconds(uint64_t t0, uint64_t t1)
	{
		return (float)((double)(t1 - t0) * mQPC2MilliSeconds);
	}

	uint64_t					mApexSimulateTickCount;
	uint64_t					mPhysXSimulateTickCount;

	static const uint32_t IgnoredSeed = UINT32_MAX;

	uint32_t getSeed();
	
private:
	void updateGravity();

	SceneStats					mApexSceneStats;

	mutable ApexCudaTestManager*	mCudaTestManager;
	mutable ApexCudaProfileManager*	mCudaProfileManager;
	bool						mCudaKernelCheckEnabled;

	/* transforms info */
#if 0 //lionel: work in progress
	void						getColMajColVecArray(const PxMat44& colVecMat44, float* const result);
	void						getColVecMat44(const float* const colMajColVecArray, PxMat44& result);
	void						multiplyColMajColVecArray(const float* const fromSpace, const float* const toSpace, float* const result);
	float						mViewColMajColVecArray[16];
	float						mProjColMajColVecArray[16];
	float						mViewProjColMajColVecArray[16];
#endif
	struct ViewMatrixProperties : public UserAllocated
	{
		ViewMatrixProperties() {}
		~ViewMatrixProperties() {}
		ViewMatrixProperties(PxMat44 v, bool l) :
			viewMatrix(v), isLookAt(l), pvdCreated(false) 	{}

		PxMat44					viewMatrix;
		bool					isUserCustomized;
		bool					isLookAt;
		bool					pvdCreated;

		ApexSimpleString		cameraName;
	};

	struct ViewMatrixLookAt : public ViewMatrixProperties
	{
		ViewMatrixLookAt() {}
		~ViewMatrixLookAt() {}
		ViewMatrixLookAt(PxMat44 v, bool l, bool r) :
			ViewMatrixProperties(v, l), isRightHand(r) {}

		bool					isRightHand;
	};

	struct ProjMatrixProperties : public UserAllocated
	{
		ProjMatrixProperties() {}
		~ProjMatrixProperties() {}
		ProjMatrixProperties(PxMat44 p, bool u, bool f) :
			projMatrix(p), isUserCustomized(u), isPerspectiveFOV(f)  {}

		PxMat44					projMatrix;
		bool					isUserCustomized;
		bool					isPerspectiveFOV;
	};

	struct ProjMatrixUserCustomized : public ProjMatrixProperties
	{
		ProjMatrixUserCustomized() {}
		~ProjMatrixUserCustomized() {}
		ProjMatrixUserCustomized(PxMat44 p, bool u, bool f, float near, float far, float fov, uint32_t w, uint32_t h) :
			ProjMatrixProperties(p, u, f), nearPlaneDistance(near), farPlaneDistance(far), fieldOfViewDegree(fov), viewportWidth(w), viewportHeight(h) {}

		float					nearPlaneDistance;
		float					farPlaneDistance;
		float					fieldOfViewDegree;
		uint32_t				viewportWidth;			//only one instance?
		uint32_t				viewportHeight;			//only one instance?
	};

	struct ProjMatrixPerspectiveFOV : public ProjMatrixProperties
	{
		ProjMatrixPerspectiveFOV() {}
		~ProjMatrixPerspectiveFOV() {}
		ProjMatrixPerspectiveFOV(PxMat44 p, bool u, bool f, bool i) :
			ProjMatrixProperties(p, u, f), isZinvert(i) {}

		bool					isZinvert;
	};

	Array<ViewMatrixProperties*>	mViewMatrices;
	Array<ProjMatrixProperties*>	mProjMatrices;
	PxMat44							mViewProjMatrix;
#if 0 //lionel: work in progress
	uint32_t					mCurrentViewID;
	uint32_t					mCurrentProjID;
	SceneCalculator*			mSceneCalculator;
	friend class SceneCalculator;
	//class SceneCalculator
	//{
	//public:
	//	SceneCalculator():s(NULL) {}
	//	~SceneCalculator() {s=NULL;}
	//	float distanceFromEye(PxVec3 to) {return (-s->getEyePosition(0) + to).magnitude();}	//lionel: use currentIDs when multiple matrices allowed
	//	friend class ApexScene;
	//private:
	//	void construct(const physx::ApexScene * scene) {s = scene;}
	//	void destruct() {s = NULL;}
	//	const physx::ApexScene * s;
	//};
#endif

	DebugRenderParams*								mDebugRenderParams;
	DebugColorParams*								mDebugColorParams;
	HashMap<const char*, RENDER_DEBUG::DebugColors::Enum>	mColorMap;

	PxVec3							mGravity;

	PairFilter						mPairFilter;

	struct UserDefinedBoundingBox
	{
		PxBounds3 bb;
		UserBoundingBoxFlags::Enum	flags;

		UserDefinedBoundingBox(const PxBounds3& _bb, UserBoundingBoxFlags::Enum _flags) :
																		bb(_bb), flags(_flags) {}
	};
	Array<UserDefinedBoundingBox>	mBBs;
};


}
} // end namespace nvidia::apex

#endif // APEX_SCENE_H