Skip to content

Commit

Permalink
visualization code for a gbuffer with dummy data as time-to-intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
likangning93 committed Oct 8, 2020
1 parent 1178307 commit 0857d1f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static double lastY;
int ui_iterations = 0;
int startupIterations = 0;
int lastLoopIterations = 0;
bool ui_showGbuffer = false;
bool ui_denoise = false;
int ui_filterSize = 80;
float ui_colorWeight = 0.45f;
Expand Down Expand Up @@ -149,19 +150,26 @@ void runCuda() {
pathtraceInit(scene);
}

uchar4 *pbo_dptr = NULL;
cudaGLMapBufferObject((void**)&pbo_dptr, pbo);

if (iteration < ui_iterations) {
uchar4 *pbo_dptr = NULL;
iteration++;
cudaGLMapBufferObject((void**)&pbo_dptr, pbo);

// execute the kernel
int frame = 0;
pathtrace(pbo_dptr, frame, iteration);
pathtrace(frame, iteration);
}

// unmap buffer object
cudaGLUnmapBufferObject(pbo);
if (ui_showGbuffer) {
showGBuffer(pbo_dptr);
} else {
showImage(pbo_dptr, iteration);
}

// unmap buffer object
cudaGLUnmapBufferObject(pbo);

if (ui_saveAndExit) {
saveImage();
pathtraceFree();
Expand Down
1 change: 1 addition & 0 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ extern int height;

extern int ui_iterations;
extern int startupIterations;
extern bool ui_showGbuffer;
extern bool ui_denoise;
extern int ui_filterSize;
extern float ui_colorWeight;
Expand Down
70 changes: 63 additions & 7 deletions src/pathtrace.cu
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,28 @@ __global__ void sendImageToPBO(uchar4* pbo, glm::ivec2 resolution,
}
}

__global__ void gbufferToPBO(uchar4* pbo, glm::ivec2 resolution, GBufferPixel* gBuffer) {
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;

if (x < resolution.x && y < resolution.y) {
int index = x + (y * resolution.x);
float timeToIntersect = gBuffer[index].t * 256.0;

pbo[index].w = 0;
pbo[index].x = timeToIntersect;
pbo[index].y = timeToIntersect;
pbo[index].z = timeToIntersect;
}
}

static Scene * hst_scene = NULL;
static glm::vec3 * dev_image = NULL;
static Geom * dev_geoms = NULL;
static Material * dev_materials = NULL;
static PathSegment * dev_paths = NULL;
static ShadeableIntersection * dev_intersections = NULL;
static GBufferPixel* dev_gBuffer = NULL;
// TODO: static variables for device memory, any extra info you need, etc
// ...

Expand All @@ -95,6 +111,8 @@ void pathtraceInit(Scene *scene) {
cudaMalloc(&dev_intersections, pixelcount * sizeof(ShadeableIntersection));
cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection));

cudaMalloc(&dev_gBuffer, pixelcount * sizeof(GBufferPixel));

// TODO: initialize any extra device memeory you need

checkCUDAError("pathtraceInit");
Expand All @@ -106,6 +124,7 @@ void pathtraceFree() {
cudaFree(dev_geoms);
cudaFree(dev_materials);
cudaFree(dev_intersections);
cudaFree(dev_gBuffer);
// TODO: clean up any extra device memory you created

checkCUDAError("pathtraceFree");
Expand Down Expand Up @@ -274,6 +293,18 @@ __global__ void shadeSimpleMaterials (
}
}

__global__ void generateGBuffer (
int num_paths,
ShadeableIntersection* shadeableIntersections,
PathSegment* pathSegments,
GBufferPixel* gBuffer) {
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < num_paths)
{
gBuffer[idx].t = shadeableIntersections[idx].t;
}
}

