Skip to content
Snippets Groups Projects
Commit 0cef46d9 authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#82] Lets try moment shadow mapping

parent a3001dec
No related branches found
No related tags found
1 merge request!70Resolve "Voxel cone tracing"
Pipeline #25976 passed
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable #extension GL_GOOGLE_include_directive : enable
#include "shadowMapping.inc" #include "lightInfo.inc"
layout(set=0, binding=0) uniform LightInfoBuffer { layout(set=0, binding=0) uniform LightInfoBuffer {
LightInfo lightInfo; LightInfo lightInfo;
...@@ -14,6 +14,6 @@ layout(location = 0) in vec4 passPos; ...@@ -14,6 +14,6 @@ layout(location = 0) in vec4 passPos;
void main() { void main() {
float z = passPos.z / passPos.w; float z = passPos.z / passPos.w;
vec2 zWarped = applyDepthWarp(z, lightInfo.warps); float z2 = z*z;
outMoments = vec4(zWarped, zWarped*zWarped); outMoments = vec4(z, z2, z2*z, z2*z2);
} }
\ No newline at end of file
...@@ -3,30 +3,39 @@ ...@@ -3,30 +3,39 @@
#include "lightInfo.inc" #include "lightInfo.inc"
vec2 applyDepthWarp(float z, vec2 warp){ // nice math blob from the moment shadow mapping presentation
z = 2 * z - 1; float ComputeMSMShadowIntensity(vec4 _4Moments, float FragmentDepth, float DepthBias, float MomentBias)
float positive = exp( z * warp.x);
float negative = -exp(-z * warp.y);
return vec2(positive, negative);
}
float rescale(float a, float b, float v)
{ {
return clamp((v - a) / (b - a), 0, 1); vec4 b=mix(_4Moments, vec4(0.5f,0.5f,0.5f,0.5f),MomentBias);
} vec3 z;
z[0]=FragmentDepth-DepthBias;
float reduceVSMBleeding(float shadowValue, float newMin) float L32D22=-b[0] * b[1] + b[2];
{ float D22= -b[0] * b[0] + b[1];
return rescale(newMin, 1.0f, shadowValue); float SquaredDepthVariance=-b[1]*b[1]+b[3];
} float D33D22=dot(vec2(SquaredDepthVariance,-L32D22),
vec2(D22, L32D22));
float chebyshevInequality(float mean, float meanSquare, float sampleIn){
float variance = meanSquare - mean * mean; float InvD22=1.0f/D22;
float d = sampleIn - mean; float L32=L32D22*InvD22;
float pMax = clamp(variance / (variance + d*d), 0, 1); vec3 c=vec3(1.0f,z[0],z[0]*z[0]);
pMax = reduceVSMBleeding(pMax, 0.1); c[1]-=b.x;
c[2]-=b.y+L32*c[1];
return pMax; c[1]*=InvD22;
c[2]*=D22/D33D22;
c[1]-=L32*c[2];
c[0]-=dot(c.yz,b.xy);
float p=c[1]/c[2];
float q=c[0]/c[2];
float r=sqrt((p*p*0.25f)-q);
z[1]=-p*0.5f-r;
z[2]=-p*0.5f+r;
vec4 Switch=
(z[2]<z[0])?vec4(z[1],z[0],1.0f,1.0f):(
(z[1]<z[0])?vec4(z[0],z[1],0.0f,1.0f):
vec4(0.0f,0.0f,0.0f,0.0f));
float Quotient=(Switch[0]*z[2]-b[0]*(Switch[0]+z[2])+b[1])
/((z[2]-Switch[1])*(z[0]-z[1]));
return clamp(Switch[2]+Switch[3]*Quotient, 0, 1);
} }
float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sampler shadowMapSampler){ float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sampler shadowMapSampler){
...@@ -39,17 +48,13 @@ float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sample ...@@ -39,17 +48,13 @@ float shadowTest(vec3 worldPos, LightInfo lightInfo, texture2D shadowMap, sample
} }
lightPos.z = clamp(lightPos.z, 0, 1); lightPos.z = clamp(lightPos.z, 0, 1);
vec2 warpedSample = applyDepthWarp(lightPos.z, lightInfo.warps);
// using exponential variance shadow mapping
vec4 shadowMapSample = texture(sampler2D(shadowMap, shadowMapSampler), lightPos.xy);
vec2 positiveMoments = shadowMapSample.rb;
vec2 negativeMoments = shadowMapSample.ga;
float s1 = chebyshevInequality(positiveMoments.r, positiveMoments.g, warpedSample.x);
float s2 = chebyshevInequality(negativeMoments.r, negativeMoments.g, warpedSample.y);
return min(s1, s2); vec4 shadowMapSample = texture(sampler2D(shadowMap, shadowMapSampler), lightPos.xy);
float depthBias = 0.f;
float momentBias = 0.000002;
return 1-ComputeMSMShadowIntensity(shadowMapSample, lightPos.z, depthBias, momentBias);
} }
#endif // #ifndef SHADOW_MAPPING_INC #endif // #ifndef SHADOW_MAPPING_INC
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment