Skip to content

Pull Deferred Shader #15

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

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
213 changes: 94 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,142 +3,117 @@ CIS565: Project 6: Deferred Shader
-------------------------------------------------------------------------------
Fall 2013
-------------------------------------------------------------------------------
Due Friday 11/15/2013
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
NOTE:
-------------------------------------------------------------------------------
This project requires any graphics card with support for a modern OpenGL
pipeline. Any AMD, NVIDIA, or Intel card from the past few years should work
fine, and every machine in the SIG Lab and Moore 100 is capable of running
this project.
---
Deferred Shading
---

-------------------------------------------------------------------------------
INTRODUCTION:
-------------------------------------------------------------------------------
In this project, you will get introduced to the basics of deferred shading. You will write GLSL and OpenGL code to perform various tasks in a deferred lighting pipeline such as creating and writing to a G-Buffer.
The whole point of deferred shading is to separate geometry from lighting calculations and change the lighting equation from an O(GE) into an O(G+E) problem. To that effect, I rendered the Sponza scene with a large number of lights.

-------------------------------------------------------------------------------
CONTENTS:
-------------------------------------------------------------------------------
The Project6 root directory contains the following subdirectories:

* base/
* PROJ_WIN/ contains the vs2010 project files
* PROJ_NIX/ contains makefile for building (tested on ubuntu 12.04 LTS)
* res/ contains resources including shader source and obj files
* src/ contains the c++ code for the project along with SOIL and tiny_obj_loader
* shared32/ contains freeglut, glm, and glew.
333K Lights, randomly colored and positioned

-------------------------------------------------------------------------------
REQUIREMENTS:
-------------------------------------------------------------------------------
![too many lights!](base/images/crazy333k_noAO.png)

In this project, you are given code for:
* Loading .obj files
* Rendering to a minimal G buffer:
* Depth
* Normal
* Color
* Eye space position
* Rendering simple ambient and directional lighting to texture
* Example post process shader to add a vignette

