Skip to content

Commit fdbcf3d

Browse files
committed
bug fix + GBuffer preivew
1 parent a43e6fd commit fdbcf3d

File tree

5 files changed

+69
-15
lines changed

5 files changed

+69
-15
lines changed

src/common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ int Settings::traceDepth = 0;
44
int Settings::toneMapping = ToneMapping::ACES;
55
int Settings::tracer = Tracer::Streamed;
66
bool Settings::sortMaterial = false;
7+
int Settings::GBufferPreviewOpt = 0;
78

89
bool State::camChanged = true;

src/common.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct ToneMapping {
2020

2121
struct Tracer {
2222
enum {
23-
Streamed = 0, SingleKernel = 1, BVHVisualize = 2
23+
Streamed = 0, SingleKernel = 1, BVHVisualize = 2, GBufferPreview = 3
2424
};
2525
};
2626

@@ -29,6 +29,7 @@ struct Settings {
2929
static int toneMapping;
3030
static int tracer;
3131
static bool sortMaterial;
32+
static int GBufferPreviewOpt;
3233
};
3334

3435
struct State {

src/pathtrace.cu

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ static thrust::device_ptr<PathSegment> devTerminatedPathsThr;
7070
static thrust::device_ptr<Intersection> devIntersectionsThr;
7171
static thrust::device_ptr<int> devIntersecMatKeysThr;
7272
static thrust::device_ptr<int> devSegmentMatKeysThr;
73+
74+
static glm::vec3* devGBufferPos = nullptr;
75+
static glm::vec3* devGBufferNorm = nullptr;
7376

7477
void InitDataContainer(GuiDataContainer* imGuiData) {
7578
guiData = imGuiData;
@@ -156,6 +159,35 @@ __global__ void generateRayFromCamera(Camera cam, int iter, int traceDepth, Path
156159
}
157160
}
158161

162+
__global__ void previewGBuffer(
163+
int iter,
164+
DevScene* scene, Camera cam,
165+
glm::vec3* image, int width, int height,
166+
int kind
167+
) {
168+
int x = blockDim.x * blockIdx.x + threadIdx.x;
169+
int y = blockDim.y * blockIdx.y + threadIdx.y;
170+
if (x >= width || y >= height) {
171+
return;
172+
}
173+
int index = y * width + x;
174+
thrust::default_random_engine rng = makeSeededRandomEngine(iter, index, 0);
175+
176+
Ray ray = sampleCamera(cam, x, y, sample4D(rng));
177+
Intersection intersec;
178+
scene->intersect(ray, intersec);
179+
180+
if (kind == 0) {
181+
image[index] += intersec.pos;
182+
}
183+
else if (kind == 1) {
184+
image[index] += (intersec.norm + 1.f) * .5f;
185+
}
186+
else if (kind == 2) {
187+
image[index] += glm::vec3(intersec.uv, 1.f);
188+
}
189+
}
190+
159191
// computeIntersections handles generating ray intersections ONLY.
160192
// Generating new rays is handled in your shader(s).
161193
// Feel free to modify the code below.
@@ -244,10 +276,7 @@ __global__ void pathIntegSampleSurface(
244276
PathSegment& segment = segments[idx];
245277
thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, 4 + depth * SamplesConsumedOneIter);
246278

247-
Material material = scene->devMaterials[intersec.matId];
248-
if (material.baseColorMapId > NullTextureId) {
249-
material.baseColor = scene->devTextureObjs->linearSample(intersec.uv);
250-
}
279+
Material material = scene->getMaterialWithTexture(intersec);
251280

252281
glm::vec3 accRadiance(0.f);
253282

@@ -339,24 +368,20 @@ __global__ void singleKernelPT(
339368
thrust::default_random_engine rng = makeSeededRandomEngine(iter, index, 0);
340369

341370
Ray ray = sampleCamera(cam, x, y, sample4D(rng));
342-
343371
Intersection intersec;
344372
scene->intersect(ray, intersec);
345373

346374
if (intersec.primId == NullPrimitive) {
347375
goto WriteRadiance;
348376
}
349377

350-
Material material = scene->devMaterials[intersec.matId];
378+
Material material = scene->getMaterialWithTexture(intersec);
379+
351380
if (material.type == Material::Type::Light) {
352381
accRadiance = material.baseColor * material.emittance;
353382
goto WriteRadiance;
354383
}
355384

356-
if (material.baseColorMapId > NullTextureId) {
357-
material.baseColor = scene->devTextureObjs->linearSample(intersec.uv);
358-
}
359-
360385
glm::vec3 throughput(1.f);
361386
intersec.wo = -ray.direction;
362387

@@ -400,7 +425,7 @@ __global__ void singleKernelPT(
400425
if (intersec.primId == NullPrimitive) {
401426
break;
402427
}
403-
material = scene->devMaterials[intersec.matId];
428+
material = scene->getMaterialWithTexture(intersec);
404429

405430
if (material.type == Material::Type::Light) {
406431
#if SCENE_LIGHT_SINGLE_SIDED
@@ -547,10 +572,15 @@ void pathTrace(uchar4* pbo, int frame, int iter) {
547572
singleKernelPT<<<singlePTBlockNum, singlePTBlockSize>>>(
548573
iter, Settings::traceDepth, hstScene->devScene, cam, devImage, cam.resolution.x, cam.resolution.y);
549574
}
550-
else {
575+
else if (Settings::tracer == Tracer::BVHVisualize) {
551576
BVHVisualize<<<singlePTBlockNum, singlePTBlockSize>>>(
552577
iter, hstScene->devScene, cam, devImage, cam.resolution.x, cam.resolution.y);
553578
}
579+
else {
580+
previewGBuffer<<<singlePTBlockNum, singlePTBlockSize>>>(
581+
iter, hstScene->devScene, cam, devImage, cam.resolution.x, cam.resolution.y,
582+
Settings::GBufferPreviewOpt);
583+
}
554584

555585
if (guiData != nullptr) {
556586
guiData->TracedDepth = Settings::traceDepth;

src/preview.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,22 +211,36 @@ void RenderImGui()
211211
static float f = 0.0f;
212212
static int counter = 0;
213213

214-
ImGui::Begin("Path Tracer Analytics"); {
214+
215+
216+
ImGui::Begin("Path Tracer Analytics", nullptr,
217+
ImGuiWindowFlags_NoMove |
218+
ImGuiWindowFlags_NoDecoration |
219+
ImGuiWindowFlags_AlwaysAutoResize |
220+
ImGuiWindowFlags_NoSavedSettings |
221+
ImGuiWindowFlags_NoFocusOnAppearing |
222+
ImGuiWindowFlags_NoNav); {
215223
ImGui::Text("Traced Depth %d", imguiData->TracedDepth);
216224
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
217225

218226
ImGui::End();
219227
}
220228

221229
ImGui::Begin("Options"); {
222-
const char* Tracers[] = { "Streamed", "Single Kernel", "BVH Visualize" };
230+
const char* Tracers[] = { "Streamed", "Single Kernel", "BVH Visualize", "GBuffer Preview" };
223231
if (ImGui::Combo("Tracer", &Settings::tracer, Tracers, IM_ARRAYSIZE(Tracers))) {
224232
State::camChanged = true;
225233
}
226234

227235
if (Settings::tracer == Tracer::Streamed) {
228236
ImGui::Checkbox("Sort Material", &Settings::sortMaterial);
229237
}
238+
else if (Settings::tracer == Tracer::GBufferPreview) {
239+
const char* Modes[] = { "Position", "Normal", "Texcoord" };
240+
if (ImGui::Combo("Mode", &Settings::GBufferPreviewOpt, Modes, IM_ARRAYSIZE(Modes))) {
241+
State::camChanged = true;
242+
}
243+
}
230244

231245
if (ImGui::InputInt("Max Depth", &Settings::traceDepth, 1, 1)) {
232246
State::camChanged = true;

src/scene.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ struct DevScene {
6464
void create(const Scene& scene);
6565
void destroy();
6666

67+
__device__ Material getMaterialWithTexture(const Intersection& intersec) {
68+
Material mat = devMaterials[intersec.matId];
69+
if (mat.baseColorMapId > NullTextureId) {
70+
mat.baseColor = devTextureObjs[mat.baseColorMapId].linearSample(intersec.uv);
71+
}
72+
return mat;
73+
}
74+
6775
__device__ int getMTBVHId(glm::vec3 dir) {
6876
glm::vec3 absDir = glm::abs(dir);
6977
if (absDir.x > absDir.y) {

0 commit comments

Comments
 (0)