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: Linshen Xiao #13

Open
wants to merge 9 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
105 changes: 100 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,111 @@ CUDA Rasterizer

**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)
* LINSHEN XIAO
* Tested on: Windows 10, Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 16.0GB, NVIDIA GeForce GTX 970M (Personal computer)

### (TODO: Your README)
## Overview

*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.
In this project, I used CUDA to implement a simplified rasterized graphics pipeline, similar to the OpenGL pipeline. I have implemented vertex shading, primitive assembly, rasterization, fragment shading, and a framebuffer.

## Features

* Basic Lambert and Blinn-Phong
* Line & Point rasterization
* Backface culling
* SSAA
* Other render style(painted style)
* Bilinear texture filtering
* Perspective correct texture coordinates

## Results

### Rotating Duck
![](renders/duck3.gif)
### Basic Lambert and Blinn shading

|Duck with texture and point light|+Blinn-Phong|+Ambient light|
|------|------|------|
|![](renders/2017-10-17_191933.png) | ![](renders/2017-10-17_191958.png) | ![](renders/duck.png) |

### Basic Normal and depth shading

|Duck with texture as color|Duck with normal as color|Duck with depth as color|
|------|------|------|
|![](renders/2017-10-17_191933.png) | ![](renders/2017-10-17_192434.png) | ![](renders/2017-10-17_192601.png) |

### Line and point rasterization

|Duck rasterized with triangles|Duck rasterized with lines|Duck rasterized with dots|
|------|------|------|
|![](renders/2017-10-17_191933.png) | ![](renders/2017-10-17_192335.png) | ![](renders/2017-10-17_192404.png) |

### Backface culling

|Duck rasterized with front faces|Duck rasterized with back faces|
|------|------|
|![](renders/2017-10-17_191933.png) | ![](renders/2017-10-17_195707.png) |

### SSAA
|No SSAA|SSAA*2|SSAA*4|
|------|------|------|
|![](renders/duck.png) | ![](renders/ssaa2.png) | ![](renders/ssaa4.png) |

Let's have a closer look:

|No SSAA|SSAA*2|SSAA*4|
|------|------|------|
|![](renders/ssaa0big.png) | ![](renders/ssaa2big.png) | ![](renders/ssaa4big.png) |

### Painted style render

|Original Duck|"Painted style" Duck|
|------|------|
|![](renders/duck.png) | ![](renders/2017-10-17_222749.png) |

### UV texture mapping with bilinear texture filtering and perspective correct texture coordinates

|Original Checkerboard|+Perspective correct texture coordinates|+Bilinear texture filtering|
|------|------|------|
| ![](renders/no_pc.png) | ![](renders/pc.png) | ![](renders/pc_&_bl.png) |

Let's have a closer look:

|No Bilinear texture filtering|+Bilinear texture filtering|+SSAA*2|
|------|------|------|
| ![](renders/nobfbig.png) | ![](renders/bfbig.png) | ![](renders/bfssaabig.png) |

## Performance Analysis

![](renders/Nobackface&backface.png)

| Pipeline | No Backface Culling | Backface Culling |
|----------------------------|---------------------|------------------|
| Rasterizer | 1716.314 | 1375.803 |
| Fragment Shader | 681.287 | 688.537 |
| Framebuffer | 103.308 | 103.252 |
| Primitive Assembly | 28.59 | 28.632 |
| Vertex Assembly and Shader | 11.014 | 11.004 |

Here we take the duck scene as an example. We can clearly see that rasterizer use most of the time, as there are lots of computation in this process, to check if the pixel of the bounding box lies inside the triangle, depth test and compute the interpolated texture coordinates and normal and so on. The render function in fragment shader also has some amount of computation for the computation for the color decided by normal, light direction and texture color.

We can see that backface culling did save time in rasterizer part. As the backface triangles are ingnored in the process, the performance did improved a bit.

![](renders/NoPC&PC.png)

| Pipeline | No bilinear texture filtering and perspective correctness | With bilinear texture filtering and perspective correctness |
|----------------------------|---------------------|------------------|
| Rasterizer | 1716.314 | 3049.726 |
| Fragment Shader | 681.287 | 717.102 |
| Framebuffer | 103.308 | 105.129 |
| Primitive Assembly | 28.59 | 28.857 |
| Vertex Assembly and Shader | 11.014 | 11.101 |

We can see that bilinear texture filtering and perspective correctness did take more time in rasterizer and fragment shader part, for the computation of perspective correct texture coordinates and averanging texture color for bilinear texture filtering did take more time than the orginal method.

### Credits

* [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)
* [Line drawing](https://github.com/ssloy/tinyrenderer/wiki/Lesson-1:-Bresenham%E2%80%99s-Line-Drawing-Algorithm)
* [Backface culling](https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/single-vs-double-sided-triangle-backface-culling)
Binary file added renders/2017-10-17_191933.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/2017-10-17_191958.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/2017-10-17_192025.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/2017-10-17_192335.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/2017-10-17_192404.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/2017-10-17_192434.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/2017-10-17_192601.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/2017-10-17_195707.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/2017-10-17_214044.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/2017-10-17_222749.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/NoPC&PC.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/Nobackface&backface.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/bfbig.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/bfssaabig.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/duck.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/duck2.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/duck3.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/no_pc.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/nobfbig.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/pc.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/pc_&_bl.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/pc_&_bl_&_ssaa.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/ssaa0big.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/ssaa2.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/ssaa2big.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/ssaa4.png
Binary file added renders/ssaa4big.png
16 changes: 13 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ void mainLoop() {
//---------RUNTIME STUFF---------
//-------------------------------
float scale = 1.0f;
float x_trans = 0.0f, y_trans = 0.0f, z_trans = -10.0f;
//for checkerboard
//float x_trans = 0.0f, y_trans = -0.5f, z_trans = -2.0f;
//float x_angle = 0.0f, y_angle = 30.0f;
//for duck
//float x_trans = 0.0f, y_trans = -0.8f, z_trans = -2.0f;
//float x_angle = 0.0f, y_angle = -0.8f;
float x_trans = 0.0f, y_trans = 0.0f, z_trans = -5.0f;
float x_angle = 0.0f, y_angle = 0.0f;
void runCuda() {
// Map OpenGL buffer object for writing from CUDA on a single GPU
Expand Down Expand Up @@ -395,6 +401,10 @@ void mouseMotionCallback(GLFWwindow* window, double xpos, double ypos)

void mouseWheelCallback(GLFWwindow* window, double xoffset, double yoffset)
{
const double s = 1.0; // sensitivity
z_trans += (float)(s * yoffset);
const double s = 0.1; // sensitivity
int temp = z_trans + (float)(s * yoffset);
if (temp < -0.5)
{
z_trans += (float)(s * yoffset);
}
}
Loading