BLI: Add square function

This patch adds the square function for both float and vector math BLI
libraries. It also renames the corresponding GLSL function for
compatibility.

Pull Request: https://projects.blender.org/blender/blender/pulls/111217
This commit is contained in:
Omar Emara
2023-08-17 15:05:52 +02:00
committed by Omar Emara
parent ab18bd6f84
commit 110cb5d0d5
9 changed files with 36 additions and 11 deletions

View File

@@ -171,6 +171,11 @@ template<typename T> inline T pow(const T &x, const T &power)
return std::pow(x, power);
}
template<typename T> inline T square(const T &a)
{
return a * a;
}
template<typename T> inline T exp(const T &x)
{
return std::exp(x);

View File

@@ -206,6 +206,17 @@ template<typename T, int Size>
return result;
}
/** Per-element square. */
template<typename T, int Size>
[[nodiscard]] inline VecBase<T, Size> square(const VecBase<T, Size> &a)
{
VecBase<T, Size> result;
for (int i = 0; i < Size; i++) {
result[i] = math::square(a[i]);
}
return result;
}
/* Per-element exponent. */
template<typename T, int Size> [[nodiscard]] inline VecBase<T, Size> exp(const VecBase<T, Size> &x)
{

View File

@@ -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

View File

@@ -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];

View File

@@ -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;

View File

@@ -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;

View File

@@ -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));

View File

@@ -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;

View File

@@ -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;
}