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 Xiang Deng #16

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
33311a9
need debug
dengxianga Oct 23, 2016
5dbe946
bug fix
dengxianga Oct 23, 2016
9335821
remove texture rendering, default red colors, simply do shading wrt l…
dengxianga Oct 23, 2016
92f92c6
line bug fixed
dengxianga Oct 23, 2016
79d4c4f
add texture opt
dengxianga Oct 23, 2016
b07b139
texture mapping ok
dengxianga Oct 23, 2016
812ab41
fix toggle
dengxianga Oct 23, 2016
d4d5279
binlinear filtering and reference
dengxianga Oct 23, 2016
e6ee75e
perspective correction and change default color to white
dengxianga Oct 24, 2016
a6925d8
first gif
dengxianga Oct 24, 2016
abbc166
duck
dengxianga Oct 24, 2016
cdb98ed
o
dengxianga Oct 24, 2016
f0f720a
lol
dengxianga Oct 24, 2016
e3b8166
fix bug
dengxianga Oct 24, 2016
049427c
readme
dengxianga Oct 24, 2016
6a63693
a
dengxianga Oct 24, 2016
f84e323
fix the crazy bug
dengxianga Oct 24, 2016
ba2b1e8
buggy yeah
dengxianga Oct 24, 2016
39e854e
d
dengxianga Oct 24, 2016
8f3bf80
vc
dengxianga Oct 24, 2016
84d7fc2
ps
dengxianga Oct 24, 2016
a9bb925
lines
dengxianga Oct 28, 2016
ebe2f34
lines gif
dengxianga Oct 28, 2016
3c455e3
points
dengxianga Oct 28, 2016
72b9f3c
r
dengxianga Oct 28, 2016
42604ad
sparse dense points
dengxianga Oct 28, 2016
28fcb11
more gif
dengxianga Oct 28, 2016
57cb9fb
chang order
dengxianga Oct 28, 2016
28d7151
readme
dengxianga Oct 28, 2016
6fe9b06
checkerboard
dengxianga Oct 28, 2016
393d0b9
checker bilinear
dengxianga Oct 28, 2016
f3f8e94
readme
dengxianga Oct 28, 2016
b7146db
analysis
dengxianga Oct 29, 2016
2c4f38a
ana
dengxianga Oct 29, 2016
170bca1
update img
dengxianga Oct 29, 2016
65644d2
analysis
dengxianga Oct 29, 2016
8c30045
more
dengxianga Oct 29, 2016
e66fe58
more
dengxianga Oct 29, 2016
8614090
more
dengxianga Oct 29, 2016
809f2eb
update readme
dengxianga Oct 29, 2016
5b4fdd7
readme
dengxianga Oct 29, 2016
375b231
push
dengxianga Oct 29, 2016
3a8c943
a
dengxianga Oct 29, 2016
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
102 changes: 95 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,106 @@
CUDA Rasterizer
===============

[CLICK ME FOR INSTRUCTION OF THIS PROJECT](./INSTRUCTION.md)
**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 4**

**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 4**
* Xiang Deng
* Tested on: Windows 10-Home, i7-6700U @ 2.6GHz 16GB, GTX 1060 6GB (Personal Computer)

* (TODO) YOUR NAME HERE
* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
* Overview
In this project we use CUDA to implement the raterizerized graphics pipeline. Major features
include vertex shading, primitive assembly, raterization and fragment shading. Here is a list of features:

### (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.
**Features:**

* Basic pipeline
* Vertex assembly and vertex shader
* Primitive assembly
* Rasterization
* Depth test
* Race avoidance with atomic
* Fragment shader (with lambert lighting)
* Fragment-to-depth-buffer writing (with atomics for race avoidance).
* A depth buffer for storing and depth testing fragments.
* Interpolation near primitive points
* Toon Shading
* UV texture mapping with bilinear texture filtering and perspective correct texture coordinates
* Support for rasterizing additional primitives: lines and points

Milk truck | Duck
:-------------------------:|:-------------------------:
![](imgs/milk1.gif) | ![](imgs/duck1.gif)

Demo of basic raterization pipeline, we used lambert lighting for the fragement shader.

