Skip to content

Project 3: Kaixiang Miao #10

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

Open
wants to merge 10 commits into
base: master
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
180 changes: 174 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,181 @@
CUDA Path Tracer
================

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 3**
* Kaixiang Miao
* Tested on: Windows 7, i7-3630QM @ 2.40GHz 8GB, GTX 660M 2GB (Lenovo Y580 laptop, personal computer)

## Screenshot

Final image:
![](./img/a.png)

No depth of field:
![](./img/b.png)

Testing depth of field:
![](./img/d.png)

Mirror:

![](./img/f.png)

No direct lighting:
![](./img/e.png)



## Features

* **Ideal diffuse surface && perfectly specular-reflective**

Setting `pathSegment.ray.origin` to intersection point (adding an offset on the direction of the reflected ray) and calculating the reflected direction.

* **Stream compaction**

Using `thrust::remove_if` and `thrust::count_if`:

void compressedPathandIntersection(int& num_paths, PathSegment *paths, bool *flag)
{
thrust::device_ptr<bool> dev_ptrFlag(flag);
thrust::device_ptr<PathSegment> dev_ptrPaths(paths);
thrust::remove_if(dev_ptrPaths, dev_ptrPaths + num_paths, dev_ptrFlag, thrust::logical_not<bool>());
num_paths = thrust::count_if(dev_ptrFlag, dev_ptrFlag + num_paths, thrust::identity<bool>());
}


* **Sorted rays by material index**

Using `thrust::tuple` and `thrust::zip_iterator`:

typedef thrust::tuple<PathSegment, ShadeableIntersection> Tuple;
class cmp
{
public:
__host__ __device__ bool operator()(const Tuple &a, const Tuple &b)
{
return a.get<1>().materialId < b.get<1>().materialId;
}
};

void SortByMaterial(int num_paths, PathSegment *dev_paths, ShadeableIntersection *dev_intersections)
{
thrust::device_ptr<PathSegment> ptrPath(dev_paths);
thrust::device_ptr<ShadeableIntersection> ptrIntersection(dev_intersections);

typedef thrust::tuple<thrust::device_ptr<PathSegment>, thrust::device_ptr<ShadeableIntersection>> IteratorTuple;
typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
ZipIterator zip_begin = thrust::make_zip_iterator(thrust::make_tuple(ptrPath, ptrIntersection));
ZipIterator zip_end = zip_begin + num_paths;
thrust::sort(zip_begin, zip_end, cmp());
}

* **Caching the first bounce intersections**

if (iter == 1)
{
generateRayFromCamera <<<blocksPerGrid2d, blockSize2d >>>(cam, iter, traceDepth, dev_paths);
checkCUDAError("generate camera ray");
tracing
cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection));
dim3 numblocksPathSegmentTracing = (num_paths + blockSize1d - 1) / blockSize1d;
computeIntersections <<<numblocksPathSegmentTracing, blockSize1d>>> (
depth
, num_paths
, dev_paths
, dev_geoms
, hst_scene->geoms.size()
, dev_intersections
, dev_lightIndex
, lightCounter
);
checkCUDAError("trace one bounce");
cudaMemcpy(dev_cachePaths, dev_paths, num_paths * sizeof(PathSegment), cudaMemcpyDeviceToDevice);
cudaMemcpy(dev_cacheIntersections, dev_intersections, num_paths * sizeof(ShadeableIntersection), cudaMemcpyDeviceToDevice);
}

* **Frensel effect**

Using Schlick's approximation and russian roulette to combine refraction and reflection.

thrust::uniform_real_distribution<float> u01(0, 1);
if (u01(rng) < (n2 - n1) / (n2 + n1) * (n2 - n1) / (n2 + n1) + (1 - (n2 - n1) / (n2 + n1) * (n2 - n1) / (n2 + n1)) * pow(1 - cosTheta, 5))
{
pathSegment.ray.direction = -2 * glm::dot(pathSegment.ray.direction, realNormal) * realNormal + pathSegment.ray.direction;
}
else
{
pathSegment.ray.direction = glm::refract(pathSegment.ray.direction, realNormal, eta);
}

* **Depth of field**

