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 4: Zhan Xiong Chin #4

Open
wants to merge 14 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
108 changes: 98 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,108 @@
CUDA Rasterizer
===============

[CLICK ME FOR INSTRUCTION OF THIS PROJECT](./INSTRUCTION.md)

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 4**

* (TODO) YOUR NAME HERE
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
* Name: Zhan Xiong Chin
* Tested on: Windows 7 Professional, Intel(R) Xeon(R) CPU E5-1630 v4 @ 3.70 GHz 3.70 GHz, GTX 1070 8192MB (SIG Lab)

![](renders/intro.gif)

## Overview

GPU-based rasterizer. Features:

* Basic rendering of .gltf models
* UV texture mapping, with bilinear filtering and perspective correct texture coordinates
* Lambert shading
* Backface culling
* Toon shading

## Overview of pipeline

The rasterization pipeline consists of the following steps:

* Vertex shading
* Transforms world coordinates into window space coordinates
* Primitive assembly
* Creates triangles from vertices
* Backface culling
* Culls backward-facing triangles
* Rasterization
* For each triangle, calculates which pixels on screen it hits
* Uses bounding-box to speed up above calculation
* Do depth test to see if fragment is the closest to camera
* Calculate texture coordinates, etc. of point using barycentric coordinates
* Uses perspective correct texture coordinates
* Texture mapping
* Calculates color from texture coordinates
* Uses bilinear filtering to smooth out low-res textures
* Edge detection (toon shading only)
* Uses Sobel filter to find edges
* Optimized using shared memory
* Fragment shading
* Calculates color of each pixel using Lambert shading
* Quantizes colors (toon shading only)
* Uses results of Sobel filter to darken edges (toon shading only)

## Performance overview

Some render timings are given below, broken down by time spent in each stage of the pipeline. Rasterization tends to take the longest to complete, most likely because of the complex calculations as well as the locking required to do the depth test. There are some models where this is a serious problem, such as the Cesium milk truck render, where the rasterizer alone takes 200000 microseconds to complete, even when all other kernels have similar timings to the ones listed below. The models used for the below timings are shown in the introduction.

![](renders/timings.png)

## Features

### Backface culling

Backface culling was used (with thrust's remove_if) to reduce the number of triangles needed to render a model. For the Stanford dragon (shown below in toon shading), backface culling reduced the number of triangles from 100000 to 45704, speeding up the rendering as shown below.

![](renders/backface_culling_timing.png)

### Texture mapping

Texture mapping has two tweaks to the basic algorithm, bilinear interpolation and perspective correctness.

For perspective correctness, the texture coordinates are adjusted according to the depths of the fragments. The importance of this correction can be seen in the below chessboard renders. The above has the correction applied, whereas the second does not.

![](renders/chessboard_perspective_correct.png)

![](renders/chessboard_perspective_incorrect.png)

Bilinear filtering is also applied to smooth out textures, by interpolating between the 4 pixels from the texture a fragment borders. This results in a smoother texture, as can be seen below. Bilinear filtering is applied to the left texture, but not the right one.

<img src="renders/cesium_bilinear.png" style="width: 275px; height: 275px" />
<img src="renders/cesium_no_bilinear.png" style="width: 275px; height: 275px" />

Both perspective correctness and bilinear filtering complicate the texture mapping code, slowing down the texture mapping kernel by about 50% when rendering the image shown in the introduction. However, this kernel is not a significant percentage of the code, so this slowdown is negligible overall.

### Toon shading

Toon shading consists of two steps: color quantization and edge detection.

![](renders/dragon_celshaded.png)

_Stanford dragon, toon shading with borders_

Color quantization creates a small number of discrete shades, causing a "flat" look rather than a smooth gradient of colors.

![](renders/dragon_lambert_cel.png)

_Stanford dragon, toon shading (no borders)_

![](renders/dragon_lambert.png)

_Stanford dragon, Lambert shading. Note the smooth coloring on the scales compared to the "blotchy" toon shading_

Then, edge detection is performed using a [Sobel filter](https://en.wikipedia.org/wiki/Sobel_operator), and all edges are rendered black.

![](renders/dragon_outline.png)

### (TODO: Your README)
_Stanford dragon, Sobel filter outline_

*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 two are combined at the end to achieve the "toon" effect.


### Credits
The toon shader does decrease performance, especially since an additional kernel needs to be executed for the Sobel filter, which comprises approximately 25% of the rendering time. To optimize this, a version of the Sobel filter was written that calculated the convolution in a grid pattern using shared memory. This halved the time required for the Sobel filter step from about 1.8 milliseconds to 0.9 milliseconds.

* [tinygltfloader](https://github.com/syoyo/tinygltfloader) by [@soyoyo](https://github.com/syoyo)
* [glTF Sample Models](https://github.com/KhronosGroup/glTF/blob/master/sampleModels/README.md)
![](renders/toon_shading_timing.png)
Binary file added gltfs/dragon/dragon.glb
Binary file not shown.
Binary file added renders/backface_culling_timing.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 renders/cesium_bilinear.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 renders/cesium_no_bilinear.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 renders/chessboard_perspective_correct.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 renders/chessboard_perspective_incorrect.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 renders/dragon_celshaded.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 renders/dragon_lambert.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 renders/dragon_lambert_cel.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 renders/dragon_outline.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 renders/intro.gif
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 renders/milk_truck.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 renders/timings.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 renders/toon_shading_timing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading