aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/module/fieldsampler/include/FieldSamplerCommon.h
blob: 8ef0b770bd7842e511880be5141c299760f83516 (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
/*
 * 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 __FIELD_SAMPLER_COMMON_H__
#define __FIELD_SAMPLER_COMMON_H__

#include "PxVec3.h"
#include "PxVec4.h"
#include <PxMat44.h>

#include <FieldSamplerIntl.h>
#include <FieldBoundaryIntl.h>

#include "PsArray.h"

#if APEX_CUDA_SUPPORT
#pragma warning(push)
#pragma warning(disable:4201)
#pragma warning(disable:4408)

#include <vector_types.h>

#pragma warning(pop)
#endif

#define FIELD_SAMPLER_MULTIPLIER_VALUE 1
#define FIELD_SAMPLER_MULTIPLIER_WEIGHT 2
//0, FIELD_SAMPLER_MULTIPLIER_VALUE or FIELD_SAMPLER_MULTIPLIER_WEIGHT
#define FIELD_SAMPLER_MULTIPLIER FIELD_SAMPLER_MULTIPLIER_WEIGHT

namespace nvidia
{
namespace apex
{
	class ApexCudaArray;
}

namespace fieldsampler
{

#define VELOCITY_WEIGHT_THRESHOLD 0.00001f

struct FieldSamplerExecuteArgs
{
	physx::PxVec3			position;
	float			mass;
	physx::PxVec3			velocity;

	float			elapsedTime;
	uint32_t			totalElapsedMS;
};

//struct FieldShapeParams
#define INPLACE_TYPE_STRUCT_NAME FieldShapeParams
#define INPLACE_TYPE_STRUCT_BASE FieldShapeDescIntl
#define INPLACE_TYPE_STRUCT_FIELDS \
	INPLACE_TYPE_FIELD(float,	fade)
#include INPLACE_TYPE_BUILD()


//struct FieldShapeGroupParams
#define INPLACE_TYPE_STRUCT_NAME FieldShapeGroupParams
#define INPLACE_TYPE_STRUCT_FIELDS \
	INPLACE_TYPE_FIELD(InplaceArray<FieldShapeParams>,	shapeArray)
#include INPLACE_TYPE_BUILD()


//struct FieldSamplerParams
#define INPLACE_TYPE_STRUCT_NAME FieldSamplerParams
#define INPLACE_TYPE_STRUCT_FIELDS \
	INPLACE_TYPE_FIELD(uint32_t,										executeType) \
	INPLACE_TYPE_FIELD(InplaceHandleBase,									executeParamsHandle) \
	INPLACE_TYPE_FIELD(InplaceEnum<FieldSamplerTypeIntl::Enum>,				type) \
	INPLACE_TYPE_FIELD(InplaceEnum<FieldSamplerGridSupportTypeIntl::Enum>,	gridSupportType) \
	INPLACE_TYPE_FIELD(float,										dragCoeff) \
	INPLACE_TYPE_FIELD(FieldShapeParams,									includeShape) \
	INPLACE_TYPE_FIELD(InplaceArray<InplaceHandle<FieldShapeGroupParams> >,	excludeShapeGroupHandleArray)
#include INPLACE_TYPE_BUILD()


//struct FieldSamplerQueryParams
#define INPLACE_TYPE_STRUCT_NAME FieldSamplerQueryParams
#include INPLACE_TYPE_BUILD()


//struct FieldSamplerParams
#define INPLACE_TYPE_STRUCT_NAME FieldSamplerParamsEx
#define INPLACE_TYPE_STRUCT_FIELDS \
	INPLACE_TYPE_FIELD(InplaceHandle<FieldSamplerParams>,	paramsHandle) \
	INPLACE_TYPE_FIELD(float,						multiplier)
#include INPLACE_TYPE_BUILD()

typedef InplaceArray< FieldSamplerParamsEx, false > FieldSamplerParamsExArray;


#if APEX_CUDA_SUPPORT || defined(__CUDACC__)

struct FieldSamplerKernelType
{
	enum Enum
	{
		POINTS,
		GRID
	};
};

struct FieldSamplerKernelParams
{
	float	elapsedTime;
	physx::PxVec3	cellSize;
	uint32_t	totalElapsedMS;
};

struct FieldSamplerGridKernelParams
{
	uint32_t numX, numY, numZ;

	PxMat44 gridToWorld;

	float mass;
};


struct FieldSamplerKernelArgs : FieldSamplerKernelParams
{
};

struct FieldSamplerPointsKernelArgs : FieldSamplerKernelArgs
{
	float4* accumField;
	float4* accumVelocity;
	const float4* positionMass;
	const float4* velocity;
};

struct FieldSamplerGridKernelArgs : FieldSamplerKernelArgs, FieldSamplerGridKernelParams
{
};

struct FieldSamplerKernelMode
{
	enum Enum
	{
		DEFAULT = 0,
		FINISH_PRIMARY = 1,
		FINISH_SECONDARY = 2
	};
};

#endif

class FieldSamplerWrapper;

struct FieldSamplerInfo
{
	FieldSamplerWrapper*	mFieldSamplerWrapper;
	float			mMultiplier;
};

#if APEX_CUDA_SUPPORT

struct FieldSamplerKernelLaunchDataIntl
{
	CUstream                                        stream;
	FieldSamplerKernelType::Enum                    kernelType;
	const FieldSamplerKernelArgs*                   kernelArgs;
	InplaceHandle<FieldSamplerQueryParams>          queryParamsHandle;
	InplaceHandle<FieldSamplerParamsExArray>        paramsExArrayHandle;
	const nvidia::Array<FieldSamplerInfo>*           fieldSamplerArray;
	uint32_t                                    activeFieldSamplerCount;
	FieldSamplerKernelMode::Enum                    kernelMode;
};

struct FieldSamplerPointsKernelLaunchDataIntl : FieldSamplerKernelLaunchDataIntl
{
	uint32_t                                    threadCount;
	uint32_t                                    memRefSize;
};

struct FieldSamplerGridKernelLaunchDataIntl : FieldSamplerKernelLaunchDataIntl
{
	uint32_t									threadCountX;
	uint32_t									threadCountY;
	uint32_t									threadCountZ;
	ApexCudaArray*								accumArray;
};

#endif

APEX_CUDA_CALLABLE PX_INLINE float evalFade(float dist, float fade)
{
	float x = (1 - dist) / (fade + 1e-5f);
	return physx::PxClamp<float>(x, 0, 1);
}

APEX_CUDA_CALLABLE PX_INLINE float evalFadeAntialiasing(float dist, float fade, float cellRadius)
{
	const float f = fade;
	const float r = cellRadius;
	const float x = dist - 1.0f;

	float res = 0.0f;
	//linear part
	//if (x - r < -f)
	{
		const float a = PxMin(x - r, -f);
		const float b = PxMin(x + r, -f);

		res += (b - a);
	}
	//quadratic part
	if (f >= 1e-5f)
	{
		//if (x - r < 0.0f && x + r > -f)
		{
			const float a = physx::PxClamp(x - r, -f, 0.0f);
			const float b = physx::PxClamp(x + r, -f, 0.0f);

			res += (a*a - b*b) / (2 * f);
		}
	}
	return res / (2 * r);
}


APEX_CUDA_CALLABLE PX_INLINE float evalDistInShapeNONE(const FieldShapeDescIntl& /*shapeParams*/, const physx::PxVec3& /*worldPos*/)
{
	return 0.0f; //always inside
}

