Skip to content

Commit 7d10d90

Browse files
committed
BVH visualizer
1 parent b332204 commit 7d10d90

File tree

8 files changed

+87
-53
lines changed

8 files changed

+87
-53
lines changed

src/common.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
int Settings::traceDepth = 0;
44
int Settings::toneMapping = ToneMapping::ACES;
5-
bool Settings::visualizeBVH = false;
5+
int Settings::tracer = Tracer::Streamed;
66
bool Settings::sortMaterial = false;
7-
bool Settings::singleKernel = false;
87

98
bool State::camChanged = true;

src/common.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
#define SCENE_LIGHT_SINGLE_SIDED true
1010

11-
#define BVH_DEBUG_VISUALIZATION false
1211
#define BVH_DISABLE false
1312

1413
#define ENABLE_GBUFFER false
@@ -19,12 +18,17 @@ struct ToneMapping {
1918
};
2019
};
2120

21+
struct Tracer {
22+
enum {
23+
Streamed = 0, SingleKernel = 1, BVHVisualize = 2
24+
};
25+
};
26+
2227
struct Settings {
2328
static int traceDepth;
2429
static int toneMapping;
25-
static bool visualizeBVH;
30+
static int tracer;
2631
static bool sortMaterial;
27-
static bool singleKernel;
2832
};
2933

3034
struct State {

src/main.cpp

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ void runCuda() {
232232
}
233233

234234
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
235+
Camera& cam = renderState->camera;
236+
235237
if (action == GLFW_PRESS) {
236238
switch (key) {
237239
case GLFW_KEY_ESCAPE:
@@ -244,19 +246,29 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
244246
case GLFW_KEY_T:
245247
Settings::toneMapping = (Settings::toneMapping + 1) % 3;
246248
break;
249+
case GLFW_KEY_LEFT_SHIFT:
250+
cam.position += glm::vec3(0.f, -.1f, 0.f);
251+
break;
247252
case GLFW_KEY_SPACE:
248-
State::camChanged = true;
253+
cam.position += glm::vec3(0.f, .1f, 0.f);
254+
break;
255+
case GLFW_KEY_R:
249256
renderState = &scene->state;
250-
Camera& cam = renderState->camera;
251257
cam.lookAt = ogLookAt;
258+
State::camChanged = true;
252259
break;
253260
}
254261
}
255262
}
256263

264+
void mouseScrollCallback(GLFWwindow* window, double offsetX, double offsetY) {
265+
zoom -= offsetY;
266+
zoom = std::fmax(0.1f, zoom);
267+
State::camChanged = true;
268+
}
269+
257270
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
258-
if (MouseOverImGuiWindow())
259-
{
271+
if (MouseOverImGuiWindow()) {
260272
return;
261273
}
262274
leftMousePressed = (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS);
@@ -265,7 +277,12 @@ void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
265277
}
266278

