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: Eyad Almoamen #23

Open
wants to merge 11 commits into
base: main
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
76 changes: 71 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,77 @@ 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)
* Eyad Almoamen
* [LinkedIn](https://www.linkedin.com/in/eyadalmoamen/), [personal website](https://eyadnabeel.com)
* Tested on: Windows 11, i7-10750H CPU @ 2.60GHz 2.59 GHz 16GB, RTX 2070 Super Max-Q Design 8GB (Personal Computer)

### (TODO: Your README)
Introduction
================
I've built a GPU accelerated monte carlo path tracer using CUDA and C++. The parallelization is happening on a ray-by-ray basis, with the terminated rays being eliminated via stream compaction and sorted by material type in order to avoid warp divergence. The path tracer takes in a scene description .txt file and outputs a rendered image.

Features implemented include:

* [Specular Reflective Material](#specular-reflective-material)
* [Refractive Material](#refractive-material)
* [Thin Lens Model DOF](#thin-lens-model-dof)
* [Motion Blur](#motion-blur)
* [Stochastic Antialiasing](#stochastic-antialiasing)
* [Direct Lighting](#direct-lighting)

#Specular Reflective Material
================
The specular reflective material either reflects light perfectly (incident angle == exitent angle), or diffusely, the rate of each is manually set and the two percentages sum up to 100% (for example, if the material was 63% specular, it'd have to be 37% diffuse):

<img align="center" src="img/cornell.2022-10-11_03-01-03z.11379samp.png" width=50% height=50%>

#Refractive Material
================
The specular refractive material either reflects light or transmits it according to [Snell's Law](https://en.wikipedia.org/wiki/Snell%27s_law), the rate of each is based on the material type and index of refration. This is usually calculated by the [Fresnel Equations](https://en.wikipedia.org/wiki/Fresnel_equations), however, here I use the [Schlick approximation](https://en.wikipedia.org/wiki/Schlick%27s_approximation) to calculate the rates as it's more computationally efficient with a very low error rate:

<img align="center" src="img/cornell.2022-10-11_02-20-06z.5201samp.png" width=50% height=50%>

<img align="center" src="img/cornell.2022-10-11_00-50-38z.5598samp.png" width=50% height=50%>

#Thin Lens Model DOF
================
I utilized the [Thin Lens Model](https://pbr-book.org/3ed-2018/Camera_Models/Projective_Camera_Models#TheThinLensModelandDepthofField) in order to replace the pinhole camera we have with a more realistic virtual lens which allows me to introduce depth of field effects and bokeh:

| Focal Distance | 0 | 3 | 8.5 | 20.5 |
| :------- | :-------: | :-------: | :-------: | :-------: |
| Iterations | 7759 | 5082 | 5142 | 5009 |
| Scene | <img src="img/cornell.2022-10-11_02-43-13z.7759samp.png"> | <img src="img/cornell.2022-10-11_01-23-17z.5082samp.png"> | <img src="img/cornell.2022-10-10_23-09-12z.5142samp.png"> | <img src="img/cornell.2022-10-11_01-07-49z.5009samp.png"> |

#Motion Blur
================
I added a velocity component to the geometry struct and that allows me to render the image in such a way that it seems the object is moving in the direction of the velocity:

#Stochastic Antialiasing
================
I added support for stochastic antialiasing by jittering the ray produced from the camera randomly within the range of a pixel length:

| Antialiasing | Without | With |
| :------- | :-------: | :-------: |
| Scene | <img src="img/cornell.2022-10-11_03-38-02z.1000samp.png"> | <img src="img/cornell.2022-10-11_03-40-14z.1000samp.png"> |
| Scene | <img src="img/cornell.2022-10-11_03-54-58z.1000samp.png"> | <img src="img/cornell.2022-10-11_03-53-19z.1000samp.png"> |

#Direct Lighting
================
To optimize the result and speed up the convergence of the image, I had the pathtracer trace its last ray to a light source in the scene, guaranteeing that we get light contribution. To demonstrate, I've rendered the same scene up to 1000 iterations with and without direct lighting:

| Direct Lighting | Without | With |
| :------- | :-------: | :-------: |
| Scene | <img src="img/"> | <img src="img/"> |

Performance Testing
================
I ran a few tests to see the effect of some of the optimizations I've performed on this path tracer:

The effect of caching is very much evident and it increases as the size of the image increases:

<img align="center" src="img/cachingchart.png" width=50% height=50%>

This is because we're precomputing a potentially very large computation, sparing ourselves the trouble for upcoming iterations

*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.
The effect of material sorting doesn't seem to be too encouraging; initially I tried testing it on a scene with one material, it wasn't an improvement (since we'd be sorting to avoid nonexistent warp divergence). However I switched to a scene with diffuse, reflective, and refractive material to no avail:

<img align="center" src="img/materialsortchart.png" width=50% height=50%>
Binary file added img/Screenshot 2022-10-02 014924.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/aa.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/cachingchart.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/coolmirror.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.2022-10-10_04-17-01z.10000samp.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.2022-10-10_04-28-41z.10000samp.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.2022-10-10_04-40-08z.10000samp.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.2022-10-10_04-55-29z.10000samp.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.2022-10-10_05-07-44z.20000samp.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.2022-10-10_05-16-01z.1000samp.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.2022-10-10_05-17-31z.10000samp.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.2022-10-10_05-30-40z.50000samp.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.2022-10-10_23-09-12z.5142samp.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.2022-10-10_23-39-57z.1327samp.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.2022-10-11_00-50-38z.5597samp.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.2022-10-11_00-50-38z.5598samp.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.2022-10-11_01-07-49z.5009samp.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.2022-10-11_01-23-17z.5082samp.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.2022-10-11_02-20-06z.5170samp.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.2022-10-11_02-20-06z.5201samp.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.2022-10-11_02-43-13z.7754samp.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.2022-10-11_02-43-13z.7759samp.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.2022-10-11_03-01-03z.11378samp.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.2022-10-11_03-01-03z.11379samp.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.2022-10-11_03-38-02z.1000samp.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.2022-10-11_03-38-02z.997samp.png
Binary file added img/cornell.2022-10-11_03-38-02z.999samp.png
Binary file added img/cornell.2022-10-11_03-40-14z.1000samp.png
Binary file added img/cornell.2022-10-11_03-53-19z.1000samp.png
Binary file added img/cornell.2022-10-11_03-54-58z.1000samp.png
Binary file added img/cornell.2022-10-11_03-58-36z.1000samp.png
Binary file added img/materialsortchart.png
Binary file added img/materialsorttest.png
Binary file added img/noaa.png
Binary file added img/thinLens8_aa.png
Binary file added img/thinlens2.png
Binary file added img/thinlens20.png
Binary file added img/thinlens8.png
Binary file added img/thinlens8comp.png
166 changes: 166 additions & 0 deletions scenes/aatest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Red Light
MATERIAL 0
RGB 1 1 1
SPECEX 0
SPECRGB 0 0 0
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 10

// Blue Light
MATERIAL 1
RGB 0.3 0.3 0.7
SPECEX 0
SPECRGB 0 0 0
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 10

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

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

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

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

// Refractive Material
MATERIAL 6
RGB .98 .98 .98
SPECEX 0
SPECRGB .98 .98 .98
REFL 0
REFR 1
REFRIOR 0
EMITTANCE 0

// White Light
MATERIAL 7
RGB 1 1 1
SPECEX 0
SPECRGB 0 0 0
REFL 0
REFR 0
REFRIOR 0
EMITTANCE 1


// Camera
CAMERA
RES 1080 1080
FOVY 45
ITERATIONS 1000
DEPTH 32
LENSR 0
FOCALD 7.5
FILE cornell
EYE 0.0 7 10.5
LOOKAT 0 7 0
UP 0 1 0


// Ceiling light
OBJECT 0
cube
material 0
TRANS 0 25 2
ROTAT 0 0 0
SCALE 4 0.1 4
VELOCITY 0 0 0

// Floor
OBJECT 1
cube
material 2
TRANS 0 0 0
ROTAT 0 0 0
SCALE 100 .01 100
VELOCITY 0 0 0

// Back wall
OBJECT 2
cube
material 3
TRANS -5 3.75 -5
ROTAT 0 90 0
SCALE .01 7.5 10
VELOCITY 0 0 0

// Back wall
OBJECT 3
cube
material 4
TRANS 5 3.75 -5
ROTAT 0 90 0
SCALE .01 7.5 10
VELOCITY 0 0 0

// Back wall
OBJECT 4
cube
material 4
TRANS -5 11.25 -5
ROTAT 0 90 0
SCALE .01 7.5 10
VELOCITY 0 0 0

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

// Back wall
OBJECT 6
cube
material 3
TRANS -5 18.75 -5
ROTAT 0 90 0
SCALE .01 7.5 10
VELOCITY 0 0 0

// Back wall
OBJECT 7
cube
material 4
TRANS 5 18.75 -5
ROTAT 0 90 0
SCALE .01 7.5 10
VELOCITY 0 0 0
31 changes: 25 additions & 6 deletions scenes/cornell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,24 @@ REFR 0
REFRIOR 0
EMITTANCE 0

// Refractive Material
MATERIAL 5
RGB .98 .98 .98
SPECEX 0
SPECRGB .98 .98 .98
REFL 0
REFR 1
REFRIOR 0
EMITTANCE 0

// Camera
CAMERA
RES 800 800
RES 512 512
FOVY 45
ITERATIONS 5000
DEPTH 8
LENSR 0.2
FOCALD 8
FILE cornell
EYE 0.0 5 10.5
LOOKAT 0 5 0
Expand All @@ -67,14 +79,16 @@ material 0
TRANS 0 10 0
ROTAT 0 0 0
SCALE 3 .3 3
VELOCITY 0 0 0

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

// Ceiling
OBJECT 2
Expand All @@ -83,6 +97,7 @@ material 1
TRANS 0 10 0
ROTAT 0 0 90
SCALE .01 10 10
VELOCITY 0 0 0

// Back wall
OBJECT 3
Expand All @@ -91,27 +106,31 @@ material 1
TRANS 0 5 -5
ROTAT 0 90 0
SCALE .01 10 10
VELOCITY 0 0 0

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

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

// Sphere
OBJECT 6
sphere
material 4
TRANS -1 4 -1
material 5
TRANS 0 5 0
ROTAT 0 0 0
SCALE 3 3 3
VELOCITY 0 0 0
8 changes: 8 additions & 0 deletions scenes/defaultsettings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Definitions for features
#define DEBUG 0
#define BOUNCES 4
#define MATERIALSORT 1
#define CACHING 0
#define ANTIALIASING 1
#define THINLENS 1
#define MOTIONBLUR 1
Loading