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: Ricky Rajani #16

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3a01387
part one partially implemented, higher materials not working
Sep 21, 2017
09d4dc7
fixed small bugs but still not working
Sep 21, 2017
4d23241
works with fake and real shaders -- this is a bug
Sep 21, 2017
99f034e
refraction implemented but not tested
Sep 21, 2017
3eed3f8
fixed bug, part one working I think
Sep 22, 2017
facf7a1
add caching first bounce, contiguous memory for materials, and partia…
Sep 25, 2017
c2c2d4a
small fixes
Sep 25, 2017
2b25ab5
changes to dof, added motion blur and antialiasing
Sep 25, 2017
7908692
partially added code for mesh loading
Sep 26, 2017
2b20736
Update README.md
rickyrajani Sep 26, 2017
ccd6ca2
Update README.md
rickyrajani Sep 27, 2017
9f7cace
Update README.md
rickyrajani Sep 27, 2017
ca506a2
Update README.md
rickyrajani Sep 27, 2017
fbb18fa
removed useless code and added screenshots
Sep 27, 2017
4ad5041
Merge branch 'master' of https://github.com/rickyrajani/Project3-CUDA…
Sep 27, 2017
b7e7e6c
Update README.md
rickyrajani Sep 27, 2017
bae9fb8
graphs
Sep 27, 2017
1143e04
Merge branch 'master' of https://github.com/rickyrajani/Project3-CUDA…
Sep 27, 2017
07a00fc
graph fix
Sep 27, 2017
e173fa7
graph fix
Sep 27, 2017
1aaccc3
this is dumb
Sep 27, 2017
d54fada
ugh graphs
Sep 27, 2017
bf1a098
Update README.md
rickyrajani Sep 27, 2017
325b5fc
Update README.md
rickyrajani Sep 27, 2017
7a3edd2
refract screenshot
Sep 27, 2017
00908d7
Merge branch 'master' of https://github.com/rickyrajani/Project3-CUDA…
Sep 27, 2017
7b03826
Update README.md
rickyrajani Sep 27, 2017
def15bc
Update README.md
rickyrajani Sep 27, 2017
f352736
Update README.md
rickyrajani Sep 28, 2017
d4f8692
Update README.md
rickyrajani Oct 2, 2017
3d926d8
Update README.md
rickyrajani Oct 2, 2017
c744521
Update README.md
rickyrajani Oct 2, 2017
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
endif()

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