267279
void mousePositionCallback(GLFWwindow* window, double xpos, double ypos) {
268-
if (xpos == lastX || ypos == lastY) return; // otherwise, clicking back into window causes re-start
280+
Camera& cam = renderState->camera;
281+
282+
if (xpos == lastX || ypos == lastY) {
283+
return; // otherwise, clicking back into window causes re-start
284+
}
285+
269286
if (leftMousePressed) {
270287
// compute new camera parameters
271288
phi -= (xpos - lastX) / width;
@@ -274,13 +291,13 @@ void mousePositionCallback(GLFWwindow* window, double xpos, double ypos) {
274291
State::camChanged = true;
275292
}
276293
else if (rightMousePressed) {
277-
zoom += (ypos - lastY) / height;
278-
zoom = std::fmax(0.1f, zoom);
294+
float dy = (ypos - lastY) / height;
295+
cam.position.y += dy;
296+
cam.lookAt.y += dy;
279297
State::camChanged = true;
280298
}
281299
else if (middleMousePressed) {
282300
renderState = &scene->state;
283-
Camera& cam = renderState->camera;
284301
glm::vec3 forward = cam.view;
285302
forward.y = 0.0f;
286303
forward = glm::normalize(forward);

src/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ extern int height;
3333

3434
void runCuda();
3535
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
36+
void mouseScrollCallback(GLFWwindow* window, double offsetX, double offsetY);
3637
void mousePositionCallback(GLFWwindow* window, double xpos, double ypos);
3738
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods);

src/material.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,6 @@ struct Material {
115115
Light
116116
};
117117

118-
std::string toString() const {
119-
std::stringstream ss;
120-
ss << "[Type = " << type << ", BaseColor = " << vec3ToString(baseColor) << "]";
121-
return ss.str();
122-
}
123-
124118
__device__ glm::vec3 lambertianBSDF(glm::vec3 n, glm::vec3 wo, glm::vec3 wi) {
125119
return baseColor * PiInv;
126120
}
@@ -263,5 +257,6 @@ struct Material {
263257
float ior;
264258
float emittance;
265259

266-
int textureId;
260+
int baseColorMapId;
261+
int normalMapId;
267262
};

src/pathtrace.cu

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -241,19 +241,6 @@ __global__ void pathIntegSampleSurface(
241241
return;
242242
}
243243

244-
#if BVH_DEBUG_VISUALIZATION
245-
float logDepth = 0.f;
246-
int size = scene->BVHSize;
247-
while (size) {
248-
logDepth += 1.f;
249-
size >>= 1;
250-
}
251-
segment.radiance = glm::vec3(float(intersec.primId) / logDepth * .1f);
252-
//segment.radiance = intersec.primitive > 16 ? glm::vec3(1.f) : glm::vec3(0.f);
253-
segment.remainingBounces = 0;
254-
return;
255-
#endif
256-
257244
PathSegment& segment = segments[idx];
258245
thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, 4 + depth * SamplesConsumedOneIter);
259246

@@ -430,6 +417,29 @@ WriteRadiance:
430417
image[index] += accRadiance;
431418
}
432419

