Skip to content

Commit

Permalink
Included Experimental Shading system
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyQBD committed Jun 21, 2014
1 parent afce00a commit 0ddfb86
Show file tree
Hide file tree
Showing 9 changed files with 308 additions and 24 deletions.
34 changes: 34 additions & 0 deletions res/shaders/common.glh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#if (GLSL_VERSION >= 150)
#define DeclareFragOutput(locationNumber, type) out type outputLocation##locationNumber
#define SetFragOutput(locationNumber, val) outputLocation##locationNumber = val

#define texture2D(tex, coord) texture(tex, coord)

#if defined(VS_BUILD)
#define varying out
#elif defined(FS_BUILD)
#define varying in
#endif
#else
#define DeclareFragOutput(locationNumber)
#define SetFragOutput(locationNumber, val) gl_FragData[locationNumber] = val
#endif

#if (GLSL_VERSION >= 400)
#define mad(a, b, c) fma(a, b, c)
#else
#define mad(a, b, c) (a * b + c)
#endif

#define float2 vec2
#define float3 vec3
#define float4 vec4
#define int2 ivec2
#define int3 ivec3
#define int4 ivec4
#define bool2 bvec2
#define bool3 bvec3
#define bool4 bvec4

#define lerp(a, b, t) mix(a, b, t)
#define saturate(a) clamp(a, 0.0, 1.0)
76 changes: 76 additions & 0 deletions res/shaders/filter-fxaa.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "common.glh"
// FXAA shader, GLSL code adapted from:
// http://horde3d.org/wiki/index.php5?title=Shading_Technique_-_FXAA
// Whitepaper describing the technique:
// http://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf

varying vec2 texCoord0;

#if defined(VS_BUILD)
attribute vec3 position;
attribute vec2 texCoord;

uniform mat4 T_MVP;

void main()
{
texCoord0 = texCoord;
gl_Position = T_MVP * vec4(position, 1.0);
}

#elif defined(FS_BUILD)
uniform sampler2D R_filterTexture;
uniform vec3 R_inverseFilterTextureSize;
uniform float R_fxaaSpanMax;
uniform float R_fxaaReduceMin;
uniform float R_fxaaReduceMul;

DeclareFragOutput(0, vec4);
void main()
{
vec2 texCoordOffset = R_inverseFilterTextureSize.xy;

vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaTL = dot(luma, texture2D(R_filterTexture, texCoord0.xy + (vec2(-1.0, -1.0) * texCoordOffset)).xyz);
float lumaTR = dot(luma, texture2D(R_filterTexture, texCoord0.xy + (vec2(1.0, -1.0) * texCoordOffset)).xyz);
float lumaBL = dot(luma, texture2D(R_filterTexture, texCoord0.xy + (vec2(-1.0, 1.0) * texCoordOffset)).xyz);
float lumaBR = dot(luma, texture2D(R_filterTexture, texCoord0.xy + (vec2(1.0, 1.0) * texCoordOffset)).xyz);
float lumaM = dot(luma, texture2D(R_filterTexture, texCoord0.xy).xyz);

vec2 dir;
dir.x = -((lumaTL + lumaTR) - (lumaBL + lumaBR));
dir.y = ((lumaTL + lumaBL) - (lumaTR + lumaBR));

float dirReduce = max((lumaTL + lumaTR + lumaBL + lumaBR) * (R_fxaaReduceMul * 0.25), R_fxaaReduceMin);
float inverseDirAdjustment = 1.0/(min(abs(dir.x), abs(dir.y)) + dirReduce);

dir = min(vec2(R_fxaaSpanMax, R_fxaaSpanMax),
max(vec2(-R_fxaaSpanMax, -R_fxaaSpanMax), dir * inverseDirAdjustment));

dir.x = dir.x * step(1.0, abs(dir.x));
dir.y = dir.y * step(1.0, abs(dir.y));

//float dirStep = max(step(1.0, abs(dir.x)), step(1.0, abs(dir.y)));
//dir.x = dir.x * dirStep;
//dir.y = dir.y * dirStep;

dir = dir * texCoordOffset;

vec3 result1 = (1.0/2.0) * (
texture2D(R_filterTexture, texCoord0.xy + (dir * vec2(1.0/3.0 - 0.5))).xyz +
texture2D(R_filterTexture, texCoord0.xy + (dir * vec2(2.0/3.0 - 0.5))).xyz);

vec3 result2 = result1 * (1.0/2.0) + (1.0/4.0) * (
texture2D(R_filterTexture, texCoord0.xy + (dir * vec2(0.0/3.0 - 0.5))).xyz +
texture2D(R_filterTexture, texCoord0.xy + (dir * vec2(3.0/3.0 - 0.5))).xyz);

float lumaMin = min(lumaM, min(min(lumaTL, lumaTR), min(lumaBL, lumaBR)));
float lumaMax = max(lumaM, max(max(lumaTL, lumaTR), max(lumaBL, lumaBR)));
float lumaResult2 = dot(luma, result2);

if(lumaResult2 < lumaMin || lumaResult2 > lumaMax)
SetFragOutput(0, vec4(result1, 1.0));
else
SetFragOutput(0, vec4(result2, 1.0));
}
#endif
36 changes: 36 additions & 0 deletions res/shaders/filter-gausBlur7x1.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "common.glh"

