Blender V5.3
gpu_shader_math_vector_safe.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
13template<typename VecT> VecT safe_mod(VecT a, VecT b)
14{
15 return select(VecT(0), mod(a, b), notEqual(b, VecT(0)));
16}
20
26{
27 return (b != 0.0f) ? mod(a, float2(b)) : float2(0);
28}
30{
31 return (b != 0.0f) ? mod(a, float3(b)) : float3(0);
32}
34{
35 return (b != 0.0f) ? mod(a, float4(b)) : float4(0);
36}
37
43{
44 return float2(compatible_mod(a.x, b), compatible_mod(a.y, b));
45}
46
51{
52 return float3(compatible_mod(a.x, b), compatible_mod(a.y, b), compatible_mod(a.z, b));
53}
54
59{
60 return float4(compatible_mod(a.x, b),
61 compatible_mod(a.y, b),
62 compatible_mod(a.z, b),
63 compatible_mod(a.w, b));
64}
65
74
82
87{
88 return float4(compatible_mod(a.x, b.x),
89 compatible_mod(a.y, b.y),
90 compatible_mod(a.z, b.z),
91 compatible_mod(a.w, b.w));
92}
93
98template<typename VecT> VecT safe_divide(VecT a, VecT b)
99{
100 return select(VecT(0), a / b, notEqual(b, VecT(0)));
101}
105
106/* NOTE: Cannot overload templates. */
107
113{
114 return (b != 0.0f) ? (a / b) : float2(0);
115}
117{
118 return (b != 0.0f) ? (a / b) : float3(0);
119}
121{
122 return (b != 0.0f) ? (a / b) : float4(0);
123}
124
128template<typename VecT> VecT safe_normalize_and_get_length(VecT vector, float &out_length)
129{
131 constexpr float threshold = 1e-35f;
132 if (length_squared > threshold) {
133 float length_inv = inversesqrt(length_squared);
134 out_length = 1.0f / length_inv;
135 return vector * length_inv;
136 }
137 /* Either the vector is small or one of its values contained `nan`. */
138 out_length = 0.0f;
139 VecT result = VecT(0.0f);
140 result[0] = 1.0f;
141 return result;
142}
146
150template<typename VecT> VecT safe_normalize(VecT vector)
151{
152 float unused_length = 0.0f;
153 return safe_normalize_and_get_length(vector, unused_length);
154}
158
162template<typename VecT> VecT normalize_fallback(VecT vector, VecT fallback)
163{
165 constexpr float threshold = 1e-35f;
166 if (length_squared > threshold) {
168 }
169 /* Either the vector is small or one of its values contained `nan`. */
170 return fallback;
171}
175
180template<typename VecT> VecT safe_rcp(VecT a)
181{
182 return select(VecT(0.0f), (1.0f / a), notEqual(a, VecT(0.0f)));
183}
187
192float2 fallback_pow(float2 a, float b, float2 fallback)
193{
194 return float2(fallback_pow(a.x, b, fallback.x), fallback_pow(a.y, b, fallback.y));
195}
196float3 fallback_pow(float3 a, float b, float3 fallback)
197{
198 return float3(fallback_pow(a.x, b, fallback.x),
199 fallback_pow(a.y, b, fallback.y),
200 fallback_pow(a.z, b, fallback.z));
201}
202float4 fallback_pow(float4 a, float b, float4 fallback)
203{
204 return float4(fallback_pow(a.x, b, fallback.x),
205 fallback_pow(a.y, b, fallback.y),
206 fallback_pow(a.z, b, fallback.z),
207 fallback_pow(a.w, b, fallback.w));
208}
209
211{
212 return float2(compatible_pow(a.x, b.x), compatible_pow(a.y, b.y));
213}
215{
216 return float3(compatible_pow(a.x, b.x), compatible_pow(a.y, b.y), compatible_pow(a.z, b.z));
217}
219{
220 return float4(compatible_pow(a.x, b.x),
221 compatible_pow(a.y, b.y),
222 compatible_pow(a.z, b.z),
223 compatible_pow(a.w, b.w));
224}
225
230{
231 return float2(wrap(a.x, b.x, c.x), wrap(a.y, b.y, c.y));
232}
233
237{
238 return float3(wrap(a.x, b.x, c.x), wrap(a.y, b.y, c.y), wrap(a.z, b.z, c.z));
239}
240
244{
245 return float4(
246 wrap(a.x, b.x, c.x), wrap(a.y, b.y, c.y), wrap(a.z, b.z, c.z), wrap(a.w, b.w, c.w));
247}
VecBase< bool, D > notEqual(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T inversesqrt(T) RET
#define select(A, B, C)
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
float safe_rcp(float a)
float wrap(float a, float b, float c)
float fallback_pow(float x, float y, float fallback)
float safe_divide(float a, float b)
float compatible_pow(float x, float y)
float safe_mod(float a, float b)
float compatible_mod(float a, float b)
float length_squared(VecT a)
VecT safe_normalize(VecT vector)
VecT normalize_fallback(VecT vector, VecT fallback)
VecT safe_normalize_and_get_length(VecT vector, float &out_length)
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225