-
Notifications
You must be signed in to change notification settings - Fork 0
/
Julia4D.cu
399 lines (319 loc) · 10.4 KB
/
Julia4D.cu
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
#ifdef _WIN32
# define WINDOWS_LEAN_AND_MEAN
# define NOMINMAX
# include <windows.h>
#endif
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, GL
#include <GL/glew.h>
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// includes
#include <cutil.h>
#include <cutil_gl_error.h>
#include <cuda_gl_interop.h>
////////////////////////////////////////////////////////////////////////////////
// constants
const unsigned int window_width = 512;
const unsigned int window_height = 512;
/* maximum
const unsigned int mesh_width = 512;
const unsigned int mesh_height = 512;
const unsigned int mesh_depth = 1024;
*/
#define threads_per_block 64
const unsigned int mesh_depth = threads_per_block*2; // multiple of 265! depth³ must fit in signed int (265*5=1280)
GLuint vbo;
GLuint vboNormals;
int once = 0;
int countd = 0;
unsigned char *dbuffer;
unsigned char *dbuffer2;
unsigned char *dhbuffer;
int *countbuffer;
int *hcountbuffer;
unsigned int _displayListId;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
float4 C = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
float e = 0.0f;
////////////////////////////////////////////////////////////////////////////////
// kernels
#include <Julia4D_kernel.cu>
////////////////////////////////////////////////////////////////////////////////
// declaration, forward
void runTest( int argc, char** argv);
// GL functionality
CUTBoolean initGL();
void createBuffers();
void deleteVBO( GLuint* vbo);
// rendering callbacks
void display();
void keyboard( unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
// Cuda functionality
void runCuda( );
void checkResultCuda( int argc, char** argv, const GLuint& vbo);
////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main( int argc, char** argv)
{
runTest( argc, argv);
CUT_EXIT(argc, argv);
}
////////////////////////////////////////////////////////////////////////////////
//! Run a simple test for CUDA
////////////////////////////////////////////////////////////////////////////////
void runTest( int argc, char** argv)
{
CUT_DEVICE_INIT(argc, argv);
// Create GL context
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( window_width, window_height);
glutCreateWindow( "Cuda GL interop");
// initialize GL
if( CUTFalse == initGL()) {
return;
}
// register callbacks
glutDisplayFunc( display);
glutKeyboardFunc( keyboard);
glutMouseFunc( mouse);
glutMotionFunc( motion);
// create VBO
createBuffers( );
dhbuffer = new unsigned char[mesh_depth*mesh_depth*mesh_depth/8];
hcountbuffer = new int[mesh_depth*mesh_depth];
// run the cuda part
runCuda( );
// check result of Cuda step
// checkResultCuda( argc, argv, vbo);
// start rendering mainloop
glutMainLoop();
}
////////////////////////////////////////////////////////////////////////////////
//! Run the Cuda part of the computation
////////////////////////////////////////////////////////////////////////////////
void runCuda()
{
if (once != 0) return;
// execute the kernel
/*
64 threads per block is minimal and makes sense only if there are multiple active
blocks per multiprocessor. 192 or 256 threads per block is better and usually allows
for enough registers to compile.
*/
dim3 grid(mesh_depth/threads_per_block, mesh_depth);
dim3 block(threads_per_block);
kernel<<< grid, block>>>(dbuffer, mesh_depth, C, e, 2);
CUDA_SAFE_CALL(cudaThreadSynchronize());
kernel2<<< grid, block>>>(dbuffer, countbuffer, mesh_depth);
CUDA_SAFE_CALL(cudaThreadSynchronize());
CUDA_SAFE_CALL(cudaMemcpy(hcountbuffer, countbuffer, mesh_depth*mesh_depth*4, cudaMemcpyDeviceToHost));
countd = 0;
for (int i = 0; i < mesh_depth*mesh_depth; i++){
countd += hcountbuffer[i];
}
// initialize buffer object
glBindBuffer( GL_ARRAY_BUFFER, vbo);
glBufferData( GL_ARRAY_BUFFER, countd * 4 * 4 * sizeof( float), 0, GL_DYNAMIC_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, vboNormals);
glBufferData( GL_ARRAY_BUFFER, countd * 4 * 4 * sizeof( float), 0, GL_DYNAMIC_DRAW);
glBindBuffer( GL_ARRAY_BUFFER, 0);
// register buffer object with CUDA
float4 *dptr;
float3 *dptrNormals;
CUDA_SAFE_CALL(cudaGLMapBufferObject( (void**)&dptr, vbo));
CUDA_SAFE_CALL(cudaGLMapBufferObject( (void**)&dptrNormals, vboNormals));
kernel3<<< grid, block>>>(dbuffer, countbuffer, dptr, dptrNormals, mesh_depth);
CUDA_SAFE_CALL(cudaThreadSynchronize());
CUDA_SAFE_CALL(cudaGLUnmapBufferObject( vbo));
CUDA_SAFE_CALL(cudaGLUnmapBufferObject( vboNormals));
once = 1;
}
////////////////////////////////////////////////////////////////////////////////
//! Initialize GL
////////////////////////////////////////////////////////////////////////////////
CUTBoolean initGL()
{
// initialize necessary OpenGL extensions
glewInit();
if (! glewIsSupported( "GL_VERSION_2_0 "
"GL_ARB_pixel_buffer_object"
)) {
fprintf( stderr, "ERROR: Support for necessary OpenGL extensions missing.");
fflush( stderr);
return CUTFalse;
}
// default initialization
glClearColor( 0.0, 0.0, 0.0, 1.0);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
// glDisable( GL_DEPTH_TEST);
// viewport
glViewport( 0, 0, window_width, window_height);
// projection
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)window_width / (GLfloat) window_height, 0.1, 10.0);
CUT_CHECK_ERROR_GL();
return CUTTrue;
}
////////////////////////////////////////////////////////////////////////////////
//! Create VBO
////////////////////////////////////////////////////////////////////////////////
void createBuffers()
{
CUDA_SAFE_CALL(cudaMalloc((void **) &dbuffer, mesh_depth*mesh_depth*mesh_depth/8));
//CUDA_SAFE_CALL(cudaMalloc((void **) &dbuffer2, mesh_height*mesh_width*mesh_depth/8));
CUDA_SAFE_CALL(cudaMalloc((void **) &countbuffer, mesh_depth*mesh_depth*4));
glGenBuffers( 1, &vbo);
glGenBuffers( 1, &vboNormals);
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(vbo));
CUDA_SAFE_CALL(cudaGLRegisterBufferObject(vboNormals));
CUT_CHECK_ERROR_GL();
}
////////////////////////////////////////////////////////////////////////////////
//! Delete VBO
////////////////////////////////////////////////////////////////////////////////
void deleteVBO( GLuint* vbo)
{
}
////////////////////////////////////////////////////////////////////////////////
//! Display callback
////////////////////////////////////////////////////////////////////////////////
void display()
{
// run CUDA kernel to generate vertex positions
runCuda();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set view matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, -1.0, -4.0, 0.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glTranslatef(0.0, 0.0, translate_z);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(4, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboNormals);
glNormalPointer(GL_FLOAT, 0, 0);
// glColorPointer(4, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
// glEnableClientState(GL_COLOR_ARRAY);
glColor3f(1.0, 1.0, 1.0);
glDrawArrays(GL_QUADS, 0, countd*4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
// glDisableClientState(GL_COLOR_ARRAY);
glutSwapBuffers();
glutPostRedisplay();
}
////////////////////////////////////////////////////////////////////////////////
//! Keyboard events handler
////////////////////////////////////////////////////////////////////////////////
void keyboard( unsigned char key, int /*x*/, int /*y*/)
{
switch( key) {
case( 27) :
exit( 0);
break;
case( 'q') :
once = 0;
e += 0.1f;
break;
case( 'a') :
once = 0;
e -= 0.1f;
break;
case( 'w') :
once = 0;
C.x += 0.1f;
break;
case( 's') :
once = 0;
C.x -= 0.1f;
break;
case( 'e') :
once = 0;
C.y += 0.1f;
break;
case( 'd') :
once = 0;
C.y -= 0.1f;
break;
case( 'r') :
once = 0;
C.z += 0.1f;
break;
case( 'f') :
once = 0;
C.z -= 0.1f;
break;
case( 't') :
once = 0;
C.w += 0.1f;
break;
case( 'g') :
once = 0;
C.w -= 0.1f;
break;
}
}
////////////////////////////////////////////////////////////////////////////////
//! Mouse event handlers
////////////////////////////////////////////////////////////////////////////////
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN) {
mouse_buttons |= 1<<button;
} else if (state == GLUT_UP) {
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
glutPostRedisplay();
}
void motion(int x, int y)
{
float dx, dy;
dx = x - mouse_old_x;
dy = y - mouse_old_y;
if (mouse_buttons & 1) {
rotate_x += dy * 0.2;
rotate_y += dx * 0.2;
} else if (mouse_buttons & 4) {
translate_z += dy * 0.01;
}
mouse_old_x = x;
mouse_old_y = y;
}
////////////////////////////////////////////////////////////////////////////////
//! Check if the result is correct or write data to file for external
//! regression testing
////////////////////////////////////////////////////////////////////////////////
void checkResultCuda( int argc, char** argv, const GLuint& vbo)
{
CUT_CHECK_ERROR_GL();
}