VC
![](imgs/VC1.gif)

Cow with Lines | VC with Lines |Truck with Lines
:-------------------------:|:-------------------------: |:-------------------------:
![](imgs/cow2.gif) | ![](imgs/VC2.gif) |![](imgs/truck2.gif)

Here is a demo of rasterizing lines.



Duck with Points (dense) | Duck with Points (sparse) |Truck with Points (sparse)
:-------------------------:|:-------------------------: |:-------------------------:
![](imgs/duck3.gif) | ![](imgs/duck4.gif) | ![](imgs/truck3.gif)

Here is a demo of rasterizing points;
Bonus: I am not just rendering the primitive points, it's not beautiful. Sparse points are interpolated near the primitive points, but the
user is allowed to control the sparsity of points though on the top of raterize.cu..

Checkerboard with perspective correction | Checkerboard without perspective correction
:-------------------------:|:-------------------------:
![](imgs/checkerboard.gif) | ![](imgs/checkerboard2.gif)

Perspective correction: please see the reference https://en.wikipedia.org/wiki/Texture_mapping#Perspective_correctness. Without perspective correction,
we can observe the 'distorted' image generated from the raterization pipeline.

Checkerboard with bilinear filtering | Checkerboard without bilinear filtering
:-------------------------:|:-------------------------:
![](imgs/checkwithbin.JPG) | ![](imgs/checkwithnobin.JPG)

Bilinear filtering, reference https://en.wikipedia.org/wiki/Bilinear_filtering, we can observe the smoothing effect on the lines over the checkerboard.

Duck toon | Di toon
:-------------------------:|:-------------------------:
![](imgs/duck5.gif) | ![](imgs/di2.gif)

Toon Shading, https://www.garagegames.com/community/forums/viewthread/24977

Cow | Di|Engine | Buggy| Flower with Lines
:-------------------------:|:-------------------------: |:-------------------------:|:-------------------------: |:-------------------------:
![](imgs/cow1.gif) | ![](imgs/di1.gif) |![](imgs/engine1.gif) | ![](imgs/buggy1.gif) | ![](imgs/flower.gif)

# Analysis

Pipeline Timing(ms) - VC | Pipeline Timing(ms) - Cow |Pipeline Timing(ms) - Box
:-------------------------:|:-------------------------: |:-------------------------:
![](imgs/ana1.JPG) | ![](imgs/ana2.JPG) |![](imgs/ana3.JPG)
![](imgs/ana4.JPG) | ![](imgs/ana5.JPG) |![](imgs/ana6.JPG)
triangle width-mean :20 | triangle width-mean :2~3 |triangle width-mean :77

Charts above shows that the number of primitives is not the dominant factor effecting the breakdown of time spent in each pipeline stage.
Indeed, the stage that consumes most of the time is rasterizing; however, it's not proportional to the number of primitives.
In cases of cow, we have >5000 primitives in total, the rasterizing stage only consumes 36 percent of the total time;
on the contrast, the box only has 12 primitives, the rasterizing consumes more than 93 percent of the total time.
A more careful examination of the rasterizing stage reveals another important factor in the rasterizing stage: the primitive size.
As for each kernel we have to iterate through the entire boundary box to compute each fragments within a triangle, increasing the average triangle size
might significantly decrease the performance. If we can saturate the threads as much as possible while decrease the average size of the triangle, we
can expect an improvment in the performance.

On the other hand, in case our average triangle is small enough, we might need to concern about memory allocation and access. We
could for example, use shared memomry to decrease the overhead of memomry allocation and access for initializing the depth buffer as well as the memory access in other kernel functions (we can find more evidence
as we observe the overhead of primitive assembly, rending and depth init in the left two charts).
It would be cool to implement tile based rasterizing in the future.

*PS: looking for models: just we & just tank : ]

*CMakeLists change
'common.h' file is added to the cmakelist file; sm20 --> sm61.

### Credits

