Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d00b898
Added some code
Sep 25, 2019
e912b55
Fixed some bugs. Stream compaction fails
Sep 25, 2019
4afbda4
Some more code
Sep 25, 2019
cb04d6c
Dont know what is hapenning
Sep 26, 2019
dccde97
Working base code
Sep 26, 2019
cc672ac
Merge branch 'master' into clean_code_again
karvesaket Sep 26, 2019
fc730bd
Merge pull request #1 from karvesaket/clean_code_again
karvesaket Sep 26, 2019
fe841d0
Added refraction code
Sep 26, 2019
5c14eb1
Refraction and Anti-Aliasing
Sep 26, 2019
d42e3ba
Motion Blur
Sep 27, 2019
ce54ca4
Shared stream compaction
Sep 28, 2019
5706f30
Added some more files
Sep 28, 2019
9fe15b5
Added support for different objects
Sep 28, 2019
b7ad8c5
Update README.md
karvesaket Sep 29, 2019
ad00263
Update README.md
karvesaket Sep 29, 2019
b436b1f
Something is wrong
Sep 29, 2019
ee2eb6b
Added scenes
Sep 30, 2019
b2c5067
Add files via upload
karvesaket Sep 30, 2019
80dacde
Add files via upload
karvesaket Sep 30, 2019
724fe92
Add files via upload
karvesaket Sep 30, 2019
7363863
Update README.md
karvesaket Sep 30, 2019
228a34f
Update README.md
karvesaket Sep 30, 2019
b1ccb46
Update README.md
karvesaket Sep 30, 2019
ab9027f
Add files via upload
karvesaket Sep 30, 2019
076c2f2
Update README.md
karvesaket Sep 30, 2019
91c283f
Add files via upload
karvesaket Sep 30, 2019
6951489
Update README.md
karvesaket Sep 30, 2019
1e9e761
Add files via upload
karvesaket Sep 30, 2019
1e4923a
Update README.md
karvesaket Sep 30, 2019
0f76215
Update README.md
karvesaket Sep 30, 2019
971cfa9
Update README.md
karvesaket Sep 30, 2019
008c45f
Update README.md
karvesaket Sep 30, 2019
2e5dc03
Update README.md
karvesaket Sep 30, 2019
f2e4fb1
Final
Sep 30, 2019
503f210
Merge branch 'master' of https://github.com/karvesaket/Project3-CUDA-…
Sep 30, 2019
d6d2157
final
Sep 30, 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
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ list(SORT sources)
source_group(Headers FILES ${headers})
source_group(Sources FILES ${sources})

#add_subdirectory(stream_compaction) # TODO: uncomment if using your stream compaction
include_directories(.)
add_subdirectory(stream_compaction) # TODO: uncomment if using your stream compaction
add_subdirectory(tiny_obj) # TODO: uncomment if using your stream compaction


