-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.cpp
724 lines (606 loc) · 18.4 KB
/
particles.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*
* GPU Smoldyn: Smoldyn algorithm ported to the GPU using CUDA 2.2
* Writtern By Lorenzo Dematté, 2010-2011
*
* This file is part of GPU Smoldyn
*
* GPU Smoldyn is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GPU Smoldyn is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
* Based on algorithm and source code of Smoldyn, written by Steve Andrews, 2003.
*
* Portions taken by code examples in NVIDIA Whitepapers, GPU Gems 2 and 3,
* Copyright 1993-2009 NVIDIA Corporation, Addison-Wesley and the original authors.
*
*/
#pragma warning (disable: 4201)
#pragma warning (disable: 4408)
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <GL/glew.h>
#if defined (_WIN32)
#include <GL/wglew.h>
#endif
#if defined(__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <cutil_inline.h>
#include <cutil_gl_inline.h>
#include <cuda_gl_interop.h>
#include <rendercheck_gl.h>
#include "particleSystem.h"
#include "render_particles.h"
#include "paramgl.h"
//#include "MersenneTwister.h"
#define MAX_EPSILON_ERROR 5.00f
#define THRESHOLD 0.30f
#define GRID_SIZE 64
#define NUM_PARTICLES 4096
// Define the files that are to be save and the reference images for validation
const char *sOriginal[] =
{
"particles.ppm",
NULL
};
const char *sReference[] =
{
"particles_4.ppm",
NULL
};
const char *sOriginalBin[] =
{
"particles.bin",
NULL
};
const char *sReferenceBin[] =
{
"ref_particles.bin",
NULL
};
const uint width = 800, height = 600;
// view params
int ox, oy;
int buttonState = 0;
float camera_trans[] = {0, 0, -3};
float camera_rot[] = {0, 0, 0};
float camera_trans_lag[] = {0, 0, -3};
float camera_rot_lag[] = {0, 0, 0};
const float inertia = 0.1;
ParticleRenderer::DisplayMode displayMode = ParticleRenderer::PARTICLE_SPHERES;
int mode = 0;
bool displayEnabled = true;
bool bPause = false;
bool displaySliders = false;
bool wireframe = false;
bool demoMode = false;
int idleCounter = 0;
int demoCounter = 0;
const int idleDelay = 2000;
enum { M_VIEW = 0, M_MOVE };
uint numParticles = 0;
uint3 gridSize;
int numIterations = 0; // run until exit
int currentIteration = 0;
// simulation parameters
float timestep = 0.5f;
float damping = 1.0f;
float gravity = 0.0003f;
int iterations = 1;
int ballr = 10;
float collideSpring = 0.5f;;
float collideDamping = 0.02f;;
float collideShear = 0.1f;
float collideAttraction = 0.0f;
ParticleSystem *psystem = 0;
// fps
static int fpsCount = 0;
static int fpsLimit = 1;
unsigned int timer;
ParticleRenderer *renderer = 0;
float modelView[16];
ParamListGL *params;
// Auto-Verification Code
const int frameCheckNumber = 4;
unsigned int frameCount = 0;
unsigned int g_TotalErrors = 0;
bool g_Verify = false;
bool g_bQAReadback = false;//true; //false;
bool g_bGLVerify = false;
bool g_bFBODisplay = false;
// CheckFBO/BackBuffer class objects
CheckRender *g_CheckRender = NULL;
#define MAX(a,b) ((a > b) ? a : b)
const char *sSDKsample = "Smoldyn Simulation";
extern "C" void cudaInit(int argc, char **argv);
extern "C" void cudaGLInit(int argc, char **argv);
extern "C" void copyArrayFromDevice(void* host, const void* device, unsigned int vbo, int size);
// initialize particle system
void initParticleSystem(int numParticles, uint3 gridSize, bool bUseOpenGL)
{
psystem = new ParticleSystem(numParticles, gridSize, bUseOpenGL);
psystem->reset(ParticleSystem::CONFIG_GRID);
if (bUseOpenGL) {
renderer = new ParticleRenderer;
renderer->setParticleRadius(psystem->getParticleRadius());
//renderer->setTypesBuffer(psystem->getTypesBuffer());
}
cutilCheckError(cutCreateTimer(&timer));
}
void cleanup()
{
cutilCheckError( cutDeleteTimer( timer));
if (g_CheckRender) {
delete g_CheckRender; g_CheckRender = NULL;
}
}
// initialize OpenGL
void initGL(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("Smoldyn on GPU");
glewInit();
if (!glewIsSupported("GL_VERSION_2_0 GL_VERSION_1_5 GL_ARB_multitexture GL_ARB_vertex_buffer_object")) {
fprintf(stderr, "Required OpenGL extensions missing.");
exit(-1);
}
#if defined (_WIN32)
if (wglewIsSupported("WGL_EXT_swap_control")) {
// disable vertical sync
wglSwapIntervalEXT(0);
}
#endif
glEnable(GL_DEPTH_TEST);
glClearColor(0.25, 0.25, 0.25, 1.0);
glutReportErrors();
}
void runBenchmark(int iterations)
{
cudaThreadSynchronize();
cutilCheckError(cutStartTimer(timer));
for (int i = 0; i < iterations; ++i)
{
if (!psystem->update())
break;
}
cudaThreadSynchronize();
cutilCheckError(cutStopTimer(timer));
float milliseconds = cutGetTimerValue(timer);
printf("%d particles, total time for %d iterations: %0.3f ms\n", numParticles, iterations, milliseconds);
if (g_bQAReadback) {
float *hPos = (float *)malloc(sizeof(float)*4*psystem->getNumParticles());
copyArrayFromDevice(hPos, psystem->getCudaPosVBO(),
0, sizeof(float)*4*psystem->getNumParticles());
g_CheckRender->dumpBin((void *)hPos, sizeof(float)*4*psystem->getNumParticles(), (char *)sOriginalBin[0]);
if (!g_CheckRender->compareBin2BinFloat( sOriginalBin[0], sReferenceBin[0],
sizeof(float)*4*psystem->getNumParticles(), MAX_EPSILON_ERROR, THRESHOLD))
g_TotalErrors++;
printf("TEST %s\n", (g_TotalErrors > 0) ? "FAILED!" : "PASSED!");
}
}
void AutoQATest()
{
if (g_CheckRender && g_CheckRender->IsQAReadback()) {
char temp[256];
sprintf(temp, "AutoTest: Particles");
glutSetWindowTitle(temp);
exit(0);
}
}
void computeFPS()
{
frameCount++;
fpsCount++;
if (fpsCount == fpsLimit-1) {
g_Verify = true;
}
if (fpsCount == fpsLimit) {
char fps[256];
float ifps = 1.f / (cutGetAverageTimerValue(timer) / 1000.f);
sprintf(fps, "%s Smoldyn GPU (%d molecules): %3.1f fps",
((g_CheckRender && g_CheckRender->IsQAReadback()) ? "AutoTest: " : ""), numParticles, ifps);
glutSetWindowTitle(fps);
fpsCount = 0;
if (g_CheckRender && !g_CheckRender->IsQAReadback()) fpsLimit = (int)MAX(ifps, 1.f);
cutilCheckError(cutResetTimer(timer));
AutoQATest();
}
}
void display()
{
cutilCheckError(cutStartTimer(timer));
// update the simulation
if (!bPause)
{
if (!psystem->update())
{
///TODO: print something
exit(0);
}
++currentIteration;
if (currentIteration == numIterations)
{
//TODO: get time and exit
exit(0);
}
if (renderer)
renderer->setBufferObjects(psystem->getPosBuffer(), psystem->getTypesBuffer(), psystem->getNumParticles());
}
// render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for (int c = 0; c < 3; ++c)
{
camera_trans_lag[c] += (camera_trans[c] - camera_trans_lag[c]) * inertia;
camera_rot_lag[c] += (camera_rot[c] - camera_rot_lag[c]) * inertia;
}
glTranslatef(camera_trans_lag[0], camera_trans_lag[1], camera_trans_lag[2]);
glRotatef(camera_rot_lag[0], 1.0, 0.0, 0.0);
glRotatef(camera_rot_lag[1], 0.0, 1.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
// cube
glColor3f(1.0, 1.0, 1.0);
glutWireCube(2.0);
// collider
//glPushMatrix();
//float3 p = psystem->getColliderPos();
//glTranslatef(p.x, p.y, p.z);
//glColor3f(1.0, 0.0, 0.0);
//glutSolidSphere(psystem->getColliderRadius(), 20, 10);
//glPopMatrix();
if (renderer && displayEnabled)
{
renderer->display(displayMode);
}
if (displaySliders) {
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); // invert color
glEnable(GL_BLEND);
params->Render(0, 0);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
}
cutilCheckError(cutStopTimer(timer));
if (g_CheckRender && g_CheckRender->IsQAReadback() && g_Verify) {
// readback for QA testing
printf("> (Frame %d) Readback BackBuffer\n", frameCount);
g_CheckRender->readback( width, height );
g_CheckRender->savePPM(sOriginal[0], true, NULL);
if (!g_CheckRender->PPMvsPPM (sOriginal[0], sReference[0], MAX_EPSILON_ERROR, THRESHOLD)) {
g_TotalErrors++;
}
g_Verify = false;
}
glutSwapBuffers();
glutReportErrors();
computeFPS();
}
inline float frand()
{
return rand() / (float) RAND_MAX;
}
void reshape(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (float) w / (float) h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0, 0, w, h);
if (renderer) {
renderer->setWindowSize(w, h);
renderer->setFOV(60.0);
}
}
void mouse(int button, int state, int x, int y)
{
int mods;
if (state == GLUT_DOWN)
buttonState |= 1<<button;
else if (state == GLUT_UP)
buttonState = 0;
mods = glutGetModifiers();
if (mods & GLUT_ACTIVE_SHIFT) {
buttonState = 2;
} else if (mods & GLUT_ACTIVE_CTRL) {
buttonState = 3;
}
ox = x; oy = y;
demoMode = false;
idleCounter = 0;
if (displaySliders) {
if (params->Mouse(x, y, button, state)) {
glutPostRedisplay();
return;
}
}
glutPostRedisplay();
}
// transfrom vector by matrix
void xform(float *v, float *r, GLfloat *m)
{
r[0] = v[0]*m[0] + v[1]*m[4] + v[2]*m[8] + m[12];
r[1] = v[0]*m[1] + v[1]*m[5] + v[2]*m[9] + m[13];
r[2] = v[0]*m[2] + v[1]*m[6] + v[2]*m[10] + m[14];
}
// transform vector by transpose of matrix
void ixform(float *v, float *r, GLfloat *m)
{
r[0] = v[0]*m[0] + v[1]*m[1] + v[2]*m[2];
r[1] = v[0]*m[4] + v[1]*m[5] + v[2]*m[6];
r[2] = v[0]*m[8] + v[1]*m[9] + v[2]*m[10];
}
void ixformPoint(float *v, float *r, GLfloat *m)
{
float x[4];
x[0] = v[0] - m[12];
x[1] = v[1] - m[13];
x[2] = v[2] - m[14];
x[3] = 1.0f;
ixform(x, r, m);
}
void motion(int x, int y)
{
float dx, dy;
dx = x - ox;
dy = y - oy;
if (displaySliders) {
if (params->Motion(x, y)) {
ox = x; oy = y;
glutPostRedisplay();
return;
}
}
switch(mode)
{
case M_VIEW:
if (buttonState == 3) {
// left+middle = zoom
camera_trans[2] += (dy / 100.0) * 0.5 * fabs(camera_trans[2]);
}
else if (buttonState & 2) {
// middle = translate
camera_trans[0] += dx / 100.0;
camera_trans[1] -= dy / 100.0;
}
else if (buttonState & 1) {
// left = rotate
camera_rot[0] += dy / 5.0;
camera_rot[1] += dx / 5.0;
}
break;
case M_MOVE:
{
float translateSpeed = 0.003f;
float3 p = psystem->getColliderPos();
if (buttonState==1) {
float v[3], r[3];
v[0] = dx*translateSpeed;
v[1] = -dy*translateSpeed;
v[2] = 0.0f;
ixform(v, r, modelView);
p.x += r[0];
p.y += r[1];
p.z += r[2];
} else if (buttonState==2) {
float v[3], r[3];
v[0] = 0.0f;
v[1] = 0.0f;
v[2] = dy*translateSpeed;
ixform(v, r, modelView);
p.x += r[0];
p.y += r[1];
p.z += r[2];
}
psystem->setColliderPos(p);
}
break;
}
ox = x; oy = y;
demoMode = false;
idleCounter = 0;
glutPostRedisplay();
}
// commented out to remove unused parameter warnings in Linux
void key(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case ' ':
bPause = !bPause;
break;
case 13:
if (!psystem->update())
exit(0);
if (renderer)
renderer->setBufferObjects(psystem->getPosBuffer(), psystem->getTypesBuffer(), psystem->getNumParticles());
break;
case '\033':
case 'q':
exit(0);
break;
case 'v':
mode = M_VIEW;
break;
case 'm':
mode = M_MOVE;
break;
case 'p':
displayMode = (ParticleRenderer::DisplayMode)
((displayMode + 1) % ParticleRenderer::PARTICLE_NUM_MODES);
break;
case 'd':
psystem->dumpGrid();
break;
case 'u':
psystem->dumpParticles(0, numParticles-1);
break;
case 'r':
displayEnabled = !displayEnabled;
break;
case '1':
psystem->reset(ParticleSystem::CONFIG_GRID);
break;
case '2':
psystem->reset(ParticleSystem::CONFIG_RANDOM);
break;
case 'w':
wireframe = !wireframe;
break;
case 'h':
displaySliders = !displaySliders;
break;
}
demoMode = false;
idleCounter = 0;
glutPostRedisplay();
}
void special(int k, int x, int y)
{
if (displaySliders) {
params->Special(k, x, y);
}
demoMode = false;
idleCounter = 0;
}
void idle(void)
{
if ((idleCounter++ > idleDelay) && (demoMode==false)) {
demoMode = true;
printf("Entering demo mode\n");
}
glutPostRedisplay();
}
void initParams()
{
if (g_bQAReadback) {
timestep = 0.0f;
damping = 0.0f;
gravity = 0.0f;
ballr = 1;
collideSpring = 0.0f;
collideDamping = 0.0f;
collideShear = 0.0f;
collideAttraction = 0.0f;
} else {
// create a new parameter list
params = new ParamListGL("misc");
params->AddParam(new Param<float>("time step", timestep, 0.0, 1.0, 0.01, ×tep));
params->AddParam(new Param<float>("damping", damping, 0.0, 1.0, 0.001, &damping));
params->AddParam(new Param<float>("gravity", gravity, 0.0, 0.001, 0.0001, &gravity));
params->AddParam(new Param<int>("ball radius", ballr, 1, 20, 1, &ballr));
params->AddParam(new Param<float>("collide spring", collideSpring, 0.0, 1.0, 0.001, &collideSpring));
params->AddParam(new Param<float>("collide damping", collideDamping, 0.0, 0.1, 0.001, &collideDamping));
params->AddParam(new Param<float>("collide shear", collideShear, 0.0, 0.1, 0.001, &collideShear));
params->AddParam(new Param<float>("collide attract", collideAttraction, 0.0, 0.1, 0.001, &collideAttraction));
}
}
void mainMenu(int i)
{
key((unsigned char) i, 0, 0);
}
void initMenus()
{
glutCreateMenu(mainMenu);
glutAddMenuEntry("Reset block [1]", '1');
glutAddMenuEntry("Reset random [2]", '2');
glutAddMenuEntry("Add sphere [3]", '3');
glutAddMenuEntry("View mode [v]", 'v');
glutAddMenuEntry("Move cursor mode [m]", 'm');
glutAddMenuEntry("Toggle point rendering [p]", 'p');
glutAddMenuEntry("Toggle animation [ ]", ' ');
glutAddMenuEntry("Step animation [ret]", 13);
glutAddMenuEntry("Toggle sliders [h]", 'h');
glutAddMenuEntry("Quit (esc)", '\033');
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
uint gridDim = GRID_SIZE;
if (argc > 1) {
cutGetCmdLineArgumenti( argc, (const char**) argv, "n", (int *) &numParticles);
cutGetCmdLineArgumenti( argc, (const char**) argv, "grid", (int *) &gridDim);
if (cutCheckCmdLineFlag(argc, (const char **)argv, "qatest") ||
cutCheckCmdLineFlag(argc, (const char **)argv, "noprompt"))
{
g_bQAReadback = true;
fpsLimit = frameCheckNumber;
numIterations = 1;
}
if (cutCheckCmdLineFlag(argc, (const char **)argv, "glverify"))
{
numIterations = 1;
g_bGLVerify = true;
fpsLimit = frameCheckNumber;
}
}
gridSize.x = gridSize.y = gridSize.z = gridDim;
printf("grid: %d x %d x %d = %d cells\n", gridSize.x, gridSize.y, gridSize.z, gridSize.x*gridSize.y*gridSize.z);
printf("particles: %d\n", numParticles);
bool benchmark = cutCheckCmdLineFlag(argc, (const char**) argv, "benchmark") != 0;
cutGetCmdLineArgumenti( argc, (const char**) argv, "i", &numIterations);
if (g_bQAReadback) {
cudaInit(argc, argv);
} else {
initGL(argc, argv);
cudaGLInit(argc, argv);
}
InitializeRandomNumbers();
initParticleSystem(numParticles, gridSize, !g_bQAReadback);
initParams();
if (!g_bQAReadback)
initMenus();
if (g_bQAReadback) {
g_CheckRender = new CheckBackBuffer(numParticles*sizeof(float), 1, 4, false);
g_CheckRender->setPixelFormat(GL_RGBA);
g_CheckRender->setExecPath(argv[0]);
g_CheckRender->EnableQAReadback(true);
}
if (g_bGLVerify) {
g_CheckRender = new CheckBackBuffer(width, height, 4);
g_CheckRender->setPixelFormat(GL_RGBA);
g_CheckRender->setExecPath(argv[0]);
g_CheckRender->EnableQAReadback(true);
}
if (benchmark || g_bQAReadback)
{
if (numIterations <= 0)
numIterations = 300;
runBenchmark(numIterations);
}
else
{
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(key);
glutSpecialFunc(special);
glutIdleFunc(idle);
atexit(cleanup);
glutMainLoop();
}
if (psystem)
delete psystem;
cudaThreadExit();
cutilExit(argc, argv);
}