APEX_CUDA_CALLABLE PX_INLINE float evalDistInShapeSPHERE(const FieldShapeDescIntl& shapeParams, const physx::PxVec3& worldPos)
{
	const physx::PxVec3 shapePos = shapeParams.worldToShape.transform(worldPos);
	const float radius = shapeParams.dimensions.x;
	return shapePos.magnitude() / radius;
}

APEX_CUDA_CALLABLE PX_INLINE float evalDistInShapeBOX(const FieldShapeDescIntl& shapeParams, const physx::PxVec3& worldPos)
{
	const physx::PxVec3 shapePos = shapeParams.worldToShape.transform(worldPos);
	const physx::PxVec3& halfSize = shapeParams.dimensions;
	physx::PxVec3 unitPos(shapePos.x / halfSize.x, shapePos.y / halfSize.y, shapePos.z / halfSize.z);
	return physx::PxVec3(physx::PxAbs(unitPos.x), physx::PxAbs(unitPos.y), physx::PxAbs(unitPos.z)).maxElement();
}

APEX_CUDA_CALLABLE PX_INLINE float evalDistInShapeCAPSULE(const FieldShapeDescIntl& shapeParams, const physx::PxVec3& worldPos)
{
	const physx::PxVec3 shapePos = shapeParams.worldToShape.transform(worldPos);
	const float radius = shapeParams.dimensions.x;
	const float halfHeight = shapeParams.dimensions.y * 0.5f;

	physx::PxVec3 clampPos = shapePos;
	clampPos.y -= physx::PxClamp(shapePos.y, -halfHeight, +halfHeight);

	return clampPos.magnitude() / radius;
}

APEX_CUDA_CALLABLE PX_INLINE float evalDistInShape(const FieldShapeDescIntl& shapeParams, const physx::PxVec3& worldPos)
{
	switch (shapeParams.type)
	{
	case FieldShapeTypeIntl::NONE:
		return evalDistInShapeNONE(shapeParams, worldPos);
	case FieldShapeTypeIntl::SPHERE:
		return evalDistInShapeSPHERE(shapeParams, worldPos);
	case FieldShapeTypeIntl::BOX:
		return evalDistInShapeBOX(shapeParams, worldPos);
	case FieldShapeTypeIntl::CAPSULE:
		return evalDistInShapeCAPSULE(shapeParams, worldPos);
	default:
		return 1.0f; //always outside
	};
}

