forked from NVIDIA/cuda-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleSystem.cpp
368 lines (290 loc) · 10.3 KB
/
ParticleSystem.cpp
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
/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* 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.
*/
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define HELPERGL_EXTERN_GL_FUNC_IMPLEMENTATION
#include <helper_gl.h>
#include <cuda_runtime.h>
#include <cuda_gl_interop.h>
#include <helper_functions.h>
#include <helper_cuda.h>
#include "ParticleSystem.h"
#include "ParticleSystem.cuh"
#include "particles_kernel.cuh"
#ifndef CUDART_PI_F
#define CUDART_PI_F 3.141592654f
#endif
/*
This handles the particle simulation using CUDA
*/
ParticleSystem::ParticleSystem(uint numParticles, bool bUseVBO, bool bUseGL)
: m_bInitialized(false),
m_bUseVBO(bUseVBO),
m_numParticles(numParticles),
m_particleRadius(0.1f),
m_doDepthSort(false),
m_timer(NULL),
m_time(0.0f) {
m_params.gravity = make_float3(0.0f, 0.0f, 0.0f);
m_params.globalDamping = 1.0f;
m_params.noiseSpeed = make_float3(0.0f, 0.0f, 0.0f);
_initialize(numParticles, bUseGL);
}
ParticleSystem::~ParticleSystem() {
_free();
m_numParticles = 0;
}
void ParticleSystem::_initialize(int numParticles, bool bUseGL) {
assert(!m_bInitialized);
createNoiseTexture(64, 64, 64);
m_numParticles = numParticles;
// allocate GPU arrays
m_pos.alloc(m_numParticles, m_bUseVBO, true); // create as VBO
m_vel.alloc(m_numParticles, m_bUseVBO, true);
m_sortKeys.alloc(m_numParticles);
m_indices.alloc(m_numParticles, m_bUseVBO, false,
true); // create as index buffer
sdkCreateTimer(&m_timer);
setParameters(&m_params);
m_bInitialized = true;
}
void ParticleSystem::_free() { assert(m_bInitialized); }
// step the simulation
void ParticleSystem::step(float deltaTime) {
assert(m_bInitialized);
m_params.time = m_time;
setParameters(&m_params);
m_pos.map();
m_vel.map();
// integrate particles
integrateSystem(m_pos.getDevicePtr(), m_pos.getDeviceWritePtr(),
m_vel.getDevicePtr(), m_vel.getDeviceWritePtr(), deltaTime,
m_numParticles);
m_pos.unmap();
m_vel.unmap();
m_pos.swap();
m_vel.swap();
m_time += deltaTime;
}
// depth sort the particles
void ParticleSystem::depthSort() {
if (!m_doDepthSort) {
return;
}
m_pos.map();
m_indices.map();
// calculate depth
calcDepth(m_pos.getDevicePtr(), m_sortKeys.getDevicePtr(),
m_indices.getDevicePtr(), m_sortVector, m_numParticles);
// radix sort
sortParticles(m_sortKeys.getDevicePtr(), m_indices.getDevicePtr(),
m_numParticles);
m_pos.unmap();
m_indices.unmap();
}
uint *ParticleSystem::getSortedIndices() {
// copy sorted indices back to CPU
m_indices.copy(GpuArray<uint>::DEVICE_TO_HOST);
return m_indices.getHostPtr();
}
// random float [0, 1]
inline float frand() { return rand() / (float)RAND_MAX; }
// signed random float [-1, 1]
inline float sfrand() { return frand() * 2.0f - 1.0f; }
// random signed vector
inline vec3f svrand() { return vec3f(sfrand(), sfrand(), sfrand()); }
// random point in circle
inline vec2f randCircle() {
vec2f r;
do {
r = vec2f(sfrand(), sfrand());
} while (length(r) > 1.0f);
return r;
}
// random point in sphere
inline vec3f randSphere() {
vec3f r;
do {
r = svrand();
} while (length(r) > 1.0f);
return r;
}
// initialize in regular grid
void ParticleSystem::initGrid(vec3f start, uint3 size, vec3f spacing,
float jitter, vec3f vel, uint numParticles,
float lifetime) {
srand(1973);
float4 *posPtr = m_pos.getHostPtr();
float4 *velPtr = m_vel.getHostPtr();
for (uint z = 0; z < size.z; z++) {
for (uint y = 0; y < size.y; y++) {
for (uint x = 0; x < size.x; x++) {
uint i = (z * size.y * size.x) + (y * size.x) + x;
if (i < numParticles) {
vec3f pos = start + spacing * vec3f((float)x, (float)y, (float)z) +
svrand() * jitter;
posPtr[i] = make_float4(pos.x, pos.y, pos.z, 0.0f);
velPtr[i] = make_float4(vel.x, vel.y, vel.z, lifetime);
}
}
}
}
}
// initialize in random positions within cube
void ParticleSystem::initCubeRandom(vec3f origin, vec3f size, vec3f vel,
float lifetime) {
float4 *posPtr = m_pos.getHostPtr();
float4 *velPtr = m_vel.getHostPtr();
for (uint i = 0; i < m_numParticles; i++) {
vec3f pos = origin + svrand() * size;
posPtr[i] = make_float4(pos.x, pos.y, pos.z, 0.0f);
velPtr[i] = make_float4(vel.x, vel.y, vel.z, lifetime);
}
}
// add sphere on regular grid
void ParticleSystem::addSphere(uint &index, vec3f pos, vec3f vel, int r,
float spacing, float jitter, float lifetime) {
float4 *posPtr = m_pos.getHostPtr();
float4 *velPtr = m_vel.getHostPtr();
uint start = index;
uint count = 0;
for (int z = -r; z <= r; z++) {
for (int y = -r; y <= r; y++) {
for (int x = -r; x <= r; x++) {
vec3f delta = vec3f((float)x, (float)y, (float)z) * spacing;
float dist = length(delta);
if ((dist <= spacing * r) && (index < m_numParticles)) {
// vec3f p = pos + delta + svrand()*jitter;
posPtr[index] = make_float4(pos.x, pos.y, pos.z, 0.0f);
velPtr[index] = make_float4(vel.x, vel.y, vel.z, lifetime);
index++;
count++;
}
}
}
}
m_pos.copy(GpuArray<float4>::HOST_TO_DEVICE, start, count);
m_vel.copy(GpuArray<float4>::HOST_TO_DEVICE, start, count);
}
void ParticleSystem::reset(ParticleConfig config) {
switch (config) {
default:
case CONFIG_RANDOM:
initCubeRandom(vec3f(0.0, 1.0, 0.0), vec3f(1.0, 1.0, 1.0), vec3f(0.0f),
100.0);
break;
case CONFIG_GRID: {
float jitter = m_particleRadius * 0.01f;
uint s = (int)ceilf(powf((float)m_numParticles, 1.0f / 3.0f));
uint gridSize[3];
gridSize[0] = gridSize[1] = gridSize[2] = s;
initGrid(vec3f(-1.0, 0.0, -1.0), make_uint3(s, s, s),
vec3f(m_particleRadius * 2.0f), jitter, vec3f(0.0),
m_numParticles, 100.0);
} break;
}
m_pos.copy(GpuArray<float4>::HOST_TO_DEVICE);
m_vel.copy(GpuArray<float4>::HOST_TO_DEVICE);
}
// particle emitters
void ParticleSystem::discEmitter(uint &index, vec3f pos, vec3f vel, vec3f vx,
vec3f vy, float r, int n, float lifetime,
float lifetimeVariance) {
float4 *posPtr = m_pos.getHostPtr();
float4 *velPtr = m_vel.getHostPtr();
uint start = index;
uint count = 0;
for (int i = 0; i < n; i++) {
vec2f delta = randCircle() * r;
if (index < m_numParticles) {
vec3f p = pos + delta.x * vx + delta.y * vy;
float lt = lifetime + frand() * lifetimeVariance;
posPtr[index] = make_float4(p.x, p.y, p.z, 0.0f);
velPtr[index] = make_float4(vel.x, vel.y, vel.z, lt);
index++;
count++;
}
}
m_pos.copy(GpuArray<float4>::HOST_TO_DEVICE, start, count);
m_vel.copy(GpuArray<float4>::HOST_TO_DEVICE, start, count);
}
void ParticleSystem::sphereEmitter(uint &index, vec3f pos, vec3f vel,
vec3f spread, float r, int n, float lifetime,
float lifetimeVariance) {
float4 *posPtr = m_pos.getHostPtr();
float4 *velPtr = m_vel.getHostPtr();
uint start = index;
uint count = 0;
for (int i = 0; i < n; i++) {
vec3f x = randSphere();
// float dist = length(x);
if (index < m_numParticles) {
vec3f p = pos + x * r;
float age = 0.0;
float lt = lifetime + frand() * lifetimeVariance;
vec3f dir = randSphere();
dir.y = fabs(dir.y);
vec3f v = vel + dir * spread;
posPtr[index] = make_float4(p.x, p.y, p.z, age);
velPtr[index] = make_float4(v.x, v.y, v.z, lt);
index++;
count++;
}
}
m_pos.copy(GpuArray<float4>::HOST_TO_DEVICE, start, count);
m_vel.copy(GpuArray<float4>::HOST_TO_DEVICE, start, count);
}
void ParticleSystem::setModelView(float *m) {
for (int i = 0; i < 16; i++) {
m_modelView.m[i] = m[i];
}
}
// dump particles to stdout for debugging
void ParticleSystem::dumpParticles(uint start, uint count) {
m_pos.copy(GpuArray<float4>::DEVICE_TO_HOST);
float4 *pos = m_pos.getHostPtr();
m_vel.copy(GpuArray<float4>::DEVICE_TO_HOST);
float4 *vel = m_vel.getHostPtr();
for (uint i = start; i < start + count; i++) {
printf("%d: ", i);
printf("pos: (%.4f, %.4f, %.4f, %.4f)\n", pos[i].x, pos[i].y, pos[i].z,
pos[i].w);
printf("vel: (%.4f, %.4f, %.4f, %.4f)\n", vel[i].x, vel[i].y, vel[i].z,
vel[i].w);
}
}
// dump particles to a system memory host
void ParticleSystem::dumpBin(float4 **posData, float4 **velData) {
m_pos.copy(GpuArray<float4>::DEVICE_TO_HOST);
*posData = m_pos.getHostPtr();
m_vel.copy(GpuArray<float4>::DEVICE_TO_HOST);
*velData = m_vel.getHostPtr();
}