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: Lan Lou #6

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
f13707b
obj loading done
LanLou123 Sep 25, 2018
09bd8a3
add obj testexample
LanLou123 Sep 25, 2018
ec8e630
dragon staff
LanLou123 Sep 25, 2018
6af9678
dragon test
LanLou123 Sep 25, 2018
3a4eb13
Delete dragontest.JPG
LanLou123 Sep 25, 2018
45b7e51
artorias sword
LanLou123 Sep 25, 2018
115e32a
Add files via upload
LanLou123 Sep 25, 2018
ae3147c
Delete cornell.2018-09-25_17-38-41z.111samp.png
LanLou123 Sep 25, 2018
5eef6a3
Add files via upload
LanLou123 Sep 25, 2018
f831c19
Add files via upload
LanLou123 Sep 25, 2018
3f1bf86
Add files via upload
LanLou123 Sep 26, 2018
087d215
bouding box & antialiasing
LanLou123 Sep 26, 2018
44efcde
anothersowrdimg
LanLou123 Sep 26, 2018
24a9234
KD tree done
LanLou123 Sep 29, 2018
5f8a06b
KDtree done
LanLou123 Sep 29, 2018
14526a1
add some files
LanLou123 Sep 29, 2018
3416e13
KD tree iterative solution corrected
LanLou123 Sep 29, 2018
d17a66e
minor fix
LanLou123 Sep 29, 2018
c3e7217
Add files via upload
LanLou123 Sep 29, 2018
d36b22c
Add files via upload
LanLou123 Sep 30, 2018
7edb42b
Add files via upload
LanLou123 Sep 30, 2018
0fe39b8
add toggles
LanLou123 Sep 30, 2018
6acd54a
Add files via upload
LanLou123 Sep 30, 2018
aa701d9
Add files via upload
LanLou123 Sep 30, 2018
b76f107
Add files via upload
LanLou123 Sep 30, 2018
a1a3c7b
Add files via upload
LanLou123 Sep 30, 2018
17c0d97
Update README.md
LanLou123 Sep 30, 2018
f21ff72
add interactions
LanLou123 Oct 1, 2018
087e39c
add reference
LanLou123 Oct 1, 2018
b5491bf
Update README.md
LanLou123 Oct 2, 2018
56997cc
Update README.md
LanLou123 Oct 2, 2018
05efa94
Update README.md
LanLou123 Oct 3, 2018
545e79d
Add files via upload
LanLou123 Oct 3, 2018
d5ec683
add some explainations
LanLou123 Oct 3, 2018
344b9ef
Update README.md
LanLou123 Oct 3, 2018
46658b1
pre merge
LanLou123 Nov 14, 2018
b3e1e47
spectrum rendering
LanLou123 Nov 16, 2018
1e5e059
add img
LanLou123 Nov 16, 2018
2b41004
Update README.md
LanLou123 Nov 16, 2018
6ce9b25
Update README.md
LanLou123 Feb 2, 2019
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
125 changes: 119 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,126 @@
CUDA Path Tracer
================

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

* (TODO) YOUR NAME HERE
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
a scene including different materials : **pure reflective**, **diffuse**, and **refraction with reflect** based on Schlick's approximation

### (TODO: Your README)
![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/cornell.2018-09-30_16-46-42z.4007samp.png)

*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.
### Glass Dragon

```resolution``` : 1000X1000 ```iteration``` : 2000 ```render time``` : 25min ```vertex count``` : 12.5k

![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/dragonKD2000iter.png )

### Interactions

![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/ddfddddddd.gif )

# Features:
- **Basic features**
- A shading kernel with BSDF evaluation for:
- Ideal diffuse shader.
- perfect specular reflective surface.
- Stream compaction for terminating unwanted thread from thread pool using thrust::partition
- material sorting using thrust::sort_by_key
- caching first bounce information for future iteration use
- **Advanced features**
- refreaction with Frensel effects using Schlick's approximation
- physically based depth of field

- in short, this effect is achieved by jittering the ray within a certain area.
- to be more clear, for each ray, we will first create a point having a specific distance (i.e the focal length) away from the cam .
- then, according to the conventional knowledge we learned from high school, when we are looking through the lens, we can think it as we are actually looking from another position dpending on the rfraction ratio of the lens, simmilarily, we will change the origin of the ray in the code by using a concentric disk sampling method as well as the focal radius as a parameter to determine how obvious we want the effect to be, then, since we have the origin and target, we can have our new DOF ray.

![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/doffix.JPG)

