-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cu
397 lines (322 loc) · 13.7 KB
/
main.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
// ----------------------------------------------------------------------------------------------------
//
// File name: main.cu
// Created By: Haard Panchal
// Create Date: 03/11/2020
//
// Description:
// Main file for the Ray Tracing project. The file implements the parallel CUDA algorithm.
// You can also use it to create the world that would be used to render the final result.
//
// History:
// 03/10/19: H. Panchal Created the file
//
// Declaration:
// N/A
//
// ----------------------------------------------------------------------------------------------------
// #define MESHDEBUG
// #define MATERIALDEBUG
// #define AREALIGHTDEBUG
// #define SHADOWDEBUG
// #define CUDADEBUG
// #define RENDERDEBUG
// #define INITDEBUG
#define ACTUALRENDER
#include <iostream>
#include <math.h>
#include <curand_kernel.h>
#define STB_IMAGE_IMPLEMENTATION
#include "libs/stb_image.h"
#include "Vector3.h"
#include "Ray.h"
#include "Camera.h"
#include "World.h"
#include "Sphere.h"
#include "Plane.h"
#include "TriangularMesh.h"
#include "PointLight.h"
#include "DirectionalLight.h"
#include "SpotLight.h"
#include "AreaLight.h"
#include "Material.h"
#include "TextureMaterial.h"
#include "RenderEngine.h"
/* Function: initializeEngine
//
// The function adds different objects to World.
// All the object must be initialized onto the heap.
//
// Parameters:
// World ** world: A pointer to a pointer to a world object
// int w: The width of the resulting image
// int h: The height of the resulting image
//
// Return:
// void
*/
__global__
void initializeWorld(World ** world, int w, int h, unsigned char ** array_of_images, int * img_w, int * img_h, int * img_chns, int n_imgs) {
*world = new World();
TextureMaterial * m1 = new TextureMaterial();
m1->setColorImage(img_w[0], img_h[0], img_chns[0], array_of_images[0]);
TextureMaterial * m2 = new TextureMaterial();
m2->setColorImage(img_w[1], img_h[1], img_chns[1], array_of_images[1]);
TextureMaterial * m3 = new TextureMaterial();
m3->setColorImage(img_w[2], img_h[2], img_chns[2], array_of_images[2]);
Vector3 color(0.3f, 0.8f, 0.3f);
Vector3 center(-2.0, 0.0, 0.0);
center = rotateAroundAxis(center, 40.0 * PI / 180.0, Vector3(0.0f, 1.0f, 0.0f));
float r = 0.5f;
Sphere * s = new Sphere(center, r, color);
s->setMaterial(*m2);
(*world)->addVisibleObject(s);
center = rotateAroundAxis(center, 120.0 * PI / 180.0, Vector3(0.0f, 1.0f, 0.0f));
s = new Sphere(center, r, color);
s->setMaterial(*m2);
(*world)->addVisibleObject(s);
center = rotateAroundAxis(center, 120.0 * PI / 180.0, Vector3(0.0f, 1.0f, 0.0f));
s = new Sphere(center, r, color);
s->setMaterial(*m2);
(*world)->addVisibleObject(s);
Vector3 color5(1.0f, 0.0f, 0.1f);
Vector3 center2(0.5, 0.0, 0.0);
float r2 = 10;
Sphere * s2 = new Sphere(center2, r2, color5);
s2->setMaterial(*m1);
(*world)->addVisibleObject(s2);
float beam_angle = 40.0;
float falloff_angle = 10.0;
beam_angle = beam_angle * PI / 180.0;
falloff_angle = falloff_angle * PI / 180.0;
Vector3 spotlightpos(0.0, 3.0, 0.0f);
Vector3 spotlightdir = - spotlightpos;
SpotLight * spotlight = new SpotLight(spotlightpos, spotlightdir, beam_angle, falloff_angle);
(*world)->addLight(spotlight);
Vector3 spotlightpos2(1.0f, 3.0, 4.0);
Vector3 spotlightdir2 = - spotlightpos2;
SpotLight * spotlight2 = new SpotLight(spotlightpos2, spotlightdir2, beam_angle, falloff_angle);
// (*world)->addLight(spotlight2);
Vector3 area_light_pos(-4.0, 2.0, 0);
Vector3 area_light_dir = - area_light_pos;
Vector3 area_light_up(0.0, 1.0, 0.0);
AreaLight * areaLigth = new AreaLight(area_light_pos, area_light_dir, area_light_up, 0.1, 0.1);
// (*world)->addLight(areaLigth);
Vector3 color2(213.0f / 255.99f, 241.00f / 255.99f, 11.0f / 255.99f); // 52, 235, 140
Vector3 point(0.0, -2.5, 0.0);
Vector3 normal(0, 1.0, 0.0);
Plane * p = new Plane(normal, point, color2);
// p->setMaterial(*m3);
(*world)->addVisibleObject(p);
Vector3 color3(0.1f, 0.2f, 0.8f);
Vector3 point2(4.5, 0.0, 0.0);
Vector3 normal2(-1.0, 0.0, 0.0f);
Plane * p2 = new Plane(normal2, point2, color3);
// p2->setMaterial(*m3);
// (*world)->addVisibleObject(p2);
Vector3 positioncam(-2.0, 1.0, 5.0);
Vector3 lookat(0.0f, 0.0f, 0.0f);
Vector3 direction = lookat - positioncam;
Vector3 updir(0.0, 1.0, 0.0);
float aspect_ratio = (float(w))/(float(h));
float distance_from_screen = 0.70;
Camera * cam = new Camera(positioncam, direction, updir, aspect_ratio, 1.0, distance_from_screen);
cam->setLensSize(0.3, 0.3);
cam->setFocus(4);
(*world)->setCamera(*cam);
}
/* Function: addWorldToEngine
//
// The function initializes the RenderEngine
// An already initialized World object is passed to the RenderEngine
//
// Parameters:
// int w: Width of the rendered image
// int h: Height of the rendered image
// RenderEngine ** r_engine: Pointer to a pointer to the RenderEngine object
// World ** world: Pointer to a pointer
//
// Return:
// void
*/
__global__
void addWorldToEngine(int w, int h, RenderEngine ** r_engine, World ** world, int samples) {
*r_engine = new RenderEngine(w, h, **world);
(* r_engine)->setAntiAliasing(samples);
}
__global__
void addMeshToWorld(World ** world, Vector3 * mesh_vertex_data, Vector3 * mesh_normal_data, int no_of_triangles, unsigned char ** array_of_images, int * img_w, int * img_h, int * img_chns, int n_imgs) {
Vector3 center(0.0f, 0.0f, 0.0f);
Vector3 color(0.0f, 0.0f, 1.0f);
#ifdef MESHDEBUG
for(int i = 0; i < no_of_triangles * 3; i++) {
printf("i: %d V: %f %f %f\n", i, mesh_vertex_data[i].x(), mesh_vertex_data[i].y(), mesh_vertex_data[i].z());
}
for(int i = 0; i < no_of_triangles * 3; i++) {
printf("i: %d N: %f %f %f\n", i, mesh_normal_data[i].x(), mesh_normal_data[i].y(), mesh_normal_data[i].z());
}
#endif
TextureMaterial * m1 = new TextureMaterial();
m1->setColorImage(img_w[0], img_h[0], img_chns[0], array_of_images[0]);
TriangularMesh * t_mesh = new TriangularMesh(center, color, mesh_vertex_data, mesh_normal_data, no_of_triangles);
t_mesh->setMaterial(*m1);
(*world)->addVisibleObject(t_mesh);
}
__global__
void updateWorldObjects(World ** world, int t) {
int total_objects = (*world)->getTotalVisibleObjects();
for(int i = 0; i < total_objects; i++) {
((*world)->getVisibleObject(i))->update(t);
}
}
/* Function: Parallelize Render for each pixels
//
// The kernel CUDA function implements the parallel threads for rendering each pixel.
// The rendered pixels are stored in the frame_buffer array
//
// Parameters:
//
//
//
//
// Return:
// void
*/
__global__
void renderPixels(RenderEngine ** r_engine, Vector3 * frame_buffer, curandState * rand_sequence, int w, int h) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
const int j = blockIdx.y * blockDim.y + threadIdx.y;
const int index_ij = j * w + i;
curand_init(1984 + index_ij, 0, 0, &rand_sequence[index_ij]);
frame_buffer[index_ij] += (*r_engine)->renderPixelSampling(i, j, rand_sequence[index_ij]);
#ifdef CUDADEBUG
printf("End of renderPixels\n");
printf("framebuffer: i: %d r: %d c: %d\n", index_ij, i, j);
#endif
}
__global__
void initializeFrameBuffer(Vector3 * frame_buffer, int w, int h) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
const int j = blockIdx.y * blockDim.y + threadIdx.y;
const int index_ij = j * w + i;
frame_buffer[index_ij] = Vector3(0.0f, 0.0f, 0.0f);
}
__global__
void normalizeFrameBuffer(Vector3 * frame_buffer, int w, int h, float n) {
const int i = blockIdx.x * blockDim.x + threadIdx.x;
const int j = blockIdx.y * blockDim.y + threadIdx.y;
const int index_ij = j * w + i;
frame_buffer[index_ij] /= n;
}
/* Function: main
//
// Parses the argument list. Initializes the relevant objects and starts rendering.
//
// Parameters:
//
// int argc: Number of arguments
// char *argv[]: List of the arguments
//
// Return:
// int: 0 if successful
*/
int main(int argc, char *argv[]) {
// Loading images for textures
int n_imgs = 3;
unsigned char * host_imgs[n_imgs];
int img_w[n_imgs], img_h[n_imgs], img_chns[n_imgs];
// -------- Load Images Here ------- //
host_imgs[0] = stbi_load("textures/universe.jpg", &img_w[0], &img_h[0], &img_chns[0], 0);
host_imgs[1] = stbi_load("textures/wall.jpg", &img_w[1], &img_h[1], &img_chns[1], 0);
host_imgs[2] = stbi_load("textures/smile.png", &img_w[2], &img_h[2], &img_chns[2], 0);
#ifdef MATERIALDEBUG
std::cout<<img_w[2]<<" "<<img_h[2]<<" "<<img_chns[2]<<std::endl;
#endif
// Allocating devices memory to the images on the device
unsigned char * temp_array[n_imgs];
unsigned char ** array_of_images = 0; // Pointer to be allocated device memory
int * img_w_d;
int * img_h_d;
int * img_chns_d;
gpuErrchk(cudaMalloc(&img_w_d, n_imgs * sizeof(int)));
gpuErrchk(cudaMemcpy(img_w_d, img_w, n_imgs * sizeof(int), cudaMemcpyHostToDevice));
gpuErrchk(cudaMalloc(&img_h_d, n_imgs * sizeof(int)));
gpuErrchk(cudaMemcpy(img_h_d, img_h, n_imgs * sizeof(int), cudaMemcpyHostToDevice));
gpuErrchk(cudaMalloc(&img_chns_d, n_imgs * sizeof(int)));
gpuErrchk(cudaMemcpy(img_chns_d, img_chns, n_imgs * sizeof(int), cudaMemcpyHostToDevice));
if(array_of_images == 0) {
gpuErrchk(cudaMalloc(&array_of_images, sizeof(unsigned char*) * n_imgs));
}
for(int i = 0; i < n_imgs; i++) {
gpuErrchk(cudaMalloc(&temp_array[i], img_w[i] * img_h[i] * img_chns[i] * sizeof(unsigned char)));
gpuErrchk(cudaMemcpy(&(array_of_images[i]), &(temp_array[i]), sizeof(unsigned char *), cudaMemcpyHostToDevice));//copy child pointer to device
gpuErrchk(cudaMemcpy(temp_array[i], host_imgs[i], img_w[i] * img_h[i] * img_chns[i] * sizeof(unsigned char), cudaMemcpyHostToDevice)); // copy image to device
}
// Loading Meshes and Normals
Vector3 ** mesh_vertex_data;
Vector3 ** mesh_normal_data;
gpuErrchk(cudaMallocManaged(&mesh_vertex_data, sizeof(Vector3 *)));
gpuErrchk(cudaMallocManaged(&mesh_normal_data, sizeof(Vector3 *)));
std::string obj_file_name = "models/tetrahedron.obj";
int no_of_triangles = loadOBJ(obj_file_name, mesh_vertex_data, mesh_normal_data);
// Creating the required arrays for starting the rendering sequence
int wid_cuda = 1920, hgt_cuda = 1080;
int samples = 16;
Vector3 * frame_buffer_cuda;
gpuErrchk(cudaMallocManaged(&frame_buffer_cuda, wid_cuda * hgt_cuda * sizeof(Vector3)));
curandState * rand_sequence;
gpuErrchk(cudaMallocManaged(&rand_sequence, wid_cuda * hgt_cuda * sizeof(curandState)));
// Double Pointer: Done so that memory could be directly allocated to the object
// with the call of new constructor inside the global function.
World ** world_cuda;
gpuErrchk(cudaMallocManaged(&world_cuda, sizeof(World *)));
RenderEngine ** r_engine_cuda;
gpuErrchk(cudaMallocManaged(&r_engine_cuda, sizeof(RenderEngine *)));
initializeWorld<<<1, 1>>>(world_cuda, wid_cuda, hgt_cuda, array_of_images, img_w_d, img_h_d, img_chns_d, n_imgs);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
addMeshToWorld<<<1, 1>>>(world_cuda, *mesh_vertex_data, *mesh_normal_data, no_of_triangles, array_of_images, img_w_d, img_h_d, img_chns_d, n_imgs);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
addWorldToEngine<<<1, 1>>>(wid_cuda, hgt_cuda, r_engine_cuda, world_cuda, samples);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
const int block_size_side = 16;
const dim3 block_size(block_size_side, block_size_side);
const int grid_size_hgt = (hgt_cuda + block_size_side - 1)/block_size_side;
const int grid_size_wid = (wid_cuda + block_size_side - 1)/block_size_side;
const dim3 grid_size(grid_size_wid, grid_size_hgt);
#ifdef CUDADEBUG
std::cout<<"Grid Sizes: "<<grid_size_hgt<<" "<<grid_size_wid<<std::endl;
std::cout<<"Block Sizes: "<<block_size_side<<" "<<block_size_side<<std::endl;
#endif
// initializeFrameBuffer<<<grid_size, block_size>>>(frame_buffer_cuda, wid_cuda, hgt_cuda);
// gpuErrchk(cudaPeekAtLastError());
// gpuErrchk(cudaDeviceSynchronize());
// renderPixels<<<grid_size, block_size>>>(r_engine_cuda, frame_buffer_cuda, rand_sequence, wid_cuda, hgt_cuda);
// gpuErrchk(cudaPeekAtLastError());
// gpuErrchk(cudaDeviceSynchronize());
// updateWorldObjects<<<1, 1>>>(world_cuda);
// gpuErrchk(cudaPeekAtLastError());
// gpuErrchk(cudaDeviceSynchronize());
// renderPixels<<<grid_size, block_size>>>(r_engine_cuda, frame_buffer_cuda, rand_sequence, wid_cuda, hgt_cuda);
// gpuErrchk(cudaPeekAtLastError());
// gpuErrchk(cudaDeviceSynchronize());
// normalizeFrameBuffer<<<grid_size, block_size>>>(frame_buffer_cuda, wid_cuda, hgt_cuda, 2);
// gpuErrchk(cudaPeekAtLastError());
// gpuErrchk(cudaDeviceSynchronize());
int total_frames = 60;
for(int t = 0; t < total_frames; t++) {
initializeFrameBuffer<<<grid_size, block_size>>>(frame_buffer_cuda, wid_cuda, hgt_cuda);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
updateWorldObjects<<<1, 1>>>(world_cuda, float(t));
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
renderPixels<<<grid_size, block_size>>>(r_engine_cuda, frame_buffer_cuda, rand_sequence, wid_cuda, hgt_cuda);
gpuErrchk(cudaPeekAtLastError());
gpuErrchk(cudaDeviceSynchronize());
makeImage(frame_buffer_cuda, wid_cuda, hgt_cuda, "video/video4/out" + std::to_string(t) + ".ppm" );
}
return 0;
}