Blender V5.3
gpu_shader_math_vector_compare.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
9/* ---------------------------------------------------------------------- */
12
16template<typename VecT> bool is_zero(VecT vec)
17{
18 return all(equal(vec, VecT(0.0f)));
19}
20template bool is_zero<float2>(float2);
21template bool is_zero<float3>(float3);
22template bool is_zero<float4>(float4);
23
27template<typename VecT> bool is_any_zero(VecT vec)
28{
29 return any(equal(vec, VecT(0.0f)));
30}
34
38template<typename VecT> bool is_equal(VecT a, VecT b, float epsilon)
39{
40 return all(lessThanEqual(abs(a - b), VecT(epsilon)));
41}
42template bool is_equal<float2>(float2, float2, float);
43template bool is_equal<float3>(float3, float3, float);
44template bool is_equal<float4>(float4, float4, float);
45
50template<typename VecT, int dim>
51bool almost_equal_relative(VecT a, VecT b, const float epsilon_factor)
52{
53 for (int i = 0; i < dim; i++) {
54 if (abs(a[i] - b[i]) > epsilon_factor * abs(a[i])) {
55 return false;
56 }
57 }
58 return true;
59}
60template bool almost_equal_relative<float2, 2>(float2 a, float2 b, const float epsilon_factor);
61template bool almost_equal_relative<float3, 3>(float3 a, float3 b, const float epsilon_factor);
62template bool almost_equal_relative<float4, 4>(float4 a, float4 b, const float epsilon_factor);
63
64/* Checks are flipped so NAN doesn't assert because we're making sure the value was
65 * normalized and in the case we don't want NAN to be raising asserts since there
66 * is nothing to be done in that case. */
67template<typename VecT> bool is_unit_scale(VecT v)
68{
69 constexpr float assert_unit_epsilon = 0.0002f;
70 float test_unit = dot(v, v);
71 return (!(abs(test_unit - 1.0f) >= assert_unit_epsilon) ||
72 !(abs(test_unit) >= assert_unit_epsilon));
73}
77
VecBase< bool, D > lessThanEqual(VecOp< T, D >, VecOp< T, D >) RET
bool any(VecOp< bool, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
bool all(VecOp< bool, D >) RET
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
bool is_equal(float a, float b, const float epsilon)
bool is_unit_scale(float4x4 m)
bool is_zero(float3x3 a)
bool is_any_zero(VecT vec)
bool almost_equal_relative(VecT a, VecT b, const float epsilon_factor)