@@ -286,6 +286,17 @@ vec2 sdf_to_screen_uv(vec2 p_sdf) {
286286 return p_sdf * canvas_data.sdf_to_screen;
287287}
288288
289+ // Emulate textureProjLod by doing it manually because the source texture is not an actual depth texture that can be used for this operation.
290+ // Since the sampler is configured to nearest use one textureGather tap to emulate bilinear.
291+ float texture_shadow(vec4 p) {
292+ // Manually round p to the nearest texel because textureGather uses strange rounding rules.
293+ vec2 unit_p = floor (p.xy / canvas_data.shadow_pixel_size) * canvas_data.shadow_pixel_size;
294+ float depth = p.z;
295+ float fx = fract (p.x / canvas_data.shadow_pixel_size);
296+ vec2 tap = textureGather(sampler2D (shadow_atlas_texture, shadow_sampler), unit_p.xy).zw;
297+ return mix (step (tap.y, depth), step (tap.x, depth), fx);
298+ }
299+
289300#GLOBALS
290301
291302#ifdef LIGHT_CODE_USED
@@ -396,32 +407,32 @@ vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv
396407 uint shadow_mode = light_array.data[light_base].flags & LIGHT_FLAGS_FILTER_MASK;
397408
398409 if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) {
399- shadow = textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv, 0.0 ).x ;
410+ shadow = texture_shadow( shadow_uv) ;
400411 } else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) {
401412 vec4 shadow_pixel_size = vec4 (light_array.data[light_base].shadow_pixel_size, 0.0 , 0.0 , 0.0 );
402413 shadow = 0.0 ;
403- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 2.0 , 0.0 ).x ;
404- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size, 0.0 ).x ;
405- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv, 0.0 ).x ;
406- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size, 0.0 ).x ;
407- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 2.0 , 0.0 ).x ;
414+ shadow += texture_shadow( shadow_uv - shadow_pixel_size * 2.0 ) ;
415+ shadow += texture_shadow( shadow_uv - shadow_pixel_size) ;
416+ shadow += texture_shadow( shadow_uv) ;
417+ shadow += texture_shadow( shadow_uv + shadow_pixel_size) ;
418+ shadow += texture_shadow( shadow_uv + shadow_pixel_size * 2.0 ) ;
408419 shadow /= 5.0 ;
409420 } else { // PCF13
410421 vec4 shadow_pixel_size = vec4 (light_array.data[light_base].shadow_pixel_size, 0.0 , 0.0 , 0.0 );
411422 shadow = 0.0 ;
412- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 6.0 , 0.0 ).x ;
413- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 5.0 , 0.0 ).x ;
414- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 4.0 , 0.0 ).x ;
415- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 3.0 , 0.0 ).x ;
416- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 2.0 , 0.0 ).x ;
417- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size, 0.0 ).x ;
418- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv, 0.0 ).x ;
419- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size, 0.0 ).x ;
420- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 2.0 , 0.0 ).x ;
421- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 3.0 , 0.0 ).x ;
422- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 4.0 , 0.0 ).x ;
423- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 5.0 , 0.0 ).x ;
424- shadow += textureProjLod( sampler2DShadow (shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 6.0 , 0.0 ).x ;
423+ shadow += texture_shadow( shadow_uv - shadow_pixel_size * 6.0 ) ;
424+ shadow += texture_shadow( shadow_uv - shadow_pixel_size * 5.0 ) ;
425+ shadow += texture_shadow( shadow_uv - shadow_pixel_size * 4.0 ) ;
426+ shadow += texture_shadow( shadow_uv - shadow_pixel_size * 3.0 ) ;
427+ shadow += texture_shadow( shadow_uv - shadow_pixel_size * 2.0 ) ;
428+ shadow += texture_shadow( shadow_uv - shadow_pixel_size) ;
429+ shadow += texture_shadow( shadow_uv) ;
430+ shadow += texture_shadow( shadow_uv + shadow_pixel_size) ;
431+ shadow += texture_shadow( shadow_uv + shadow_pixel_size * 2.0 ) ;
432+ shadow += texture_shadow( shadow_uv + shadow_pixel_size * 3.0 ) ;
433+ shadow += texture_shadow( shadow_uv + shadow_pixel_size * 4.0 ) ;
434+ shadow += texture_shadow( shadow_uv + shadow_pixel_size * 5.0 ) ;
435+ shadow += texture_shadow( shadow_uv + shadow_pixel_size * 6.0 ) ;
425436 shadow /= 13.0 ;
426437 }
427438
0 commit comments