diff --git a/source/blender/blenlib/BLI_math_base.hh b/source/blender/blenlib/BLI_math_base.hh index 9e56a0de401..6176f9724bf 100644 --- a/source/blender/blenlib/BLI_math_base.hh +++ b/source/blender/blenlib/BLI_math_base.hh @@ -171,6 +171,11 @@ template inline T pow(const T &x, const T &power) return std::pow(x, power); } +template inline T square(const T &a) +{ + return a * a; +} + template inline T exp(const T &x) { return std::exp(x); diff --git a/source/blender/blenlib/BLI_math_vector.hh b/source/blender/blenlib/BLI_math_vector.hh index 4051b21dae7..f59848fc3f4 100644 --- a/source/blender/blenlib/BLI_math_vector.hh +++ b/source/blender/blenlib/BLI_math_vector.hh @@ -206,6 +206,17 @@ template return result; } +/** Per-element square. */ +template +[[nodiscard]] inline VecBase square(const VecBase &a) +{ + VecBase result; + for (int i = 0; i < Size; i++) { + result[i] = math::square(a[i]); + } + return result; +} + /* Per-element exponent. */ template [[nodiscard]] inline VecBase exp(const VecBase &x) { diff --git a/source/blender/blenlib/tests/BLI_math_vector_test.cc b/source/blender/blenlib/tests/BLI_math_vector_test.cc index 0a4fa80e79c..0e99619d210 100644 --- a/source/blender/blenlib/tests/BLI_math_vector_test.cc +++ b/source/blender/blenlib/tests/BLI_math_vector_test.cc @@ -180,4 +180,13 @@ TEST(math_vector, exp) EXPECT_NEAR(result.z, 20.085536923187668f, 1e-6f); } +TEST(math_vector, square) +{ + const float3 a(1.0f, 2.0f, 3.0f); + const float3 result = math::square(a); + EXPECT_NEAR(result.x, 1.0f, 1e-6f); + EXPECT_NEAR(result.y, 4.0f, 1e-6f); + EXPECT_NEAR(result.z, 9.0f, 1e-6f); +} + } // namespace blender::tests diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_eval_lib.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_eval_lib.glsl index 4524e3981c7..23ac010ba89 100644 --- a/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_eval_lib.glsl +++ b/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_eval_lib.glsl @@ -86,7 +86,7 @@ vec3 lightprobe_irradiance_grid_bias_sample_coord(IrradianceGridData grid_data, /* Biases. See McGuire's presentation. */ positional_weight += 0.001; - geometry_weight = square_f(geometry_weight) + 0.2 + grid_data.facing_bias; + geometry_weight = square(geometry_weight) + 0.2 + grid_data.facing_bias; trilinear_weights[i] = saturate(positional_weight * geometry_weight * validity_weight); total_weight += trilinear_weights[i]; diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_load_comp.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_load_comp.glsl index 4ebd8e0213d..1dd3dd0cbe4 100644 --- a/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_load_comp.glsl +++ b/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_load_comp.glsl @@ -81,7 +81,7 @@ void main() continue; } float dist_sqr = length_squared(vec3(offset)); - if (dist_sqr > square_f(dilation_radius)) { + if (dist_sqr > square(dilation_radius)) { continue; } float weight = 1.0 / dist_sqr; diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_offset_comp.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_offset_comp.glsl index 2754c2c6b94..dc0ab649bf0 100644 --- a/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_offset_comp.glsl +++ b/source/blender/draw/engines/eevee_next/shaders/eevee_lightprobe_irradiance_offset_comp.glsl @@ -13,8 +13,8 @@ int find_closest_surfel(ivec3 grid_coord, vec3 P) { int surfel_first = imageLoad(cluster_list_img, grid_coord).r; - float search_radius_sqr = square_f(capture_info_buf.max_virtual_offset + - capture_info_buf.min_distance_to_surface); + float search_radius_sqr = square(capture_info_buf.max_virtual_offset + + capture_info_buf.min_distance_to_surface); int closest_surfel = -1; float closest_distance_sqr = 1e10; diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_bilateral_comp.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_bilateral_comp.glsl index 861b8266009..ac3f5115209 100644 --- a/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_bilateral_comp.glsl +++ b/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_bilateral_comp.glsl @@ -34,7 +34,7 @@ float bilateral_depth_weight(vec3 center_N, vec3 center_P, vec3 sample_P) float bilateral_spatial_weight(float sigma, vec2 offset_from_center) { /* From https://github.com/tranvansang/bilateral-filter/blob/master/fshader.frag */ - float fac = -1.0 / square_f(sigma); + float fac = -1.0 / square(sigma); /* Take two standard deviation. */ fac *= 2.0; float weight = exp2(fac * length_squared(offset_from_center)); diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_temporal_comp.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_temporal_comp.glsl index d980ac15e8c..998c6710485 100644 --- a/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_temporal_comp.glsl +++ b/source/blender/draw/engines/eevee_next/shaders/eevee_ray_denoise_temporal_comp.glsl @@ -60,14 +60,14 @@ LocalStatistics local_statistics_get(ivec2 texel, vec3 center_radiance) /* Use YCoCg for clamping and accumulation to avoid color shift artifacts. */ vec3 radiance_YCoCg = colorspace_YCoCg_from_scene_linear(radiance.rgb); result.mean += radiance_YCoCg; - result.moment += square_f(radiance_YCoCg); + result.moment += square(radiance_YCoCg); weight_accum += 1.0; } } float inv_weight = safe_rcp(weight_accum); result.mean *= inv_weight; result.moment *= inv_weight; - result.variance = abs(result.moment - square_f(result.mean)); + result.variance = abs(result.moment - square(result.mean)); result.deviation = max(vec3(1e-4), sqrt(result.variance)); result.clamp_min = result.mean - result.deviation; result.clamp_max = result.mean + result.deviation; diff --git a/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl b/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl index 933de43002c..8cf07263af4 100644 --- a/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl +++ b/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl @@ -57,19 +57,19 @@ uint square_uint(uint v) { return v * v; } -float square_f(float v) +float square(float v) { return v * v; } -vec2 square_f(vec2 v) +vec2 square(vec2 v) { return v * v; } -vec3 square_f(vec3 v) +vec3 square(vec3 v) { return v * v; } -vec4 square_f(vec4 v) +vec4 square(vec4 v) { return v * v; }