cuda_add_executable(${CMAKE_PROJECT_NAME} ${sources} ${headers})
target_link_libraries(${CMAKE_PROJECT_NAME}
${LIBRARIES}
#stream_compaction # TODO: uncomment if using your stream compaction
stream_compaction # TODO: uncomment if using your stream compaction
tiny_obj
)
166 changes: 161 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,167 @@ 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)
* Saket Karve
* [LinkedIn](https://www.linkedin.com/in/saket-karve-43930511b/), [twitter](), etc.
* Tested on: Windows 10 Education, Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz 16GB, NVIDIA Quadro P1000 @ 4GB (Moore 100B Lab)

### (TODO: Your README)
### Highlights

*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.
![](img/gold_cubes.png)

![](img/gold.png)

![](img/complex_without_cube_yellow.png)

![](img/cornell_penn.png)

### Features implemented

- Visual artefacts
- Shading different materials
- Ideal diffuse and shading
- Perfect Specular reflection
- Refraction with Fresnel effects and total internal reflection \[EXTRA CREDIT\]
- Stochastic Anti-aliasing \[EXTRA FEATURE\]
- Motion Blur \[EXTRA FEATURE\]
- Arbitrary mesh loading and rendering (OBJ) \[EXTRA FEATURE\]
- Performance improvements
- Path termination using Stream Compaction
- Cache first bounce
- Sort by materials
- Stream Compaction using shared memory \[EXTRA FEATURE\]

### Shading different materials

![](img/cornell_multiple.png)

#### Ideal Diffuse

When rays fall on an object with ideal diffuse, the ray is scattered randomnly sampled from a uniform distribution in any direction within the hemisphere centered at the point of incidence. This gives a matt finish to the object.

![](img/cornell_diffuse.png)

#### Perfect Specular reflection

When rays fall on an object with perfectly specular material, it always (with 100% probability) reflects at an angle equal to the incidence angle on the other side of the normal. This gives a shiny mirror like finish to the object. Reflectons of objects around can be seen. Since it does not allow any light to pass through, we can see a shadow on the side which is away from light.

![](img/cornell_perfect_reflection_1.png)

#### Refraction

When a ray falls on a refractive surface, it penetrates inside the object making an angle with the normal determined by the refractive index of the material the rays comes from and that of the object. This angle is determined by Snell's law.

A perfectly refractive object is rendered as below.

![](img/cornell_refr_15_without_fresnel.png)

However, objects are not perfectly refractive. Some proportion of the rays are reflected depending on the refractive indices of the materials and the angle of incidence of the ray. The reflectance coefficient (proportion of rays which refract) is calculated by Fresnel's law. Also, for rays coming from a material with higher refractive index to one with a lower refractive index, some rays reflect perfectly if the angle of incidence is more than a perticuar value (called critical angle). This is called total internal reflection.

The render of an object with refraction wil be as follows.

![](img/cornell_refraction_15.png)

Render of a sphere with refractive index = 2.41 (diamond)

![](img/cornell_refraction_241.png)

Render of sphere with 50% reflection and 50% chance of refraction (with Fresnel's effect)

![](img/cornell_half_refl_half_refr.png)

### Anti-Aliasing

Depending on the resolution, when images are rendered, the pixels show a stair-step like lines near the edges of objects. This can be reduced by approximating the pixel values around that pixel. This makes the edge more smooth. For thie path tracer, this is implemented by adding some random noise (jitter) to the pixel value when generating rays from the camera. This means, we shoot a ray to a point in an area around the actual pixel randomly sampling from a unifrom distribution. At every iteration, the ray strikes at a different point around the pixel (stochastically) and thus across iterations, creates an average effect at each pixel.

The render with and without antialiasing can be seen in the following figure. These renders are obtained after running the path tracer for 2000 iterations for both cases.

| Without Antialiasing | With visualization |
| ---------------------|---------------------- |
| ![](img/cornell_without_AA_2000.png) | ![](img/cornell_with_AA_2000.png) |

The zoomed in version of the above images are as follows. This shows how the render is jittery without antialiasing more clearly.

| Without Antialiasing | With Antialiasing |
| ---------------------|---------------------- |
| ![](img/without_AA_zoom.PNG) | ![](img/with_AA_zoom.PNG) |

### Motion Blur

Motion blur shows the effect of an object in motion which results when the image being recorded changes due to long exposure to the camera. The following renders are obtained for an object with ideal diffuse and a refractive object with motion blur.

![](img/cornell_modtion_blur_diffuse.png)

### Arbitrary mesh loading and rendering

Different objects are loaded from their OBJ files using the TinyObj library functions. Objects are loaded as a list of triangles represented by their vertices. To render these objects, the intersection of the ray is computed with all the triangles part of the object (mesh) to check if it intersects with the object. If it does, the closest point of intersection among all the triangles is determined.

Different objects were loaded in the scene.

![](img/droid.png)

### Path termination using stream compaction

When rays are either generated from the camera or bounced off some object do not hit any object in the scene the rays are terminated. This means these rays do not bounce further. Since our implementation of path tracing is per ray i.e. we launch threads per ray (rather than per pixel), the threads for such terminated rays are simply idling. So, if we compact the array of rays after every bounce we can save a lot of GPU resources by launching only as many thread as there are active rays.

The following graph shows the remaining active rays after certain number of iterations.

![](img/sc_rem_bounces.PNG)

Time taken to complete 1 iteration (Measured across 10 iterations)

- Without stream compaction = 0.0343 sec
- With stream compaction = 0.0735 sec

Stream compaction has a greater effect when the scene is open as opposed to cornell which is a closed scene. With an open scene, the time taken for 1000 iterations with stream compaction is,

- Closed scene = 0.0735 sec
- Open scene = 0.0303 sec


### Cache First Bounce

Since the first ray which is generated from the camera hits the same point, the first bounce will be the same for all iterations. Diffused materials scatter the ray in some random direction with some probability, but this caching does not cause any difference in the render even if we always use the same first bounce. So, I save the first bounce for each ray and load it directly from the cached version at each iteration.

Time taken to complete 1 iteration (Measured across 10 iterations)

- Without caching = 0.0761 sec
- With caching = 0.0741 sec

### Sort by materials

Sorting rays by the materials it hits allows the threads in a single warp to have more chances of hitting the same material. This reduces warp divergence and is thus expected to improve performance. The imporvement in performance is not visible for simple scenes with less objects. For complex scenes, the image rendering takes lesser time when sorted.

Time taken to complete 1 iteration (Measured across 10 iterations)

- Without Sorting = 0.0770 sec
- With Sorting = 0.214 sec

Since the scene had very few materials, sorting actually did not help as it should. However, if the scene is quite complex, then material sorting should make it much more efficient per iteration.

### Stream Compaction with shared memory

I implemented stream compaction using shared memory to improve stream compaction's performance. The stream compaction analysis shown above is done using Thrust's ```thrust::partition()``` function. The work efficient stream compaction implemented in the [previous project]() has a lot of global memory accesses for the up-sweep and down-sweep. This creates a bottleneck as global memory is slow to access and thus reduces efficiency. Using shared memory helps reduce the global memory access by loading required data to shared memory and then doing the required computations. However, since shared memory is limited and threads can not be synced across blocks, so this implementation is not trivial.

I implemented the stream compaction with shared memory in project 2's repository and it resulted in the following improvement in time for stream compaction.

![](img/sc_plot.PNG)

The above graph shows how shared memory takes lesser time with increasing array size for stream compaction.

When adding stream compaction with shared memory to the Path Tracer, I was getting some Out of Memory issues for images with 800 x 800 resolution. The path tracer works perfectly with lesser resolution of upto 400 x 400

### Bloopers

Some of the really interesting bloopers I got while implementing this project.

- When the random seed was not unique for every bounce of the ray, some wierd banding was observed.

![](img/blooper_banding_seed.png)

- When the index of refraction was reciprocal of what is should be.

![](img/blooper_donut_incorrect_ior.png)

- When a very small noise was added to the origin of the scattered ray.

![](img/blooper_full_white.png)
Binary file added img/India1.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/India_reflective.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/blooper_banding_seed.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/blooper_donut_incorrect_ior.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/blooper_full_white.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/complex_rotated_cube_white.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/complex_without_cube_white.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/complex_without_cube_yellow.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_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/cornell_half_refl_half_refr.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_modtion_blur_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/cornell_mption_blur_refr.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_multiple.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_penn.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_perfect_reflection_1.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_refr_15_without_fresnel.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_refraction_133.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_refraction_15.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_refraction_241.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_with_AA_2000.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_without_AA_2000.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/droid.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/gold.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/gold_cubes.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/sc_plot.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/sc_rem_bounces.PNG
Binary file added img/stream_compaction.PNG
Binary file added img/with_AA_zoom.PNG
Binary file added img/without_AA_zoom.PNG
20 changes: 20 additions & 0 deletions objects/BlackAndRedFloatingRobot.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Blender MTL File: 'BlackAndRedFloatingRobot.blend'
# Material Count: 2

newmtl Material.001
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.000000 0.000000 0.000000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2

newmtl Material.002
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 1.000000 0.028567 0.000000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
71 changes: 71 additions & 0 deletions objects/BrandenburgGate.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
newmtl Preview
Ns 50
Ka 0 0 0
Kd 0.925138 0.781762 0.639699
Ks 0.0915311 0.0604877 0.189959
newmtl 09_-_BronzeStatues
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd BronzeStatues1.jpg
newmtl 02_-_Marble
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd MarbleColumns2.jpg
newmtl 04_-_BronzeRoof
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd BronzeRoof1.jpg
newmtl 05_-_Fregio
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd Fregio1.jpg
newmtl 05_-_MarbleColumns
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd MarbleColumns2.jpg
newmtl 03_-_Wall
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd WhiteWall1.jpg
newmtl 08_-_DarkerWall
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd DarkerWall1.jpg
newmtl 01_-_Columns
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd Columns1.jpg
newmtl 06_-_SideGliph
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd SideGliph1.jpg
newmtl 01_-_TempleFront
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd TempleFront1.jpg
newmtl 07_-_SideBuilding
Ns 10
Ka 0 0 0
Kd 0.8 0.8 0.8
Ks 0 0 0
map_Kd WindowsBuilding1.jpg
46 changes: 46 additions & 0 deletions objects/Pikachu MTL.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Blender MTL File: 'Pikachu For Sketchfab.blend'
# Material Count: 4

newmtl Cheek
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.229953 0.000486 0.008264
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd C:\Blender EXE\Character\Pokedex3D_art_1.jpg

newmtl Eye,_Ear
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.000000 0.000000 0.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd C:\Blender EXE\Character\Pokedex3D_art_1.jpg

newmtl Eye_02
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd C:\Blender EXE\Character\Pokedex3D_art_1.jpg

newmtl Skin
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.717674 0.513855 0.002238
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd C:\Blender EXE\Character\Pokedex3D_art_1.jpg
20 changes: 20 additions & 0 deletions objects/android.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Blender MTL File: 'android.blend'
# Material Count: 2

newmtl android
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.296990 0.451769 0.032732
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2

newmtl oko
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.000000 0.000000 0.000000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
46 changes: 46 additions & 0 deletions objects/bb8.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 30.01.2016 20:42:29

newmtl wire_153228153
Ns 32
d 1
Tr 0
Tf 1 1 1
illum 2
Ka 0.6000 0.8941 0.6000
Kd 0.6000 0.8941 0.6000
Ks 0.3500 0.3500 0.3500

newmtl Material__61
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.7176 0.6902 0.6588
Kd 0.7176 0.6902 0.6588
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000

newmtl Material__62
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0275 0.0275 0.0275
Kd 0.0275 0.0275 0.0275
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000

newmtl wire_028149177
Ns 32
d 1
Tr 0
Tf 1 1 1
illum 2
Ka 0.1098 0.5843 0.6941
Kd 0.1098 0.5843 0.6941
Ks 0.3500 0.3500 0.3500
Loading