You are required to implement:
* Either of the following effects
* Bloom (feel free to use [GPU Gems](http://http.developer.nvidia.com/GPUGems/gpugems_ch21.html) as a rough guide)
* "Toon" Shading (with basic silhouetting)
* Point light sources
* An additional G buffer slot and some effect showing it off

**NOTE**: Implementing separable convolution will require another link in your pipeline and will count as an extra feature if you do performance analysis with a standard one-pass 2D convolution. The overhead of rendering and reading from a texture _may_ offset the extra computations for smaller 2D kernels.

You must implement two of the following extras:
* The effect you did not choose above
* Screen space ambient occlusion
* Compare performance to a normal forward renderer with
* No optimizations
* Coarse sort geometry front-to-back for early-z
* Z-prepass for early-z
* Optimize g-buffer format, e.g., pack things together, quantize, reconstruct z from normal x and y (because it is normalized), etc.
* Must be accompanied with a performance analysis to count
* Additional lighting and pre/post processing effects! (email first please, if they are good you may add multiple).
15K Lights

-------------------------------------------------------------------------------
README
-------------------------------------------------------------------------------
All students must replace or augment the contents of this Readme.md in a clear
manner with the following:
![still quite too many!](base/images/15K_lights.png)

* A brief description of the project and the specific features you implemented.
* At least one screenshot of your project running.
* A 30 second or longer video of your project running. To create the video you
can use http://www.microsoft.com/expression/products/Encoder4_Overview.aspx
* A performance evaluation (described in detail below).
###Controls
* Up-Down : Change Y size of kernel
* Left-Right : Change X size of kernel
* [ : Toggle back one step in *modes*
* ] : Toggle forward one step in *modes*
* 1 : Depth Visual
* 2 : Normal Visual
* 3 : Color Visual
* 4 : Position Visual
* 5 : Light Visual
* 0 : Main Mode

-------------------------------------------------------------------------------
PERFORMANCE EVALUATION
-------------------------------------------------------------------------------
The performance evaluation is where you will investigate how to make your
program more efficient using the skills you've learned in class. You must have
performed at least one experiment on your code to investigate the positive or
negative effects on performance.
###Modes
* Simple pass through effect
* Bloom with 2D Kernel
* Bloom with Separable Kernel
* Glow with 2D Kernel
* Bloom with Separable Kernel
* Vignette
* SSAO Pass
* SSAO Blended Pass

We encourage you to get creative with your tweaks. Consider places in your code
that could be considered bottlenecks and try to improve them.

Each student should provide no more than a one page summary of their
optimizations along with tables and or graphs to visually explain any
performance differences.
---
Features
---

-------------------------------------------------------------------------------
THIRD PARTY CODE POLICY
-------------------------------------------------------------------------------
* Use of any third-party code must be approved by asking on the Google groups.
If it is approved, all students are welcome to use it. Generally, we approve
use of third-party code that is not a core part of the project. For example,
for the ray tracer, we would approve using a third-party library for loading
models, but would not approve copying and pasting a CUDA function for doing
refraction.
* Third-party code must be credited in README.md.
* Using third-party code without its approval, including using another
student's code, is an academic integrity violation, and will result in you
receiving an F for the semester.
The following features were implemented.

-------------------------------------------------------------------------------
SELF-GRADING
-------------------------------------------------------------------------------
* On the submission date, email your grade, on a scale of 0 to 100, to Liam,
[email protected], with a one paragraph explanation. Be concise and
realistic. Recall that we reserve 30 points as a sanity check to adjust your
grade. Your actual grade will be (0.7 * your grade) + (0.3 * our grade). We
hope to only use this in extreme cases when your grade does not realistically
reflect your work - it is either too high or too low. In most cases, we plan
to give you the exact grade you suggest.
* Projects are not weighted evenly, e.g., Project 0 doesn't count as much as
the path tracer. We will determine the weighting at the end of the semester
based on the size of each project.
####Bloom Shading (2D separable kernels implemented)
All pixels beyond a certain threshold in value (after lighting) were blurred based on the x and y radius. This can be seen below:

No Bloom:

![lightsnobloom](base/images/more lights no blur.png)

Y-direction only bloom:

![lightsbloom](base/images/more lights plus length blur.png)

The separability of the 2D Gaussian kernel was exploited using yet another frame buffer object and extra textures to ping-pong with and uses the following pipeline:

![bloomPipeline](base/images/separable.png)

This separability of blur is a massive speed improvement. This is discussed further in the performance section.

####Glow Shading : Extra G-Buffer slot

The object "short box" is selected based on its name and made to glow using a similar separable-blur technique as is used for the bloom shading. This uses an extra G-Buffer slot: the alpha channel of the position texture. A value of 0 or 1 is written out here based on whether the object being rendered is the "short box" or not.

No Glow:

![no Glow](base/images/box_noGlow.png)

With Glow:

![Glow](base/images/box_Glow.png)

####Specular Highlights: Compacted G-Buffer usage

Since I'm personally fond of specular highlights, I decided to use them in the code. I've picked up the specular value from the mtl files of the obj loaded and it is packed into a G Buffer during the first phase of the system. Since the glow buffer is quite a waste of a 32 bit floating point value for just the 0 or 1 glow, I decided to use it for specular exponent as well.

The buffer is packed as ````2 * spec_ex + glow? ```` and decoded when required.

You can see the specular as a little white dash on the green wall and a little bit on the blue box as well.

![spec](base/images/box_specular.png)

####Screen Space Ambient Occlusion (attempt)
I based my implementation on this [paper](http://graphics.cs.williams.edu/papers/SAOHPG12/) and referred to their implementation and pseudo code along with trying to over randomize the inputs to the code. I couldn't get it to work entirely correctly but this is the result I get.

![AO](base/images/AmbientOcclusion.png)

![AO](base/images/AOComp.png)

####Light Visualization
Lights can be visualized in the scene as follows

![lettherebelights](base/images/lightVisualizations.png)

---
Performance
---
I must say, I'm very impressed with the number of lights that are handled by a deferred shading pipeline for extremely complex geometry without completely blocking up the GPU. It's amazing to see this delineation of lighting calculation and a nice geometry pass.

Since I did separable kernels, I decided to graph out the time. As noticed, since the separation of a 2D kernel results in a change from O(MN) to O(M+N) (quite like deferred shading itself!), we are able to run much much larger kernel sizes off the bat. We notice that there is a slight overhead of creating yet another frame buffer object and having to setup another texture, but compared to the speed up, this cost is marginal. It is overtaken by the lowest of texture look up calls (10 vs 6+overhead).

![performance](base/images/chart.png)

---
SUBMISSION
Coming up soon
---
As with the previous projects, you should fork this project and work inside of
your fork. Upon completion, commit your finished project back to your fork, and
make a pull request to the master repository. You should include a README.md
file in the root directory detailing the following

* A brief description of the project and specific features you implemented
* At least one screenshot of your project running.
* A link to a video of your project running.
* Instructions for building and running your project if they differ from the
base code.
* A performance writeup as detailed above.
* A list of all third-party code used.
* This Readme file edited as described above in the README section.
I have a few cool screen space effects planned out in the near future.

---
ACKNOWLEDGEMENTS
---
This project makes use of [tinyobjloader](http://syoyo.github.io/tinyobjloader/) and [SOIL](http://lonesock.net/soil.html)
* This project makes use of [tinyobjloader](http://syoyo.github.io/tinyobjloader/) and [SOIL](http://lonesock.net/soil.html)
* For reference on SSAO implementation [Separabe, screen space Ambient Occlusion](http://floored.com/blog/2013/ssao-screen-space-ambient-occlusion.html)
4 changes: 2 additions & 2 deletions base/PROJ_WIN/P6/P6/P6.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>..\..\..\..\shared32\glew\lib;..\..\..\..\shared32\freeglut\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>freeglut.lib;glew32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>../Debug/soil.lib;freeglut.lib;glew32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
Expand All @@ -67,7 +67,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>..\..\..\..\shared32\glew\lib;..\..\..\..\shared32\freeglut\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>freeglut.lib;glew32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>../Release/SOIL.lib;freeglut.lib;glew32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SubSystem>Console</SubSystem>
</Link>
</ItemDefinitionGroup>
Expand Down
Binary file added base/images/15K_lights.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 base/images/AOComp.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 base/images/AONone.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 base/images/AmbientOcclusion.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 base/images/blur_step1.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 base/images/blur_step2.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 base/images/blur_step3.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 base/images/blur_step4.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 base/images/blur_step5.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 base/images/box_Glow.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 base/images/box_bloom.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 base/images/box_noGlow.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 base/images/box_specular.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 base/images/chart.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 base/images/crazy333k.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 base/images/crazy333k_noAO.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 base/images/lightVisualizations.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 base/images/more lights no blur.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 base/images/more lights plus length blur.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 base/images/separable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion base/res/cornell/cornell_box.mtl
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ newmtl green
Ka 0 0 0
Kd 0 1 0
Ks 0 0 0
Ns 15.0

newmtl blue
Ka 0 0 0
Kd 0 0 1
Ks 0 0 0
Ns 25.0

newmtl light
Ka 20 20 20
Kd 1 1 1
Kd 12 12 12
Ks 0 0 0

2 changes: 1 addition & 1 deletion base/res/cornell/cornell_box.obj
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ v 556.0 548.8 0.0
f -4 -3 -2 -1

o short_block
usemtl white
usemtl blue

v 130.0 165.0 65.0
v 82.0 165.0 225.0
Expand Down
22 changes: 18 additions & 4 deletions base/res/shaders/ambient.frag
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ vec3 sampleNrm(vec2 texcoords) {
}

//Helper function to automicatlly sample and unpack positions
vec3 samplePos(vec2 texcoords) {
return texture(u_Positiontex,texcoords).xyz;
vec4 samplePos(vec2 texcoords) {
return texture(u_Positiontex,texcoords);
}

//Helper function to automicatlly sample and unpack positions
Expand Down Expand Up @@ -99,7 +99,12 @@ void main() {
float lin_depth = linearizeDepth(exp_depth,u_Near,u_Far);

vec3 normal = sampleNrm(fs_Texcoord);
vec3 position = samplePos(fs_Texcoord);

vec4 pos = samplePos(fs_Texcoord);
vec3 position = pos.xyz;
// Shininess and glow was packed
float shininess = 2.0*floor(pos.w/2.0);

vec3 color = sampleCol(fs_Texcoord);
vec3 light = u_Light.xyz;
float strength = u_Light.w;
Expand All @@ -108,7 +113,16 @@ void main() {
} else {
float ambient = u_LightIl;
float diffuse = max(0.0, dot(normalize(light),normal));
out_Color = vec4(color*(strength*diffuse + ambient),1.0f);

// position is in eye space, so view vector should be negative position?
vec3 halfVec = normalize(normalize(-position) + normalize(light));
float spec = clamp(dot(halfVec, normal),0.0,1.0);
if(shininess < 0.0001)
spec = 0.0;
else
spec = pow(spec,shininess);

out_Color = vec4(0.0);//vec4(color*(strength*(diffuse + spec) + ambient),1.0f);
}
return;
}
Expand Down
18 changes: 15 additions & 3 deletions base/res/shaders/directional.frag
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ vec3 sampleNrm(vec2 texcoords) {
}

//Helper function to automicatlly sample and unpack positions
vec3 samplePos(vec2 texcoords) {
return texture(u_Positiontex,texcoords).xyz;
vec4 samplePos(vec2 texcoords) {
return texture(u_Positiontex,texcoords);
}

//Helper function to automicatlly sample and unpack positions
Expand Down Expand Up @@ -99,12 +99,24 @@ void main() {
float lin_depth = linearizeDepth(exp_depth,u_Near,u_Far);

vec3 normal = sampleNrm(fs_Texcoord);
vec3 position = samplePos(fs_Texcoord);
vec4 pos = samplePos(fs_Texcoord);

vec3 position = pos.xyz;
// Shininess and glow was packed
float shininess = 2.0*floor(pos.w/2.0);

vec3 color = sampleCol(fs_Texcoord);
vec3 light = u_Light.xyz;
float lightRadius = u_Light.w;

float diffuse = max(0.0, dot(normalize(light),normal));


// position is in eye space, so view vector should be negative position?
vec3 halfVec = normalize(normalize(-position) + normalize(light));
float spec = dot(halfVec, normal);
spec = pow(spec,shininess);

out_Color = vec4(color*u_LightIl*diffuse,1.0f);
}

Expand Down
8 changes: 6 additions & 2 deletions base/res/shaders/pass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

uniform float u_Far;
uniform vec3 u_Color;
uniform float u_Glow;
uniform float u_Shininess;

in vec3 fs_Normal;
in vec4 fs_Position;
Expand All @@ -13,6 +15,8 @@ out vec4 out_Color;
void main(void)
{
out_Normal = vec4(normalize(fs_Normal),0.0f);
out_Position = vec4(fs_Position.xyz,1.0f); //Tuck position into 0 1 range
out_Color = vec4(u_Color,1.0);
// shininess can fit in 31 bits, glow just really needs 1 bit
out_Position = vec4(fs_Position.xyz,(u_Shininess * 2 + u_Glow)); //Tuck position into 0 1 range
// Potentially use alpha channel for something useful like emmission light or not! : for Glow shading
out_Color = vec4(u_Color, 1.0);
}
Loading