Skip to content
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

[3.x] Fix GLES 2 SpotLight bug with shadow filter mode #69826

Merged
merged 1 commit into from
Dec 13, 2022
Merged
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
16 changes: 6 additions & 10 deletions drivers/gles2/shaders/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -1499,19 +1499,19 @@ LIGHT_SHADER_CODE
#endif

#define SAMPLE_SHADOW_TEXEL(p_shadow, p_pos, p_depth) step(p_depth, SHADOW_DEPTH(texture2D(p_shadow, p_pos)))
#define SAMPLE_SHADOW_TEXEL_PROJ(p_shadow, p_pos) step(p_pos.z, SHADOW_DEPTH(texture2DProj(p_shadow, p_pos)))

float sample_shadow(highp sampler2D shadow, highp vec4 spos) {
spos.xyz /= spos.w;
vec2 pos = spos.xy;
float depth = spos.z;

#ifdef SHADOW_MODE_PCF_13

// Soft PCF filter adapted from three.js:
// https://github.com/mrdoob/three.js/blob/0c815022849389cbe6de14a93e1c2fc7e4b21c18/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js#L148-L182
// This method actually uses 16 shadow samples. This soft filter isn't needed in GLES3
// as we can use hardware-based linear filtering instead of emulating it in the shader
// like we're doing here.
spos.xyz /= spos.w;
vec2 pos = spos.xy;
float depth = spos.z;
vec2 f = fract(pos * (1.0 / shadow_pixel_size) + 0.5);
pos -= f * shadow_pixel_size;

Expand Down Expand Up @@ -1549,10 +1549,6 @@ float sample_shadow(highp sampler2D shadow, highp vec4 spos) {

#ifdef SHADOW_MODE_PCF_5

spos.xyz /= spos.w;
vec2 pos = spos.xy;
float depth = spos.z;

float avg = SAMPLE_SHADOW_TEXEL(shadow, pos, depth);
avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(shadow_pixel_size.x, 0.0), depth);
avg += SAMPLE_SHADOW_TEXEL(shadow, pos + vec2(-shadow_pixel_size.x, 0.0), depth);
Expand All @@ -1562,9 +1558,9 @@ float sample_shadow(highp sampler2D shadow, highp vec4 spos) {

#endif

#if !defined(SHADOW_MODE_PCF_5) || !defined(SHADOW_MODE_PCF_13)
#if !defined(SHADOW_MODE_PCF_5) && !defined(SHADOW_MODE_PCF_13)

return SAMPLE_SHADOW_TEXEL_PROJ(shadow, spos);
return SAMPLE_SHADOW_TEXEL(shadow, pos, depth);
#endif
}

Expand Down