Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project 3: Gangzheng Tong #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,57 @@ 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)
* Gangzheng Tong
* Tested on: Windows 10, i7-8750H @ 2.20GHz 16GB, RTX 2070 8GB (personal laptop)

### (TODO: Your README)
### Overview
![CUDA Path Tracer](img/pathtracer.gif)

*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.
**Path tracing** is a [computer graphics](https://en.wikipedia.org/wiki/Computer_graphics "Computer graphics") [Monte Carlo method](https://en.wikipedia.org/wiki/Monte_Carlo_method "Monte Carlo method") of [rendering](https://en.wikipedia.org/wiki/Rendering_(computer_graphics) "Rendering (computer graphics)") images of three-dimensional scenes such that the [global illumination](https://en.wikipedia.org/wiki/Global_illumination "Global illumination") is faithful to reality [Widipediea]
In this project I implemented the basic path tracing algorithm with C++ and Cuda, taking advantage of the highly parallelial nature of GPU and achieved an interactive renderer.

![](https://www.scratchapixel.com/images/upload/shading-intro2/shad2-globalillum1a.png?)
The general idea is to shoot a large number of rays from the camera, compute the intersections with the objects in the scene, and scatter more rays from the intersection point based on the material properties such as glassy or matte. If after a few bounces the scattered ray reaches a light source, we can shade the path segment based on the light color and emittance,

### Features
3 Different materials:
* Diffuse
* Perfectly Specular
* Refraction with Frensel effects using [Schlick's approximation]([https://en.wikipedia.org/wiki/Schlick%27s_approximation](https://en.wikipedia.org/wiki/Schlick%27s_approximation))

![3 Materials](img/3_materials.png)

![Motion Blur](img/mb.png)

![Depth of Field](img/dof.png)

**Other features:**
* Direct Light
* Sort by Material
* First Bounce Cache
* Stream Compaction for Removing Terminated Rays


### Performance Analysis
* First Bounce Cache. By cache the first intersection data in the first iteration, we are essentially save one ComputeIntersection() for all subsequent iterations. Given that our path tracer has 8 bounces limit, we are saving up to 1/8 GPU time theoretically.
![time (ms) comparison of with/without using first cache](img/cache_time.PNG)

* Sort by Material
Before launch the shading kernel, we first sort the path segments (rays) by the material id first. By doing this the rays with the same material tend to by processed in the same blocks, so as minimizing the time divergence brought by the different material shading algorithm. For example, if a material requires complicated computation and takes significant longer time than the sample material, ideally we want to group than separately so as to avoid threads in the same warp finish at the different time.
However, with my implementation, due to all materials are relatively simple and does not differ too much in terms of kernel execution time, the result is not optimal. It's not even compensate the overhead introduce by the sorting operation.

![time (ms) comparison of using or not using sorting by materials](img/sort_material.PNG)

* Stream Compaction - remove the terminated rays.
During the rays bounce in the scene, it's often the case that a ray will be terminated before the bouncing limit, either because it hits the light source, or it does not hit anything. It makes sense to remove these rays before launch the shading kernel to reduce the threads numbers. The effect is especially obvious after a few bounces when very few rays remaining. It's help even more when the scene is an open environment because more rays will hit nothing in the first few bounces.

![Number of remaining rays](img/compaction.PNG)

![Number of remaining rays](img/compaction_time.PNG)


### References
* [PBRT online edition](http://www.pbr-book.org/)
* [Schlick's approximation - Wikipedia](https://en.wikipedia.org/wiki/Schlick%27s_approximation)
* [Thrust :: CUDA Toolkit Documentation - Nvidia's GPU](http://docs.nvidia.com/cuda/thrust/index.html)
* Thanks Mark Xu for explaining the Stream Compaction
Binary file added img/3_materials.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 img/cache_time.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 img/compaction.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 img/compaction_time.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 img/dof.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 img/mb.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 img/only_diffuse.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 img/pathtracer.gif
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 img/refraction_wrong.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 img/shad2-globalillum1a.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 img/sort_material.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 20 additions & 2 deletions scenes/cornell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ REFR 0
REFRIOR 0
EMITTANCE 0

// Transimisive
MATERIAL 5
RGB 0.98 0.98 0.98
SPECEX 0
SPECRGB 0.98 0.98 0.98
REFL 0
REFR 1
REFRIOR 1.3
EMITTANCE 0

// Camera
CAMERA
RES 800 800
Expand Down Expand Up @@ -111,7 +121,15 @@ SCALE .01 10 10
// Sphere
OBJECT 6
sphere
material 4
TRANS -1 4 -1
material 5
TRANS -1 4 4
ROTAT 0 0 0
SCALE 3 3 3

// Front wall
//OBJECT 7
//cube
//material 1
//TRANS 0 5 13
//ROTAT 0 -90 0
//SCALE 100 1000 1000
153 changes: 153 additions & 0 deletions scenes/cornell_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Emissive material (light)
MATERIAL 0
RGB 1 1 1
SPECEX 0
SPECRGB 0 0 0
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 10

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

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

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

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

// Transimisive
MATERIAL 5
RGB 0.98 0.98 0.98
SPECEX 0
SPECRGB 0.98 0.98 0.98
REFL 0
REFR 1
REFRIOR 1.3
EMITTANCE 0


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


// Ceiling light
OBJECT 0
cube
material 0
TRANS 0 10 0
ROTAT 0 0 0
SCALE 3 .3 3

// 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 1, transimissive
OBJECT 6
sphere
material 5
TRANS 0 4 0
ROTAT 0 0 0
SCALE 3 3 3

// Sphere 2, specular
OBJECT 7
sphere
material 4
TRANS -3 4 0
ROTAT 0 0 0
SCALE 2 2 2


// Sphere 3, lambert
OBJECT 8
sphere
material 3
TRANS 3 4 0
ROTAT 0 0 0
SCALE 2 2 2

// Front wall
OBJECT 9
cube
material 3
TRANS 5 5 11
ROTAT 0 270 0
SCALE .01 50 50
Loading