-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluidsGL.cpp
More file actions
656 lines (566 loc) · 17.7 KB
/
fluidsGL.cpp
File metadata and controls
656 lines (566 loc) · 17.7 KB
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
/*
* Copyright 1993-2015 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
// This is a ported CPU version of the CUDA fluidsGL demo
// originally published by NVidia.
//
// Port made by Jesús Martín Berlanga
//
// Info and documentation at rtfluids.bdevel.org
// OpenGL Graphics includes
#if defined(__APPLE__) || defined(MACOSX)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#include <GLUT/glut.h>
#ifndef glutCloseFunc
#define glutCloseFunc glutWMCloseFunc
#endif
#else
#include <GL/glew.h>
#include <GL/freeglut.h>
#endif
// Includes
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "defines.h"
#include "fluidsGL_cpu.hpp"
#define MAX_EPSILON_ERROR 1.0f
const char *sSDKname = "fluidsGL";
void cleanup(void);
void reshape(int x, int y);
static cData *vxfield = NULL;
static cData *vyfield = NULL;
cData *hvfield = NULL;
static int wWidth = MAX(512, DIM);
static int wHeight = MAX(512, DIM);
// FPS counter
static int fpsCount = 0;
static int time = 0;
static int base_time = 0;
static int timeLimit = 1000;
static float fps = 0;
// Variables for the automatic stress test
static bool stress_test = false;
static int stress_test_step_index = 0;
static int stress_test_n_steps = 10000;
static int fps_n_accumulations = 0;
static float fps_accumulation = 0;
static int clicked = 0;
// Particle data
GLuint vbo = 0; // OpenGL vertex buffer object
static cData *particles = NULL; // particle positions in host memory
static int lastx = 0, lasty = 0;
// Texture pitch
size_t tPitch = 0; // Now this is compatible with gcc in 64-bit
char *ref_file = NULL;
bool g_bQAAddTestForce = true;
int g_TotalErrors = 0;
// 10 vueltas de bucle en el test
int g_dumpFrames = 10;
// Para el test:
// No recorrer matrizes enteras,
// nos llevaría demasiado tiempo,
// recorrer 40 veces menos (saltando
// de 40 en 40).
int g_compareDivisor = 1;
bool g_bExitESC = false;
//#define DUMPEABLE
#ifdef DUMPEABLE
FILE* dump_file = NULL;
void dump_title(const char* title) {
if(dump_file != NULL) {
fprintf(dump_file,
"\n\n\n///////////////////////////////////[%s]///////////////////////////////////\n"
,title);
}
}
void dump(const char* tag) {
if(dump_file != NULL) {
fprintf(dump_file,
"\n\n=============[%s]=============\n"
,tag);
// Dump de hvfield
fprintf(dump_file,"\n----[vfield]----\n");
for(int x = 0; x < DIM; x = x + g_compareDivisor) {
for(int y = 0; y < DIM; y = y + g_compareDivisor) {
int i = y*DIM + x;
cData iv = hvfield[i];
fprintf(dump_file,"(%f,%f)",iv.x,iv.y);
}
fprintf(dump_file,"\n");
}
// Dump de vxfield
fprintf(dump_file,"\n----[vxfield memory]----\n");
for(int x = 0; x < CPADW; x = x + g_compareDivisor) {
for(int y = 0; y < DIM; y = y + g_compareDivisor) {
int i = y*CPADW + x;
cData vx = vxfield[i];
fprintf(dump_file,"|%f|%f",
vx.x,vx.y
);
}
fprintf(dump_file,"\n");
}
// Dump de vyfield
fprintf(dump_file,"\n----[vyfield memory]----\n");
for(int x = 0; x < CPADW; x = x + g_compareDivisor) {
for(int y = 0; y < DIM; y = y + g_compareDivisor) {
int i = y*CPADW + x;
cData vy = vyfield[i];
fprintf(dump_file,"|%f|%f",
vy.x,vy.y
);
}
fprintf(dump_file,"\n");
}
}
}
#endif
void gl_print_errors(const char* where) {
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
fprintf(stderr, "OpenGL error at %s: %#04x\n", where, err);
}
}
cData* glBufferMap;
#ifdef DUMPEABLE
void dumpMapGLBuffer() {
fprintf(dump_file, "\n''glBufferMap mapGLBuffer()''''''''''''''''''''\n");
for(int x = 0; x < DIM; x = x + g_compareDivisor) {
for(int y = 0; y < DIM; y = y + g_compareDivisor) {
int i = y*DIM + x;
cData part = glBufferMap[i];
fprintf(dump_file,"(%f,%f)",part.x,part.y);
}
fprintf(dump_file,"\n");
}
fprintf(dump_file, "\n'''''''''''''''''''''''''''''''''''\n");
}
#endif
void mapGlBuffer() {
glBindBuffer(GL_ARRAY_BUFFER, vbo); // Establish to which buffer we will do the mapping
// Map buffer with:
// void *glMapBufferRange(GLenum target, GLintptr offset,
// GLsizeiptr length, GLbitfield access);
glBufferMap = (cData*) glMapBufferRange(GL_ARRAY_BUFFER, 0,
sizeof(cData) * DS, GL_MAP_WRITE_BIT | GL_MAP_READ_BIT);
if(glBufferMap == NULL) {
fprintf(stderr, "Failed to map GL_ARRAY_BUFFER\n");
gl_print_errors("glBufferMap");
exit(EXIT_FAILURE);
}
glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind
#ifdef DUMPEABLE
dumpMapGLBuffer();
#endif
}
void unmapGLBuffer() {
#ifdef DUMPEABLE
dumpMapGLBuffer();
#endif
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Unmap buffer if we will no longer operate with it
// in order to evade unexpected errors.
// For example, if a buffer is used for feedback at the
// same time it's used for another purpose it will cause
// an error. e.g. mapped buffer at the same time we call
// glDrawArrays will cause an error.
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
extern "C" void addForces(cData *v, int dx, int dy, int spx, int spy, float fx, float fy, int r);
extern "C" void advectVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy, float dt);
extern "C" void diffuseProject(cData *vx, cData *vy, int dx, int dy, float dt, float visc);
extern "C" void updateVelocity(cData *v, float *vx, float *vy, int dx, int pdx, int dy);
extern "C" void advectParticles(cData *part, cData *v, int dx, int dy, float dt);
void simulateFluids(void)
{
// simulate fluid
#ifdef DUMPEABLE
dump_title("simulateFluids");
#endif
#ifdef DUMPEABLE
dump("advectVelocity (pre)");
#endif
advectVelocity(hvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM, DT);
#ifdef DUMPEABLE
dump("advectVelocity (post)");
#endif
#ifdef DUMPEABLE
dump("diffuseProject (pre)");
#endif
diffuseProject(vxfield, vyfield, CPADW, DIM, DT, VIS);
#ifdef DUMPEABLE
dump("diffuseProject (post)");
#endif
#ifdef DUMPEABLE
dump("updateVelocity (pre)");
#endif
updateVelocity(hvfield, (float *)vxfield, (float *)vyfield, DIM, RPADW, DIM);
#ifdef DUMPEABLE
dump("updateVelocity (post)");
#endif
mapGlBuffer();
#ifdef DUMPEABLE
dump("advectParticles (pre)");
#endif
advectParticles(glBufferMap, hvfield, DIM, DIM, DT);
#ifdef DUMPEABLE
dump("advectParticles (post)");
#endif
unmapGLBuffer();
}
void display(void)
{
if (!ref_file)
{
simulateFluids();
}
// render points from vertex buffer
glClear(GL_COLOR_BUFFER_BIT);
glColor4f(0,1,0,0.5f);
glPointSize(1);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnableClientState(GL_VERTEX_ARRAY);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, 0, NULL);
gl_print_errors("glVertexPointer");
glDrawArrays(GL_POINTS, 0, DS);
gl_print_errors("glDrawArrays");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
if (ref_file)
{
return;
}
if(stress_test && stress_test_step_index < stress_test_n_steps) {
if(stress_test_step_index % 10 == 0) {
// Add Force (simulate click)
// each 10 steps
int x = wWidth/(stress_test_step_index/100+1);
int y = wHeight/(stress_test_step_index/100+1);
float fx = (x / (float)wWidth);
float fy = (y / (float)wHeight);
int nx = (int)(fx * DIM);
int ny = (int)(fy * DIM);
int ddx = 35;
int ddy = 35;
fx = ddx / (float)wWidth;
fy = ddy / (float)wHeight;
int spy = ny-FR;
int spx = nx-FR;
addForces(hvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy, FR);
lastx = x;
lasty = y;
}
stress_test_step_index++;
}
fpsCount++;
time = glutGet(GLUT_ELAPSED_TIME);
if ((time - base_time) > timeLimit)
{
// multiply by 1000 to convert ms to seconds
fps=fpsCount*1000.0f/(time - base_time);
// update last taken fps time (base_time)
base_time = time;
// reset fps count
fpsCount=0;
char fps_string[256];
sprintf(fps_string, "CPU/GL Stable Fluids (%d x %d): %f fps", DIM, DIM, fps);
glutSetWindowTitle(fps_string);
if(stress_test) {
printf("%f fps (%d/%d)\n",fps,stress_test_step_index,stress_test_n_steps);
fps_accumulation += fps;
fps_n_accumulations++;
// Exit test just right after
// the last measure.
if(stress_test_step_index >= stress_test_n_steps) {
printf("fps average: %f\n",fps_accumulation/fps_n_accumulations);
g_bExitESC = true;
#if defined (__APPLE__) || defined(MACOSX)
exit(EXIT_SUCCESS);
#else
glutDestroyWindow(glutGetWindow());
return;
#endif
}
}
}
// Finish timing before swap buffers to avoid refresh sync
glutSwapBuffers();
glutPostRedisplay();
}
/*
* Escribe en un fichero auxiliar los resultados
* de cada uno de los pasos del algoritmo.
*/
void dumpTest()
{
#ifdef DUMPEABLE
dump_file = fopen("dump_test/dump.txt", "w");
#endif
reshape(wWidth, wHeight);
for (int count=0; count<g_dumpFrames; count++)
{
simulateFluids();
// add in a little force so the automated testing is interesting.
if (ref_file)
{
#ifdef DUMPEABLE
dump("addForces (pre)");
#endif
int x = wWidth/(count+1);
int y = wHeight/(count+1);
float fx = (x / (float)wWidth);
float fy = (y / (float)wHeight);
int nx = (int)(fx * DIM);
int ny = (int)(fy * DIM);
#ifdef DUMPEABLE
fprintf(dump_file,"\n----[dumpTest automatic forces]----\n");
fprintf(dump_file, "\tx = %d/(%d+1) = %d",wWidth,count,x);
fprintf(dump_file, "\ty = %d/(%d+1) = %d",wHeight,count,y);
fprintf(dump_file, "\tfx = %d/((float)%d) = %f",x,wWidth,fx);
fprintf(dump_file, "\tfy = %d/((float)%d) = %f",y,wHeight,fy);
#endif
int ddx = 35;
int ddy = 35;
fx = ddx / (float)wWidth;
fy = ddy / (float)wHeight;
int spy = ny-FR;
int spx = nx-FR;
#ifdef DUMPEABLE
fprintf(dump_file, "\tFORCE * DT * fx = %f * %f * fx = %f",FORCE,DT,FORCE * DT * fx);
fprintf(dump_file, "\tFORCE * DT * fx = %f * %f * fx = %f\n",FORCE,DT,FORCE * DT * fy);
#endif
addForces(hvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy, FR);
#ifdef DUMPEABLE
dump("addForces (post)");
#endif
lastx = x;
lasty = y;
}
}
display();
#ifdef DUMPEABLE
fclose(dump_file);
#endif
}
// very simple von neumann middle-square prng. can't use rand() in -qatest
// mode because its implementation varies across platforms which makes testing
// for consistency in the important parts of this program difficult.
float myrand(void)
{
static int seed = 72191;
char sq[22];
if (ref_file)
{
seed *= seed;
sprintf(sq, "%010d", seed);
// pull the middle 5 digits out of sq
sq[8] = 0;
seed = atoi(&sq[3]);
return seed/99999.f;
}
else
{
return rand()/(float)RAND_MAX;
}
}
void initParticles(cData *p, int dx, int dy)
{
int i, j;
for (i = 0; i < dy; i++)
{
for (j = 0; j < dx; j++)
{
p[i*dx+j].x = (j+0.5f+(myrand() - 0.5f))/dx;
p[i*dx+j].y = (i+0.5f+(myrand() - 0.5f))/dy;
}
}
}
void keyboard(unsigned char key, int x, int y)
{
// Only enable key functionallity if
// the automatic stress test is not
// taking place
if(!stress_test) switch (key)
{
case 27:
g_bExitESC = true;
#if defined (__APPLE__) || defined(MACOSX)
exit(EXIT_SUCCESS);
#else
glutDestroyWindow(glutGetWindow());
return;
#endif
break;
case 'r':
memset(hvfield, 0, sizeof(cData) * DS);
initParticles(particles, DIM, DIM);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS,
particles, GL_DYNAMIC_DRAW_ARB);
glBindBuffer(GL_ARRAY_BUFFER, 0);
break;
default:
break;
}
}
void click(int button, int updown, int x, int y)
{
// Only enable click functionallity if
// the automatic stress test is not
// taking place
if(!stress_test) {
lastx = x;
lasty = y;
clicked = !clicked;
}
}
void motion(int x, int y)
{
// Convert motion coordinates to domain
float fx = (lastx / (float)wWidth);
float fy = (lasty / (float)wHeight);
int nx = (int)(fx * DIM);
int ny = (int)(fy * DIM);
if (clicked && nx < DIM-FR && nx > FR-1 && ny < DIM-FR && ny > FR-1)
{
int ddx = x - lastx;
int ddy = y - lasty;
fx = ddx / (float)wWidth;
fy = ddy / (float)wHeight;
int spy = ny-FR;
int spx = nx-FR;
addForces(hvfield, DIM, DIM, spx, spy, FORCE * DT * fx, FORCE * DT * fy, FR);
lastx = x;
lasty = y;
}
glutPostRedisplay();
}
void reshape(int x, int y)
{
wWidth = x;
wHeight = y;
glViewport(0, 0, x, y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1, 1, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}
void cleanup(void)
{
// Free all host and device resources
free(hvfield);
free(particles);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDeleteBuffers(1, &vbo);
}
int initGL(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(wWidth, wHeight);
glutCreateWindow("Compute Stable Fluids");
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return false;
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(click);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
return true;
}
int main(int argc, char **argv)
{
char* lastArg = argv[argc-1];
if (strcmp(lastArg,"test") == 0)
{
// Restablecemos la dimensión del problema por defecto
// a algo mas manejable que sirva para comprobar el funcionamiento
// a simple vista.
DIM = 8;
FR = 2; // Dismunimos radio de aplicacion de fuerza
// a un tamaño razonable para nuestra pequeña dimensión
updateVariables();
}
else if (strcmp(lastArg,"stress_test") == 0)
{
printf("Starting automatic stress test...\n");
stress_test = true;
}
#if defined(__linux__)
setenv ("DISPLAY", ":0", 0);
#endif
printf("%s Starting...\n\n", sSDKname);
// First initialize OpenGL context, so we can properly set the GL for CUDA.
// This is necessary in order to achieve optimal performance with OpenGL/CUDA interop.
if (false == initGL(&argc, argv))
{
exit(EXIT_SUCCESS);
}
// Allocate and initialize host data
GLint bsize;
hvfield = (cData *)malloc(sizeof(cData) * DS);
memset(hvfield, 0, sizeof(cData) * DS);
vxfield = (cData*) malloc(sizeof(cData) * PDS);
vyfield = (cData*) malloc(sizeof(cData) * PDS);
// Create particle array
particles = (cData *)malloc(sizeof(cData) * DS);
memset(particles, 0, sizeof(cData) * DS);
initParticles(particles, DIM, DIM);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(cData) * DS,
particles, GL_DYNAMIC_DRAW_ARB);
// DRAW: Buffer de escritura/lectura
glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bsize);
if (bsize != ((int) (sizeof(cData) * DS)))
goto EXTERR;
glBindBuffer(GL_ARRAY_BUFFER, 0);
if (strcmp(lastArg,"test") == 0)
{
ref_file = (char*) 1;
printf("Starting dump test...\n");
dumpTest();
cleanup();
}
else
{
#if defined (__APPLE__) || defined(MACOSX)
atexit(cleanup);
#else
glutCloseFunc(cleanup);
#endif
glutMainLoop();
}
if (!ref_file)
{
exit(EXIT_SUCCESS);
}
return 0;
EXTERR:
printf("Failed to initialize GL extensions.\n");
exit(EXIT_FAILURE);
}