aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/module/basicios/src/BasicIosActorCPU.cpp
blob: aec76a88132bb9b2354eeb08229de176a461d339 (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
/*
 * Copyright (c) 2008-2015, 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 "Apex.h"
#include "SceneIntl.h"
#include "ApexSDKIntl.h"

#include "BasicIosActor.h"
#include "BasicIosActorCPU.h"
#include "BasicIosAssetImpl.h"
#include "IofxAsset.h"
#include "IofxActor.h"
#include "ModuleBasicIosImpl.h"
#include "BasicIosSceneCPU.h"
#include "RenderDebugInterface.h"
#include "AuthorableObjectIntl.h"
#include "FieldSamplerQueryIntl.h"
#include "PxMath.h"
#include "ApexMirroredArray.h"

namespace nvidia
{
namespace basicios
{

#pragma warning(disable: 4355) // 'this' : used in base member initializer list

BasicIosActorCPU::BasicIosActorCPU(
    ResourceList& list,
    BasicIosAssetImpl& asset,
    BasicIosScene& scene,
    nvidia::apex::IofxAsset& iofxAsset)
	: BASIC_IOS_ACTOR(list, asset, scene, iofxAsset, false)
	, mSimulateTask(*this)
{
	initStorageGroups(mSimulationStorage);

	mLifeTime.setSize(mMaxParticleCount);
	mLifeSpan.setSize(mMaxTotalParticleCount);
	mInjector.setSize(mMaxTotalParticleCount);
	mBenefit.setSize(mMaxTotalParticleCount);

	if (mAsset->mParams->collisionWithConvex)
	{
		mConvexPlanes.reserve(MAX_CONVEX_PLANES_COUNT);
		mConvexVerts.reserve(MAX_CONVEX_VERTS_COUNT);
		mConvexPolygonsData.reserve(MAX_CONVEX_POLYGONS_DATA_SIZE);
	}
	if (mAsset->mParams->collisionWithTriangleMesh)
	{
		mTrimeshVerts.reserve(MAX_TRIMESH_VERTS_COUNT);
		mTrimeshIndices.reserve(MAX_TRIMESH_INDICES_COUNT);
	}

	mNewIndices.resize(mMaxParticleCount);
}
BasicIosActorCPU::~BasicIosActorCPU()
{
}

void BasicIosActorCPU::submitTasks()
{
	BasicIosActorImpl::submitTasks();

	mInjectorsCounters.setSize(mInjectorList.getSize(), ApexMirroredPlace::CPU); 
	PxTaskManager* tm = mBasicIosScene->getApexScene().getTaskManager();
	tm->submitUnnamedTask(mSimulateTask);
}

void BasicIosActorCPU::setTaskDependencies()
{
	BasicIosActorImpl::setTaskDependencies(&mSimulateTask, false);
}

void BasicIosActorCPU::fetchResults()
{
	BASIC_IOS_ACTOR::fetchResults();
}

namespace
{
class FieldAccessor
{
	const PxVec4* mField;
public:
	explicit FieldAccessor(const PxVec4* field)
	{
		mField = field;
	}

	PX_INLINE void operator()(unsigned int srcIdx, PxVec3& velocityDelta)
	{
		if (mField != NULL)
		{
			velocityDelta += mField[srcIdx].getXYZ();
		}
	}
};
}

void BasicIosActorCPU::simulateParticles()
{
	float deltaTime = mBasicIosScene->getApexScene().getPhysXSimulateTime();
	const PxVec3& eyePos = mBasicIosScene->getApexScene().getEyePosition();

	mTotalElapsedTime += deltaTime;

	PxVec3 gravity = -mUp;

	uint32_t totalCount = mParticleCount + mInjectedCount;
	uint32_t activeCount = mLastActiveCount + mInjectedCount;

	mParticleBudget = mMaxParticleCount;
	if (mParticleBudget > activeCount)
	{
		mParticleBudget = activeCount;
	}
	uint32_t targetCount = mParticleBudget;

	uint32_t maxStateID = 0; //we could drop state in case targetCount = 0

	for(uint32_t i = 0; i < mInjectorList.getSize(); ++i)
	{
		mInjectorsCounters[i] = 0; 
	}

	if (targetCount > 0)
	{
		maxStateID = mParticleCount;
		for (uint32_t i = 0; i < maxStateID; ++i)
		{
			mNewIndices[i] = IosBufferDescIntl::NOT_A_PARTICLE;
		}

		uint32_t boundCount = 0;
		if (activeCount > targetCount)
		{
			boundCount = activeCount - targetCount;
		}

		float benefitMin = PxMin(mLastBenefitMin, mInjectedBenefitMin);
		float benefitMax = PxMax(mLastBenefitMax, mInjectedBenefitMax);
		PX_ASSERT(benefitMin <= benefitMax);
		benefitMax *= 1.00001f;

		uint32_t boundBin = computeHistogram(totalCount, benefitMin, benefitMax, boundCount);
		for (uint32_t i = 0, boundIndex = 0; i < totalCount; ++i)
		{
			float benefit = mBenefit[i];
			if (benefit > -FLT_MAX)
			{
				PX_ASSERT(benefit >= benefitMin && benefit < benefitMax);

				uint32_t bin = uint32_t((benefit - benefitMin) * HISTOGRAM_BIN_COUNT / (benefitMax - benefitMin));
				if (bin < boundBin)
				{
					mBenefit[i] = -FLT_MAX;
					continue;
				}
				if (bin == boundBin && boundIndex < boundCount)
				{
					mBenefit[i] = -FLT_MAX;
					++boundIndex;
				}
			}
		}

		checkBenefit(totalCount);
		checkHoles(totalCount);
	}
	mLastActiveCount = 0;
	mLastBenefitSum  = 0.0f;
	mLastBenefitMin  = +FLT_MAX;
	mLastBenefitMax  = -FLT_MAX;

	if (targetCount > 0)
	{
		const InjectorParams* injectorParamsList = DYNAMIC_CAST(BasicIosSceneCPU*)(mBasicIosScene)->mInjectorParamsArray.begin();

		FieldAccessor fieldAccessor(mFieldSamplerQuery ? mField.getPtr() : 0);

		SimulationParams simParams;
		mSimulationParamsHandle.fetch(mSimulationStorage, simParams);

		for (uint32_t dest = 0, srcHole = targetCount; dest < targetCount; ++dest)
		{
			uint32_t src = dest;
			//do we have a hole in dest region?
			if (!(mBenefit[dest] > -FLT_MAX))
			{
				//skip holes in src region
				while (!(mBenefit[srcHole] > -FLT_MAX))
				{
					++srcHole;
				}
				PX_ASSERT(srcHole < totalCount);
				src = srcHole++;
			}
			//do we have a new particle?
			bool isNewParticle = (src >= mParticleCount);

			unsigned int injIndex;
			float benefit = simulateParticle(
			                    &simParams, mSimulationStorage, injectorParamsList,
			                    deltaTime, gravity, eyePos,
			                    isNewParticle, src, dest,
			                    mBufDesc.pmaPositionMass->getPtr(), mBufDesc.pmaVelocityLife->getPtr(), mBufDesc.pmaActorIdentifiers->getPtr(),
								mLifeSpan.getPtr(), mLifeTime.getPtr(), mInjector.getPtr(), mBufDesc.pmaCollisionNormalFlags->getPtr(), mBufDesc.pmaUserData->getPtr(),
			                    fieldAccessor, injIndex
			                );

			if (injIndex < mInjectorsCounters.getSize())
			{
				++mInjectorsCounters[injIndex]; 
			}
			
			if (!isNewParticle)
			{
				mNewIndices[src] = dest;
			}
			else
			{
				mBufDesc.pmaInStateToInput->get(maxStateID) = dest | IosBufferDescIntl::NEW_PARTICLE_FLAG;
				++maxStateID;
			}

			mBenefit[dest] = benefit;
			if (benefit > -FLT_MAX)
			{
				mLastBenefitSum += benefit;
				mLastBenefitMin = PxMin(mLastBenefitMin, benefit);
				mLastBenefitMax = PxMax(mLastBenefitMax, benefit);
				++mLastActiveCount;
			}
		}

		//update stateToInput
		for (uint32_t i = 0; i < mParticleCount; ++i)
		{
			uint32_t src = mBufDesc.pmaOutStateToInput->get(i);
			PX_ASSERT( src < mParticleCount );
			mBufDesc.pmaInStateToInput->get(i) = mNewIndices[src];
		}
	}
	checkInState(totalCount);

	mParticleCount = targetCount;

	/* Oh! Manager of the IOFX! do your thing */
	mIofxMgr->updateEffectsData(deltaTime, mParticleCount, mParticleCount, maxStateID);
}

