Rewrite textureProjLod usage to avoid a Vulkan validation error.

This commit is contained in:
Dario 2025-05-14 11:15:28 -03:00
parent 428a762e98
commit 499fff51fb
4 changed files with 35 additions and 25 deletions

View file

@ -286,6 +286,17 @@ vec2 sdf_to_screen_uv(vec2 p_sdf) {
return p_sdf * canvas_data.sdf_to_screen;
}
// Emulate textureProjLod by doing it manually because the source texture is not an actual depth texture that can be used for this operation.
// Since the sampler is configured to nearest use one textureGather tap to emulate bilinear.
float texture_shadow(vec4 p) {
// Manually round p to the nearest texel because textureGather uses strange rounding rules.
vec2 unit_p = floor(p.xy / canvas_data.shadow_pixel_size) * canvas_data.shadow_pixel_size;
float depth = p.z;
float fx = fract(p.x / canvas_data.shadow_pixel_size);
vec2 tap = textureGather(sampler2D(shadow_atlas_texture, shadow_sampler), unit_p.xy).zw;
return mix(step(tap.y, depth), step(tap.x, depth), fx);
}
#GLOBALS
#ifdef LIGHT_CODE_USED
@ -396,32 +407,32 @@ vec4 light_shadow_compute(uint light_base, vec4 light_color, vec4 shadow_uv
uint shadow_mode = light_array.data[light_base].flags & LIGHT_FLAGS_FILTER_MASK;
if (shadow_mode == LIGHT_FLAGS_SHADOW_NEAREST) {
shadow = textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv, 0.0).x;
shadow = texture_shadow(shadow_uv);
} else if (shadow_mode == LIGHT_FLAGS_SHADOW_PCF5) {
vec4 shadow_pixel_size = vec4(light_array.data[light_base].shadow_pixel_size, 0.0, 0.0, 0.0);
shadow = 0.0;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 2.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 2.0, 0.0).x;
shadow += texture_shadow(shadow_uv - shadow_pixel_size * 2.0);
shadow += texture_shadow(shadow_uv - shadow_pixel_size);
shadow += texture_shadow(shadow_uv);
shadow += texture_shadow(shadow_uv + shadow_pixel_size);
shadow += texture_shadow(shadow_uv + shadow_pixel_size * 2.0);
shadow /= 5.0;
} else { //PCF13
vec4 shadow_pixel_size = vec4(light_array.data[light_base].shadow_pixel_size, 0.0, 0.0, 0.0);
shadow = 0.0;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 6.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 5.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 4.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 3.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size * 2.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv - shadow_pixel_size, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 2.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 3.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 4.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 5.0, 0.0).x;
shadow += textureProjLod(sampler2DShadow(shadow_atlas_texture, shadow_sampler), shadow_uv + shadow_pixel_size * 6.0, 0.0).x;
shadow += texture_shadow(shadow_uv - shadow_pixel_size * 6.0);
shadow += texture_shadow(shadow_uv - shadow_pixel_size * 5.0);
shadow += texture_shadow(shadow_uv - shadow_pixel_size * 4.0);
shadow += texture_shadow(shadow_uv - shadow_pixel_size * 3.0);
shadow += texture_shadow(shadow_uv - shadow_pixel_size * 2.0);
shadow += texture_shadow(shadow_uv - shadow_pixel_size);
shadow += texture_shadow(shadow_uv);
shadow += texture_shadow(shadow_uv + shadow_pixel_size);
shadow += texture_shadow(shadow_uv + shadow_pixel_size * 2.0);
shadow += texture_shadow(shadow_uv + shadow_pixel_size * 3.0);
shadow += texture_shadow(shadow_uv + shadow_pixel_size * 4.0);
shadow += texture_shadow(shadow_uv + shadow_pixel_size * 5.0);
shadow += texture_shadow(shadow_uv + shadow_pixel_size * 6.0);
shadow /= 13.0;
}

View file

@ -109,8 +109,8 @@ layout(set = 0, binding = 1, std140) uniform CanvasData {
uint directional_light_count;
float tex_to_sdf;
float shadow_pixel_size;
uint flags;
uint pad2;
}
canvas_data;