Skip to content

Commit

Permalink
RTSS: PSSM3 shadows - correctly handle sampling behind far plane
Browse files Browse the repository at this point in the history
  • Loading branch information
paroj committed May 16, 2020
1 parent 1405bd6 commit e34f532
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Media/RTShaderLib/GLSL/SGXLib_IntegratedPSSM.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ void SGX_ShadowPCF4(in sampler2D shadowMap, in vec4 shadowMapPos, in vec2 offset
vec2 uv = shadowMapPos.xy;
vec3 o = vec3(offset, -offset.x) * 0.3;

float depth = texture2D(shadowMap, uv.xy - o.xy).r;
if(depth >= 1.0)
{
// early out, if sampling behind far plane
c = 1.0;
return;
}

// Note: We using 2x2 PCF. Good enough and is a lot faster.
c = (shadowMapPos.z <= texture2D(shadowMap, uv.xy - o.xy).r) ? 1.0 : 0.0; // top left
c = (shadowMapPos.z <= depth) ? 1.0 : 0.0; // top left
c += (shadowMapPos.z <= texture2D(shadowMap, uv.xy + o.xy).r) ? 1.0 : 0.0; // bottom right
c += (shadowMapPos.z <= texture2D(shadowMap, uv.xy + o.zy).r) ? 1.0 : 0.0; // bottom left
c += (shadowMapPos.z <= texture2D(shadowMap, uv.xy - o.zy).r) ? 1.0 : 0.0; // top right
Expand Down
7 changes: 7 additions & 0 deletions Media/RTShaderLib/HLSL_Cg/SGXLib_IntegratedPSSM.cg
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ void SGX_ShadowPCF4(sampler2D shadowMap, float4 shadowMapPos, float2 offset, out
float2 uv = shadowMapPos.xy;
float3 o = float3(offset, -offset.x) * 0.3f;

float depth = tex2D(shadowMap, uv.xy - o.xy).r;
if(depth >= 1.0)
{
// early out, if sampling behind far plane
c = 1.0;
return;
}
// Note: We using 2x2 PCF. Good enough and is a lot faster.
c = (shadowMapPos.z <= tex2D(shadowMap, uv.xy - o.xy).r) ? 1 : 0; // top left
c += (shadowMapPos.z <= tex2D(shadowMap, uv.xy + o.xy).r) ? 1 : 0; // bottom right
Expand Down

0 comments on commit e34f532

Please sign in to comment.