uint32_t BasicIosActorCPU::computeHistogram(uint32_t dataCount, float dataMin, float dataMax, uint32_t& bound)
{
	const float* dataArray = mBenefit.getPtr();

	uint32_t histogram[HISTOGRAM_BIN_COUNT];

	//clear Histogram
	for (uint32_t i = 0; i < HISTOGRAM_BIN_COUNT; ++i)
	{
		histogram[i] = 0;
	}
	//accum Histogram
	for (uint32_t i = 0; i < dataCount; ++i)
	{
		float data = dataArray[i];
		if (data >= dataMin && data < dataMax)
		{
			uint32_t bin = uint32_t((data - dataMin) * HISTOGRAM_BIN_COUNT / (dataMax - dataMin));
			++histogram[bin];
		}
	}
	//compute CDF from Histogram
	uint32_t countSum = 0;
	for (uint32_t i = 0; i < HISTOGRAM_BIN_COUNT; ++i)
	{
		uint32_t count = histogram[i];
		countSum += count;
		histogram[i] = countSum;
	}

	//binary search in CDF
	uint32_t beg = 0;
	uint32_t end = HISTOGRAM_BIN_COUNT;
	while (beg < end)
	{
		uint32_t mid = beg + ((end - beg) >> 1);
		if (bound > histogram[mid])
		{
			beg = mid + 1;
		}
		else
		{
			end = mid;
		}
	}

	checkHistogram(bound, histogram[beg], histogram[HISTOGRAM_BIN_COUNT - 1]);

	if (beg > 0)
	{
		bound -= histogram[beg - 1];
	}

	return beg;
}

}
} // namespace nvidia