Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
e33a3e7
update gui
hazard-10 Sep 29, 2024
7ef7f3c
add basic light bounce logic
hazard-10 Sep 30, 2024
f5b44b3
set un-hit rays to black at last trace depth
hazard-10 Sep 30, 2024
f5d50d0
re-name shadeFakeMat to shadeMaterial
hazard-10 Sep 30, 2024
93bb708
Finished basic render w/o sorting. Fixed bugs. Add stream compaction.
hazard-10 Sep 30, 2024
3622c3f
add GUI for selecting obj files
hazard-10 Sep 30, 2024
c74be29
add tinyobj, adjust scene init logic
hazard-10 Sep 30, 2024
66c78b1
add obj triangles and material, can do basic rendering
hazard-10 Oct 1, 2024
fc7837e
merge two material vectors into one
hazard-10 Oct 1, 2024
3ef1a6b
add display room to obj files
hazard-10 Oct 1, 2024
ab00709
add display room to obj files
hazard-10 Oct 1, 2024
ca2c54c
organize GUI
hazard-10 Oct 1, 2024
c184749
Merge branch 'main' of https://github.com/hazard-10/Project3-CUDA-Pat…
hazard-10 Oct 1, 2024
0758571
add auto-centralize to obj
hazard-10 Oct 1, 2024
7dec589
use deltaTime instead of avg time for benchmark
hazard-10 Oct 1, 2024
3834c10
create bvh files
hazard-10 Oct 1, 2024
8a2709d
declare all bvh related structs
hazard-10 Oct 1, 2024
a88742c
update size_t to int to use -1
hazard-10 Oct 1, 2024
db88616
add BVH construction, with bugs
hazard-10 Oct 2, 2024
d8c3074
add bvh UI
hazard-10 Oct 2, 2024
97e2f5e
fixed bvh construction bug
hazard-10 Oct 2, 2024
24bf304
rename variable
hazard-10 Oct 2, 2024
f56c14f
add bvh iteration on cuda, with bug
hazard-10 Oct 2, 2024
6121a3c
fixed bvh iteration bug 1
hazard-10 Oct 2, 2024
a7df47e
change recursion to iterative method on Cuda BVH, Damn
hazard-10 Oct 2, 2024
512b8cd
fixed side disappearing issue, still remains a bvh bbox problem
hazard-10 Oct 2, 2024
288aa03
fixed bvh traversal bug.
hazard-10 Oct 2, 2024
0c6ee77
rename function since traversal is no longer recursive but iterative
hazard-10 Oct 2, 2024
b71db07
add basic bounding volume culling for obj
hazard-10 Oct 2, 2024
836bb95
update BVH value range
hazard-10 Oct 2, 2024
096725a
add support for choosing BVH constraint between depth or leaf size
hazard-10 Oct 2, 2024
61b85ea
fix centralize
hazard-10 Oct 2, 2024
f5f4c12
delete unused code, update default opt values
hazard-10 Oct 2, 2024
524603a
add perfect specular
hazard-10 Oct 2, 2024
53708e4
fix unexpected bug caused by opt value change
hazard-10 Oct 2, 2024
93c269d
add refraction
hazard-10 Oct 3, 2024
4d7d34d
add anti-aliasing
hazard-10 Oct 3, 2024
60869e9
fix UI and default display
hazard-10 Oct 3, 2024
da633a4
add sort by material
hazard-10 Oct 3, 2024
f17ec9a
polish ui and prompt
hazard-10 Oct 3, 2024
d9a311b
add ui part one
hazard-10 Oct 3, 2024
e270116
add readme
hazard-10 Oct 6, 2024
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: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ build
.LSOverride

# Icon must end with two \r
Icon
Icon

# Thumbnails
._*
Expand Down Expand Up @@ -189,7 +189,6 @@ install_manifest.txt
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
Expand Down Expand Up @@ -276,7 +275,6 @@ artifacts/
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
Expand Down
83 changes: 83 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"files.associations": {
"cstdlib": "cpp",
"filesystem": "cpp",
"vector": "cpp",
"xstring": "cpp",
"iostream": "cpp",
"algorithm": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"format": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"functional": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ranges": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"utility": "cpp",
"valarray": "cpp",
"xfacet": "cpp",
"xhash": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocbuf": "cpp",
"xlocinfo": "cpp",
"xlocmes": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp",
"xmemory": "cpp",
"xtr1common": "cpp",
"xtree": "cpp",
"xutility": "cpp"
}
}
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ set(headers
src/sceneStructs.h
src/preview.h
src/utilities.h
src/tiny_obj_loader.h
src/bvh.h
)