varying vec2 texCoord0;

#if defined(VS_BUILD)
attribute vec3 position;
attribute vec2 texCoord;

uniform mat4 T_MVP;

void main()
{
gl_Position = T_MVP * vec4(position, 1.0);
texCoord0 = texCoord;
}

#elif defined(FS_BUILD)
uniform vec3 R_blurScale;
uniform sampler2D R_filterTexture;

DeclareFragOutput(0, vec4);
void main()
{
vec4 color = vec4(0.0);

color += texture2D(R_filterTexture, texCoord0 + (vec2(-3.0) * R_blurScale.xy)) * (1.0/64.0);
color += texture2D(R_filterTexture, texCoord0 + (vec2(-2.0) * R_blurScale.xy)) * (6.0/64.0);
color += texture2D(R_filterTexture, texCoord0 + (vec2(-1.0) * R_blurScale.xy)) * (15.0/64.0);
color += texture2D(R_filterTexture, texCoord0 + (vec2(0.0) * R_blurScale.xy)) * (20.0/64.0);
color += texture2D(R_filterTexture, texCoord0 + (vec2(1.0) * R_blurScale.xy)) * (15.0/64.0);
color += texture2D(R_filterTexture, texCoord0 + (vec2(2.0) * R_blurScale.xy)) * (6.0/64.0);
color += texture2D(R_filterTexture, texCoord0 + (vec2(3.0) * R_blurScale.xy)) * (1.0/64.0);

SetFragOutput(0, color);
}
#endif
24 changes: 24 additions & 0 deletions res/shaders/filter-null.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "common.glh"

varying vec2 texCoord0;

#if defined(VS_BUILD)
attribute vec3 position;
attribute vec2 texCoord;

uniform mat4 T_MVP;

void main()
{
gl_Position = T_MVP * vec4(position, 1.0);
texCoord0 = texCoord;
}
#elif defined(FS_BUILD)
uniform sampler2D R_filterTexture;

DeclareFragOutput(0, vec4);
void main()
{
SetFragOutput(0, texture2D(R_filterTexture, texCoord0));
}
#endif
47 changes: 47 additions & 0 deletions res/shaders/forward-ambient.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "common.glh"

varying vec2 texCoord0;
varying vec3 worldPos0;
varying mat3 tbnMatrix;

#if defined(VS_BUILD)
attribute vec3 position;
attribute vec2 texCoord;
attribute vec3 normal;
attribute vec3 tangent;

uniform mat4 T_model;
uniform mat4 T_MVP;

void main()
{
gl_Position = T_MVP * vec4(position, 1.0);
texCoord0 = texCoord;
worldPos0 = (T_model * vec4(position, 1.0)).xyz;

vec3 n = normalize((T_model * vec4(normal, 0.0)).xyz);
vec3 t = normalize((T_model * vec4(tangent, 0.0)).xyz);
t = normalize(t - dot(t, n) * n);

vec3 biTangent = cross(t, n);
tbnMatrix = mat3(t, biTangent, n);
}
#elif defined(FS_BUILD)
#include "sampling.glh"

uniform vec3 R_ambient;
uniform vec3 C_eyePos;
uniform sampler2D diffuse;
uniform sampler2D dispMap;

uniform float dispMapScale;
uniform float dispMapBias;

DeclareFragOutput(0, vec4);
void main()
{
vec3 directionToEye = normalize(C_eyePos - worldPos0);
vec2 texCoords = CalcParallaxTexCoords(dispMap, tbnMatrix, directionToEye, texCoord0, dispMapScale, dispMapBias);
SetFragOutput(0, texture2D(diffuse, texCoords) * vec4(R_ambient, 1));
}
#endif
24 changes: 24 additions & 0 deletions res/shaders/shadowMapGenerator.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "common.glh"

#if defined(VS_BUILD)
attribute vec3 position;

uniform mat4 T_MVP;