cuda_add_executable(${CMAKE_PROJECT_NAME}
Expand All @@ -82,7 +82,7 @@ cuda_add_executable(${CMAKE_PROJECT_NAME}

target_link_libraries(${CMAKE_PROJECT_NAME}
src
#stream_compaction # TODO: uncomment if using your stream compaction
stream_compaction # TODO: uncomment if using your stream compaction
${CORELIBS}
)

Expand Down
75 changes: 70 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,76 @@ 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)
* Ricky Rajani
* Tested on: Windows 7, i7-6700 @ 3.40GHz 16GB, NVIDIA Quadro K620 (Moore 100C Lab)

### (TODO: Your README)
This project implements a CUDA-based path tracer capable of rendering globally-illuminated images very quickly. Base code was provided; however, the core renderer was implemented by the author.

*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.
### Core Features:
- A shading kernel with BSDF evaluation for Ideal Diffuse surfaces [PBRT 8.3] and perfectly specular-reflective (mirrored) surfaces
- Path continuation/termination using Stream Compaction
- Path segments and intersections are contiguous in memory by material type before shading
- First bounce intersections are cached for re-use across all subsequent iterations

### Extra Features:
- Refraction [PBRT 8.2] with Frensel effects using Schlick's approximation
- Physically-based depth-of-field (by jittering rays within an aperture) [PBRT 6.2.3]
- Stochastic Sampled Antialiasing
- Motion blur through averaging samples at different times during the animation

*All features are easily toggleable

# Samples - 5000 iterations

Open scene with anti-aliasing

![](img/samples/open-basic.PNG)

Closed scene with anti-aliasing (only one light)

![](img/samples/closed-basic.PNG)

Open scene with depth-of-field

![](img/samples/dof2.PNG)

![](img/samples/dof.PNG)

Open scene with motion blur

![](img/samples/motion2.PNG)

Open scene with refraction (index of refraction is 1.5)

![](img/samples/refract2.png)

Open scene with reflection

![](img/samples/refract.PNG)

# Performance Analysis

![](img/optimizations-open.PNG)

![](img/optimizations-closed-graph.PNG)

![](img/active-paths-graph.PNG)

### Stream Compaction:

Stream compaction helps most after a few bounces. It reduces the number of active paths during an iteration. The plot above shows the reduction in paths for open and closed scenes during one iteration. The thrust implementation (remove-if) was used for stream compaction as it was significantly faster than the work-efficient compaction.

Using compaction reduces time spent on computing intersection of rays on object considerably. However, the drawback with stream compaction is time spent removing inactive paths. Thrust takes a significant amount of time with compaction which decreases its benefits.

Examining the data, it is also evident that stream compaction is beneficial only for open scenes as the number of paths terminating in a closed scene is significantly less than in an open scene.

### Caching First Bounce:

Caching the first bounce during an iteration reduces the time complexity significantly. However, this optimization can't be used under certain circumstances where the scene is not static, such as depth of field or motion blur.

### Sorting Path Segments and Intersections:

Sorting path segments and intersections seems to not be beneficial for the scene (cornell) used as there are not that many materials used. In this scenario, sorting actually increases the time complexity and is not an optimization. In the open scene, it is more efficient to remove inactive paths rather than making them contiguous in memory. In the closed scene, since memory doesn't decrease significantly, sorting will take longer. In both cases, there still are not enough materials in the scene to balance the cost of sorting.

## References
- [PBRT] Physically Based Rendering, Second Edition: From Theory to Implementation. Pharr, Matt and Humphreys, Greg. 2010.
Binary file added img/active-paths-graph.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/optimizations-closed-graph.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/optimizations-open.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/samples/closed-basic.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/samples/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/samples/dof2.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/samples/motion.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/samples/motion2.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/samples/open-basic.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/samples/refract.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/samples/refract2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 48 additions & 2 deletions scenes/cornell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ REFR 0
REFRIOR 0
EMITTANCE 0

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

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

// Camera
CAMERA
RES 800 800
Expand All @@ -59,7 +79,6 @@ EYE 0.0 5 10.5
LOOKAT 0 5 0
UP 0 1 0


// Ceiling light
OBJECT 0
cube
Expand All @@ -71,7 +90,7 @@ SCALE 3 .3 3
// Floor
OBJECT 1
cube
material 1
material 6
TRANS 0 0 0
ROTAT 0 0 0
SCALE 10 .01 10
Expand Down Expand Up @@ -115,3 +134,30 @@ material 4
TRANS -1 4 -1
ROTAT 0 0 0
SCALE 3 3 3
MOTION -1 1 0

// Cube
OBJECT 7
cube
material 6
TRANS 2 6 -1
ROTAT 45 45 0
SCALE 2.5 2 3
MOTION 1 -1 0

// Sphere
OBJECT 8
sphere
material 3
TRANS -3 2 2
ROTAT 0 0 0
SCALE 1 4 1
MOTION 1 1 0

// Floor light
OBJECT 9
cube
material 5
TRANS 0 0 0
ROTAT 0 0 0
SCALE 5 .3 4
40 changes: 37 additions & 3 deletions src/interactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,41 @@ void scatterRay(
glm::vec3 normal,
const Material &m,
thrust::default_random_engine &rng) {
// TODO: implement this.
// A basic implementation of pure-diffuse shading will just call the
// calculateRandomDirectionInHemisphere defined above.
// TODO: implement this.
// A basic implementation of pure-diffuse shading will just call the
// calculateRandomDirectionInHemisphere defined above.

if (m.hasReflective > 0) {
pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
pathSegment.color *= m.specular.color;
}
else if (m.hasRefractive > 0) {
// Schlick's Approximation implementation
thrust::uniform_real_distribution<float>urefract(0, 1);
float R_0 = std::pow((1.0f - m.indexOfRefraction) / (1.0f + m.indexOfRefraction), 2.0f);
float dot = fabs(glm::dot(normal, pathSegment.ray.direction)); // use fabs not abs for precise calculation
float fresnel = R_0 + (1.0f - R_0) * std::pow(1.0f - dot, 5.0f);

if (urefract(rng) < fresnel) {
pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
}
else {
float refractNum = 0.0f;
if (pathSegment.isRefract) {
refractNum = m.indexOfRefraction;
}
else {
refractNum = 1.0f / m.indexOfRefraction;
}
// Snell's law
pathSegment.ray.direction = glm::refract(pathSegment.ray.direction, normal, refractNum);
pathSegment.isRefract = urefract(rng) > 0.5;
}
}
else {
pathSegment.ray.direction = calculateRandomDirectionInHemisphere(normal, rng);
}

pathSegment.color *= m.color;
pathSegment.ray.origin = EPSILON * pathSegment.ray.direction + intersect;
}
Loading