Expand Down
Binary file added imgs/VC1.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 imgs/VC2.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 imgs/ana1.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 imgs/ana2.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 imgs/ana3.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 imgs/ana4.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 imgs/ana5.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 imgs/ana6.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 imgs/buggy1.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 imgs/checkerboard.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 imgs/checkerboard2.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 imgs/checkwithbin.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 imgs/checkwithnobin.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 imgs/cow1.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 imgs/cow2.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 imgs/cow3.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 imgs/di1.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 imgs/di2.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 imgs/duck1.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 imgs/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 imgs/duck4.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 imgs/duck5.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 imgs/engine1.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 imgs/flower.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 imgs/milk1.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 imgs/truck2.gif
Binary file added imgs/truck3.gif
Binary file added imgs/truckwithbin.JPG
3 changes: 2 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
set(SOURCE_FILES
"rasterize.cu"
"common.h"
"rasterize.h"
"rasterizeTools.h"
)

cuda_add_library(src
${SOURCE_FILES}
OPTIONS -arch=sm_20
OPTIONS -arch=sm_61
)
126 changes: 126 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#include <cmath>
#include <cstdio>
#include <cuda.h>
#include <cuda_runtime.h>
#include <thrust/random.h>
#include <util/checkCUDAError.h>
#include <util/tiny_gltf_loader.h>
#include "rasterizeTools.h"
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "util/utilityCore.hpp"
namespace {

typedef unsigned short VertexIndex;
typedef glm::vec3 VertexAttributePosition;
typedef glm::vec3 VertexAttributeNormal;
typedef glm::vec2 VertexAttributeTexcoord;
typedef unsigned char TextureData;

typedef unsigned char BufferByte;

enum PrimitiveType{
Point = 1,
Line = 2,
Triangle = 3
};

struct VertexOut {
glm::vec4 pos;

// TODO: add new attributes to your VertexOut
// The attributes listed below might be useful,
// but always feel free to modify on your own

glm::vec3 eyePos; // eye space position used for shading
glm::vec3 eyeNor; // eye space normal used for shading, cuz normal will go wrong after perspective transformation
glm::vec3 col;
glm::vec2 texcoord0;
TextureData* dev_diffuseTex = NULL;
int texWidth, texHeight;
int texture;
// ...
};

struct Primitive {
PrimitiveType primitiveType = Triangle; // C++ 11 init
VertexOut v[3];

TextureData* dev_diffuseTex;
int texWidth, texHeight;
int texture;
};

struct Fragment {
glm::vec3 color;

// TODO: add new attributes to your Fragment
// The attributes listed below might be useful,
// but always feel free to modify on your own

glm::vec3 eyePos; // eye space position used for shading
glm::vec3 eyeNor;
VertexAttributeTexcoord texcoord0;
TextureData* dev_diffuseTex;
int texWidth, texHeight;
int texture;
// ...
};

struct PrimitiveDevBufPointers {
int primitiveMode; //from tinygltfloader macro
PrimitiveType primitiveType;
int numPrimitives;
int numIndices;
int numVertices;

// Vertex In, const after loaded
VertexIndex* dev_indices;
VertexAttributePosition* dev_position;
VertexAttributeNormal* dev_normal;
VertexAttributeTexcoord* dev_texcoord0;

// Materials, add more attributes when needed

// TODO: add more attributes when needed
TextureData* dev_diffuseTex;
int texWidth, texHeight;
int texture;

// Vertex Out, vertex used for rasterization, this is changing every frame
VertexOut* dev_verticesOut;
};

}

static std::map<std::string, std::vector<PrimitiveDevBufPointers>> mesh2PrimitivesMap;


static int width = 0;
static int height = 0;

static int totalNumPrimitives = 0;
static Primitive *dev_primitives = NULL;
static Fragment *dev_fragmentBuffer = NULL;
static glm::vec3 *dev_framebuffer = NULL;

static int * dev_depth = NULL; // you might need this buffer when doing depth test
#define blockSize 256

//TILEs::::::::::::::::::::::

#define tileSizeR2 256
static int tileWidth;
static int tileHeight;
static int numTilesV;
static int numTilesH;

struct Tile{
float maxx;
float maxy;
float minx;
float miny;
int numPrims = 0;
};
//TILEs______________________
static Tile * dev_tiles;
Loading