-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
379 lines (300 loc) · 10.4 KB
/
main.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
#include <getopt.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <thread>
#include <vector>
#include "brdf.hpp"
#include "camera.hpp"
#include "colors.hpp"
#include "error.hpp"
#include "geometry.hpp"
#include "gpuRender.hpp"
#include "image.hpp"
#include "input.hpp"
#include "intersection.hpp"
#include "mesh.hpp"
#include "output.hpp"
#include "random.hpp"
#include "sampleScenes.hpp"
#include "scene.hpp"
#include "tonemapping.hpp"
using namespace glm;
#define INV2PI (1.0f / (2.0f * M_PI))
#define INVPI (1.0f / M_PI)
Image environment = loadEnvironmentImage("environment/evening_road_01_puresky_2k.hdr");
// Atomically incremented index to control which threads works on which pixel
std::atomic<uint> atomicIdx{0u};
/*
TODO:
Stackless traversal
Better memory access patterns
Specular dielectric
Transmission
Alpha texture
Lights
Robust self-intersection fix
Normal mapping
*/
//----------------------- Rotations --------------------------
inline vec3 rotate(vec3 p, vec4 q) {
return 2.0f * cross(vec3(q), p * q.w + cross(vec3(q), p)) + p;
}
inline vec3 rotateX(vec3 p, float angle) {
return rotate(p, vec4(sin(angle / 2.0), 0.0, 0.0, cos(angle / 2.0)));
}
inline vec3 rotateY(vec3 p, float angle) {
return rotate(p, vec4(0.0, sin(angle / 2.0), 0.0, cos(angle / 2.0)));
}
inline vec3 rotateZ(vec3 p, float angle) {
return rotate(p, vec4(0.0, 0.0, sin(angle / 2.0), cos(angle / 2.0)));
}
vec3 getEnvironment(const vec3& direction) {
// Rotate environment map
vec3 sampleDir = normalize(rotateY(direction, -M_PI));
uint u = environment.width * (atan2f(sampleDir.z, sampleDir.x) * INV2PI + 0.5f);
uint v = environment.height * acosf(sampleDir.y) * INVPI;
uint idx = min(u + v * environment.width, (environment.width * environment.height) - 1);
return environment[idx];
}
vec3 getIllumination(Ray ray,
const Scene& scene,
uint& rngState,
int& bounceCount,
uint& testCount) {
// Initialize light to white and track attenuation
vec3 col{1};
for (uint i = 0; i < bounceCount; i++) {
HitRecord closestHit{};
uint meshIdx = scene.intersect(ray, closestHit, testCount);
if (ray.t < FLT_MAX) {
const Mesh& mesh{scene.meshes[meshIdx]};
vec3 p = ray.origin + ray.direction * ray.t;
vec3 N = normalize(vec3(mesh.normalMatrix * vec4(mesh.geometry->getNormal(closestHit.hitIndex, closestHit.barycentric), 0.0f)));
if (dot(ray.direction, N) > 0.0f) {
N = -N;
}
vec3 V = -ray.direction;
const float metalness = {mesh.material->metalness};
const float roughness = {mesh.material->roughness};
vec2 uv = mesh.geometry->getTexCoord(closestHit.hitIndex, closestHit.barycentric);
const vec3 albedo = mesh.material->getAlbedo(uv);
const vec3 emissive = mesh.material->getEmissive(uv);
vec3 F0 = mix(mesh.material->F0, albedo, metalness);
vec3 localCol{};
vec3 sampleDir{};
if (metalness == 0.0f) {
//--------------------- Diffuse ------------------------
vec2 Xi = getRandomVec2(rngState);
sampleDir = importanceSampleCosine(Xi, N);
/*
The discrete Riemann sum for the lighting equation is
1/N * Σ(brdf(l, v) * L(l) * dot(l, n)) / pdf(l))
Lambertian BRDF is c/PI and the pdf for cosine sampling is dot(l, n)/PI
PI term and dot products cancel out leaving just c * L(l)
*/
localCol = albedo;
} else {
//--------------------- Specular ------------------------
vec2 Xi = getRandomVec2(rngState);
// Get a random halfway vector around the surface normal (in world space)
vec3 H = importanceSampleGGX(Xi, N, roughness);
// Generate sample direction as view ray reflected around h (note sign)
sampleDir = normalize(reflect(-V, H));
float NdotL = dot_c(N, sampleDir);
float NdotV = dot_c(N, V);
float NdotH = dot_c(N, H);
float VdotH = dot_c(V, H);
vec3 F = fresnel(VdotH, F0);
float G = smiths(NdotV, NdotL, roughness);
/*
The following can be simplified as the D term and many dot products cancel out
float D = distribution(NdotH, roughness);
// Cook-Torrance BRDF
vec3 brdfS = D * F * G / max(0.0001, (4.0 * NdotV * NdotL));
float pdfSpecular = (D * NdotH) / (4.0 * VdotH);
vec3 specular = (L(sampleDir) * brdfS * NdotL) / pdfSpecular;
*/
// Simplified from the above
localCol = (F * G * VdotH) / (NdotV * NdotH);
}
col *= localCol + emissive;
ray = Ray{p + 1e-4f * N, sampleDir, 1.0f / sampleDir, FLT_MAX};
} else {
col *= getEnvironment(ray.direction);
break;
}
}
return col;
}
//-------------------------- Render ---------------------------
void render(
const Scene& scene,
const Camera& camera,
Image& image,
const uint samples,
const int maxBounces,
const bool renderBVH,
const uint threadId) {
Ray ray{.origin = camera.position};
vec2 fragCoord{};
uint rngState{1031};
const vec2 resolution{image.width, image.height};
const float inverseSize = 1.0f / image.data.size();
for (auto idx = atomicIdx.fetch_add(1, std::memory_order_relaxed);
idx < image.data.size();
idx = atomicIdx.fetch_add(1, std::memory_order_relaxed)) {
if (threadId < 1) {
// First thread outputs progress
std::cout << "\r" << int(101.0f * (float)idx * inverseSize) << "%";
}
fragCoord = vec2{(float)(idx % image.width), std::floor((float)idx / image.width)};
vec3 col{0};
for (uint s = 0u; s < (renderBVH ? 1u : samples); s++) {
vec2 fC = fragCoord;
if (!renderBVH && samples > 1u) {
// Jitter position for antialiasing
fC += 0.5f * (2.0f * getRandomVec2(rngState) - 1.0f);
}
ray.direction = rayDirection(resolution, camera.fieldOfView, fC);
ray.direction = normalize(viewMatrix(camera.position, camera.target, camera.up) * ray.direction);
ray.invDirection = 1.0f / ray.direction;
ray.t = FLT_MAX;
uint bvhTests = 0u;
if (renderBVH) {
HitRecord closestHit{};
// Get number of BVH tests for primary ray
scene.intersect(ray, closestHit, bvhTests);
image[idx] = vec3(bvhTests);
} else {
// Path trace scene
int bounces = maxBounces;
col += getIllumination(ray, scene, rngState, bounces, bvhTests);
}
}
if (!renderBVH) {
// Average result
col /= samples;
// An attempt at colour grading
col *= smoothstep(vec3{-0.75f}, vec3{1.45f}, col);
// Tonemapping
col = ACESFilm(0.275f * col);
// Gamma correction
col = pow(col, vec3{1.0f / 2.2f});
// Output data
image[idx] = col;
}
}
}
int main(int argc, char** argv) {
// Default values
uint width{750};
uint height{400};
uint samples{32};
uint bounces{6};
uint numThreads{10};
bool renderBVH{false};
bool cudaRender{true};
SampleScene sampleScene{THREE_STL};
// Parse command line arguments
int opt;
while ((opt = getopt(argc, argv, " w:h:s:b:t:p:ad:")) != -1) {
switch (opt) {
case 'w':
width = atoi(optarg);
continue;
case 'h':
height = atoi(optarg);
continue;
case 's':
samples = atoi(optarg);
continue;
case 'b':
bounces = atoi(optarg);
continue;
case 't':
numThreads = atoi(optarg);
continue;
case 'p':
sampleScene = static_cast<SampleScene>(atoi(optarg));
continue;
case 'a':
renderBVH = true;
continue;
case 'd':
cudaRender = atoi(optarg) == 0;
continue;
default:
std::cout << "Options\n-d\t0: CUDA (default), 1: CPU\n-w\t<width>\n-h\t<height>\n-s\t<samples>\n-t\t<threads>\n-b\t<bounces>\n-p\t<0|1|2>\tpreset scene\n-a\trender BVH heat map (only main ray, single sample, no jitter)\n";
return EXIT_SUCCESS;
break;
}
break;
}
if (renderBVH) {
samples = 1u;
bounces = 1u;
}
if (cudaRender) {
numThreads = 1u;
std::cout << "Rendering using CUDA" << std::endl;
} else {
std::cout << "Rendering using CPU" << std::endl;
}
std::cout << "\nDimensions: [" << width << ", " << height << "]\tSamples: " << samples
<< "\tBounces: " << bounces << "\tThreads: " << numThreads << std::endl
<< std::endl;
Image image{width, height};
// Initialize data to black
for (auto& v : image.data) {
v = vec3{0};
}
Camera camera{
.position = 1.0f * vec3{0.5f, 0.25f, -0.8f},
.target = vec3{0},
.up = normalize(vec3{0, 1, 0}),
.fieldOfView = 45.0f};
/* Timer */ auto start{std::chrono::steady_clock::now()};
// Scene scene(meshes);
std::vector<std::shared_ptr<Geometry>> geometryPool{};
std::vector<std::shared_ptr<Material>> materialPool{};
Scene scene{};
getScene(sampleScene, scene, geometryPool, materialPool, camera);
// ----- Render geometry_ ----- //
if (cudaRender) {
renderGPU(scene, geometryPool, materialPool, camera, image, environment, samples, bounces, renderBVH);
} else {
/* Timer */ start = std::chrono::steady_clock::now();
std::vector<std::thread> threads(numThreads);
// Launch threads
for (uint i = 0u; i < numThreads; i++) {
threads[i] = std::thread(render,
std::ref(scene),
std::ref(camera),
std::ref(image),
samples, bounces, renderBVH, i);
}
// Wait for all threads to finish
for (auto& t : threads) {
t.join();
}
/* Timer */ std::chrono::duration<double> elapsed_seconds = std::chrono::steady_clock::now() - start;
/* Timer */ std::cout << "\nRender time: " << std::floor(elapsed_seconds.count() * 1e4f) / 1e4f << " s\n";
}
// ----- Output ----- //
if (renderBVH) {
vec3 maxElement = *std::max_element(image.data.begin(), image.data.end(), [](vec3& a, vec3& b) { return a.x < b.x; });
std::cout << "Maximum BVH tests: " << maxElement.x << std::endl;
float inverseMaxElement = 1.0f / maxElement.x;
for (vec3& p : image.data) {
if (p.x > 0.0f) {
p = afmhot(p.x * inverseMaxElement);
}
}
}
outputToFile(image);
return EXIT_SUCCESS;
}