set(sources
Expand All @@ -74,6 +76,8 @@ set(sources
src/scene.cpp
src/preview.cpp
src/utilities.cpp
src/tiny_obj_loader.cc
src/bvh.cpp
)

set(imgui_headers
Expand Down
195 changes: 190 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,198 @@
![Cover](img/UI.png)

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)
* Jiahang Mao
* [LinkedIn](https://www.linkedin.com/in/jay-jiahang-m-b05608192/)
* Tested on: Windows 11, i5-13600kf @ 5.0GHz 64GB, RTX 4090 24GB, Personal Computer

## 1. Key feature implemented

Part 1
1. Cuda path tracing with lambertian and specular shading kernel
2. Unhit path termination using Stream Compaction
3. Sort ray-scene intersection by material type
4. Stochastic sampled antialiasing

Part 2
1. Obj mesh import.
2. BVH. Tree construction done on CPU, traversal with GPU. Configurable with either max depth or max leaf size.
3. Refraction. Support transmissive-only and glass-like material.

## 2. Build

Use C++ 17 or later instead of default C++ 14 with VS2022. Reason is because I added `<filesystem.h>` to support importing files using windows file system.

Change under property -> C/C++ -> Language -> C++ Language Standard

![How to](img/clang_update.png)


## 3. Navigating UI

I made some updates to the UI, building on top of the imgui.

![UI](img/UI.png)

#### Mesh Options
* `Centralize mesh` When loading loading arbitrary obj mesh, this option automatically compute the center and scale, then apply a transformation to the center of camera's front pov.
* `Mesh transformation` Post-import transformation also customizable.
#### Acceleration Structure
A single choice between three options
* `None` No accleration structure will be applied. For each render pass the intersection checks each ray against all geometry in the scene.
* `Basic Bounding Volume Culling` Conceptually equivalent to a depth-equals-to-one BVH. Construct a single bounding box surrounding the mesh.
* `Bounding Volume Hierarchy` Construct a tree-like structure to organize the geometry. The tree is built on CPU and traversed on GPU.
![UI](img/bvh_ui.png)
* `Bins to Split Per Axis` Controls the number of bins used when determining the best split plane during BVH construction.
* `BVH Constraint` Choose between using a maximum leaf size or maximum depth as the stopping criterion for BVH construction.
* `Max Leaf Size` The maximum number of primitives allowed in a leaf node.
* `Max Depth` The maximum depth allowed for the BVH tree.
#### Render Options
* `Use Path Tracing` Toggle between path tracing and basic shading. Latter suited for quick view of geometry.
* `Sort by Material` Enable/disable sorting of ray-scene intersections by material type.
#### Loading file
* Support either .json or .obj file. When importing an obj file, automatically load a display room scene under ./scenes/display_room.json

![UI](img/file_UI.png)
#### Program output
* Command prompt will have useful info about the mesh and bvh for debugging.
```console
--- Mesh Info ---
Loaded 5664 vertices, 11272 faces, and 1 face materials.
Auto-centering object...
Geometric Center: vec3(0.108734, 0.431662, 0.382079)
Average Distance to center : 1.03667
--- BVH Info ---
Building BVH...
Validating BVH...
BVH is valid. Max depth: 50, largest leaf size: 69
```
## 4. Performance benchmark

#### 4.1 Stream Compaction, on open and close scene


A sample output of number of rays after stream compaction after each iteration. Tested on scenes/cornell_refraction.json

Results 1:
Tested with cornell_refraction.json there are a total of 7 materials and 9 geometries, all of them are either box or spheres.

![SC1](img/sc_bar.png)
```
num_paths: 588297 at depth 1 of iter18
num_paths: 481950 at depth 2 of iter18
num_paths: 398397 at depth 3 of iter18
num_paths: 326704 at depth 4 of iter18
num_paths: 266102 at depth 5 of iter18
num_paths: 217403 at depth 6 of iter18
num_paths: 178307 at depth 7 of iter18
num_paths: 0 at depth 8 of iter18
num_paths: 588302 at depth 1 of iter19
num_paths: 482180 at depth 2 of iter19
num_paths: 397826 at depth 3 of iter19
num_paths: 326109 at depth 4 of iter19
num_paths: 265074 at depth 5 of iter19
num_paths: 216725 at depth 6 of iter19
num_paths: 177920 at depth 7 of iter19
num_paths: 0 at depth 8 of iter19
num_paths: 588302 at depth 1 of iter20
num_paths: 482791 at depth 2 of iter20
num_paths: 398975 at depth 3 of iter20
num_paths: 327015 at depth 4 of iter20
num_paths: 266191 at depth 5 of iter20
num_paths: 217700 at depth 6 of iter20
num_paths: 178610 at depth 7 of iter20
num_paths: 0 at depth 8 of iter20
```

Results 2:
Tested with cornell_refraction_close.json. Comparing to cornell_refraction.json, it has one more cube at back of the camera so light can't escape the room.

![SC1](img/sc_bar_close.png)
```
num_paths: 588103 at depth 1 of iter18
num_paths: 552057 at depth 2 of iter18
num_paths: 522357 at depth 3 of iter18
num_paths: 493064 at depth 4 of iter18
num_paths: 464186 at depth 5 of iter18
num_paths: 437812 at depth 6 of iter18
num_paths: 413273 at depth 7 of iter18
num_paths: 0 at depth 8 of iter18
num_paths: 588112 at depth 1 of iter19
num_paths: 551931 at depth 2 of iter19
num_paths: 522363 at depth 3 of iter19
num_paths: 492947 at depth 4 of iter19
num_paths: 464198 at depth 5 of iter19
num_paths: 437899 at depth 6 of iter19
num_paths: 413139 at depth 7 of iter19
num_paths: 0 at depth 8 of iter19
num_paths: 588110 at depth 1 of iter20
num_paths: 552150 at depth 2 of iter20
num_paths: 522766 at depth 3 of iter20
num_paths: 493317 at depth 4 of iter20
num_paths: 464677 at depth 5 of iter20
num_paths: 438364 at depth 6 of iter20
num_paths: 413396 at depth 7 of iter20
num_paths: 0 at depth 8 of iter20
```

The average render time is higher with close room scene. Since there is a lot less light terminated during rendering, more kernels need to be launched at each render pass, causing higher render time.

### BVH Reults

BVH is constructed on CPU, then ray-scene intersection kernel iteratively looks for closest hit along the BVH Tree. BVH accept three arguments: number of bins to split per axis ( when constructing BVH), max leaf size acceptable, and max depth acceptable.

*Test scene used is cow.obj, contains 2903 vertices, 5804 faces, and 1 diffuse material*

*Config 0 is using a single volume containing the whole mesh.*

#### Using max leaf count as bvh constraint

```
Max leaf count is set, max depth is generated on the fly.
config 0: Single volume
config 1: Bins per axis: 32, Max depth: 33, largest leaf size: 10
config 2: Bins per axis: 32, Max depth: 22, largest leaf size: 27
config 3: Bins per axis: 32, Max depth: 11, largest leaf size: 63
config 4: Bins per axis: 32, Max depth: 10, largest leaf size: 120
config 5: Bins per axis: 32, Max depth: 8, largest leaf size: 229
```
![maxleaf](img/max_leaf.png)

#### Using max depth as bvh constraint
```
Max depth is set, max leaf count is generated on the fly.
config 0: Single volume, 2995
config 1: Bins per axis: 32, Max depth: 4, largest leaf size: 1253
config 2: Bins per axis: 32, Max depth: 10, largest leaf size: 102
config 3: Bins per axis: 32, Max depth: 20, largest leaf size: 30
config 4: Bins per axis: 32, Max depth: 38, largest leaf size: 10
config 5: Bins per axis: 32, Max depth: 100, largest leaf size: 162
```
![maxdepth](img/maxdepth.png)

#### BVH Conclusion
The BVH performance varies a lot given different tree construction configuration. Overall the
best BVH result is slight worse than adding a simple bounding volume culling. I think that BVH will show improvement over vanilla bounding volume when we use a much denser mesh with 10k or more vertices in it.

### Sorting by material type

Another option is to sort by material type during each render pass so that each block will have similar performance. Here is the result. Tested with cornell_refraction scene.
```
No sort: Not soring by material type.
One partition: Only group one type of material. Used for performance comparison
Full Partition: All materials are grouped by type. Using thrust::parition
Stable Partition: All materials are grouped by type. Using thrust::stable_parition
Full sort: All materials types are sorted thrust::stable_sort
```
![matsort](img/matsort.png)

Turning on material sort hinders performance. I expect to that scenes with many more material types will see noticeable benefit from using this option.

### (TODO: Your README)
### 3rd-party code used

*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.
* https://github.com/tinyobjloader/tinyobjloader/

Binary file added img/UI.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/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/basic_obj.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/bvh_ui.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/clang_update.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_cow.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/cover.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/file_UI.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/matsort.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/max_leaf.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/maxdepth.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/refraction.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_bar.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_bar_close.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions obj_files/box/issue-177.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
newmtl white
Ka 0 0 0
Kd 1 1 1
Ks 0 0 0

newmtl red
Ka 0 0 0
Kd 1 0 0
Ks 0 0 0

newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0

newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0

newmtl light
Ka 20 20 20
Kd 1 1 1
Ks 0 0 0
31 changes: 31 additions & 0 deletions obj_files/box/issue-177.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mtllib issue-177.mtl

v 0.000000 2.000000 2.000000
v 0.000000 0.000000 2.000000
v 2.000000 0.000000 2.000000
v 2.000000 2.000000 2.000000
v 0.000000 2.000000 0.000000
v 0.000000 0.000000 0.000000
v 2.000000 0.000000 0.000000
v 2.000000 2.000000 0.000000
# 8 vertices

g front cube
usemtl white
f 1 2 3 4
g back cube
# expects white material
f 8 7 6 5
g right cube
usemtl red
f 4 3 7 8
g top cube
usemtl white
f 5 1 4 8
g left cube
usemtl green
f 5 6 2 1
g bottom cube
usemtl white
f 2 6 7 3
# 6 elements
Loading