Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
255 changes: 250 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,256 @@ CUDA Path Tracer

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 3**

* (TODO) YOUR NAME HERE
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
## Path tracing with CUDA on the GPU
### Connie Chang
* [LinkedIn](https://www.linkedin.com/in/conniechang44), [Demo Reel](https://www.vimeo.com/ConChang/DemoReel)
* Tested on: Windows 10, Intel Xeon CPU E5-1630 v4 @ 3.70 GHz, GTX 1070 8GB (SIG Lab)

### (TODO: Your README)
![](builds/direct.F.2018-09-29_00-48-35z.5000samp.png)
A simple scene, rendered with full lighting

*DO NOT* leave the README to the last minute! It is a crucial part of the
project, and we will not be able to grade you without a good README.
![](builds/custom.N.2018-09-30_23-35-33z.5000samp.png)
A render using Naive with 5000 iterations. Materials include diffuse, perfectly specular reflective, perfectly specular refractive, and glass.


## Introduction
The goal of this project is to write a path tracer that runs on the GPU using CUDA. Each thread of the GPU followed a ray, and updated color calculations as it bounced in the scene. Three different integration algorithms were implemented: Naive, Direct Lighting, and Full Lighting. In addition, this project supports three light scattering models: Diffuse Lambert, reflective, and refractive. The latter two use a Fresnel dielectric computation to accurately simulate the ratio of reflection to refraction. The three scattering methods can be combined in any way to create interesting materials. To make the final image look nicer, anti-aliasing was added by jittering the rays as they left the camera.

Some optimizations were added to speed up the render. To reduce warp divergence, dead rays are filtered out with stream compaction. I used thrust::partition to simulate this. Therefore, the dead rays do not occupy any threads and the live rays are bundled together in warps. Furthermore, there is an option to cache the first bounce of the first ray casted from each pixel, reducing the computations required for the first bounce on subsequent samples. However, this cache does not work with anti-aliasing. Lastly, there is another option to sort materials such that rays hitting the same material are bundled together. Unfortunately, this did not provide much of an optimization.

## Features
* Naive Integrator
* Direct Lighting Integrator with Multiple Importance Sampling
* Full Lighting Integrator
* Anti-aliasing
* Fresnel dielectric
* Square plane geometry
* Sampling square plane

## Renders
All renders have 5000 iterations and are anti-aliased.

![](builds/cornell.2018-09-27_01-41-51z.5000samp.png)
Naive

![](builds/direct.2018-09-28_22-55-38z.5000samp.png)
Direct Lighting

![](builds/direct.F.2018-09-29_00-48-35z.5000samp.png)
Full Lighting

![](builds/direct.2018-09-28_03-39-58z.845samp.png)
Perfectly Specular Reflection with naive integrator

![](builds/direct.2018-09-28_04-52-45z.5000samp.png)
Perfectly Specular Refraction with naive integrator

![](builds/direct.N.2018-10-01_00-35-39z.5000samp.png)
Glass Material with a full lighting integrator

## Usage and Scene File Description
An example scene file looks like this:
```
INTEGRATOR F
MATERIALSORT 0
FIRSTCACHE 0
ANTIALIAS 1
STREAMCOMPACT 1

// Emissive material (light)
MATERIAL 0
RGB 1 1 1
SPECEX 0
SPECRGB 0 0 0
DIFFUSE 1
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 5

// Diffuse white
MATERIAL 1
RGB .98 .98 .98
SPECEX 0
SPECRGB 0 0 0
DIFFUSE 1
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 0

// Diffuse red
MATERIAL 2
RGB .85 .35 .35
SPECEX 0
SPECRGB 0 0 0
DIFFUSE 1
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 0

// Diffuse green
MATERIAL 3
RGB .35 .85 .35
SPECEX 0
SPECRGB 0 0 0
DIFFUSE 1
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 0

// Specular white
MATERIAL 4
RGB .98 .98 .98
SPECEX 0
SPECRGB .98 .98 .98
DIFFUSE 0
REFL 1
REFR 1
REFRIOR 1.6
EMITTANCE 0

// Camera
CAMERA
RES 800 800
FOVY 45
ITERATIONS 5000
DEPTH 8
FILE direct
EYE 0.0 5 10.5
LOOKAT 0 5 0
UP 0 1 0


// Ceiling light
OBJECT 0
squareplane
material 0
TRANS 0 9.9 0
ROTAT 90 0 0
SCALE 3 3 1

// Floor
OBJECT 1
cube
material 1
TRANS 0 0 0
ROTAT 0 0 0
SCALE 10 .01 10

// Ceiling
OBJECT 2
cube
material 1
TRANS 0 10 0
ROTAT 0 0 90
SCALE .01 10 10

// Back wall
OBJECT 3
cube
material 1
TRANS 0 5 -5
ROTAT 0 90 0
SCALE .01 10 10

// Left wall
OBJECT 4
cube
material 2
TRANS -5 5 0
ROTAT 0 0 0
SCALE .01 10 10

// Right wall
OBJECT 5
cube
material 3
TRANS 5 5 0
ROTAT 0 0 0
SCALE .01 10 10

// Sphere
OBJECT 6
sphere
material 4
TRANS -1 4 -1
ROTAT 0 0 0
SCALE 1.5 1.5 1.5
```

The first section sets some flags for the render. The first line tells the path tracer which integrator to use. 'N' is for Naive, 'D' is for Direct Lighting, and 'F' is for Full Lighting. The second flag turns material sorting on or off, with 1 being on and 0 being off. Next is the flag for caching the first bounce. Last is the flag for turning on anti-aliasing. Note that if anti-aliasing is turned on, the first bounce cache will be turned off regardless of what the scene file says. The following configuration uses the Full Lighting integrator with anti-aliasing and stream compaction, while material sort and first bounce cache are turned off.
```
INTEGRATOR F
MATERIALSORT 0
FIRSTCACHE 0
ANTIALIAS 1
STREAMCOMPACT 1
```

Next, the materials are described. The first line tells the program that it is about to read a material and gives it a unique ID number. Then, we have color ranging from 0 - 1, the specular exponent, and the specular color. The next three are flags for whether this material has diffuse, reflective, or refractive components. REFRIOR is the index of refraction. Lastly, EMITTANCE is how much light this material emits if it emits any at all. The following example has a material ID of 4, color and specular color of (0.98, 0.98, 0.98), and a specular exponent of 0. It does not have a diffuse component, but has both reflection and refraction, creating a glass-like material. It's index of refraction is 1.6, and it does not emit any light.
```
MATERIAL 4
RGB .98 .98 .98
SPECEX 0
SPECRGB .98 .98 .98
DIFFUSE 0
REFL 1
REFR 1
REFRIOR 1.6
EMITTANCE 0
```

Then, there is the camera description. The first line tells the program that it's about to read camera information. That is followed by the camera resolution, field of view vertically, the number of samples per pixel, the depth of each ray sample, and the file name to store the image. Finally, it has the position of the camera, the point it is looking at, and the direction of its up vector.
```
CAMERA
RES 800 800
FOVY 45
ITERATIONS 5000
DEPTH 8
FILE direct
EYE 0.0 5 10.5
LOOKAT 0 5 0
UP 0 1 0
```

The last section describes the geometry in the scene. The first line says we are reading a geometry OBJECT and gives it a unique ID number. The second line says what kind of geometry this is. This project supports squareplane, cube, and sphere. Next is this geometry's material, using the material ID. The last three lines are the transformations applied to this geometry.
```
OBJECT 0
squareplane
material 0
TRANS 0 9.9 0
ROTAT 90 0 0
SCALE 3 3 1
```

## Performance
Timings are measured using chronos. Only the first 10 iterations were measured and averaged. The rendered image was the Cornell box with a glass sphere, rendered with the naive integrator with 5000 iterations.

Below is a graph exhibiting the average runtimes for each kernel. Not surprisingly, the full lighting kernel takes the longest, but there is great improvement from the stream compaction and material sort. Compute intersections has some improvement from the stream compaction and material sort. Generate camera ray is about the same except for the first cache bounce. Interestingly, the first ray is cached in hopes of improving the time to generate the subsequent first rays. However, it appears the check for whether the ray is cached adds too much time.
![](img/kernel_times-bar_graph.png)

Here is the same information in a stacked graph.
![](img/kernel_times-stacked_graph.png)

All in all, stream compaction and material sort seem to have positive impacts. However, as seen in the next graph, that is not the case. Material sort, in reality, slows down the overall runtime drastically. While the sorting improves the timings in the kernels, it is occupying a lot of time outside of those kernels. It must be that sorting the materials takes too long. Another strange observation is that the render without any optimizations is the fastest. Like material sort, there must be a lot of overhead for these optimizations.
![](img/total_times.png)

Next is a graph showing how stream compaction decreases shading time at each depth within a single iteration. Since the dead rays are removed at each depth, there are less rays to compute. However, stream compaction only works on open scenes where rays are most likely to die from not intersecting the scene. In a closed scene, rays do not die as frequently so barely any rays are compacted out.
![](img/stream_compaction_depth.png)

## Bloopers
I did not realize the random number generator was using the same seed for each depth:
![](builds/cornell.2018-09-23_20-20-42z.100samp.png)

I accidentally made every material reflective:
![](builds/cornell.2018-09-25_20-52-24z.213samp.png)
![](builds/cornell.2018-09-26_00-39-00z.79samp.png)

I was handling total internal reflections incorrectly:
![](builds/direct.2018-09-28_03-27-46z.5000samp.png)

Backward direct lighting:
![](builds/direct.2018-09-28_20-58-16z.5000samp.png)
Binary file added builds/cis565_path_tracer.VC.db
Binary file not shown.
Binary file added builds/cornell.2018-09-22_18-25-49z.5000samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_19-22-33z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_19-23-29z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_19-24-58z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_19-39-02z.1samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_19-47-56z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_19-57-04z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_20-19-03z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-22_21-05-17z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-23_00-30-29z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-23_20-19-15z.12samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-23_20-20-42z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_00-29-42z.63samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_00-48-36z.21samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_04-31-53z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_04-33-36z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_04-34-57z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_15-04-56z.100samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-24_15-12-19z.502samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-25_17-17-59z.5000samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-25_19-56-20z.5000samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-25_20-52-24z.213samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-26_00-39-00z.79samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-26_00-45-51z.22samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-26_00-47-40z.5000samp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added builds/cornell.2018-09-26_16-39-18z.1162samp.png
Binary file added builds/cornell.2018-09-26_17-23-35z.83samp.png
Binary file added builds/cornell.2018-09-26_18-45-52z.3046samp.png
Binary file added builds/cornell.2018-09-26_19-25-04z.2087samp.png
Binary file added builds/cornell.2018-09-26_19-26-57z.5000samp.png
Binary file added builds/cornell.2018-09-26_19-35-00z.1281samp.png
Binary file added builds/cornell.2018-09-26_20-59-20z.5000samp.png
Binary file added builds/cornell.2018-09-26_23-13-12z.1609samp.png
Binary file added builds/cornell.2018-09-26_23-15-08z.5000samp.png
Binary file added builds/cornell.2018-09-26_23-19-44z.5000samp.png
Binary file added builds/cornell.2018-09-27_00-27-51z.5000samp.png
Binary file added builds/cornell.2018-09-27_01-41-51z.5000samp.png
Binary file added builds/custom.N.2018-09-30_23-35-33z.5000samp.png
Binary file added builds/direct.2018-09-27_02-52-22z.1704samp.png
Binary file added builds/direct.2018-09-27_16-53-48z.18samp.png
Binary file added builds/direct.2018-09-27_20-45-04z.5000samp.png
Binary file added builds/direct.2018-09-28_01-45-33z.5000samp.png
Binary file added builds/direct.2018-09-28_02-24-13z.5000samp.png
Binary file added builds/direct.2018-09-28_03-27-46z.5000samp.png
Binary file added builds/direct.2018-09-28_03-39-58z.845samp.png
Binary file added builds/direct.2018-09-28_03-42-13z.5000samp.png
Binary file added builds/direct.2018-09-28_03-52-58z.5000samp.png
Binary file added builds/direct.2018-09-28_04-35-13z.5000samp.png
Binary file added builds/direct.2018-09-28_04-39-59z.1538samp.png
Binary file added builds/direct.2018-09-28_04-48-33z.5000samp.png
Binary file added builds/direct.2018-09-28_04-52-45z.5000samp.png
Binary file added builds/direct.2018-09-28_20-58-16z.5000samp.png
Binary file added builds/direct.2018-09-28_22-38-13z.498samp.png
Binary file added builds/direct.2018-09-28_22-50-44z.5000samp.png
Binary file added builds/direct.2018-09-28_22-53-16z.5000samp.png
Binary file added builds/direct.2018-09-28_22-55-38z.5000samp.png
Binary file added builds/direct.D.2018-09-30_18-30-33z.6samp.png
Binary file added builds/direct.F.2018-09-30_17-36-05z.244samp.png
Binary file added builds/direct.N.2018-10-01_00-09-27z.5000samp.png
Binary file added builds/direct.N.2018-10-01_00-12-39z.5000samp.png
Binary file added builds/direct.N.2018-10-01_00-17-06z.5000samp.png
Binary file added builds/direct.N.2018-10-01_00-35-39z.5000samp.png
Binary file added img/kernel_times-bar_graph.png
Binary file added img/kernel_times-stacked_graph.png
Binary file added img/stream_compaction_depth.png
Binary file added img/total_times.png
Loading