Skip to content

Commit

Permalink
Implemented stable Poisson filtering demo. Source of inspiration:
Browse files Browse the repository at this point in the history
  • Loading branch information
vcoda committed Jul 20, 2020
1 parent 773ae53 commit c4f64e3
Show file tree
Hide file tree
Showing 13 changed files with 810 additions and 1 deletion.
15 changes: 15 additions & 0 deletions aggregated-graphics-samples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "deferred-shading", "deferre
{67B01ECD-2613-4FA0-84F7-87F5BCF40EB1} = {67B01ECD-2613-4FA0-84F7-87F5BCF40EB1}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "shadowmapping-poisson-stable", "shadowmapping-poisson-stable\shadowmapping-poisson-stable.vcxproj", "{DD90A343-7897-442B-9D19-7FF0676E80EB}"
ProjectSection(ProjectDependencies) = postProject
{8D9D4A3E-439A-4210-8879-259B20D992CA} = {8D9D4A3E-439A-4210-8879-259B20D992CA}
{51CC36B3-921E-4853-ACD5-CB6CBC27FBA1} = {51CC36B3-921E-4853-ACD5-CB6CBC27FBA1}
{67B01ECD-2613-4FA0-84F7-87F5BCF40EB1} = {67B01ECD-2613-4FA0-84F7-87F5BCF40EB1}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand Down Expand Up @@ -123,6 +130,14 @@ Global
{3FA565CC-1B90-4EF7-9E43-8031ABA1118B}.Release|x64.Build.0 = Release|x64
{3FA565CC-1B90-4EF7-9E43-8031ABA1118B}.Release|x86.ActiveCfg = Release|Win32
{3FA565CC-1B90-4EF7-9E43-8031ABA1118B}.Release|x86.Build.0 = Release|Win32
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Debug|x64.ActiveCfg = Debug|x64
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Debug|x64.Build.0 = Debug|x64
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Debug|x86.ActiveCfg = Debug|Win32
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Debug|x86.Build.0 = Debug|Win32
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Release|x64.ActiveCfg = Release|x64
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Release|x64.Build.0 = Release|x64
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Release|x86.ActiveCfg = Release|Win32
{DD90A343-7897-442B-9D19-7FF0676E80EB}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions framework/framework.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<ClInclude Include="shaders\common\cotangentFrame.h" />
<ClInclude Include="shaders\common\linearizeDepth.h" />
<ClInclude Include="shaders\common\noise2d.h" />
<ClInclude Include="shaders\common\noise3d.h" />
<ClInclude Include="shaders\common\poisson16.h" />
<ClInclude Include="shaders\common\poisson32.h" />
<ClInclude Include="shaders\common\poisson8.h" />
Expand Down
3 changes: 3 additions & 0 deletions framework/framework.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
<ClInclude Include="textureLoader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="shaders\common\noise3d.h">
<Filter>Resource Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="arcball.cpp">
Expand Down
2 changes: 1 addition & 1 deletion framework/shaders/common/noise2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
float hash(vec2 p)
{
p = 50. * fract(p * 0.3183099 + vec2(0.71, 0.113));
return -1. + 2. * fract(p.x * p.y * (p.x + p.y));
return fract(p.x * p.y * (p.x + p.y));
}

float noise(vec2 p)
Expand Down
23 changes: 23 additions & 0 deletions framework/shaders/common/noise3d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// https://www.shadertoy.com/view/4sfGzS
float hash(vec3 p) // replace this by something better
{
p = fract( p*0.3183099+.1 );
p *= 17.0;
return fract( p.x*p.y*p.z*(p.x+p.y+p.z) );
}

float noise( in vec3 x )
{
vec3 i = floor(x);
vec3 f = fract(x);
f = f*f*(3.0-2.0*f);

return mix(mix(mix( hash(i+vec3(0,0,0)),
hash(i+vec3(1,0,0)),f.x),
mix( hash(i+vec3(0,1,0)),
hash(i+vec3(1,1,0)),f.x),f.y),
mix(mix( hash(i+vec3(0,0,1)),
hash(i+vec3(1,0,1)),f.x),
mix( hash(i+vec3(0,1,1)),
hash(i+vec3(1,1,1)),f.x),f.y),f.z);
}
Binary file added screenshots/shadowmapping-poisson-stable.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions shadowmapping-poisson-stable/shaders/pcf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "common/poisson32.h"

