-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer_window.cpp
242 lines (209 loc) · 7.67 KB
/
tracer_window.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
#include "tracer_window.h"
#include "imgui/imgui.h"
bool render_to_file = false;
Moving moving{};
TracerWindow::TracerWindow(CUstream stream,
OptixPipeline pipeline,
OptixShaderBindingTable sbt,
Params params,
int window_width,
int window_height,
std::string outfile,
TriangleGAS *triangles)
: window_width(window_width), width(window_width - SIDE_PANEL_WIDTH), height(window_height),
buf_width(width / DOWNSAMPLING), buf_height(height / DOWNSAMPLING), outfile(std::move(outfile)),
output_buffer(sutil::CUDAOutputBufferType::CUDA_DEVICE, buf_width, buf_height), params(params),
triangles(triangles), pipeline(pipeline), stream(stream), sbt(sbt)
{
//
// Initialize GLFW state
//
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) throw std::runtime_error("Failed to initialize GLFW");
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);// To make Apple happy -- should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(window_width, height, "sfef", nullptr, nullptr);
if (!window) throw std::runtime_error("Failed to create GLFW window");
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);
//
// Initialize GL state
//
initGL();
buffer.pixel_format = sutil::BufferImageFormat::UNSIGNED_BYTE4;
buffer.width = buf_width;
buffer.height = buf_height;
buffer.data = output_buffer.getHostPointer();
GL_CHECK(glGenBuffers(1, &pbo));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, pbo));
GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
pixelFormatSize(buffer.pixel_format) * buffer.width * buffer.height,
buffer.data,
GL_STREAM_DRAW));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
// sutil::displayBufferWindow(argv[0], buffer);
//
// Display loop
//
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
}
void TracerWindow::run() noexcept
{
GLDisplay display(buffer.pixel_format);
#ifdef NDEBUG
spf = 5;
#else
spf = 1;
#endif
params.image = output_buffer.map();
params.image_width = buf_width;
params.image_height = buf_height;
params.alloc_film();
int framebuf_res_x = 0, framebuf_res_y = 0;
spdlog::info("start loop");
while (glfwWindowShouldClose(window) == 0) {
glfwPollEvents();
// glfwWaitEvents();
params.dirty = false;
imgui();
update_camera();
params.image = output_buffer.map();
params.image_width = buf_width;
params.image_height = buf_height;
if (params.dirty) params.dt = 0;
params.frame_step();
const CUdeviceptr d_param = reinterpret_cast<CUdeviceptr>(params.new_device_ptr());
// const CUdeviceptr d_param = 0;
// cudaMalloc((void**)&d_param, sizeof(Params));
// cudaMemcpy((void*)d_param, (const void*)¶ms, sizeof(Params), cudaMemcpyHostToDevice);
OPTIX_CHECK(optixLaunch(pipeline, stream, d_param, sizeof(Params), &sbt, buf_width, buf_height, /*depth=*/1));
CUDA_SYNC_CHECK();
output_buffer.unmap();
CUDA_CHECK(cudaFree(reinterpret_cast<void *>(d_param)));
buffer.data = output_buffer.getHostPointer();
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, pbo));
GL_CHECK(glBufferData(GL_ARRAY_BUFFER,
pixelFormatSize(buffer.pixel_format) * buffer.width * buffer.height,
buffer.data,
GL_STREAM_DRAW));
GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
ImGui::Render();
glfwGetFramebufferSize(window, &framebuf_res_x, &framebuf_res_y);
display.display(
buffer.width, buffer.height, SIDE_PANEL_WIDTH, 0, framebuf_res_x - SIDE_PANEL_WIDTH, framebuf_res_y, pbo);
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
}
glfwDestroyWindow(window);
glfwTerminate();
}
void TracerWindow::imgui() noexcept
{
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::SetNextWindowPos({ 0, 0 }, ImGuiCond_::ImGuiCond_Always);
ImGui::SetNextWindowSize({ static_cast<float>(SIDE_PANEL_WIDTH), static_cast<float>(height) }, ImGuiCond_Always);
#ifdef NDEBUG
ImGui::Begin("Release", 0, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
#else
ImGui::Begin("Debug", 0, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse);
#endif
if (ImGui::CollapsingHeader("Window")) {
ImGui::Text("Window Width: %d", window_width);
ImGui::Text("Window Height: %d", height);
ImGui::Text("Buffer Width: %d", width);
ImGui::Text("Buffer Height: %d", height);
ImGui::Text("Camera:");
// ImGui::Text("(%.2f, %.2f, %.2f", params.cam_eye.x, params.cam_eye.y, params.cam_eye.z);
}
if (ImGui::CollapsingHeader("Settings")) {
params.dirty |= ImGui::Checkbox("Orthographic", &orhto);
if (ImGui::CollapsingHeader("Camera")) {
params.dirty |= ImGui::SliderFloat("Camera Speed", &camera_speed, 0.01f, 1.0f);
params.dirty |= ImGui::DragFloat("Eye X", &camera_eye.x);
params.dirty |= ImGui::DragFloat("Eye Y", &camera_eye.y);
params.dirty |= ImGui::DragFloat("Eye Z", &camera_eye.z);
cam.set_eye(camera_eye);
}
params.dirty |= ImGui::SliderFloat("T Factor", ¶ms.tfactor, 0.0, 1.0);
params.dirty |= ImGui::SliderFloat("FOV", &fov, 1.0f, 180.0f);
params.dirty |= ImGui::SliderFloat("Aperture", &aperture, 0.0f, 1.0f);
params.dirty |= ImGui::DragFloat("Focal length", &fod);
params.dirty |= ImGui::SliderInt("SPF", &spf, 0, 10);
params.samples_per_frame = static_cast<unsigned int>(pow(2, spf));
ImGui::Text("Samples per Frame: %d", params.samples_per_frame);
}
device.imgui();
if (triangles != nullptr) triangles->imgui();
ImGui::End();
if (render_to_file && !outfile.empty()) {
render_to_file = false;
sutil::saveImage(outfile.c_str(), buffer, false);
spdlog::info("saving rendered image to {}", outfile);
spdlog::info("total samples: {}", params.dt);
}
}
void TracerWindow::update_camera() noexcept
{
if (moving.forward) {
cam.move_forward();
params.dirty = true;
}
if (moving.backward) {
cam.move_backward();
params.dirty = true;
}
if (moving.left) {
cam.move_left();
params.dirty = true;
}
if (moving.right) {
cam.move_right();
params.dirty = true;
}
if (moving.turn_left) {
cam.turn_left();
params.dirty = true;
}
if (moving.turn_right) {
cam.turn_right();
params.dirty = true;
}
if (moving.up) {
cam.move_up();
params.dirty = true;
}
if (moving.down) {
cam.move_down();
params.dirty = true;
}
if (moving.turn_up) {
cam.turn_up();
params.dirty = true;
}
if (moving.turn_down) {
cam.turn_down();
params.dirty = true;
}
cam.set_fov(fov);
cam.set_fd(fod);
cam.set_ortho(orhto);
cam.set_aperture(aperture);
cam.set_speed(camera_speed);
cam.compute_uvw();
cam.update_device_ptr(params.camera);
};
void initGL()
{
spdlog::debug("init GL");
if (gladLoadGL() == 0) throw std::runtime_error("Failed to initialize GL");
GL_CHECK(glClearColor(0.212f, 0.271f, 0.31f, 1.0f));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT));
}