Blender V5.3
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"
19
21
22/* The polynomial forms of the spherical basis functions.
23 * [sloan2010] Appendix A2. */
25
26/* Band L=0. */
28{
29 return 0.282094792f;
30}
31
32/* Band L=1. */
34{
35 return -0.488602512f * v.y;
36}
38{
39 return 0.488602512f * v.z;
40}
42{
43 return -0.488602512f * v.x;
44}
45
46/* Band L=2. */
48{
49 return 1.092548431f * (v.x * v.y);
50}
52{
53 return -1.092548431f * (v.y * v.z);
54}
56{
57 return 0.315391565f * (3.0f * v.z * v.z - 1.0f);
58}
60{
61 return -1.092548431f * (v.x * v.z);
62}
64{
65 return 0.546274215f * (v.x * v.x - v.y * v.y);
66}
67
68/* Band L=3. */
70{
71 return -0.590043589f * v.y * (3.0f * v.x * v.x - v.y * v.y);
72}
74{
75 return 2.890611442f * (v.y * v.x * v.z);
76}
78{
79 return -0.457045799f * v.y * (-1.0f + 5.0f * v.z * v.z);
80}
82{
83 return 0.373176332f * v.z * (5.0f * v.z * v.z - 3.0f);
84}
86{
87 return -0.457045799f * v.x * (-1.0f + 5.0f * v.z * v.z);
88}
90{
91 return 1.445305721f * v.z * (v.x * v.x - v.y * v.y);
92}
94{
95 return -0.5900435899f * v.x * (v.x * v.x - 3.0f * v.y * v.y);
96}
97
98template<class T>
103
104template<class T>
106spherical_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 +
111}
112
113template<class T>
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 +
122}
123
124template<class T>
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 +
136 spherical_harmonics_polynomial_form<3, -1>(direction) * Mn1 +
141}
142
143/* Evaluate bands L1, L2, and L3. */
145 const ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest,
146 const float3 direction)
147{
148 float3 result;
149
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
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
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
#define ccl_device_forceinline
#define ccl_device_inline
#define ccl_device_template_spec
#define ccl_global
#define CCL_NAMESPACE_END
ccl_device_inline float3 spherical_harmonics_evaluate_rest(const ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest, const float3 direction)
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)
ccl_device_forceinline T spherical_harmonics_evaluate_band_L1(const float3 direction, const T Mn1, const T M0, const T Mp1)
ccl_device_forceinline T spherical_harmonics_evaluate_band_L0(const float3 direction, const T M0)
CCL_NAMESPACE_BEGIN ccl_device_inline float spherical_harmonics_polynomial_form(float3 v)
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)
#define T
ccl_device_inline float3 spherical_harmonics_get(const ccl_global PackedSphericalHarmonicsRest &packed_spherical_harmonics_rest)