float pcf(sampler2DShadow shadowMap, vec4 clipPos, float bias, mat2 jitter)
{
clipPos.z += bias;
float sum = 0.;
for (int i = 0; i < 32; ++i)
{
vec2 offset = jitter * poisson32[i];
sum += textureProj(shadowMap, vec4(clipPos.xy + offset, clipPos.zw));
}
return sum/32.;
}
116 changes: 116 additions & 0 deletions shadowmapping-poisson-stable/shaders/phong.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#include "common/transforms.h"
#include "common/noise2d.h"
#include "common/noise3d.h"
#include "pcf.h"

#include "common/linearizeDepth.h"

layout(constant_id = 0) const bool c_screenSpaceNoise = true;
layout(constant_id = 1) const bool c_showNoise = false;

layout(binding = 2) uniform Light
{
vec4 viewPos;
vec4 ambient;
vec4 diffuse;
vec4 specular;
} light;

layout(binding = 3) uniform Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
float shininess;
} surface;

layout(binding = 4) uniform Parameters
{
vec4 screenSize; // x, y, 1/x, 1/y
float radius;
float zbias;
float jitterDensity;
};

layout(binding = 5) uniform sampler2DShadow shadowMap;

layout(location = 0) in vec4 worldPos;
layout(location = 1) in vec3 viewPos;
layout(location = 2) in vec3 viewNormal;
layout(location = 3) in vec2 texCoord;

layout(location = 0) out vec3 oColor;

float screenNoise(vec4 fragCoord)
{
vec2 uv = fragCoord.xy * screenSize.zw;
float aspectRatio = screenSize.x * screenSize.w;
uv.x *= aspectRatio;
return noise(uv * screenSize.x);
}

float worldNoise(vec3 worldPos, float z, float density)
{
float w = 1./z;
return noise(worldPos * density * w);
}

mat2 jitter(float a, float x, float y)
{
float s = sin(a);
float c = cos(a);
mat2 rot = mat2(
c,-s,
s, c);
mat2 scale = mat2(
x, 0.,
0., y);
return rot * scale;
}

vec3 phong(vec3 n, vec3 l, vec3 v,
vec3 Ka, vec3 Ia,
vec3 Kdiff, vec3 Idiff,
vec3 Kspec, vec3 Ispec,
float shininess,
float shadow)
{
float NdL = max(dot(n, l), 0.);
vec3 r = reflect(-l, n);
float RdV = max(dot(r, v), 0.);
return Ka * Ia + shadow * (Kdiff * NdL * Idiff) + (Kspec * pow(RdV, shininess) * Ispec);
}

void main()
{
vec3 n = normalize(viewNormal);
vec3 l = normalize(light.viewPos.xyz - viewPos);
vec3 v = -normalize(viewPos);

float theta;
if (c_screenSpaceNoise)
theta = screenNoise(gl_FragCoord);
else
theta = worldNoise(worldPos.xyz, viewPos.z, jitterDensity);

float shadow;
if (dot(n, l) <= 0.)
shadow = 0.;
else
{
vec4 shadowPos = shadowProj * worldPos;
vec2 scale = radius/textureSize(shadowMap, 0) * shadowPos.w;
mat2 jitMat = jitter(theta * TWO_PI, scale.x, scale.y);
shadow = pcf(shadowMap, shadowPos, zbias, jitMat);
}

oColor = phong(n, l, v,
surface.ambient.rgb, light.ambient.rgb,
surface.diffuse.rgb, light.diffuse.rgb,
surface.specular.rgb, light.specular.rgb,
surface.shininess, shadow);
if (c_showNoise)
oColor *= theta;
}
14 changes: 14 additions & 0 deletions shadowmapping-poisson-stable/shaders/shadowMap.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#include "common/transforms.h"

layout(location = 0) in vec4 position;

out gl_PerVertex {
vec4 gl_Position;
};

void main()
{
gl_Position = worldLightProj * position;
}
21 changes: 21 additions & 0 deletions shadowmapping-poisson-stable/shaders/transform.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#version 450
#extension GL_GOOGLE_include_directive : enable
#include "common/transforms.h"

layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;

layout(location = 0) out vec4 oWorldPos;
layout(location = 1) out vec3 oViewPos;
layout(location = 2) out vec3 oViewNormal;
out gl_PerVertex {
vec4 gl_Position;
};

void main()
{
oWorldPos = world * position;
oViewPos = (worldView * position).xyz;
oViewNormal = mat3(normalView) * normal;
gl_Position = worldViewProj * position;
}
Loading

0 comments on commit c4f64e3

Please sign in to comment.