-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering.cu
562 lines (442 loc) · 20.3 KB
/
rendering.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
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
/*
* Nbody simulation data afterprocess program.
* Rewrite after a big deletion.
* Resbi 27/11/2024
*
* The CUDA version, no more to say.
* Resbi 30/11/2024
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <omp.h>
#include <time.h>
//#include <unistd.h>
#include <io.h>
// Dont use this.
//#include <cuda_fp16.h>
#ifndef __linux__
#include <windows.h>
#endif
#ifdef __linux__
double __getMillisecond() {
struct timeval time;
gettimeofday(&time, NULL);
return (double)(time.tv_sec * 1000.0 + time.tv_usec / 1000.0);
}
#define getMillisecond __getMillisecond
#else
#define getMillisecond GetTickCount
#endif
#ifdef __linux__
#define __aligned_alloc aligned_alloc
#define __aligned_free free
#else
#define __aligned_alloc(__alignment__, __size__) _aligned_malloc(__size__, __alignment__)
#define __aligned_free _aligned_free
#endif
#define PRECISION double
#define PRECISION_4 double4
#define STAR_RADIUS (PRECISION)5e15
// Must be divisible by blockDim.x!!!
#define TILE_SIZE 128
// Todo: different precision when rendering
#define PRECISION_RENDERING float
#define __aligned_alloc_cuda(__alignment__, __size__) malloc(__size__)
#define __aligned_free_cuda free
// debugging
//#define DEBUG_ALL
#ifdef DEBUG_ALL
#define DEBUG_MEMORY
#define DEBUG_FILE
#endif
void writeBMP(char *image,
const char* filename,
int H,
int W) {
// Renormalizing size
int l = W / 4 * 4;
// Headers
int bmi[] = { l*H + 54,0,54,40,W,H,1 | 3 * 8 << 16,0,l*H,0,0,100,0 };
FILE *fp = fopen(filename, "wb");
#ifdef DEBUG_FILE
if (fp == NULL) {
printf("[ERR] File open error in writeBMP!\n\twhile opening %s\n", filename);
}
#endif
fprintf(fp, "BM");
fwrite(&bmi, 52, 1, fp);
// Write image
fwrite(image, 3, l*H, fp);
fclose(fp);
//free(fp);
}
// return a pointer to image
__global__ void render_CUDA(long image_size_width,
long image_size_hight,
long image_size_length,
PRECISION_RENDERING *image_camera_position,
PRECISION_RENDERING *image_screen_position,
PRECISION_RENDERING *image_screen_basis_w,
PRECISION_RENDERING *image_screen_basis_h,
long data_point_number,
PRECISION_4 *data_point_position,
char *image) {
unsigned long pixel_index = blockIdx.x * blockDim.x + threadIdx.x;
//if (pixel_index < image_size_length) {
PRECISION_RENDERING pixel_accumulation = 0;
long pixel_x = pixel_index % image_size_width - image_size_width / 2;
long pixel_y = pixel_index / image_size_width - image_size_hight / 2;
PRECISION_RENDERING pixel_position[3];
PRECISION_RENDERING pixel_normal[3];
PRECISION_RENDERING pixel_normal_norm_reverse;
PRECISION_RENDERING pixel_point_relative[3];
PRECISION_RENDERING pixel_point_distance_reverse;
PRECISION_RENDERING pixel_innerproduct;
PRECISION_RENDERING pixel_point_angleradius;
PRECISION_RENDERING pixel_point_angle;
pixel_position[0] = image_screen_position[0] + pixel_x * image_screen_basis_w[0] + pixel_y * image_screen_basis_h[0];
pixel_position[1] = image_screen_position[1] + pixel_x * image_screen_basis_w[1] + pixel_y * image_screen_basis_h[1];
pixel_position[2] = image_screen_position[2] + pixel_x * image_screen_basis_w[2] + pixel_y * image_screen_basis_h[2];
pixel_normal[0] = pixel_position[0] - image_camera_position[0];
pixel_normal[1] = pixel_position[1] - image_camera_position[1];
pixel_normal[2] = pixel_position[2] - image_camera_position[2];
pixel_normal_norm_reverse = rsqrtf(pixel_normal[0] * pixel_normal[0] +
pixel_normal[1] * pixel_normal[1] +
pixel_normal[2] * pixel_normal[2]);
pixel_normal[0] = pixel_normal[0] * pixel_normal_norm_reverse;
pixel_normal[1] = pixel_normal[1] * pixel_normal_norm_reverse;
pixel_normal[2] = pixel_normal[2] * pixel_normal_norm_reverse;
// Tile size must be divisible by block dim, at least for now.
__shared__ PRECISION_4 tile_point_position[TILE_SIZE];
for (long tile_index = 0; tile_index < data_point_number / TILE_SIZE; tile_index++) {
__syncthreads();
for (int tile_sync_index = 0; tile_sync_index < TILE_SIZE / blockDim.x; tile_sync_index++) {
tile_point_position[tile_sync_index * blockDim.x + threadIdx.x].x = data_point_position[tile_index * TILE_SIZE + tile_sync_index * blockDim.x + threadIdx.x].x;
tile_point_position[tile_sync_index * blockDim.x + threadIdx.x].y = data_point_position[tile_index * TILE_SIZE + tile_sync_index * blockDim.x + threadIdx.x].y;
tile_point_position[tile_sync_index * blockDim.x + threadIdx.x].z = data_point_position[tile_index * TILE_SIZE + tile_sync_index * blockDim.x + threadIdx.x].z;
tile_point_position[tile_sync_index * blockDim.x + threadIdx.x].w = data_point_position[tile_index * TILE_SIZE + tile_sync_index * blockDim.x + threadIdx.x].w;
}
__syncthreads();
for (long point_index = 0; point_index < TILE_SIZE; point_index++) {
pixel_point_relative[0] = tile_point_position[point_index].x - pixel_position[0];
pixel_point_relative[1] = tile_point_position[point_index].y - pixel_position[1];
pixel_point_relative[2] = tile_point_position[point_index].z - pixel_position[2];
pixel_point_distance_reverse = rsqrtf(pixel_point_relative[0] * pixel_point_relative[0] +
pixel_point_relative[1] * pixel_point_relative[1] +
pixel_point_relative[2] * pixel_point_relative[2]);
pixel_innerproduct = (pixel_point_relative[0] * pixel_normal[0] +
pixel_point_relative[1] * pixel_normal[1] +
pixel_point_relative[2] * pixel_normal[2]);
if (pixel_innerproduct > 0) {
// These are what makes it sloooooow
pixel_point_angleradius = atanf(STAR_RADIUS * pixel_point_distance_reverse);
pixel_point_angle = acosf(pixel_innerproduct * pixel_point_distance_reverse);
//if (pixel_point_angle < 4 * pixel_point_angleradius) {
//pixel_accumulation += 1;
// This is what makes it sloooooow
pixel_accumulation += 10 * __expf(- pixel_point_angle * pixel_point_angle / (pixel_point_angleradius * pixel_point_angleradius * 2));
//}
/*
PRECISION pixel_judge = pixel_point_angle * pixel_point_angle / (2 * pixel_point_angleradius);
if (pixel_judge < 1) {
pixel_accumulation += 10 * (1 - pixel_judge);
}
*/
}
}
}
/*
if (pixel_accumulation > 1) {
printf("(%ld) %f\n", pixel_index, pixel_accumulation);
}
*/
image[pixel_index * 3 + 0] = (char)(255 * atan(pixel_accumulation) * 2 / 3.1416);
image[pixel_index * 3 + 1] = (char)(255 * atan(pixel_accumulation) * 2 / 3.1416);
image[pixel_index * 3 + 2] = (char)(255 * atan(pixel_accumulation) * 2 / 3.1416);
//}
}
// return a pointer to data
PRECISION_4 *readData(char *data_file_name,
long *data_point_number) {
FILE *file = fopen(data_file_name, "rb");
// Failed to open!
if (file == NULL) {
*data_point_number = -1;
#ifdef DEBUG_FILE
printf("[ERR] File open error in readData()!\n\twhile opening %s\n", data_file_name);
#endif
return NULL;
}
// Read number of points, in ULL
unsigned long long temp_data_point_number;
#ifdef DEBUG_FILE
int debug_file_reading_point_number = fread(&temp_data_point_number, 8, 1, file);
if ((debug_file_reading_point_number == 0) || (ferror(file))) {
printf("[ERR] File read error in readData()!\n\twhile reading data_point_number\n");
}
#else
fread(&temp_data_point_number, 8, 1, file);
#endif
*data_point_number = (long)temp_data_point_number;
PRECISION_4 *data_point_position = (PRECISION_4 *)__aligned_alloc(32, sizeof(PRECISION_4) * temp_data_point_number);
#ifdef DEBUG_MEMORY
if (data_point_position == NULL) {
printf("[ERR] Memory alloc error in readData()!\n\twhile allocating data_point_position\n");
}
#endif
#ifdef DEBUG_FILE
int debug_file_reading_point_position = fread(data_point_position, sizeof(PRECISION_4), temp_data_point_number, file);
if ((debug_file_reading_point_position == 0) || (ferror(file))) {
printf("[ERR] File read error in readData()!\n\twhile reading data_point_position\n");
}
#else
fread(data_point_position, sizeof(PRECISION_4), temp_data_point_number, file);
#endif
// Remember to close them!!
fclose(file);
return data_point_position;
}
// rotate a vector along the Z axis (the third basis) with angle θ
// a |x> + b |y> + c |z> ↦ a |x(θ)> + b |y> + c |z(θ)>
// = a (cos(θ) |x> - sin(θ) |z>) +
// b |y> +
// c (sin(θ) |x> + cos(θ) |z>)
// ( cos(θ) 0 sin(θ) ) (a)
// = ( 0 1 0 ) (b)
// ( -sin(θ) 0 cos(θ) ) (c)
void rotateY3d_RENDERING(PRECISION_RENDERING rotate_angle,
PRECISION_RENDERING *vector) {
PRECISION_RENDERING x = vector[0] * cosf(rotate_angle) + vector[2] * sinf(rotate_angle);
PRECISION_RENDERING z = - vector[0] * sinf(rotate_angle) + vector[2] * cosf(rotate_angle);
vector[0] = x;
vector[2] = z;
}
// rotate a vector along the Z axis (the third basis) with angle θ
// a |x> + b |y> + c |z> ↦ a |x(θ)> + b |y(θ)> + c |z>
// = a (cos(θ) |x> + sin(θ) |y>) +
// b (-sin(θ) |x> + cos(θ) |y>) +
// c |z>
// ( cos(θ) -sin(θ) 0 ) (a)
// = ( sin(θ) cos(θ) 0 ) (b)
// ( 0 0 1 ) (c)
void rotateZ3d_RENDERING(PRECISION_RENDERING rotate_angle,
PRECISION_RENDERING *vector) {
PRECISION_RENDERING x = vector[0] * cosf(rotate_angle) - vector[1] * sinf(rotate_angle);
PRECISION_RENDERING y = vector[0] * sinf(rotate_angle) + vector[1] * cosf(rotate_angle);
vector[0] = x;
vector[1] = y;
}
int main() {
#ifdef DEBUG_ALL
printf("[DEBUG] Debugging enabled!\n");
#endif
#ifdef DEBUG_MEMORY
printf("[DEBUG] DEBUG_MEMORY enabled!\n");
#endif
#ifdef DEBUG_FILE
printf("[DEBUG] DEBUG_FILE enabled!\n");
#endif
char data_file_name[128];
char data_file_prefix[32] = "datas/524288";
char image_file_name[128];
char image_file_prefix[32] = "images/524288";
long image_size_width = 1920;
long image_size_hight = 1080;
long image_size_length = image_size_width * image_size_hight;
PRECISION_RENDERING image_camera_position[3] = {5e18f, 0, 0};
PRECISION_RENDERING image_screen_position[3] = {4.9e18f, 0, 0};
PRECISION_RENDERING image_screen_basis_w[3] = {0, 0.4e18f / image_size_hight * 2, 0};
PRECISION_RENDERING image_screen_basis_h[3] = {0, 0, 0.4e18f / image_size_hight * 2};
// CUDA configurations
int3 cuda_block_size;
int3 cuda_grid_size;
cuda_block_size.x = 64;
cuda_block_size.y = 1;
cuda_block_size.z = 1;
cuda_grid_size.x = (int)image_size_length / cuda_block_size.x;
cuda_grid_size.y = 1;
cuda_grid_size.z = 1;
dim3 block(cuda_block_size.x,
cuda_block_size.y,
cuda_block_size.z);
dim3 grid(cuda_grid_size.x,
cuda_grid_size.y,
cuda_grid_size.z);
rotateY3d_RENDERING(- 3.1415926525 / 4, image_camera_position);
rotateY3d_RENDERING(- 3.1415926525 / 4, image_screen_position);
rotateY3d_RENDERING(- 3.1415926525 / 4, image_screen_basis_w);
rotateY3d_RENDERING(- 3.1415926525 / 4, image_screen_basis_h);
printf("camera_position\n\t%f\n\t%f\n\t%f\n", image_camera_position[0], image_camera_position[1], image_camera_position[2]);
printf("screen_position\n\t%f\n\t%f\n\t%f\n", image_screen_position[0], image_screen_position[1], image_screen_position[2]);
printf("screen_basis_w\n\t%f\n\t%f\n\t%f\n", image_screen_basis_w[0], image_screen_basis_w[1], image_screen_basis_w[2]);
printf("screen_basis_h\n\t%f\n\t%f\n\t%f\n", image_screen_basis_h[0], image_screen_basis_h[1], image_screen_basis_h[2]);
long data_point_number = 0;
long data_point_number_next = 0;
// image_index = image_index_base + image_index_offset
// image_index_offset is the counter.
int image_index_offset = 0;
int image_index_base = 0;
int image_index = 0;
double begin_ms;
double end_ms;
double delta_ms;
while (data_point_number >= 0) {
image_index = image_index_base + image_index_offset;
PRECISION_RENDERING *cuda_image_camera_position;// = {5e18f, 0, 0};
PRECISION_RENDERING *cuda_image_screen_position;// = {3e18f, 0, 0};
PRECISION_RENDERING *cuda_image_screen_basis_w;// = {0, 3e18f / image_size_hight * 2, 0};
PRECISION_RENDERING *cuda_image_screen_basis_h;// = {0, 0, 3e18f / image_size_hight * 2};
cudaMalloc((void**)&cuda_image_camera_position,
sizeof(PRECISION_RENDERING) * 3);
cudaMalloc((void**)&cuda_image_screen_position,
sizeof(PRECISION_RENDERING) * 3);
cudaMalloc((void**)&cuda_image_screen_basis_w,
sizeof(PRECISION_RENDERING) * 3);
cudaMalloc((void**)&cuda_image_screen_basis_h,
sizeof(PRECISION_RENDERING) * 3);
PRECISION_4 *data_point_position;
PRECISION_4 *data_point_position_next;
char *image = (char *)__aligned_alloc(32, sizeof(char) * image_size_length * 3);
char *image_previous;
#ifdef DEBUG_MEMORY
if (image == NULL) {
printf("[ERR] Memory alloc error in main()!\n\twhile allocating image\n");
}
#endif
PRECISION_4 *cuda_data_point_position;
PRECISION_4 *cuda_data_point_position_next;
char *cuda_image;
char *cuda_image_previous;
cudaMalloc((void**)&cuda_image,
sizeof(char) * image_size_length * 3);
begin_ms = getMillisecond();
// The initial frame.
if (image_index_offset == 0) {
sprintf(data_file_name, "%s,%d.nbody", data_file_prefix, image_index);
printf("[%d] Reading %s\n", image_index, data_file_name);
data_point_position = readData(data_file_name,
&data_point_number);
cudaMalloc((void**)&cuda_data_point_position,
sizeof(PRECISION_4) * data_point_number);
cudaMemcpy(cuda_data_point_position,
data_point_position,
sizeof(PRECISION_4) * data_point_number,
cudaMemcpyHostToDevice);
}
/*
for (int i = 0; i < 10; i++) {
printf("%d\n\t%.2f\n\t%.2f\n\t%.2f\n\t%.2f\n", i,
data_point_position[i].x,
data_point_position[i].y,
data_point_position[i].z,
data_point_position[i].w);
}
*/
printf("[%d] Updating camera configuration...\n", image_index);
cudaMemcpy(cuda_image_camera_position,
image_camera_position,
sizeof(PRECISION_RENDERING) * 3,
cudaMemcpyHostToDevice);
cudaMemcpy(cuda_image_screen_position,
image_screen_position,
sizeof(PRECISION_RENDERING) * 3,
cudaMemcpyHostToDevice);
cudaMemcpy(cuda_image_screen_basis_w,
image_screen_basis_w,
sizeof(PRECISION_RENDERING) * 3,
cudaMemcpyHostToDevice);
cudaMemcpy(cuda_image_screen_basis_h,
image_screen_basis_h,
sizeof(PRECISION_RENDERING) * 3,
cudaMemcpyHostToDevice);
// Asynchronously read and write files.
printf("[%d] Rendering...\n", image_index);
render_CUDA<<<grid, block>>>(image_size_width,
image_size_hight,
image_size_length,
cuda_image_camera_position,
cuda_image_screen_position,
cuda_image_screen_basis_w,
cuda_image_screen_basis_h,
data_point_number,
cuda_data_point_position,
cuda_image);
// Save the previous frame
if (image_index_offset > 0) {
sprintf(image_file_name, "%s-%d.bmp", image_file_prefix, image_index - 1);
printf("[%d] Saving image to %s\n", image_index - 1, image_file_name);
/*
// IDK how to fix this Asyc memory copy.
cudaMemcpyAsync(image_previous,
cuda_image_previous,
sizeof(char) * image_size_length * 3,
cudaMemcpyDeviceToHost);
*/
writeBMP(image_previous,
image_file_name,
image_size_hight,
image_size_width);
}
/*
// Rotate the camera
// Rotate around the Z axis
rotateZ3d_RENDERING(3.1415926525 / 360 / 2, image_camera_position);
rotateZ3d_RENDERING(3.1415926525 / 360 / 2, image_screen_position);
rotateZ3d_RENDERING(3.1415926525 / 360 / 2, image_screen_basis_w);
rotateZ3d_RENDERING(3.1415926525 / 360 / 2, image_screen_basis_h);
*/
/*
// Rotate around the Y axis
rotateY3d_RENDERING(3.1415926525 / 360 / 4, image_camera_position);
rotateY3d_RENDERING(3.1415926525 / 360 / 4, image_screen_position);
rotateY3d_RENDERING(3.1415926525 / 360 / 4, image_screen_basis_w);
rotateY3d_RENDERING(3.1415926525 / 360 / 4, image_screen_basis_h);
*/
// Read data for the next frame
sprintf(data_file_name, "%s,%d.nbody", data_file_prefix, image_index + 1);
printf("[%d] Reading %s\n", image_index + 1, data_file_name);
data_point_position_next = readData(data_file_name,
&data_point_number_next);
cudaMalloc((void**)&cuda_data_point_position_next,
sizeof(PRECISION_4) * data_point_number);
printf("[%d] Transformming data...\n", image_index + 1);
// IDK how to fix this Asyc memory copy.
cudaMemcpyAsync(cuda_data_point_position_next,
data_point_position_next,
sizeof(PRECISION_4) * data_point_number_next,
cudaMemcpyHostToDevice);
cudaThreadSynchronize();
cudaDeviceSynchronize();
cudaMemcpy(image,
cuda_image,
sizeof(char) * image_size_length * 3,
cudaMemcpyDeviceToHost);
end_ms = getMillisecond();
delta_ms = end_ms - begin_ms;
printf("[%d] Frame rendering finished! Cost %.2f s\n", image_index, delta_ms / 1000);
__aligned_free(data_point_position);
cudaFree(cuda_image_camera_position);
cudaFree(cuda_image_screen_position);
cudaFree(cuda_image_screen_basis_w);
cudaFree(cuda_image_screen_basis_h);
cudaFree(cuda_data_point_position);
if (image_index_offset > 0) {
__aligned_free(image_previous);
cudaFree(cuda_image_previous);
}
data_point_number = data_point_number_next;
cuda_data_point_position = cuda_data_point_position_next;
data_point_position = data_point_position_next;
image_previous = image;
cuda_image_previous = cuda_image;
image_index_offset++;
#ifdef DEBUG_FILE
int debug_num_closed = _fcloseall( );
if (debug_num_closed > 0) {
printf("[ERR] You remains %d files opened!\n", debug_num_closed);
}
#endif
}
return 0;
}