// Add the current iteration's output to the overall image
__global__ void finalGather(int nPaths, glm::vec3 * image, PathSegment * iterationPaths)
{
Expand All @@ -290,7 +321,7 @@ __global__ void finalGather(int nPaths, glm::vec3 * image, PathSegment * iterati
* Wrapper for the __global__ call that sets up the kernel calls and does a ton
* of memory management
*/
void pathtrace(uchar4 *pbo, int frame, int iter) {
void pathtrace(int frame, int iter) {
const int traceDepth = hst_scene->state.traceDepth;
const Camera &cam = hst_scene->state.camera;
const int pixelcount = cam.resolution.x * cam.resolution.y;
Expand Down Expand Up @@ -345,12 +376,15 @@ void pathtrace(uchar4 *pbo, int frame, int iter) {
// --- PathSegment Tracing Stage ---
// Shoot ray into scene, bounce between objects, push shading chunks

bool iterationComplete = false;
while (!iterationComplete) {
// Empty gbuffer
cudaMemset(dev_gBuffer, 0, pixelcount * sizeof(GBufferPixel));

// clean shading chunks
cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection));

bool iterationComplete = false;
while (!iterationComplete) {

// tracing
dim3 numblocksPathSegmentTracing = (num_paths + blockSize1d - 1) / blockSize1d;
computeIntersections <<<numblocksPathSegmentTracing, blockSize1d>>> (
Expand All @@ -363,8 +397,12 @@ void pathtrace(uchar4 *pbo, int frame, int iter) {
);
checkCUDAError("trace one bounce");
cudaDeviceSynchronize();
depth++;

if (depth == 0) {
generateGBuffer<<<numblocksPathSegmentTracing, blockSize1d>>>(num_paths, dev_intersections, dev_paths, dev_gBuffer);
}

depth++;

// TODO:
// --- Shading Stage ---
Expand All @@ -391,12 +429,30 @@ void pathtrace(uchar4 *pbo, int frame, int iter) {

///////////////////////////////////////////////////////////////////////////

// Send results to OpenGL buffer for rendering
sendImageToPBO<<<blocksPerGrid2d, blockSize2d>>>(pbo, cam.resolution, iter, dev_image);

// Retrieve image from GPU
cudaMemcpy(hst_scene->state.image.data(), dev_image,
pixelcount * sizeof(glm::vec3), cudaMemcpyDeviceToHost);

checkCUDAError("pathtrace");
}

void showGBuffer(uchar4* pbo) {
const Camera &cam = hst_scene->state.camera;
const dim3 blockSize2d(8, 8);
const dim3 blocksPerGrid2d(
(cam.resolution.x + blockSize2d.x - 1) / blockSize2d.x,
(cam.resolution.y + blockSize2d.y - 1) / blockSize2d.y);

gbufferToPBO<<<blocksPerGrid2d, blockSize2d>>>(pbo, cam.resolution, dev_gBuffer);
}

void showImage(uchar4* pbo, int iter) {
const Camera &cam = hst_scene->state.camera;
const dim3 blockSize2d(8, 8);
const dim3 blocksPerGrid2d(
(cam.resolution.x + blockSize2d.x - 1) / blockSize2d.x,
(cam.resolution.y + blockSize2d.y - 1) / blockSize2d.y);

// Send results to OpenGL buffer for rendering
sendImageToPBO<<<blocksPerGrid2d, blockSize2d>>>(pbo, cam.resolution, iter, dev_image);
}
4 changes: 3 additions & 1 deletion src/pathtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@

void pathtraceInit(Scene *scene);
void pathtraceFree();
void pathtrace(uchar4 *pbo, int frame, int iteration);
void pathtrace(int frame, int iteration);
void showGBuffer(uchar4 *pbo);
void showImage(uchar4 *pbo, int iter);
4 changes: 4 additions & 0 deletions src/preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ void drawGui(int windowWidth, int windowHeight) {

ImGui::Separator();

ImGui::Checkbox("Show GBuffer", &ui_showGbuffer);

ImGui::Separator();

if (ImGui::Button("Save image and exit")) {
ui_saveAndExit = true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/sceneStructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,7 @@ struct ShadeableIntersection {
glm::vec3 surfaceNormal;
int materialId;
};

struct GBufferPixel {
float t;
};

0 comments on commit 0857d1f

Please sign in to comment.