Blender V5.3
gpu_shader_math_base.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/* There are two common ways of implementing a linear interpolation: result = a + t * (b - a) and
10 * result = (1 - t) * a + t * b. The former variant is called "mix" in our code and it ensures that
11 * result always changes monotonically when t increases monotonically. This comes at the cost of
12 * the fact that generally result != b when t == 1, which becomes particularly noticeable when the
13 * magnitudes of a and b are vastly different. The latter variant is called
14 * "endvalue_preserving_mix" in our code ensures that result == b when t == 1. This comes at the
15 * cost of an additional multiplication step compared to the former version and the fact that
16 * result may not change monotonically when a and b have different signs and t increases
17 * monotonically, which however isn't noticeable in most cases as long as monotony isn't explicitly
18 * required. In general, "endvalue_preserving_mix" should be preferred over "mix" when it is
19 * important that result == b when t == 1 or when a and b may have vastly different magnitudes.*/
20float endvalue_preserving_mix(float a, float b, float t)
21{
22 return (1.0f - t) * a + t * b;
23}
24
25/* `powf` is really slow for raising to integer powers. */
26
27float pow2f(float x)
28{
29 return x * x;
30}
31float pow3f(float x)
32{
33 return x * x * x;
34}
35float pow4f(float x)
36{
37 return pow2f(pow2f(x));
38}
39float pow5f(float x)
40{
41 return pow4f(x) * x;
42}
43float pow6f(float x)
44{
45 return pow2f(pow3f(x));
46}
47float pow7f(float x)
48{
49 return pow6f(x) * x;
50}
51float pow8f(float x)
52{
53 return pow2f(pow4f(x));
54}
55
56float square(float v)
57{
58 return v * v;
59}
61{
62 return v * v;
63}
65{
66 return v * v;
67}
69{
70 return v * v;
71}
72
73float hypot(float x, float y)
74{
75 return sqrt(x * x + y * y);
76}
77
78/* Declared as _atan2 to prevent errors with `WITH_GPU_SHADER_CPP_COMPILATION` on VS2019 due
79 * to `corecrt_math` conflicting functions. */
80
81float _atan2(float y, float x)
82{
83 return atan(y, x);
84}
85#define atan2 _atan2
86
92int ceil_to_multiple(int a, int b)
93{
94 return ((a + b - 1) / b) * b;
95}
96uint ceil_to_multiple(uint a, uint b)
97{
98 return ((a + b - 1u) / b) * b;
99}
100
105int divide_ceil(int a, int b)
106{
107 return (a + b - 1) / b;
108}
109uint divide_ceil(uint a, uint b)
110{
111 return (a + b - 1u) / b;
112}
113
117void min_max(float value, float &min_v, float &max_v)
118{
119 min_v = min(value, min_v);
120 max_v = max(value, max_v);
121}
122
126bool is_equal(float a, float b, const float epsilon)
127{
128 return abs(a - b) <= epsilon;
129}
130
131float sin_from_cos(float c)
132{
133 return sqrt(max(0.0f, 1.0f - square(c)));
134}
135
136float cos_from_sin(float s)
137{
138 return sqrt(max(0.0f, 1.0f - square(s)));
139}
140
141float cos_from_tan(float t)
142{
143 return inversesqrt(1.0f + square(t));
144}
145
149int midpoint(int a, int b)
150{
151 uint smaller = uint(min(a, b));
152 uint larger = uint(max(a, b));
153 int sign = (b > a) ? 1 : -1;
154 return a + sign * int((larger - smaller) / 2u);
155}
btScalar max
#define atan
constexpr T sign(T) RET
constexpr T inversesqrt(T) RET
#define abs
#define sqrt
float pow4f(float x)
float pow2f(float x)
float hypot(float x, float y)
void min_max(float value, float &min_v, float &max_v)
float square(float v)
float _atan2(float y, float x)
float pow6f(float x)
bool is_equal(float a, float b, const float epsilon)
float cos_from_tan(float t)
float endvalue_preserving_mix(float a, float b, float t)
float pow7f(float x)
float cos_from_sin(float s)
float pow5f(float x)
int divide_ceil(int a, int b)
int ceil_to_multiple(int a, int b)
float sin_from_cos(float c)
float pow3f(float x)
float pow8f(float x)
int midpoint(int a, int b)
#define min(a, b)
Definition sort.cc:38