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: Richard Lee #6

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
44 changes: 37 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
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)
* Richard Lee
* Tested on: Windows 7, i7-3720QM @ 2.60GHz 8GB, GT 650M 4GB (Personal Computer)

## Features

* Basic Rasterization Pipeline
* Per-Vertex Color Interpolation
* Backface Culling
* Texture Mapping with Bilinear Filtering and Perspective Correction

#### Basic Pipeline

The rasterizer implements the vertex shading, rasterization, and fragment shading stages of the graphics pipeline. It stores the glTF model as triangle primitives, which are rasterized and passed into the fragment shader buffer with a depth test before being shaded and rendered with a diffuse lighting scheme.

<img src="img/truck.png" width="400" height="400">


#### Per-Vertex Color and Texture Mapping

Both per-vertex color and texture mapping with perspective correction were implemented for the rasterizer. Performing a texture lookup for each fragment had a significant performance impact on the rasterization stage, which made sense because the per-vertex method was able to just perform a simple color interpolation between the vertices. However, there is also a significant improvement in quality, as seen in the comparison below. With large triangles containing a lot of texture information, such as the milk truck model, texture mapping must be used in order to obtain a sufficient amount of detail.

<img src="img/withoutTexture.png" width="180" height="180"> <img src="img/withTexture.png" width="180" height="180">

<img src="img/textureChart.png" width="534" height="350">

#### Bilinear Filtering

Bilinear texture filtering was also implemented for the texture mapping stage, by performing a horizontal and vertical linear interpolation on the UV coordinate. This had a small performance impact due to the additional texture lookups, but removed some aliasing on the textures.

<img src="img/withoutBilinear.png" width="180" height="180"> <img src="img/withBilinear.png" width="180" height="180">

<img src="img/bilinearChart.png" width="600" height="400">

#### Backface Culling

### (TODO: Your README)
Backface culling was implemented with stream compaction by removing any primitives before the rasterization stage which were facing away from the camera. This had a negligible effect on smaller models like the duck, but had a more noticeable improvement during the rasterization on larger models like the truck due to the reduced primitive pool. In addition, this stage removed any unenclosed faces which were facing away from the camera, as seen below.

*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.
<img src="img/withoutBackface.png" width="180" height="180"> <img src="img/withBackface.png" width="180" height="180">

<img src="img/backfaceDuckChart.png" width="600" height="400"> <img src="img/backfaceTruckChart.png" width="600" height="400">

### Credits

Expand Down
Binary file added img/backfaceDuckChart.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/backfaceTruckChart.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/bilinearChart.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/textureChart.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/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 img/withBackface.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/withBilinear.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/withTexture.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/withoutBackface.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/withoutBilinear.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/withoutTexture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ set(SOURCE_FILES

cuda_add_library(src
${SOURCE_FILES}
OPTIONS -arch=sm_20
OPTIONS -arch=sm_30
)
48 changes: 42 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@
#define TINYGLTF_LOADER_IMPLEMENTATION
#include <util/tiny_gltf_loader.h>

//-------------------------------
//-------------MAIN--------------
//-------------------------------
bool textureMapping = true, bilinearFiltering = false, backfaceCulling = false;
void printState(bool printTextures = true, bool printFiltering = true, bool printBackface = true) {
if (printTextures && printFiltering && printBackface)
printf("----- Settings -----\n\n");

if (printTextures)
printf("Texture Mapping: %s\n", textureMapping ? "On" : "Off");

if (printFiltering)
printf("Bilinear Filtering: %s\n", bilinearFiltering ? "On" : "Off");

if (printBackface)
printf("Backface Culling: %s\n", backfaceCulling ? "On" : "Off");

printf("\n--------------------\n");
printf("\n");
}

int main(int argc, char **argv) {
if (argc != 2) {
Expand Down Expand Up @@ -87,6 +101,10 @@ void mainLoop() {
// VAO, shader program, and texture already bound
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glfwSwapBuffers(window);

/*if (frame > 0) {
glfwSetWindowShouldClose(window, GL_TRUE);
}*/
}
glfwDestroyWindow(window);
glfwTerminate();
Expand All @@ -96,7 +114,7 @@ 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 = -1.5f, 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 All @@ -119,7 +137,7 @@ void runCuda() {
glm::mat4 MVP = P * MV;

cudaGLMapBufferObject((void **)&dptr, pbo);
rasterize(dptr, MVP, MV, MV_normal);
rasterize(dptr, MVP, MV, MV_normal, textureMapping, bilinearFiltering, backfaceCulling);
cudaGLUnmapBufferObject(pbo);

frame++;
Expand Down Expand Up @@ -188,6 +206,8 @@ bool init(const tinygltf::Scene & scene) {
glUseProgram(passthroughProgram);
glActiveTexture(GL_TEXTURE0);

printState();

return true;
}

Expand Down Expand Up @@ -327,6 +347,23 @@ void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
if (action == GLFW_PRESS) {
if (key == GLFW_KEY_T)
{
textureMapping = !textureMapping;
printState(true, false, false);
}
else if (key == GLFW_KEY_F)
{
bilinearFiltering = !bilinearFiltering;
printState(false, true, false);
}
else if (key == GLFW_KEY_B)
{
backfaceCulling = !backfaceCulling;
printState(false, false, true);
}
}
}

//----------------------------
Expand Down Expand Up @@ -358,7 +395,6 @@ void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
mouseState = TRANSLATE;
}

}
else if (action == GLFW_RELEASE)
{
Expand Down
Loading