-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·197 lines (165 loc) · 5.36 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
#include "app.hpp"
#include "global.hpp"
#include "demos/demo.hpp"
#include "demos/demo_frame_cpu.hpp"
#include "demos/glass_spheres.hpp"
#include "demos/pretty_spheres.hpp"
#include "rendering/pipelinestate.hpp"
#if (defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER)) && defined(ENABLE_CUDA)
#include "rt_cuda/demo_frame_cuda.hpp"
#include "rt_cuda/rt_cuda.h"
#endif
#include <glfw/glfw3.h>
#include <cxxopts.hpp>
#include <algorithm>
#include <iostream>
#include <limits>
#include <string>
App app;
void KeyCallback(GLFWwindow * window, int key, int scancode, int action, int mods)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
glfwSetWindowShouldClose(window, GL_TRUE);
break;
default:
app.OnKeyButton(key, scancode, action != GLFW_RELEASE);
}
}
void MouseCallback(GLFWwindow * window, int button, int action, int mods)
{
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
app.OnMouseButton(xpos, ypos, button, action != GLFW_RELEASE);
}
void MouseMoveCallback(GLFWwindow * window, double xpos, double ypos)
{
app.OnMouseMove(xpos, ypos);
}
void ErrorCallback(int error, const char * description)
{
std::cout << "Error(" << error << "): " << description << std::endl;
}
int main(int argc, char * argv[])
{
uint32_t width = 1024;
uint32_t height = 768;
uint32_t rtThreadsCount = 4;
uint32_t samplesInRow = 2;
uint32_t demoIndex = 1;
bool enableCUDA = true;
try
{
cxxopts::Options options(argv[0], " - simple C++ ray tracer");
options.allow_unrecognised_options().add_options()(
"w,width", "Window width", cxxopts::value<uint32_t>(width)->default_value("1024"))(
"h,height", "Window height", cxxopts::value<uint32_t>(height)->default_value("768"))(
"s,samples", "Supersampling samples in row count",
cxxopts::value<uint32_t>(samplesInRow)->default_value("2"))(
"t,threads", "Ray tracing threads count",
cxxopts::value<uint32_t>(rtThreadsCount)->default_value("4"))(
"d,demo", "Demo index", cxxopts::value<uint32_t>(demoIndex)->default_value("1"))(
"c,cuda", "Enable CUDA acceleration",
cxxopts::value<bool>(enableCUDA)->default_value("true"));
options.parse(argc, argv);
}
catch (cxxopts::OptionException const & e)
{
std::cout << "Error parsing options: " << e.what() << std::endl;
return 1;
}
std::vector<std::unique_ptr<demo::Demo>> demosCpu;
{
demosCpu.emplace_back(std::make_unique<demo::PrettySpheres>(
std::make_unique<demo::DemoFrameCPU>(rtThreadsCount)));
demosCpu.emplace_back(std::make_unique<demo::GlassSpheres>(
std::make_unique<demo::DemoFrameCPU>(rtThreadsCount)));
}
std::vector<std::unique_ptr<demo::Demo>> * demos = &demosCpu;
#if (defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER)) && \
defined(ENABLE_CUDA)
std::vector<std::unique_ptr<demo::Demo>> demosGpu;
{
demosGpu.emplace_back(std::make_unique<demo::PrettySpheres>(
std::make_unique<ray_tracing_cuda::DemoFrameCUDA>()));
demosGpu.emplace_back(std::make_unique<demo::GlassSpheres>(
std::make_unique<ray_tracing_cuda::DemoFrameCUDA>()));
}
if (enableCUDA)
demos = &demosGpu;
#endif
demoIndex = std::max(static_cast<uint32_t>(1), demoIndex);
if (demoIndex > demosCpu.size())
{
std::cout << "Incorrect demo index." << std::endl;
return 1;
}
glfwSetErrorCallback(ErrorCallback);
if (!glfwInit())
return 1;
uint8_t constexpr kOpenGLMajor = 4;
uint8_t constexpr kOpenGLMinor = 1;
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, kOpenGLMajor);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, kOpenGLMinor);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 0);
GLFWwindow * window =
glfwCreateWindow(static_cast<int>(width), static_cast<int>(height), "rt", nullptr, nullptr);
if (!window)
{
glfwTerminate();
return 1;
}
glfwSetKeyCallback(window, KeyCallback);
glfwSetMouseButtonCallback(window, MouseCallback);
glfwSetCursorPosCallback(window, MouseMoveCallback);
glfwMakeContextCurrent(window);
#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER)
if (gl3wInit() < 0)
{
std::cout << "Error initialization OpenGL." << std::endl;
glfwTerminate();
return 1;
}
if (!gl3wIsSupported(kOpenGLMajor, kOpenGLMinor))
{
std::cout << "OpenGL version " << kOpenGLMajor << "." << kOpenGLMinor << " is not supported."
<< std::endl;
glfwTerminate();
return 1;
}
#if defined(ENABLE_CUDA)
if (!ray_tracing_cuda::Initialize())
return 1;
#endif
#endif
rendering::PipelineStateManager::Instance().Initialize();
if (!app.Initialize(std::move((*demos)[demoIndex - 1]), width, height, samplesInRow))
{
app.Uninitialize();
glfwTerminate();
return 1;
}
double lastTime = glfwGetTime();
bool firstFrame = true;
while (!glfwWindowShouldClose(window))
{
double const currentTime = glfwGetTime();
double const elapsedTime = currentTime - lastTime;
lastTime = currentTime;
app.Render(currentTime, elapsedTime);
if (firstFrame)
{
firstFrame = false;
app.RayTrace(false /* highQuality */);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
app.Uninitialize();
glfwTerminate();
return 0;
}