Godot Engine – Multi-platform 2D and 3D game engine
Extract bicubic lightmap filtering functions to their own file
BlueCube33102b1c48e5d329
diff --git a/servers/rendering/renderer_rd/shaders/bicubic_filter_inc.glsl b/servers/rendering/renderer_rd/shaders/bicubic_filter_inc.glslnew file mode 100644index 00000000000..cb720904b14--- /dev/null+++ b/servers/rendering/renderer_rd/shaders/bicubic_filter_inc.glsl@@ -0,0 +1,58 @@+// w0, w1, w2, and w3 are the four cubic B-spline basis functions+float w0(float a) {+ return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);+}++float w1(float a) {+ return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);+}++float w2(float a) {+ return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);+}++float w3(float a) {+ return (1.0 / 6.0) * (a * a * a);+}++// g0 and g1 are the two amplitude functions+float g0(float a) {+ return w0(a) + w1(a);+}++float g1(float a) {+ return w2(a) + w3(a);+}++// h0 and h1 are the two offset functions+float h0(float a) {+ return -1.0 + w1(a) / (w0(a) + w1(a));+}++float h1(float a) {+ return 1.0 + w3(a) / (w2(a) + w3(a));+}++vec4 texture_array_bicubic(texture2DArray tex, vec3 uv, vec2 texture_size) {+ vec2 texel_size = vec2(1.0) / texture_size;++ uv.xy = uv.xy * texture_size + vec2(0.5);++ vec2 iuv = floor(uv.xy);+ vec2 fuv = fract(uv.xy);++ float g0x = g0(fuv.x);+ float g1x = g1(fuv.x);+ float h0x = h0(fuv.x);+ float h1x = h1(fuv.x);+ float h0y = h0(fuv.y);+ float h1y = h1(fuv.y);++ vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;+ vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;+ vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;+ vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;++ return (g0(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p0, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p1, uv.z)))) ++ (g1(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p2, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p3, uv.z))));+}diff --git a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glslindex 8c6115ec900..6ae49aa02f8 100644--- a/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl+++ b/servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl@@ -917,64 +917,7 @@ layout(location = 9) in float dp_clip;layout(location = 10) in flat uint instance_index_interp;#ifdef USE_LIGHTMAP-// w0, w1, w2, and w3 are the four cubic B-spline basis functions-float w0(float a) {- return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);-}--float w1(float a) {- return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);-}--float w2(float a) {- return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);-}--float w3(float a) {- return (1.0 / 6.0) * (a * a * a);-}--// g0 and g1 are the two amplitude functions-float g0(float a) {- return w0(a) + w1(a);-}--float g1(float a) {- return w2(a) + w3(a);-}--// h0 and h1 are the two offset functions-float h0(float a) {- return -1.0 + w1(a) / (w0(a) + w1(a));-}--float h1(float a) {- return 1.0 + w3(a) / (w2(a) + w3(a));-}--vec4 textureArray_bicubic(texture2DArray tex, vec3 uv, vec2 texture_size) {- vec2 texel_size = vec2(1.0) / texture_size;-- uv.xy = uv.xy * texture_size + vec2(0.5);-- vec2 iuv = floor(uv.xy);- vec2 fuv = fract(uv.xy);-- float g0x = g0(fuv.x);- float g1x = g1(fuv.x);- float h0x = h0(fuv.x);- float h1x = h1(fuv.x);- float h0y = h0(fuv.y);- float h1y = h1(fuv.y);-- vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;- vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;- vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;- vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;-- return (g0(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p0, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p1, uv.z)))) +- (g1(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p2, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p3, uv.z))));-}+#include "../bicubic_filter_inc.glsl"#endif //USE_LIGHTMAP#ifdef USE_MULTIVIEW@@ -1840,10 +1783,10 @@ void fragment_shader(in SceneData scene_data) {vec3 lm_light_l1p1;if (sc_use_lightmap_bicubic_filter()) {- lm_light_l0 = textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 0.0), lightmaps.data[ofs].light_texture_size).rgb;- lm_light_l1n1 = (textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 1.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;- lm_light_l1_0 = (textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 2.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;- lm_light_l1p1 = (textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 3.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;+ lm_light_l0 = texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 0.0), lightmaps.data[ofs].light_texture_size).rgb;+ lm_light_l1n1 = (texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 1.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;+ lm_light_l1_0 = (texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 2.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;+ lm_light_l1p1 = (texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 3.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0;} else {lm_light_l0 = textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb;lm_light_l1n1 = (textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb - vec3(0.5)) * 2.0;@@ -1921,7 +1864,7 @@ void fragment_shader(in SceneData scene_data) {} else {if (sc_use_lightmap_bicubic_filter()) {- ambient_light += textureArray_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization;+ ambient_light += texture_array_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization;} else {ambient_light += textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization;}@@ -2381,7 +2324,7 @@ void fragment_shader(in SceneData scene_data) {const vec3 uvw = vec3(scaled_uv, float(slice));if (sc_use_lightmap_bicubic_filter()) {- shadowmask = textureArray_bicubic(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], uvw, lightmaps.data[ofs].light_texture_size).x;+ shadowmask = texture_array_bicubic(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], uvw, lightmaps.data[ofs].light_texture_size).x;} else {shadowmask = textureLod(sampler2DArray(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).x;}diff --git a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glslindex 23bb4c9f059..dd51dfd1843 100644--- a/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl+++ b/servers/rendering/renderer_rd/shaders/forward_mobile/scene_forward_mobile.glsl@@ -889,64 +889,7 @@ layout(location = 13) in highp vec4 prev_screen_position;#endif#ifdef USE_LIGHTMAP-// w0, w1, w2, and w3 are the four cubic B-spline basis functions-float w0(float a) {- return (1.0 / 6.0) * (a * (a * (-a + 3.0) - 3.0) + 1.0);-}--float w1(float a) {- return (1.0 / 6.0) * (a * a * (3.0 * a - 6.0) + 4.0);-}--float w2(float a) {- return (1.0 / 6.0) * (a * (a * (-3.0 * a + 3.0) + 3.0) + 1.0);-}--float w3(float a) {- return (1.0 / 6.0) * (a * a * a);-}--// g0 and g1 are the two amplitude functions-float g0(float a) {- return w0(a) + w1(a);-}--float g1(float a) {- return w2(a) + w3(a);-}--// h0 and h1 are the two offset functions-float h0(float a) {- return -1.0 + w1(a) / (w0(a) + w1(a));-}--float h1(float a) {- return 1.0 + w3(a) / (w2(a) + w3(a));-}--vec4 textureArray_bicubic(texture2DArray tex, vec3 uv, vec2 texture_size) {- vec2 texel_size = vec2(1.0) / texture_size;-- uv.xy = uv.xy * texture_size + vec2(0.5);-- vec2 iuv = floor(uv.xy);- vec2 fuv = fract(uv.xy);-- float g0x = g0(fuv.x);- float g1x = g1(fuv.x);- float h0x = h0(fuv.x);- float h1x = h1(fuv.x);- float h0y = h0(fuv.y);- float h1y = h1(fuv.y);-- vec2 p0 = (vec2(iuv.x + h0x, iuv.y + h0y) - vec2(0.5)) * texel_size;- vec2 p1 = (vec2(iuv.x + h1x, iuv.y + h0y) - vec2(0.5)) * texel_size;- vec2 p2 = (vec2(iuv.x + h0x, iuv.y + h1y) - vec2(0.5)) * texel_size;- vec2 p3 = (vec2(iuv.x + h1x, iuv.y + h1y) - vec2(0.5)) * texel_size;-- return (g0(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p0, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p1, uv.z)))) +- (g1(fuv.y) * (g0x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p2, uv.z)) + g1x * texture(sampler2DArray(tex, SAMPLER_LINEAR_CLAMP), vec3(p3, uv.z))));-}+#include "../bicubic_filter_inc.glsl"#endif //USE_LIGHTMAP#ifdef USE_MULTIVIEW@@ -1731,10 +1674,10 @@ void main() {hvec3 lm_light_l1p1;if (sc_use_lightmap_bicubic_filter()) {- lm_light_l0 = hvec3(textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 0.0), lightmaps.data[ofs].light_texture_size).rgb);- lm_light_l1n1 = hvec3((textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 1.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0);- lm_light_l1_0 = hvec3((textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 2.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0);- lm_light_l1p1 = hvec3((textureArray_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 3.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0);+ lm_light_l0 = hvec3(texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 0.0), lightmaps.data[ofs].light_texture_size).rgb);+ lm_light_l1n1 = hvec3((texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 1.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0);+ lm_light_l1_0 = hvec3((texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 2.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0);+ lm_light_l1p1 = hvec3((texture_array_bicubic(lightmap_textures[ofs], uvw + vec3(0.0, 0.0, 3.0), lightmaps.data[ofs].light_texture_size).rgb - vec3(0.5)) * 2.0);} else {lm_light_l0 = hvec3(textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 0.0), 0.0).rgb);lm_light_l1n1 = hvec3((textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw + vec3(0.0, 0.0, 1.0), 0.0).rgb - vec3(0.5)) * 2.0);@@ -1806,7 +1749,7 @@ void main() {} else {if (sc_use_lightmap_bicubic_filter()) {- ambient_light += hvec3(textureArray_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization);+ ambient_light += hvec3(texture_array_bicubic(lightmap_textures[ofs], uvw, lightmaps.data[ofs].light_texture_size).rgb * lightmaps.data[ofs].exposure_normalization);} else {ambient_light += hvec3(textureLod(sampler2DArray(lightmap_textures[ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).rgb * lightmaps.data[ofs].exposure_normalization);}@@ -2002,7 +1945,7 @@ void main() {const vec3 uvw = vec3(scaled_uv, float(slice));if (sc_use_lightmap_bicubic_filter()) {- shadowmask = half(textureArray_bicubic(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], uvw, lightmaps.data[ofs].light_texture_size).x);+ shadowmask = half(texture_array_bicubic(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], uvw, lightmaps.data[ofs].light_texture_size).x);} else {shadowmask = half(textureLod(sampler2DArray(lightmap_textures[MAX_LIGHTMAP_TEXTURES + ofs], SAMPLER_LINEAR_CLAMP), uvw, 0.0).x);}