void main()
{
gl_Position = T_MVP * vec4(position, 1.0);
}
#elif defined(FS_BUILD)
DeclareFragOutput(0, vec4);
void main()
{
float depth = gl_FragCoord.z;

float dx = dFdx(depth);
float dy = dFdy(depth);
float moment2 = depth * depth + 0.25 * (dx * dx + dy * dy);

SetFragOutput(0, vec4(depth, moment2, 0.0, 0.0));
}
#endif
10 changes: 5 additions & 5 deletions src/renderingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ RenderingEngine::RenderingEngine(const Window& window) :
m_window(&window),
m_tempTarget(window.GetWidth(), window.GetHeight(), 0, GL_TEXTURE_2D, GL_NEAREST, GL_RGBA, GL_RGBA, false, GL_COLOR_ATTACHMENT0),
m_planeMaterial("renderingEngine_filterPlane", m_tempTarget, 1, 8),
m_defaultShader("forward-ambient"),
m_shadowMapShader("shadowMapGenerator"),
m_nullFilter("filter-null"),
m_gausBlurFilter("filter-gausBlur7x1"),
m_fxaaFilter("filter-fxaa"),
m_defaultShader("forward-ambient", true),
m_shadowMapShader("shadowMapGenerator", true),
m_nullFilter("filter-null", true),
m_gausBlurFilter("filter-gausBlur7x1", true),
m_fxaaFilter("filter-fxaa", true),
m_altCameraTransform(Vector3f(0,0,0), Quaternion(Vector3f(0,1,0),ToRadians(180.0f))),
m_altCamera(Matrix4f().InitIdentity(), &m_altCameraTransform)
{
Expand Down
76 changes: 59 additions & 17 deletions src/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//--------------------------------------------------------------------------------
std::map<std::string, ShaderData*> Shader::s_resourceMap;
int ShaderData::s_supportedOpenGLLevel = 0;
std::string ShaderData::s_glslVersion = "";

//--------------------------------------------------------------------------------
// Forward declarations
Expand All @@ -32,7 +33,7 @@ static void String_ReplaceAll(std::string* replaceIn, const std::string& replace
//--------------------------------------------------------------------------------
// Constructors/Destructors
//--------------------------------------------------------------------------------
ShaderData::ShaderData(const std::string& fileName)
ShaderData::ShaderData(const std::string& fileName, bool useNewShaderSystem)
{
m_program = glCreateProgram();

Expand All @@ -51,27 +52,68 @@ ShaderData::ShaderData(const std::string& fileName)
glGetIntegerv(GL_MINOR_VERSION, &minorVersion);

s_supportedOpenGLLevel = majorVersion * 100 + minorVersion * 10;

if(s_supportedOpenGLLevel >= 330)
{
std::ostringstream convert;
convert << s_supportedOpenGLLevel;

s_glslVersion = convert.str();
}
else if(s_supportedOpenGLLevel >= 320)
{
s_glslVersion = "150";
}
else if(s_supportedOpenGLLevel >= 310)
{
s_glslVersion = "140";
}
else if(s_supportedOpenGLLevel >= 300)
{
s_glslVersion = "130";
}
else if(s_supportedOpenGLLevel >= 210)
{
s_glslVersion = "120";
}
else if(s_supportedOpenGLLevel >= 200)
{
s_glslVersion = "110";
}
}

std::string vertexShaderText = LoadShader(fileName + ".vs");
std::string fragmentShaderText = LoadShader(fileName + ".fs");
std::string vertexShaderText;
std::string fragmentShaderText;

if(s_supportedOpenGLLevel >= 320)
if(!useNewShaderSystem)
{
ConvertVertexShaderToGLSL150(&vertexShaderText);
ConvertFragmentShaderToGLSL150(&fragmentShaderText);
vertexShaderText = LoadShader(fileName + ".vs");
fragmentShaderText = LoadShader(fileName + ".fs");

if(s_supportedOpenGLLevel >= 320)
{
ConvertVertexShaderToGLSL150(&vertexShaderText);
ConvertFragmentShaderToGLSL150(&fragmentShaderText);
}
}
else
{
std::string shaderText = LoadShader(fileName + ".glsl");

vertexShaderText = "#version 150\n#define VS_BUILD\n#define GLSL_VERSION " + s_glslVersion + "\n" + shaderText;
fragmentShaderText = "#version 150\n#define FS_BUILD\n#define GLSL_VERSION " + s_glslVersion + "\n" + shaderText;
}

AddVertexShader(vertexShaderText);
AddFragmentShader(fragmentShaderText);
std::string attributeKeyword = s_supportedOpenGLLevel < 320 ? "attribute" : "in";
AddAllAttributes(vertexShaderText, attributeKeyword);
CompileShader();
AddShaderUniforms(vertexShaderText);
AddShaderUniforms(fragmentShaderText);
AddFragmentShader(fragmentShaderText);
std::string attributeKeyword = s_supportedOpenGLLevel < 320 ? "attribute" : "in";
AddAllAttributes(vertexShaderText, attributeKeyword);
CompileShader();
AddShaderUniforms(vertexShaderText);
AddShaderUniforms(fragmentShaderText);
}

ShaderData::~ShaderData()
Expand All @@ -84,7 +126,7 @@ ShaderData::~ShaderData()
glDeleteProgram(m_program);
}

Shader::Shader(const std::string& fileName)
Shader::Shader(const std::string& fileName, bool useNewShaderSystem)
{
m_fileName = fileName;

Expand All @@ -96,7 +138,7 @@ Shader::Shader(const std::string& fileName)
}
else
{
m_shaderData = new ShaderData(fileName);
m_shaderData = new ShaderData(fileName, useNewShaderSystem);
s_resourceMap.insert(std::pair<std::string, ShaderData*>(fileName, m_shaderData));
}
}
Expand Down
Loading

1 comment on commit 0ddfb86

@Hsifeulbhsifder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear Benny, After using this new system, I get this error (error C0107: Too many arguments to macro DeclareFragOutput) when trying to load the shadowMapShader. I am using Java/LWJGL with an Nvidea GTX640 graphics card. Thanks.

Please sign in to comment.