Jittering the rays based on a focal plane:

if (depthOfField)
{
// depth of field
cam.focalDistance = 4.0f;

glm::vec3 pointOnFocalPlane = segment.ray.origin + cam.focalDistance * segment.ray.direction;
glm::vec3 pointOnScreen = segment.ray.origin + segment.ray.direction;
thrust::default_random_engine rngx = makeSeededRandomEngine(iter, x, 0);
thrust::default_random_engine rngy = makeSeededRandomEngine(iter, y, 0);

float jitterx = (u01(rngx) - 0.5) * 2 / 40.0f;
float jittery = (u01(rngy) - 0.5) * 2 / 40.0f;
pointOnScreen += cam.right * jitterx + cam.up * jittery;

segment.ray.origin = pointOnScreen;
segment.ray.direction = pointOnFocalPlane - pointOnScreen;
}

* **Direct lighting**

Sending a ray to the light source at the last bounce:

// direct lighting
if (pathSegment.remainingBounces == 1)
{
for (int i = 0; i < lightSize; i++)
{
Geom & geomLight = geoms[lightIndex[i]];

...
}
}

* **Motion blur**

Defining time for each ray and interpolating the position based on the time:

// update position of the geometry based on ray's time
glm::vec3 interpolatedPos = (1 - pathSegment.dTime) * geom.translationStart + pathSegment.dTime * geom.translationEnd;
geom.transform = cudaBuildTransformationMatrix(interpolatedPos, geom.rotation, geom.scale);
geom.inverseTransform = glm::inverse(geom.transform);
geom.invTranspose = glm::inverseTranspose(geom.transform);

## Performance Analysis

* **Block size comparison**

It seems that `blockSize = 128` works well on my machine.

![](./img/table2.jpg)

* **Stream compaction helps most after a few bounces. Print and plot the effects of stream compaction within a single iteration (i.e. the number of unterminated rays after each bounce) and evaluate the benefits you get from stream compaction.**

Using stream compaction, the number of paths decrease after each bounce.
![](./img/table1.jpg)

* **Compare scenes which are open (like the given cornell box) and closed (i.e. no light can escape the scene). Again, compare the performance effects of stream compaction! Remember, stream compaction only affects rays which terminate, so what might you expect?**

If the scene is open, rays may be terminated earlier than the closed scene because rays are likely to hit no object. In a closed scene, rays will bounce again and again until it reaches the maximum bounce times. Thus, stream compaction works efficiently in the open scene since lots of terminated rays can be removed after each bounce while it's wasteful in a closed scene since all the rays terminate in the last bounce (last while loop) and there's no need to do stream compaction after each bounce.






* (TODO) YOUR NAME HERE
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)

### (TODO: Your README)

*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.

Binary file added img/a.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/b.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/c.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/d.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/e.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/f.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/table1.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/table2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 39 additions & 3 deletions scenes/cornell.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ REFR 0
REFRIOR 0
EMITTANCE 0

// Glass white
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
Expand All @@ -56,6 +66,7 @@ ITERATIONS 5000
DEPTH 8
FILE cornell
EYE 0.0 5 10.5
//EYE 10.0 5 1.5
LOOKAT 0 5 0
UP 0 1 0

Expand Down Expand Up @@ -103,15 +114,40 @@ SCALE .01 10 10
// Right wall
OBJECT 5
cube
material 3
material 4
TRANS 5 5 0
ROTAT 0 0 0
SCALE .01 10 10

// Sphere
OBJECT 6
sphere
material 4
TRANS -1 4 -1
material 5
TRANS 2 6 -2
ROTAT 0 0 0
SCALE 3 3 3

// Sphere
OBJECT 7
sphere
material 5
TRANS 0 6 -1
ROTAT 0 0 0
SCALE 3 3 3

// Sphere
OBJECT 8
sphere
material 5
TRANS -3.5 4 3.5
ROTAT 0 0 0
SCALE 3 3 3

// Sphere
OBJECT 9
sphere
material 3
TRANS 0 2.5 0.5
TRANSEND 0.8 1.5 0.5
ROTAT 0 0 0
SCALE 3 3 3
Loading