- stochastic sampled antialiasing
- achieved by jittering the ray before it's generated from the camera.
- this method is actually a little bit like the mutiple-sampling anti-aliasing (MSAA) method in rasterization, the basic idea for both are like this: pick neccessary information for generating color buffers from different target instead of just one.
- now we take a look at the following image, to the left of the image, what we see is the not anti-aliased version result of rendering a simple trangle, the right image , however, is what happend when we choose to jitter the rays, we will get color info from different locations, therefore giving us nicer renders.

![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/anti_aliasing_sample_points.png)

- the following image explicitly shows the benefit of AA by doing a comparision of same scene with and with out it.

AA off | AA on
------|------
![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/aaoff.JPG)|![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/aaon.JPG)
- arbitrary mesh loading and rendering
- used [tinyObj loader](http://syoyo.github.io/tinyobjloader/)
- used glm's triangle intersection method
- bounding bolumn intersection toggle
### Dragon

```resolution``` : 1000X1000 ```iteration``` : 2000 ```render time``` : 25min for glass, 28min for specular ```vertex count``` : 12.5k

Glass dragon | Specular dragon
------|------
![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/dragonKD2000iter.png )|![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/dragonspecular2000iter.png )

### Sword

a great sword of Artorias the Abysswalker from dark souls series with simple specular material
```resolution``` : 1000X1000 ```iteration``` : 5000 ```render time``` : 26min ```vertex count``` : 1k

![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/sword2.png)

- KD tree for mesh rendering acceleration
- Spectrum rendering:

![](img/cornell.2018-11-16_18-01-14z.810samp.png)

# Performance analysis:

## stream compaction
- stream compaction is needed because if we don't do this, a lot of the threads will simply contribute no efforts once their current work is done, this can result from situations like ray's remaining bounces turned to zero in code (varous reasons can cause this, like hitting the light source, or the ray simply run out of 'depth'), hence, we can simply terminate these unneeded thread stalling, and put them into use again using stream compaction.
- one thing I discover is that stram compaction would not be that good in a closed scene, which I think can be reasulting from the rays keep travling inside the box without being able to get out of it increases the reamaining bounces higher number possibility for each ray, what happens is that ray can only get terminated in two ways: meet the light source or reach the maximum bounce number.

## cache first bounce:

- We want to cache the first iteration's information because before the first bounce, all the information including ray origin ray directions are all the same in spite of different iterations.
- according to the chart bellow, by caching the first bounce, we slightly improved the efficiency

time cost to 500 iterations:

time(ms) |cached | no cache
--------------|---------|-------
test scene| 31861.30 | 34134.12

## sort material:
- For different rays, depending on the material of object they hit, the rays will perform differently, and it's always better to put rays that hit the same material closely in the same block instead of those having different materials, this will prevent some unneccesary stalling condition caused by rays' different behaviour(different thread running time).
- In terms of my own test, with sorting toggled on, the program always experience slower performace than before, I guess this is because the number of materials is not big enough to cover the efficiency lost from the sorting operation.

## bounding box for mesh
- it's quite obvious that by adding bounding box to mesh we can prevent unneccessary triangle checks from happenning, resulting in some performance improvement.
- here's my test result (naive)time cost to 100 iterations

time(sec) |added | not added
--------------|---------|-------
dragon| 597.20 | 623.11

## KD tree acceleration :

time cost to 100 iterations

time(secs) |dragon(12.5 kverts) |sword (1k verts)
--------------|---------|-------
KD accelerated| 121| 32
Nive| 603| 50

![](https://github.com/LanLou123/Project3-CUDA-Path-Tracer/raw/master/img/kdeffect.JPG )

- I choosed to implement a KD tree for the acceleration of obj loading, the following are the main features of the tree
- For the spatial division, I made the current tree node to divide along the longest axis of the node's bounding box, since it minimize the waste of extra divisions,
- Another thing about division is the magnitude of it, I choosed to use the metric that when the left child node and right child node is having more than half of their triangles in common, we stop doing the division.
- After we build the tree on cpu, we can trasfer the tree's data to GPU
- In GPU, since recursion and stl library is unavailable, I instead passed two extra buffers to GPU : first one ```dev_KDtreenode``` which is the array of kdtree nodes each having idx of it's children ,it's parents' index ,bouding box and triangle idx in gpu triangle list, second one : ```dev_gputriidxlst``` which is an array especially for storing triangle indices mapping from kdtree to the actual triangle buffer for each node.
- as for the target node(containing intersected triangle) searching algorithm, I used a mutation of an iterative in-order binary tree search method and C - style stack, I don't think this is a good solution, but it works and give me no small performance boost, but anyway, I will change to use a better method in the future.

# References
- [KD Trees for Faster Ray Tracing ](https://blog.frogslayer.com/kd-trees-for-faster-ray-tracing-with-triangles/)
- [ConcentricSampleDisk function](https://pub.dartlang.org/documentation/dartray/0.0.1/core/ConcentricSampleDisk.html)
- [GPU gem3](https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_pref01.html)
- [Schlick's approximation wiki](https://en.wikipedia.org/wiki/Schlick's_approximation)
- some iterative solutions for binary search tree
Binary file added antialiasingexample1.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 diamond.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 dragonKD2000iter.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 dragonlowpoly.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 dragonspeculartest.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 greatswordofArtorias.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/aaoff.JPG
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/aaoff.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/aaon.JPG
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/aaon.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/anothersordimg.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/anti_aliasing_sample_points.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/cornell.2018-09-30_16-46-42z.4007samp.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/cornell.2018-11-16_16-38-12z.497samp.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/cornell.2018-11-16_16-55-54z.1054samp.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/cornell.2018-11-16_18-01-14z.1074samp.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/cornell.2018-11-16_18-01-14z.810samp.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/ddfddddddd.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/doffix.JPG
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/dragonspecular2000iter.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/kdeffect.JPG
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/sword2.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/sword2000iter.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 infinitemirror.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 liquid dragon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 102 additions & 18 deletions scenes/cornell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ SPECRGB 0 0 0
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 5
EMITTANCE 2.5
DIFFUSE 0

// Diffuse white
MATERIAL 1
Expand All @@ -17,16 +18,18 @@ REFL 0
REFR 0
REFRIOR 0
EMITTANCE 0
DIFFUSE 1

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

// Diffuse green
MATERIAL 3
Expand All @@ -37,20 +40,76 @@ REFL 0
REFR 0
REFRIOR 0
EMITTANCE 0
DIFFUSE 1

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

// Refractive white
MATERIAL 5
RGB .98 .98 .98
SPECEX 0
SPECRGB 1 1 1
REFL 0
REFR 1
REFRIOR 2
EMITTANCE 0
DIFFUSE 0

// Reflective green
MATERIAL 6
RGB .98 .98 .98
SPECEX 0
SPECRGB .35 .95 .35
REFL 1
REFR 0
REFRIOR 0
EMITTANCE 0
DIFFUSE 0

// Reflective white
MATERIAL 7
RGB .98 .98 .98
SPECEX 0
SPECRGB .95 .95 .95
REFL 1
REFR 0
REFRIOR 0
EMITTANCE 0
DIFFUSE 0

MATERIAL 8
RGB .98 .98 .98
SPECEX 0
SPECRGB .25 .85 .95
REFL 1
REFR 0
REFRIOR 0
EMITTANCE 0
DIFFUSE 0


MATERIAL 9
RGB .98 .98 .98
SPECEX 0
SPECRGB .25 .85 .95
REFL 1
REFR 0
REFRIOR 0
EMITTANCE 0
DIFFUSE 1

// Camera
CAMERA
RES 800 800
RES 1000 1000
FOVY 45
ITERATIONS 5000
DEPTH 8
Expand All @@ -64,7 +123,7 @@ UP 0 1 0
OBJECT 0
cube
material 0
TRANS 0 10 0
TRANS 2 10 1
ROTAT 0 0 0
SCALE 3 .3 3

Expand All @@ -74,44 +133,69 @@ cube
material 1
TRANS 0 0 0
ROTAT 0 0 0
SCALE 10 .01 10
SCALE 10 .01 30

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

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

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

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

// Sphere
// Ceiling light
OBJECT 6
sphere
material 4
TRANS -1 4 -1
cube
material 0
TRANS -2 10 1
ROTAT 0 0 0
SCALE 3 3 3
SCALE 3 .3 3

// Ceiling light
OBJECT 7
cube
material 0
TRANS -2 10 -5
ROTAT 0 0 0
SCALE 3 .3 3

// Ceiling light
OBJECT 8
cube
material 0
TRANS 2 10 -5
ROTAT 0 0 0
SCALE 3 .3 3

OBJECT 9
mesh
../scenes/diamond.obj
material 5
TRANS 0 0 0
ROTAT 0 0 0
SCALE 2 2 2

5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(SOURCE_FILES
"intersections.h"
"glslUtility.hpp"
"glslUtility.cpp"
"tiny_obj_loader.h"
"pathtrace.cu"
"pathtrace.h"
"scene.cpp"
Expand All @@ -15,9 +16,11 @@ set(SOURCE_FILES
"preview.cpp"
"utilities.cpp"
"utilities.h"
"KDtreeNode.h"
"KDtreeNode.cpp"
)

cuda_add_library(src
${SOURCE_FILES}
OPTIONS -arch=sm_20
OPTIONS -arch=sm_60
)
Loading