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
|
/*
* 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.
*/
#ifndef __PARTICLE_IOS_COMMON_CODE_H__
#define __PARTICLE_IOS_COMMON_CODE_H__
#include "ParticleIosCommon.h"
namespace nvidia
{
namespace pxparticleios
{
#ifdef __CUDACC__
#define SIM_FETCH(name, idx) tex1Dfetch(KERNEL_TEX_REF(name), idx)
#define SIM_FLOAT4 float4
#define SIM_INT_AS_FLOAT(x) *(const float*)(&x)
#define SIM_INJECTOR_ARRAY const InjectorParamsArray&
#define SIM_FETCH_INJECTOR(injectorArray, injParams, injector) injectorArray.fetchElem(INPLACE_STORAGE_ARGS_VAL, injParams, injector)
PX_CUDA_CALLABLE PX_INLINE float splitFloat4(PxVec3& v3, const SIM_FLOAT4& f4)
{
v3.x = f4.x;
v3.y = f4.y;
v3.z = f4.z;
return f4.w;
}
PX_CUDA_CALLABLE PX_INLINE SIM_FLOAT4 combineFloat4(const PxVec3& v3, float w)
{
return make_float4(v3.x, v3.y, v3.z, w);
}
struct PxInternalParticleFlagGpu
{
enum Enum
{
//reserved (1<<0),
//reserved (1<<1),
//reserved (1<<2),
//reserved (1<<3),
//reserved (1<<4),
//reserved (1<<5),
eCUDA_NOTIFY_CREATE = (1 << 6),
eCUDA_NOTIFY_SET_POSITION = (1 << 7),
};
};
struct PxParticleFlag
{
enum Enum
{
eVALID = (1 << 0),
eCOLLISION_WITH_STATIC = (1 << 1),
eCOLLISION_WITH_DYNAMIC = (1 << 2),
eCOLLISION_WITH_DRAIN = (1 << 3),
eSPATIAL_DATA_STRUCTURE_OVERFLOW = (1 << 4),
};
};
struct PxParticleFlagGpu
{
uint16_t api; // PxParticleFlag
uint16_t low; // PxInternalParticleFlagGpu
};
#else
#define SIM_FETCH(name, idx) mem##name[idx]
#define SIM_FLOAT4 PxVec4
#define SIM_INT_AS_FLOAT(x) *(const float*)(&x)
#define SIM_INJECTOR_ARRAY const Px3InjectorParams*
#define SIM_FETCH_INJECTOR(injectorArray, injParams, injector) { injParams = injectorArray[injector]; }
PX_INLINE float splitFloat4(PxVec3& v3, const SIM_FLOAT4& f4)
{
v3 = f4.getXYZ();
return f4.w;
}
PX_INLINE SIM_FLOAT4 combineFloat4(const PxVec3& v3, float w)
{
return PxVec4(v3.x, v3.y, v3.z, w);
}
#endif
APEX_CUDA_CALLABLE PX_INLINE float calcParticleBenefit(
const Px3InjectorParams& inj, const PxVec3& eyePos,
const PxVec3& pos, const PxVec3& vel, float life)
{
float benefit = inj.mLODBias;
//distance term
float distance = (eyePos - pos).magnitude();
benefit += inj.mLODDistanceWeight * (1.0f - PxMin(1.0f, distance / inj.mLODMaxDistance));
//velocity term, TODO: clamp velocity
float velMag = vel.magnitude();
benefit += inj.mLODSpeedWeight * velMag;
//life term
benefit += inj.mLODLifeWeight * life;
return PxClamp(benefit, 0.0f, 1.0f);
}
INPLACE_TEMPL_VA_ARGS_DEF(typename FieldAccessor)
APEX_CUDA_CALLABLE PX_INLINE float simulateParticle(
INPLACE_STORAGE_ARGS_DEF, SIM_INJECTOR_ARRAY injectorArray,
float deltaTime,
PxVec3 eyePos,
bool isNewParticle,
unsigned int srcIdx,
unsigned int dstIdx,
SIM_FLOAT4* memPositionMass,
SIM_FLOAT4* memVelocityLife,
SIM_FLOAT4* memCollisionNormalFlags,
uint32_t* memUserData,
IofxActorIDIntl* memIofxActorIDs,
float* memLifeSpan,
float* memLifeTime,
float* memDensity,
unsigned int* memInjector,
FieldAccessor& fieldAccessor,
unsigned int &injIndex,
const GridDensityParams params,
#ifdef __CUDACC__
SIM_FLOAT4* memPxPosition,
SIM_FLOAT4* memPxVelocity,
SIM_FLOAT4* memPxCollision,
float* memPxDensity,
unsigned int* memNvFlags
#else
PxVec3& position,
PxVec3& velocity,
PxVec3& collisionNormal,
uint32_t& particleFlags,
float& density
#endif
)
{
CPU_INPLACE_STORAGE_ARGS_UNUSED
PX_UNUSED(memCollisionNormalFlags);
PX_UNUSED(memDensity);
PX_UNUSED(fieldAccessor);
PX_UNUSED(params);
float mass;
#ifdef __CUDACC__
PxVec3 position;
PxVec3 velocity;
PxVec3 collisionNormal;
uint32_t particleFlags;
float density;
#endif
//read
float lifeSpan = SIM_FETCH(LifeSpan, srcIdx);
unsigned int injector = SIM_FETCH(Injector, srcIdx);
IofxActorIDIntl iofxActorID = IofxActorIDIntl(SIM_FETCH(IofxActorIDs, srcIdx));
float lifeTime = lifeSpan;
if (!isNewParticle)
{
lifeTime = SIM_FETCH(LifeTime, srcIdx);
lifeTime = PxMax(lifeTime - deltaTime, 0.0f);
#ifndef __CUDACC__
mass = memPositionMass[srcIdx].w;
/* Apply field sampler velocity */
fieldAccessor(srcIdx, velocity);
#endif
}
else
{
collisionNormal = PxVec3(0.0f);
particleFlags = 0;
density = 0.0f;
splitFloat4(velocity, SIM_FETCH(VelocityLife, srcIdx));
#ifdef __CUDACC__
}
{
#endif
mass = splitFloat4(position, SIM_FETCH(PositionMass, srcIdx));
}
#ifdef __CUDACC__
if (isNewParticle)
{
memPxPosition[dstIdx] = combineFloat4(position, 0);
memPxVelocity[dstIdx] = combineFloat4(velocity, 0);
PxParticleFlagGpu flags;
flags.api = PxParticleFlag::eVALID;
flags.low = PxInternalParticleFlagGpu::eCUDA_NOTIFY_CREATE;
memNvFlags[dstIdx] = *((unsigned int*)&flags);
}
else
{
const SIM_FLOAT4 pxPosition = SIM_FETCH(PxPosition, srcIdx);
const SIM_FLOAT4 pxVelocity = SIM_FETCH(PxVelocity, srcIdx);
if (memPxDensity)
{
density = SIM_FETCH(PxDensity, srcIdx);
memPxDensity[dstIdx] = density;
}
splitFloat4(position, pxPosition);
splitFloat4(velocity, pxVelocity);
PxParticleFlagGpu flags;
*((uint32_t*)&flags) = SIM_FETCH(NvFlags, srcIdx);
/* Apply field sampler velocity */
fieldAccessor(srcIdx, velocity);
memPxVelocity[dstIdx] = combineFloat4(velocity, pxVelocity.w);
splitFloat4(collisionNormal, SIM_FETCH(PxCollision, srcIdx));
particleFlags = flags.api;
if (dstIdx != srcIdx)
{
memPxPosition[dstIdx] = pxPosition;
flags.low |= PxInternalParticleFlagGpu::eCUDA_NOTIFY_SET_POSITION;
memNvFlags[dstIdx] = *((uint32_t*)&flags);
}
}
#endif
Px3InjectorParams injParams;
SIM_FETCH_INJECTOR(injectorArray, injParams, injector);
injIndex = injParams.mLocalIndex;
// injParams.mLODBias == FLT_MAX if injector was released!
// and IOFX returns IofxActorIDIntl::NO_VOLUME for homeless/dead particles
bool validActorID = (injParams.mLODBias < FLT_MAX)
&& (isNewParticle || (iofxActorID.getVolumeID() != IofxActorIDIntl::NO_VOLUME))
&& position.isFinite() && velocity.isFinite();
if (!validActorID)
{
iofxActorID.setActorClassID(IofxActorIDIntl::IPX_ACTOR);
injIndex = UINT32_MAX;
}
//write
memLifeTime[dstIdx] = lifeTime;
memPositionMass[dstIdx] = combineFloat4(position, mass);
memVelocityLife[dstIdx] = combineFloat4(velocity, lifeTime / lifeSpan);
const uint32_t collisionFlags = (particleFlags & uint32_t(PxParticleFlag::eCOLLISION_WITH_STATIC | PxParticleFlag::eCOLLISION_WITH_DYNAMIC));
memCollisionNormalFlags[dstIdx] = combineFloat4(collisionNormal, SIM_INT_AS_FLOAT(collisionFlags));
if (memDensity != 0)
{
memDensity[dstIdx] = density;
}
if (!validActorID || dstIdx != srcIdx)
{
memIofxActorIDs[dstIdx] = iofxActorID;
}
if (dstIdx != srcIdx)
{
memLifeSpan[dstIdx] = lifeSpan;
memInjector[dstIdx] = injector;
memUserData[dstIdx] = SIM_FETCH(UserData,srcIdx);
}
float benefit = -FLT_MAX;
if (validActorID && lifeTime > 0.0f)
{
benefit = calcParticleBenefit(injParams, eyePos, position, velocity, lifeTime / lifeSpan);
}
return benefit;
}
}
} // namespace nvidia
#endif
|