Blender
V5.3
intern
cycles
util
math_spherical_harmonics.h
Go to the documentation of this file.
1
/* SPDX-FileCopyrightText: 2026 Blender Foundation
2
*
3
* SPDX-License-Identifier: Apache-2.0 */
4
5
/* Evaluation of spherical harmonics.
6
*
7
* References:
8
*
9
* [sloan2010] Peter-Pike Sloan
10
* Stupid Spherical Harmonics (SH) Trick
11
* Game Developers Conference 2008, February 2008 (updated 2/10/2010)
12
*/
13
14
#pragma once
15
16
#include "
util/math_base.h
"
17
#include "
util/types_float3.h
"
18
#include "
util/types_spherical_harmonics.h
"
19
20
CCL_NAMESPACE_BEGIN
21
22
/* The polynomial forms of the spherical basis functions.
23
* [sloan2010] Appendix A2. */
24
template
<
int
L,
int
M>
ccl_device_inline
float
spherical_harmonics_polynomial_form
(
float3
v);
25
26
/* Band L=0. */
27
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<0, 0>
(
const
float3
/*v*/
)
28
{
29
return
0.282094792f;
30
}
31
32
/* Band L=1. */
33
ccl_device_template_spec
float
spherical_harmonics_polynomial_form
<1, -1>(
const
float3
v)
34
{
35
return
-0.488602512f * v.y;
36
}
37
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<1, 0>
(
const
float3
v)
38
{
39
return
0.488602512f * v.z;
40
}
41
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<1, 1>
(
const
float3
v)
42
{
43
return
-0.488602512f * v.x;
44
}
45
46
/* Band L=2. */
47
ccl_device_template_spec
float
spherical_harmonics_polynomial_form
<2, -2>(
const
float3
v)
48
{
49
return
1.092548431f * (v.x * v.y);
50
}
51
ccl_device_template_spec
float
spherical_harmonics_polynomial_form
<2, -1>(
const
float3
v)
52
{
53
return
-1.092548431f * (v.y * v.z);
54
}
55
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<2, 0>
(
const
float3
v)
56
{
57
return
0.315391565f * (3.0f * v.z * v.z - 1.0f);
58
}
59
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<2, 1>
(
const
float3
v)
60
{
61
return
-1.092548431f * (v.x * v.z);
62
}
63
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<2, 2>
(
const
float3
v)
64
{
65
return
0.546274215f * (v.x * v.x - v.y * v.y);
66
}
67
68
/* Band L=3. */
69
ccl_device_template_spec
float
spherical_harmonics_polynomial_form
<3, -3>(
const
float3
v)
70
{
71
return
-0.590043589f * v.y * (3.0f * v.x * v.x - v.y * v.y);
72
}
73
ccl_device_template_spec
float
spherical_harmonics_polynomial_form
<3, -2>(
const
float3
v)
74
{
75
return
2.890611442f * (v.y * v.x * v.z);
76
}
77
ccl_device_template_spec
float
spherical_harmonics_polynomial_form
<3, -1>(
const
float3
v)
78
{
79
return
-0.457045799f * v.y * (-1.0f + 5.0f * v.z * v.z);
80
}
81
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<3, 0>
(
const
float3
v)
82
{
83
return
0.373176332f * v.z * (5.0f * v.z * v.z - 3.0f);
84
}
85
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<3, 1>
(
const
float3
v)
86
{
87
return
-0.457045799f * v.x * (-1.0f + 5.0f * v.z * v.z);
88
}
89
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<3, 2>
(
const
float3
v)
90
{
91
return
1.445305721f * v.z * (v.x * v.x - v.y * v.y);
92
}
93
ccl_device_template_spec
float
spherical_harmonics_polynomial_form<3, 3>
(
const
float3
v)
94
{
95
return
-0.5900435899f * v.x * (v.x * v.x - 3.0f * v.y * v.y);
96
}
97
98
template
<
class
T>
99
ccl_device_forceinline
T
spherical_harmonics_evaluate_band_L0
(
const
float3
direction,
const
T
M0)
100
{
101
return
spherical_harmonics_polynomial_form<0, 0>
(direction) * M0;
102
}
103
104
template
<
class
T>
105
ccl_device_forceinline
T
106
spherical_harmonics_evaluate_band_L1
(
const
float3
direction,
const
T
Mn1,
const
T
M0,
const
T
Mp1)
107
{
108
return
spherical_harmonics_polynomial_form
<1, -1>(direction) * Mn1 +
109
spherical_harmonics_polynomial_form<1, 0>
(direction) * M0 +
110
spherical_harmonics_polynomial_form<1, 1>
(direction) * Mp1;
111
}
112
113
template
<
class
T>
114
ccl_device_forceinline
T
spherical_harmonics_evaluate_band_L2
(
115
const
float3
direction,
const
T
Mn2,
const
T
Mn1,
const
T
M0,
const
T
Mp1,
const
T
Mp2)
116
{
117
return
spherical_harmonics_polynomial_form
<2, -2>(direction) * Mn2 +
118
spherical_harmonics_polynomial_form<2, -1>
(direction) * Mn1 +
119
spherical_harmonics_polynomial_form<2, 0>
(direction) * M0 +
120
spherical_harmonics_polynomial_form<2, 1>
(direction) * Mp1 +
121
spherical_harmonics_polynomial_form<2, 2>
(direction) * Mp2;
122
}
123
124
template
<
class
T>
125
ccl_device_forceinline
T
spherical_harmonics_evaluate_band_L3
(
const
float3
direction,
126
const
T
Mn3,
127
const
T
Mn2,
128
const
T
Mn1,
129
const
T
M0,
130
const
T
Mp1,
131
const
T
Mp2,
132
const
T
Mp3)
133
{
134
return
spherical_harmonics_polynomial_form
<3, -3>(direction) * Mn3 +
135
spherical_harmonics_polynomial_form<3, -2>
(direction) * Mn2 +
136
spherical_harmonics_polynomial_form
<3, -1>(direction) * Mn1 +
137
spherical_harmonics_polynomial_form<3, 0>
(direction) * M0 +
138
spherical_harmonics_polynomial_form<3, 1>
(direction) * Mp1 +
139
spherical_harmonics_polynomial_form<3, 2>
(direction) * Mp2 +
140
spherical_harmonics_polynomial_form<3, 3>
(direction) * Mp3;
141
}
142
143
/* Evaluate bands L1, L2, and L3. */
144
ccl_device_inline
float3
spherical_harmonics_evaluate_rest
(
145
const
ccl_global
PackedSphericalHarmonicsRest
&packed_spherical_harmonics_rest,
146
const
float3
direction)
147
{
148
float3
result;
149
150
result =
spherical_harmonics_evaluate_band_L1
(
151
direction,
152
spherical_harmonics_get<1, -1>
(packed_spherical_harmonics_rest),
153
spherical_harmonics_get<1, 0>
(packed_spherical_harmonics_rest),
154
spherical_harmonics_get<1, 1>
(packed_spherical_harmonics_rest));
155
156
result +=
spherical_harmonics_evaluate_band_L2
(
157
direction,
158
spherical_harmonics_get<2, -2>
(packed_spherical_harmonics_rest),
159
spherical_harmonics_get<2, -1>
(packed_spherical_harmonics_rest),
160
spherical_harmonics_get<2, 0>
(packed_spherical_harmonics_rest),
161
spherical_harmonics_get<2, 1>
(packed_spherical_harmonics_rest),
162
spherical_harmonics_get<2, 2>
(packed_spherical_harmonics_rest));
163
164
result +=
spherical_harmonics_evaluate_band_L3
(
165
direction,
166
spherical_harmonics_get<3, -3>
(packed_spherical_harmonics_rest),
167
spherical_harmonics_get<3, -2>
(packed_spherical_harmonics_rest),
168
spherical_harmonics_get<3, -1>
(packed_spherical_harmonics_rest),
169
spherical_harmonics_get<3, 0>
(packed_spherical_harmonics_rest),
170
spherical_harmonics_get<3, 1>
(packed_spherical_harmonics_rest),
171
spherical_harmonics_get<3, 2>
(packed_spherical_harmonics_rest),
172
spherical_harmonics_get<3, 3>
(packed_spherical_harmonics_rest));
173
174
return
result;
175
}
176
177
CCL_NAMESPACE_END
ccl_device_forceinline
#define ccl_device_forceinline
ccl_device_inline
#define ccl_device_inline
ccl_device_template_spec
#define ccl_device_template_spec
ccl_global
#define ccl_global
CCL_NAMESPACE_END
#define CCL_NAMESPACE_END
Definition
device/cuda/compat.h:10
math_base.h
spherical_harmonics_evaluate_rest
ccl_device_inline float3 spherical_harmonics_evaluate_rest(const ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest, const float3 direction)
Definition
math_spherical_harmonics.h:144
spherical_harmonics_evaluate_band_L2
ccl_device_forceinline T spherical_harmonics_evaluate_band_L2(const float3 direction, const T Mn2, const T Mn1, const T M0, const T Mp1, const T Mp2)
Definition
math_spherical_harmonics.h:114
spherical_harmonics_evaluate_band_L1
ccl_device_forceinline T spherical_harmonics_evaluate_band_L1(const float3 direction, const T Mn1, const T M0, const T Mp1)
Definition
math_spherical_harmonics.h:106
spherical_harmonics_evaluate_band_L0
ccl_device_forceinline T spherical_harmonics_evaluate_band_L0(const float3 direction, const T M0)
Definition
math_spherical_harmonics.h:99
spherical_harmonics_polynomial_form
CCL_NAMESPACE_BEGIN ccl_device_inline float spherical_harmonics_polynomial_form(float3 v)
spherical_harmonics_evaluate_band_L3
ccl_device_forceinline T spherical_harmonics_evaluate_band_L3(const float3 direction, const T Mn3, const T Mn2, const T Mn1, const T M0, const T Mp1, const T Mp2, const T Mp3)
Definition
math_spherical_harmonics.h:125
T
#define T
Definition
mball_tessellate.cc:277
CCL_NAMESPACE_BEGIN
Definition
python.cpp:41
PackedSphericalHarmonicsRest
Definition
types_spherical_harmonics.h:33
float3
Definition
sky_math.h:135
types_float3.h
types_spherical_harmonics.h
spherical_harmonics_get
ccl_device_inline float3 spherical_harmonics_get(const ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest)
Generated on
for Blender by
doxygen
1.18.0