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

Project4: Yi Guo #17

Open
wants to merge 5 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
124 changes: 118 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,128 @@ CUDA Rasterizer

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

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 4**
* Yi Guo
* Tested on: Windows 8.1, Intel(R) Core(TM)i5-4200M CPU @ 2.50GHz 8GB, NVIDIA GeForce 840M (Personal Notebook)

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

This is a rasterizer on GPU. The basic pipeline of vertex shader, primitive assembly, rasterization, fragment shader and frame buffer are implemented. Here are the images and performance analysis of the project.

### (TODO: Your README)
### ScreenShot

*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.
#### Blinning rasterization result

I slight adjust the blinning parameter to make the material some how metallic.
<table class="image">
<tr>
<td>duck.gltf (Blinn-Phong)</td>
<td>cow.gltf (Blinn-Phong)</td>
<td>CeisumMilkTruck.gltf (Blinn-Phong)</td>
</tr>
<tr>
<td><img src="renders/duck.gif"/></td>
<td><img src="renders/cow.gif"/></td>
<td><img src="renders/CesiumMilkTruck.gif"/></td>
</tr>
</table>


Here are the normal debug mode
<table class="image">
<tr>
<td>duckNormal.gltf </td>
<td>cowNormal.gltf</td>
<td>CeisumMilkTruckNormal.gltf</td>
</tr>
<tr>
<td><img src="renders/duckNormal.gif"/></td>
<td><img src="renders/cowNormal.gif"/></td>
<td><img src="renders/CesiumMilkTruckNormal.gif"/></td>
</tr>
</table>

## Performance Analysis

I use the cuda timer to record the time of each process of the rasterization. Here are the graphs.

![](./renders/TimeCost1.png)

From the graph above, we can notice that the rasterization system spends most time on rasterization and fragment shading. The time cost of vertex shading and primitive assembly is related to the number of triangles of the primitive. However, it is not fair to compare the time cost of rasterization and fragment shading of different models based on the number of triangles they have. Since for the rasterization process, each thread renders a single triangle, so the changes on the number of triangles may not cause great difference on the time cost. What really makes a difference here is the time that each thread spends on rendering a single triangle. The graph below can be a good argument.

![](./renders/TimeCost2.png)

The graph above shows the time cost of a singel model(here I use the CeisumMilkTruckNormal.gltf) with different z values. The z values here mean the distance between the model and camera. As it shows, the time cost of rasterization and fragment shading will increase dramaticlly when the model get closer and closer to the camera. The reason is when the distance between camera and model decreases, each triangle in the primitive will take up more grids on the screen than before and in each thread we have to scan a larger range to render a triangle.

There are some better solutions for rasterizing a triangle. For each triangle, instead of scanning all the pixels in the bounding box, we can computer the intersection points of each row and triangle's sides and only render the pixel between 2 intersection points. I may implement this algorithm in the future to optimize the rasterization process.

### Extra features

#### Backface Culling

I implment the Backface Culling for the rasterization system. Here is the comparison for the efficiency.

![](./renders/BackFace_Culling_Plot.png)

As the graph shows, backface culling may slightly increase the overall efficiency. BackFace culling fliters out the triangles we cannot see, but as we discussed above, the number of triangles is not the main factor of the time cost, thus it cannot cause great difference on the oveall time cost.

#### SSAA(Super Sample Anti-Aliasing)

Supersampling is used to remove aliasing (jagged and pixelated edges). It is achived by rendering the image at a higher resolution than the one being displayed, then shrinking it to the desired size, using the extra pixels for calculation. The color of a pixel in the desired size image equals to the average color values of the corresponding pixels in the higher resolution image. Since we render a image with higher resolution, the rasterize process will be slower.

<table class="image">
<tr>
<td>SSAA=1.gltf </td>
<td>SSAA=2.gltf</td>
<td>SSAA=4.gltf</td>
</tr>
<tr>
<td><img src="renders/SSAA1.png"/></td>
<td><img src="renders/SSAA2.png"/></td>
<td><img src="renders/SSAA4.png"/></td>
</tr>
</table>

