Blender V5.3
gpu_shader_common_math.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
10
11[[node]]
12void math_add(float a, float b, float /*c*/, float &result)
13{
14 result = a + b;
15}
16
17[[node]]
18void math_subtract(float a, float b, float /*c*/, float &result)
19{
20 result = a - b;
21}
22
23[[node]]
24void math_multiply(float a, float b, float /*c*/, float &result)
25{
26 result = a * b;
27}
28
29[[node]]
30void math_divide(float a, float b, float /*c*/, float &result)
31{
32 result = safe_divide(a, b);
33}
34
35[[node]]
36void math_power(float a, float b, float /*c*/, float &result)
37{
38 if (a >= 0.0f) {
39 result = compatible_pow(a, b);
40 }
41 else {
42 float fraction = mod(abs(b), 1.0f);
43 if (fraction > 0.999f || fraction < 0.001f) {
44 result = compatible_pow(a, floor(b + 0.5f));
45 }
46 else {
47 result = 0.0f;
48 }
49 }
50}
51
52[[node]]
53void math_logarithm(float a, float b, float /*c*/, float &result)
54{
55 result = (a > 0.0f && b > 0.0f) ? log2(a) / log2(b) : 0.0f;
56}
57
58[[node]]
59void math_sqrt(float a, float /*b*/, float /*c*/, float &result)
60{
61 result = (a > 0.0f) ? sqrt(a) : 0.0f;
62}
63
64[[node]]
65void math_inversesqrt(float a, float /*b*/, float /*c*/, float &result)
66{
67 result = inversesqrt(a);
68}
69
70[[node]]
71void math_absolute(float a, float /*b*/, float /*c*/, float &result)
72{
73 result = abs(a);
74}
75
76[[node]]
77void math_radians(float a, float /*b*/, float /*c*/, float &result)
78{
79 result = radians(a);
80}
81
82[[node]]
83void math_degrees(float a, float /*b*/, float /*c*/, float &result)
84{
85 result = degrees(a);
86}
87
88[[node]]
89void math_minimum(float a, float b, float /*c*/, float &result)
90{
91 result = min(a, b);
92}
93
94[[node]]
95void math_maximum(float a, float b, float /*c*/, float &result)
96{
97 result = max(a, b);
98}
99
100[[node]]
101void math_less_than(float a, float b, float /*c*/, float &result)
102{
103 result = (a < b) ? 1.0f : 0.0f;
104}
105
106[[node]]
107void math_greater_than(float a, float b, float /*c*/, float &result)
108{
109 result = (a > b) ? 1.0f : 0.0f;
110}
111
112[[node]]
113void math_round(float a, float /*b*/, float /*c*/, float &result)
114{
115 result = floor(a + 0.5f);
116}
117
118[[node]]
119void math_floor(float a, float /*b*/, float /*c*/, float &result)
120{
121 result = floor(a);
122}
123
124[[node]]
125void math_ceil(float a, float /*b*/, float /*c*/, float &result)
126{
127 result = ceil(a);
128}
129
130[[node]]
131void math_fraction(float a, float /*b*/, float /*c*/, float &result)
132{
133 result = a - floor(a);
134}
135
136[[node]]
137void math_modulo(float a, float b, float /*c*/, float &result)
138{
139 result = compatible_mod(a, b);
140}
141
142[[node]]
143void math_floored_modulo(float a, float b, float /*c*/, float &result)
144{
145 result = (b != 0.0f) ? a - floor(a / b) * b : 0.0f;
146}
147
148[[node]]
149void math_trunc(float a, float /*b*/, float /*c*/, float &result)
150{
151 result = trunc(a);
152}
153
154[[node]]
155void math_snap(float a, float b, float /*c*/, float &result)
156{
157 result = floor(safe_divide(a, b)) * b;
158}
159
160[[node]]
161void math_pingpong(float a, float b, float /*c*/, float &result)
162{
163 result = (b != 0.0f) ? abs(fract((a - b) / (b * 2.0f)) * b * 2.0f - b) : 0.0f;
164}
165
166/* Adapted from GODOT-engine math_funcs.h. */
167[[node]]
168void math_wrap(float a, float b, float c, float &result)
169{
170 result = wrap(a, b, c);
171}
172
173[[node]]
174void math_sine(float a, float /*b*/, float /*c*/, float &result)
175{
176 result = sin(a);
177}
178
179[[node]]
180void math_cosine(float a, float /*b*/, float /*c*/, float &result)
181{
182 result = cos(a);
183}
184
185[[node]]
186void math_tangent(float a, float /*b*/, float /*c*/, float &result)
187{
188 result = tan(a);
189}
190
191[[node]]
192void math_sinh(float a, float /*b*/, float /*c*/, float &result)
193{
194 result = sinh(a);
195}
196
197[[node]]
198void math_cosh(float a, float /*b*/, float /*c*/, float &result)
199{
200 result = cosh(a);
201}
202
203[[node]]
204void math_tanh(float a, float /*b*/, float /*c*/, float &result)
205{
206 result = tanh(a);
207}
208
209[[node]]
210void math_arcsine(float a, float /*b*/, float /*c*/, float &result)
211{
212 result = (a <= 1.0f && a >= -1.0f) ? asin(a) : 0.0f;
213}
214
215[[node]]
216void math_arccosine(float a, float /*b*/, float /*c*/, float &result)
217{
218 result = (a <= 1.0f && a >= -1.0f) ? acos(a) : 0.0f;
219}
220
221[[node]]
222void math_arctangent(float a, float /*b*/, float /*c*/, float &result)
223{
224 result = atan(a);
225}
226
227/* The behavior of `atan2(0, 0)` is undefined on many platforms, to ensure consistent behavior, we
228 * return 0 in this case. See !126951. */
229[[node]]
230void math_arctan2(float a, float b, float /*c*/, float &result)
231{
232 result = ((a == 0.0f && b == 0.0f) ? 0.0f : atan(a, b));
233}
234
235[[node]]
236void math_sign(float a, float /*b*/, float /*c*/, float &result)
237{
238 result = sign(a);
239}
240
241[[node]]
242void math_exponent(float a, float /*b*/, float /*c*/, float &result)
243{
244 result = exp(a);
245}
246
247[[node]]
248void math_compare(float a, float b, float c, float &result)
249{
250 result = (abs(a - b) <= max(c, 1e-5f)) ? 1.0f : 0.0f;
251}
252
253[[node]]
254void math_multiply_add(float a, float b, float c, float &result)
255{
256 result = a * b + c;
257}
258
259/* See: https://www.iquilezles.org/www/articles/smin/smin.htm. */
260[[node]]
261void math_smoothmin(float a, float b, float c, float &result)
262{
263 if (c != 0.0f) {
264 float h = max(c - abs(a - b), 0.0f) / c;
265 result = min(a, b) - h * h * h * c * (1.0f / 6.0f);
266 }
267 else {
268 result = min(a, b);
269 }
270}
271
272[[node]]
273void math_smoothmax(float a, float b, float c, float &result)
274{
275 math_smoothmin(-a, -b, c, result);
276 result = -result;
277}
278
279/* TODO(fclem): Fix dependency hell one EEVEE legacy is removed. */
281{
282 return max(a.x, max(a.y, a.z));
283}
284
286{
287 return (a.x + a.y + a.z) * (1.0f / 3.0f);
288}
btScalar max
void math_sinh(float a, float, float, float &result)
void math_add(float a, float b, float, float &result)
void math_maximum(float a, float b, float, float &result)
void math_pingpong(float a, float b, float, float &result)
void math_floor(float a, float, float, float &result)
void math_exponent(float a, float, float, float &result)
void math_fraction(float a, float, float, float &result)
void math_trunc(float a, float, float, float &result)
void math_less_than(float a, float b, float, float &result)
void math_cosh(float a, float, float, float &result)
void math_radians(float a, float, float, float &result)
void math_degrees(float a, float, float, float &result)
void math_snap(float a, float b, float, float &result)
void math_greater_than(float a, float b, float, float &result)
void math_inversesqrt(float a, float, float, float &result)
void math_tanh(float a, float, float, float &result)
void math_compare(float a, float b, float c, float &result)
void math_tangent(float a, float, float, float &result)
void math_smoothmax(float a, float b, float c, float &result)
void math_arcsine(float a, float, float, float &result)
void math_arctangent(float a, float, float, float &result)
void math_sine(float a, float, float, float &result)
float math_reduce_max(float3 a)
void math_minimum(float a, float b, float, float &result)
void math_modulo(float a, float b, float, float &result)
void math_sqrt(float a, float, float, float &result)
void math_arccosine(float a, float, float, float &result)
void math_divide(float a, float b, float, float &result)
void math_smoothmin(float a, float b, float c, float &result)
void math_subtract(float a, float b, float, float &result)
void math_round(float a, float, float, float &result)
void math_cosine(float a, float, float, float &result)
void math_floored_modulo(float a, float b, float, float &result)
void math_power(float a, float b, float, float &result)
void math_sign(float a, float, float, float &result)
void math_arctan2(float a, float b, float, float &result)
void math_absolute(float a, float, float, float &result)
void math_ceil(float a, float, float, float &result)
void math_multiply(float a, float b, float, float &result)
void math_multiply_add(float a, float b, float c, float &result)
void math_logarithm(float a, float b, float, float &result)
void math_wrap(float a, float b, float c, float &result)
float math_average(float3 a)
#define atan
#define tan
#define sin
T fract(T) RET
#define exp
#define cos
#define trunc
#define log2
#define cosh
constexpr T sign(T) RET
constexpr T inversesqrt(T) RET
#define abs
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
#define floor
#define ceil
#define sinh
#define sqrt
#define asin
constexpr T radians(T) RET
#define tanh
#define acos
constexpr T degrees(T) RET
float wrap(float a, float b, float c)
float safe_divide(float a, float b)
float compatible_pow(float x, float y)
float compatible_mod(float a, float b)
#define min(a, b)
Definition sort.cc:38
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136