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
|
/*
* Copyright (c) 2008-2017, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "ApexSceneTasks.h"
#include "FrameworkPerfScope.h"
#if PX_PHYSICS_VERSION_MAJOR == 3
#include "ScopedPhysXLock.h"
#endif
#include "PsTime.h"
namespace nvidia
{
namespace apex
{
// --------- PhysXSimulateTask
PhysXSimulateTask::PhysXSimulateTask(ApexScene& scene, CheckResultsTask& checkResultsTask)
: mScene(&scene)
, mElapsedTime(0.0f)
, mFollowingTask(NULL)
, mCheckResultsTask(checkResultsTask)
#if PX_PHYSICS_VERSION_MAJOR == 3
, mScratchBlock(NULL)
, mScratchBlockSize(0)
#endif
{}
PhysXSimulateTask::~PhysXSimulateTask()
{
#if PX_PHYSICS_VERSION_MAJOR == 3
mScratchBlock = NULL;
mScratchBlockSize = 0;
#endif
}
const char* PhysXSimulateTask::getName() const
{
return AST_PHYSX_SIMULATE;
}
void PhysXSimulateTask::run()
{
// record the pretick APEX time
StatValue dataVal;
uint64_t qpc = Time::getCurrentCounterValue();
dataVal.Float = ApexScene::ticksToMilliseconds(mScene->mApexSimulateTickCount, qpc);
APEX_CHECK_STAT_TIMER("--------- ApexBeforeTickTime (mApexSimulateTickCount)");
APEX_CHECK_STAT_TIMER("--------- Set mApexSimulateTickCount");
mScene->mApexSimulateTickCount = qpc;
mScene->setApexStatValue(ApexScene::ApexBeforeTickTime, dataVal);
// start the PhysX simulation time timer
APEX_CHECK_STAT_TIMER("--------- Set mPhysXSimulateTickCount");
mScene->mPhysXSimulateTickCount = Time::getCurrentCounterValue();
#if PX_PHYSICS_VERSION_MAJOR == 3
if (mScene->mPhysXScene)
{
PX_ASSERT(mElapsedTime >= 0.0f);
SCOPED_PHYSX_LOCK_WRITE(mScene);
mScene->mPhysXScene->simulate(mElapsedTime, &mCheckResultsTask, mScratchBlock, mScratchBlockSize, false);
}
#endif
#if PX_PHYSICS_VERSION_MAJOR == 0
if (mFollowingTask != NULL)
{
mFollowingTask->removeReference();
}
#endif
}
void PhysXSimulateTask::setElapsedTime(float elapsedTime)
{
PX_ASSERT(elapsedTime >= 0.0f);
mElapsedTime = elapsedTime;
}
void PhysXSimulateTask::setFollowingTask(PxBaseTask* following)
{
mFollowingTask = following;
}
// --------- CheckResultsTask
const char* CheckResultsTask::getName() const
{
return AST_PHYSX_CHECK_RESULTS;
}
void CheckResultsTask::run()
{
#if !APEX_DURING_TICK_TIMING_FIX
{
// mark the end of the "during tick" simulation time
StatValue dataVal;
{
uint64_t qpc = Time::getCurrentCounterValue();
dataVal.Float = ApexScene::ticksToSeconds(mScene->mApexSimulateTickCount, qpc);
APEX_CHECK_STAT_TIMER("--------- ApexDuringTickTime (mApexSimulateTickCount)");
APEX_CHECK_STAT_TIMER("--------- Set mApexSimulateTickCount");
mScene->mApexSimulateTickCount = qpc;
}
mScene->setApexStatValue(ApexScene::ApexDuringTickTime, dataVal);
}
#endif
#if PX_PHYSICS_VERSION_MAJOR == 3
{
SCOPED_PHYSX_LOCK_WRITE(mScene);
if (mScene->mPhysXScene)
{
mScene->mPhysXScene->checkResults(true);
}
}
#endif
// get the PhysX simulation time and add it to the ApexStats
{
StatValue dataVal;
{
uint64_t qpc = Time::getCurrentCounterValue();
dataVal.Float = ApexScene::ticksToMilliseconds(mScene->mPhysXSimulateTickCount, qpc);
APEX_CHECK_STAT_TIMER("--------- PhysXSimulationTime (mPhysXSimulateTickCount)");
}
mScene->setApexStatValue(ApexScene::PhysXSimulationTime, dataVal);
}
}
// --------- FetchResultsTask
const char* FetchResultsTask::getName() const
{
return AST_PHYSX_FETCH_RESULTS;
}
void FetchResultsTask::run()
{
}
void FetchResultsTask::setFollowingTask(PxBaseTask* following)
{
mFollowingTask = following;
if (mFollowingTask)
{
mFollowingTask->addReference();
}
}
/*
* \brief Called by dispatcher after Task has been run.
*
* If you re-implement this method, you must call this base class
* version before returning.
*/
void FetchResultsTask::release()
{
PxTask::release();
// copy mFollowingTask into local variable, because it might be overwritten
// as soon as mFetchResultsReady.set() is called (and before removeReference() is called on it)
PxBaseTask* followingTask = mFollowingTask;
mFollowingTask = NULL;
// Allow ApexScene::fetchResults() to run (potentially unblocking game thread)
mScene->mFetchResultsReady.set();
// remove reference to the scene completion task submitted in Scene::simulate
// this must be done after the scene's mFetchResultsReady event is set so that the
// app's completion task can be assured that fetchResults is ready to run
if (followingTask)
{
followingTask->removeReference();
}
}
#if APEX_DURING_TICK_TIMING_FIX
// --------- DuringTickCompleteTask
const char* DuringTickCompleteTask::getName() const
{
return AST_DURING_TICK_COMPLETE;
}
void DuringTickCompleteTask::run()
{
// mark the end of the "during tick" simulation time
StatValue dataVal;
uint64_t qpc = Time::getCurrentCounterValue();
dataVal.Float = ApexScene::ticksToMilliseconds(mScene->mApexSimulateTickCount, qpc);
APEX_CHECK_STAT_TIMER("--------- ApexDuringTickTime (mApexSimulateTickCount)");
APEX_CHECK_STAT_TIMER("--------- Set mApexSimulateTickCount");
mScene->mApexSimulateTickCount = qpc;
mScene->setApexStatValue(ApexScene::ApexDuringTickTime, dataVal);
}
#endif
// --------- PhysXBetweenStepsTask
const char* PhysXBetweenStepsTask::getName() const
{
return AST_PHYSX_BETWEEN_STEPS;
}
void PhysXBetweenStepsTask::run()
{
PX_ASSERT(mSubStepSize > 0.0f);
PX_ASSERT(mNumSubSteps > 0);
#if PX_PHYSICS_VERSION_MAJOR == 3
PxScene* scene = mScene.getPhysXScene();
if (scene != NULL)
{
while (mSubStepNumber < mNumSubSteps)
{
PX_PROFILE_ZONE("ApexSceneManualSubstep", GetInternalApexSDK()->getContextId());
// fetch the first substep
uint32_t errorState = 0;
{
SCOPED_PHYSX_LOCK_WRITE(&mScene);
scene->fetchResults(true, &errorState);
}
PX_ASSERT(errorState == 0);
for (uint32_t i = 0; i < mScene.mModuleScenes.size(); i++)
{
PX_PROFILE_ZONE("ModuleSceneManualSubstep", GetInternalApexSDK()->getContextId());
mScene.mModuleScenes[i]->interStep(mSubStepNumber, mNumSubSteps);
}
// run the next substep
{
SCOPED_PHYSX_LOCK_WRITE(&mScene);
scene->simulate(mSubStepSize);
}
mSubStepNumber++;
}
}
#endif
mLast->removeReference(); // decrement artificially high ref count that prevented checkresults from being executed
}
void PhysXBetweenStepsTask::setSubstepSize(float substepSize, uint32_t numSubSteps)
{
mSubStepSize = substepSize;
mNumSubSteps = numSubSteps;
}
void PhysXBetweenStepsTask::setFollower(uint32_t substepNumber, PxTask* last)
{
mSubStepNumber = substepNumber;
mLast = last;
setContinuation(last);
}
} // namespace apex
} // namespace nvidia
|