420+
__global__ void BVHVisualize(int iter, DevScene* scene, Camera cam, glm::vec3* image, int width, int height) {
421+
int x = blockDim.x * blockIdx.x + threadIdx.x;
422+
int y = blockDim.y * blockIdx.y + threadIdx.y;
423+
if (x >= width || y >= height) {
424+
return;
425+
}
426+
int index = y * width + x;
427+
428+
thrust::default_random_engine rng = makeSeededRandomEngine(iter, index, 0);
429+
Ray ray = sampleCamera(cam, x, y, sample4D(rng));
430+
431+
Intersection intersec;
432+
scene->visualizedIntersect(ray, intersec);
433+
434+
float logDepth = 0.f;
435+
int size = scene->BVHSize;
436+
while (size) {
437+
logDepth += 1.f;
438+
size >>= 1;
439+
}
440+
image[index] += glm::vec3(float(intersec.primId) / logDepth * .1f);
441+
}
442+
433443
struct CompactTerminatedPaths {
434444
__host__ __device__ bool operator() (const PathSegment& segment) {
435445
return !(segment.pixelIndex >= 0 && segment.remainingBounces <= 0);
@@ -465,22 +475,7 @@ void pathTrace(uchar4* pbo, int frame, int iter) {
465475

466476
auto devTerminatedThr = devTerminatedPathsThr;
467477

468-
if (Settings::singleKernel) {
469-
const int BlockSizeSinglePTX = 8;
470-
const int BlockSizeSinglePTY = 8;
471-
int blockNumSinglePTX = (cam.resolution.x + BlockSizeSinglePTX - 1) / BlockSizeSinglePTX;
472-
int blockNumSinglePTY = (cam.resolution.y + BlockSizeSinglePTY - 1) / BlockSizeSinglePTY;
473-
474-
dim3 singlePTBlockNum(blockNumSinglePTX, blockNumSinglePTY);
475-
dim3 singlePTBlockSize(BlockSizeSinglePTX, BlockSizeSinglePTY);
476-
477-
singleKernelPT<<<singlePTBlockNum, singlePTBlockSize>>>(
478-
iter, Settings::traceDepth, hstScene->devScene, cam, devImage, cam.resolution.x, cam.resolution.y);
479-
if (guiData != nullptr) {
480-
guiData->TracedDepth = Settings::traceDepth;
481-
}
482-
}
483-
else {
478+
if (Settings::tracer == Tracer::Streamed) {
484479
bool iterationComplete = false;
485480
while (!iterationComplete) {
486481
// clean shading chunks
@@ -531,6 +526,28 @@ void pathTrace(uchar4* pbo, int frame, int iter) {
531526
int numContributing = devTerminatedThr.get() - devTerminatedPaths;
532527
finalGather<<<numBlocksPixels, BlockSizeGather>>>(numContributing, devImage, devTerminatedPaths);
533528
}
529+
else {
530+
const int BlockSizeSinglePTX = 8;
531+
const int BlockSizeSinglePTY = 8;
532+
int blockNumSinglePTX = (cam.resolution.x + BlockSizeSinglePTX - 1) / BlockSizeSinglePTX;
533+
int blockNumSinglePTY = (cam.resolution.y + BlockSizeSinglePTY - 1) / BlockSizeSinglePTY;
534+
535+
dim3 singlePTBlockNum(blockNumSinglePTX, blockNumSinglePTY);
536+
dim3 singlePTBlockSize(BlockSizeSinglePTX, BlockSizeSinglePTY);
537+
538+
if (Settings::tracer == Tracer::SingleKernel) {
539+
singleKernelPT<<<singlePTBlockNum, singlePTBlockSize>>>(
540+
iter, Settings::traceDepth, hstScene->devScene, cam, devImage, cam.resolution.x, cam.resolution.y);
541+
}
542+
else {
543+
BVHVisualize<<<singlePTBlockNum, singlePTBlockSize>>>(
544+
iter, hstScene->devScene, cam, devImage, cam.resolution.x, cam.resolution.y);
545+
}
546+
547+
if (guiData != nullptr) {
548+
guiData->TracedDepth = Settings::traceDepth;
549+
}
550+
}
534551

535552
// Send results to OpenGL buffer for rendering
536553
sendImageToPBO<<<blocksPerGrid2D, blockSize2D>>>(pbo, cam.resolution, iter, devImage, Settings::toneMapping);

src/preview.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ bool init() {
152152
}
153153
glfwMakeContextCurrent(window);
154154
glfwSetKeyCallback(window, keyCallback);
155+
glfwSetScrollCallback(window, mouseScrollCallback);
155156
glfwSetCursorPosCallback(window, mousePositionCallback);
156157
glfwSetMouseButtonCallback(window, mouseButtonCallback);
157158

@@ -218,10 +219,12 @@ void RenderImGui()
218219
}
219220

220221
ImGui::Begin("Options"); {
221-
if (ImGui::Checkbox("Single Kernel PT", &Settings::singleKernel)) {
222+
const char* Tracers[] = { "Streamed", "Single Kernel", "BVH Visualize" };
223+
if (ImGui::Combo("Tracer", &Settings::tracer, Tracers, IM_ARRAYSIZE(Tracers))) {
222224
State::camChanged = true;
223225
}
224-
if (!Settings::singleKernel) {
226+
227+
if (Settings::tracer == Tracer::Streamed) {
225228
ImGui::Checkbox("Sort Material", &Settings::sortMaterial);
226229
}
227230

src/sceneStructs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ struct RenderState {
5050
std::string imageName;
5151
};
5252

53-
struct Material;
54-
5553
struct Intersection {
5654
__device__ Intersection() {}
5755

0 commit comments

Comments
 (0)