#### Perspective Correction

<table class="image">
<tr>
<td>checkboard NoPerspective</td>
<td>checkboard Perspective</td>
</tr>
<tr>
<td><img src="renders/checkboardNoPerspective.gif"/></td>
<td><img src="renders/checkboardPerspective.gif"/></td>
</tr>
</table>

#### Bilinear Interpolation

<table class="image">
<tr>
<td>No Bilinear Interpolation</td>
<td>Bilinear Interpolation</td>
</tr>
<tr>
<td><img src="renders/Nobilinear.png"/></td>
<td><img src="renders/Bilinear.png"/></td>
</tr>
</table>

#### Different Render Mode(triangle, line ,point)

For the line rendering, I use the [Bresenham algorithm](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm)
<table class="image">
<tr>
<td>duck.gltf (Blinn-Phong)</td>
<td>cow.gltf (Blinn-Phong)</td>
<td>CeisumMilkTruck.gltf (Blinn-Phong)</td>
</tr>
<tr>
<td><img src="renders/duckDifferentMode.gif"/></td>
<td><img src="renders/cowDifferentMode.gif"/></td>
<td><img src="renders/CesiumMilkTruckDifferentMode.gif"/></td>
</tr>
</table>

### Credits

Expand Down
Binary file added renders/BackFace_Culling_Plot.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/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/CesiumMilkTruck.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/CesiumMilkTruckDifferentMode.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/CesiumMilkTruckNormal.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/Nobilinear.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/SSAA1.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/SSAA4.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/TimeCost1.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/TimeCost2.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/checkboardNoPerspective.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/checkboardPerspective.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/cow.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/cowDifferentMode.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/cowLine.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/cowNormal.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/duck.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/duckDifferentMode.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/duckNormal.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 26 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ void mainLoop() {
//---------RUNTIME STUFF---------
//-------------------------------
float scale = 1.0f;
float x_trans = 0.0f, y_trans = 0.0f, z_trans = -10.0f;
float x_trans = 0.0f, y_trans = -0.5f, z_trans = -6.0f;
float x_angle = 0.0f, y_angle = 0.0f;
float timecount[5] = { 0.f,0.f,0.f,0.f,0.f };
int loop = 0;
int renderMode = 0;//0: Triangle, 1: Line: 2: Point
void runCuda() {
// Map OpenGL buffer object for writing from CUDA on a single GPU
// No data is moved (Win & Linux). When mapped to CUDA, OpenGL should not use this buffer
Expand All @@ -120,11 +123,27 @@ void runCuda() {
glm::mat4 MVP = P * MV;

cudaGLMapBufferObject((void **)&dptr, pbo);
rasterize(dptr, MVP, MV, MV_normal);
rasterize(dptr, MVP, MV, MV_normal,renderMode,timecount);
cudaGLUnmapBufferObject(pbo);

frame++;
fpstracker++;
//y_angle += 0.01f;
/*loop++;
if (loop == 60) {
FILE *fp = fopen("time.txt", "a+");
float total = 0;
for (int i = 0; i < 5; i++) {
fprintf(fp, "%f ", timecount[i]);
total += timecount[i];
}
fprintf(fp, "%f ", total);

fprintf(fp, "\n");
fclose(fp);
printf("DONE\n");
exit(0);
}*/
}

//-------------------------------
Expand Down Expand Up @@ -328,6 +347,10 @@ void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
else if (key == GLFW_KEY_T && action==GLFW_RELEASE) {
renderMode++;
if (renderMode > 2) renderMode = 0;
}
}

//----------------------------
Expand Down Expand Up @@ -395,6 +418,6 @@ void mouseMotionCallback(GLFWwindow* window, double xpos, double ypos)

void mouseWheelCallback(GLFWwindow* window, double xoffset, double yoffset)
{
const double s = 1.0; // sensitivity
const double s = 0.2; // sensitivity
z_trans += (float)(s * yoffset);
}
Loading