APEX_CUDA_CALLABLE PX_INLINE physx::PxVec3 scaleToShape(const FieldShapeDescIntl& shapeParams, const physx::PxVec3& worldVec)
{
	switch (shapeParams.type)
	{
	case FieldShapeTypeIntl::SPHERE:
	case FieldShapeTypeIntl::CAPSULE:
	{
		const float radius = shapeParams.dimensions.x;
		return physx::PxVec3(worldVec.x / radius, worldVec.y / radius, worldVec.z / radius);
	}
	case FieldShapeTypeIntl::BOX:
	{
		const physx::PxVec3& halfSize = shapeParams.dimensions;
		return physx::PxVec3(worldVec.x / halfSize.x, worldVec.y / halfSize.y, worldVec.z / halfSize.z);
	}
	default:
		return worldVec;
	};
}


APEX_CUDA_CALLABLE PX_INLINE float evalWeightInShape(const FieldShapeParams& shapeParams, const physx::PxVec3& position)
{
	float dist = nvidia::fieldsampler::evalDistInShape(shapeParams, position);
	return nvidia::fieldsampler::evalFade(dist, shapeParams.fade) * shapeParams.weight;
}

APEX_CUDA_CALLABLE PX_INLINE void accumFORCE(const FieldSamplerExecuteArgs& args,
	const physx::PxVec3& field, float fieldW,
	physx::PxVec4& accumAccel, physx::PxVec4& accumVelocity)
{
	PX_UNUSED(accumVelocity);

	physx::PxVec3 newAccel = ((1 - accumAccel.w) * fieldW * args.elapsedTime / args.mass) * field;
	accumAccel.x += newAccel.x;
	accumAccel.y += newAccel.y;
	accumAccel.z += newAccel.z;
}

APEX_CUDA_CALLABLE PX_INLINE void accumACCELERATION(const FieldSamplerExecuteArgs& args,
	const physx::PxVec3& field, float fieldW,
	physx::PxVec4& accumAccel, physx::PxVec4& accumVelocity)
{
	PX_UNUSED(accumVelocity);

	physx::PxVec3 newAccel = ((1 - accumAccel.w) * fieldW * args.elapsedTime) * field;
	accumAccel.x += newAccel.x;
	accumAccel.y += newAccel.y;
	accumAccel.z += newAccel.z;
}

APEX_CUDA_CALLABLE PX_INLINE void accumVELOCITY_DIRECT(const FieldSamplerExecuteArgs& args,
	const physx::PxVec3& field, float fieldW,
	physx::PxVec4& accumAccel, physx::PxVec4& accumVelocity)
{
	PX_UNUSED(args);

	physx::PxVec3 newVelocity = ((1 - accumAccel.w) * fieldW) * field;
	accumVelocity.x += newVelocity.x;
	accumVelocity.y += newVelocity.y;
	accumVelocity.z += newVelocity.z;
	accumVelocity.w = PxMax(accumVelocity.w, fieldW);
}

APEX_CUDA_CALLABLE PX_INLINE void accumVELOCITY_DRAG(const FieldSamplerExecuteArgs& args, float dragCoeff,
	const physx::PxVec3& field, float fieldW,
	physx::PxVec4& accumAccel, physx::PxVec4& accumVelocity)
{
#if 1
	const float dragFieldW = PxMin(fieldW * dragCoeff * args.elapsedTime / args.mass, 1.0f);
	accumVELOCITY_DIRECT(args, field, dragFieldW, accumAccel, accumVelocity);
#else
	const physx::PxVec3 dragForce = (field - args.velocity) * dragCoeff;
	accumFORCE(args, dragForce, fieldW, accumAccel, accumVelocity);
#endif
}


} // namespace apex
}
#ifdef __CUDACC__

template <int queryType>
struct FieldSamplerExecutor;
/*
{
	INPLACE_TEMPL_ARGS_DEF
	static inline __device__ physx::PxVec3 func(const nvidia::fieldsampler::FieldSamplerParams* params, const nvidia::fieldsampler::FieldSamplerExecuteArgs& args, float& fieldWeight);
};
*/

template <int queryType>
struct FieldSamplerIncludeWeightEvaluator
{
	INPLACE_TEMPL_ARGS_DEF
	static inline __device__ float func(const nvidia::fieldsampler::FieldSamplerParams* params, const physx::PxVec3& position, const physx::PxVec3& cellSize)
	{
		return nvidia::fieldsampler::evalWeightInShape(params->includeShape, position);
	}
};

#endif

#endif