Blender V5.3
eevee_ltc_lib.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
11
12#pragma once
13
15#include "eevee_defines.hh"
18#include "gpu_shader_compat.hh"
22
23namespace eevee::ltc {
24
25namespace detail {
26
32float diffuse_sphere_integral(sampler2DArray util_tx, float avg_dir_z, float form_factor)
33{
34#if 1
35 /* use tabulated horizon-clipped sphere */
36 float2 uv = float2(avg_dir_z * 0.5f + 0.5f, form_factor);
38
40#else
41 /* Cheap approximation. Less smooth and have energy issues. */
42 return max((form_factor * form_factor + avg_dir_z) / (form_factor + 1.0f), 0.0f);
43#endif
44}
45
51{
52 /* Normalize the polynomial */
53 coefs.xyz /= coefs.w;
54 /* Divide middle coefficients by three */
55 coefs.yz /= 3.0f;
56
57 float A = coefs.w;
58 float B = coefs.z;
59 float C = coefs.y;
60 float D = coefs.x;
61
62 /* Compute the Hessian and the discriminant */
63 float3 delta = float3(-coefs.zy * coefs.zz + coefs.yx, dot(float2(coefs.z, -coefs.y), coefs.xy));
64
65 /* Discriminant */
66 float discr = dot(float2(4.0f * delta.x, -delta.y), delta.zy);
67
68 /* Clamping avoid NaN output on some platform. (see #67060) */
69 float sqrt_discr = sqrt(clamp(discr, 0.0f, FLT_MAX));
70
71 float2 xlc, xsc;
72
73 /* Algorithm A */
74 {
75 float C_a = delta.x;
76 float D_a = -2.0f * B * delta.x + delta.y;
77
78 /* Take the cubic root of a normalized complex number */
79 float theta = atan(sqrt_discr, -D_a) / 3.0f;
80
81 float _2_sqrt_C_a = 2.0f * sqrt(-C_a);
82 float x_1a = _2_sqrt_C_a * cos(theta);
83 float x_3a = _2_sqrt_C_a * cos(theta + (2.0f / 3.0f) * M_PI);
84
85 float xl;
86 if ((x_1a + x_3a) > 2.0f * B) {
87 xl = x_1a;
88 }
89 else {
90 xl = x_3a;
91 }
92
93 xlc = float2(xl - B, A);
94 }
95
96 /* Algorithm D */
97 {
98 float C_d = delta.z;
99 float D_d = -D * delta.y + 2.0f * C * delta.z;
100
101 /* Take the cubic root of a normalized complex number */
102 float theta = atan(D * sqrt_discr, -D_d) / 3.0f;
103
104 float _2_sqrt_C_d = 2.0f * sqrt(-C_d);
105 float x_1d = _2_sqrt_C_d * cos(theta);
106 float x_3d = _2_sqrt_C_d * cos(theta + (2.0f / 3.0f) * M_PI);
107
108 float xs;
109 if (x_1d + x_3d < 2.0f * C) {
110 xs = x_1d;
111 }
112 else {
113 xs = x_3d;
114 }
115
116 xsc = float2(-D, xs + C);
117 }
118
119 float E = xlc.y * xsc.y;
120 float F = -xlc.x * xsc.y - xlc.y * xsc.x;
121 float G = xlc.x * xsc.x;
122
123 float2 xmc = float2(C * F - B * G, -B * F + C * E);
124
125 float3 root = float3(xsc.x / xsc.y, xmc.x / xmc.y, xlc.x / xlc.y);
126
127 if (root.x < root.y && root.x < root.z) {
128 root.xyz = root.yxz;
129 }
130 else if (root.z < root.x && root.z < root.y) {
131 root.xyz = root.xzy;
132 }
133
134 return root;
135}
136
146{
147 float x = dot(v1, v2);
148 float y = abs(x);
149
150 float a = 0.8543985f + (0.4965155f + 0.0145206f * y) * y;
151 float b = 3.4175940f + (4.1616724f + y) * y;
152 float v = a / b;
153
154 float theta_sintheta = (x > 0.0f) ? v : 0.5f * inversesqrt(max(1.0f - x * x, 1e-7f)) - v;
155
156 return cross(v1, v2) * theta_sintheta;
157}
158
163float upper_parabole(float x, float scale, float y_1)
164{
165 x = saturate(x / y_1);
166 return saturate((x * scale + x) / (scale + x));
167}
168
174{
175 float3 adjoint_z = float3(+(Minv[1][0] * Minv[2][1] - Minv[2][0] * Minv[1][1]),
176 -(Minv[0][0] * Minv[2][1] - Minv[2][0] * Minv[0][1]),
177 +(Minv[0][0] * Minv[1][1] - Minv[1][0] * Minv[0][1]));
178 return normalize(adjoint_z);
179}
180
185{
186 /* First, find an approximate dominant BxDF direction D. Then, find a vector T on the light
187 * plane, coplanar with D and L, i.e. T = cross(cross(L, D), shape.N). */
189 float3 T = normalize_fallback(D * dot(L, shape.N) - L * dot(D, shape.N), float3(0.0));
190
191 /* Find t where D intersects the line L + t*T. */
192 float TD = dot(T, D);
193 float t = safe_divide(dot(D, L) * TD - dot(T, L), 1.0f - square(TD));
194
195 if (t >= 0.0 && t < shape.disk_radius_projected) {
196 /* If t lies within the disk radius on the positive side, we do not attenuate. */
197 return 1.0;
198 }
199 /* Otherwise, restrict t to within on the disk radius. */
200 t = min(abs(t), shape.disk_radius_projected);
201
202 /* Compute L + t*T and warp it into LTC space. This disk point bounds the light shape in LTC
203 * space, so we can use its z-component to safely fit an attenuation factor. */
204 float attenuation = saturate(normalize(ltc_data.Minv * (L + t * T)).z);
205 attenuation = upper_parabole(attenuation, 0.15f, 0.5f);
206 attenuation += (1.0f - attenuation) * ltc_data.attenuation_factor;
207
208 return attenuation;
209}
210} // namespace detail
211
215float evaluate_quad(sampler2DArray util_tx, LightShape shape, LightVector lv, LTCData ltc_data)
216{
217 /* Transform the quad corners into LTC space, and project on to sphere. */
218 float3 V[4] = {normalize(ltc_data.Minv * shape.v[0]),
219 normalize(ltc_data.Minv * shape.v[1]),
220 normalize(ltc_data.Minv * shape.v[2]),
221 normalize(ltc_data.Minv * shape.v[3])};
222
223 /* Approximation using a sphere with the same form factor as the unclipped quad.
224 * Finding a clipped sphere's form factor is easier than clipping the quad. */
225 float3 avg_dir;
226 avg_dir = detail::edge_integral_vec(V[0], V[1]);
227 avg_dir += detail::edge_integral_vec(V[1], V[2]);
228 avg_dir += detail::edge_integral_vec(V[2], V[3]);
229 avg_dir += detail::edge_integral_vec(V[3], V[0]);
230
231 float form_factor_inv = inversesqrt(dot(avg_dir, avg_dir));
232 float avg_dir_z = (avg_dir * form_factor_inv).z;
233 float form_factor = saturate(1.0f / form_factor_inv);
234 /* The form factor should always be finite. Check that the previous saturate works as filter. */
235 // assert(!isnan(form_factor) && !isinf(form_factor));
236
237 switch (ltc_data.form_factor_type) {
239 /* The clipped sphere approximation causes light leakage for low roughness;
240 * we attenuate with a fitted function that removes some energy below the horizon. */
241 form_factor *= detail::form_factor_attenuation(shape, ltc_data, lv.L);
242 form_factor *= detail::diffuse_sphere_integral(util_tx, avg_dir_z, form_factor);
243 break;
244 default: /* LTCFormFactorType::TwoSidedCosineSphere */
245 form_factor *= M_1_PI;
246 break;
247 }
248
249 return form_factor;
250}
251
257float evaluate_disk(sampler2DArray util_tx, LightShape shape, LightVector lv, LTCData ltc_data)
258{
259 /* Intermediate step: init ellipse. */
260 float3 C = 0.5f * (shape.v[0] + shape.v[2]);
261 float3 V1 = 0.5f * (shape.v[1] - shape.v[2]);
262 float3 V2 = 0.5f * (shape.v[1] - shape.v[0]);
263
264 /* Transform ellipse into LTC space. */
265 C = ltc_data.Minv * C;
266 V1 = ltc_data.Minv * V1;
267 V2 = ltc_data.Minv * V2;
268
269 /* Compute eigenvectors of new ellipse. */
270 float d11 = dot(V1, V1);
271 float d22 = dot(V2, V2);
272 float d12 = dot(V1, V2);
273 float a, inv_b; /* Eigenvalues */
274 constexpr float threshold = 0.0007f; /* Can be adjusted. Fix artifacts. */
275 if (abs(d12) / sqrt(d11 * d22) > threshold) {
276 float tr = d11 + d22;
277 float det = -d12 * d12 + d11 * d22;
278
279 /* use sqrt matrix to solve for eigenvalues */
280 det = sqrt(det);
281 float u = 0.5f * sqrt(tr - 2.0f * det);
282 float v = 0.5f * sqrt(tr + 2.0f * det);
283 float e_max = (u + v);
284 float e_min = (u - v);
285 e_max *= e_max;
286 e_min *= e_min;
287
288 float3 V1_, V2_;
289 if (d11 > d22) {
290 V1_ = d12 * V1 + (e_max - d11) * V2;
291 V2_ = d12 * V1 + (e_min - d11) * V2;
292 }
293 else {
294 V1_ = d12 * V2 + (e_max - d22) * V1;
295 V2_ = d12 * V2 + (e_min - d22) * V1;
296 }
297
298 a = 1.0f / e_max;
299 inv_b = e_min;
300 V1 = normalize(V1_);
301 V2 = normalize(V2_);
302 }
303 else {
304 a = 1.0f / d11;
305 inv_b = d22;
306 V1 *= sqrt(a);
307 V2 *= inversesqrt(inv_b);
308 }
309
310 /* Now find a front facing ellipse with the same solid angle. */
311
312 float3 V3 = normalize(cross(V1, V2));
313 if (dot(C, V3) < 0.0f) {
314 V3 *= -1.0f;
315 }
316
317 float L = dot(V3, C);
318 float inv_L = 1.0f / L;
319 float x0 = dot(V1, C) * inv_L;
320 float y0 = dot(V2, C) * inv_L;
321
322 float ab = a * inv_b;
323 inv_b *= square(inv_L);
324 float t = 1.0f + x0 * x0;
325
326 /* Compared to the original LTC implementation, we scale the polynomial by `b` to avoid numerical
327 * issues when light size is small.
328 * i.e., instead of solving `c0 * e^3 + c1 * e^2 + c2 * e + c3 = 0`,
329 * we solve `c0/b^3 * (be)^3 + c1/b^2 * (be)^2 + c2/b * be + c3 = 0`. */
330 float c0 = ab * inv_b;
331 float c1 = ab * (t + y0 * y0) - c0 - inv_b;
332 float c2 = inv_b - ab * t - (1.0f + y0 * y0);
333 float c3 = 1.0f;
334
335 float3 roots = detail::solve_cubic(float4(c0, c1, c2, c3));
336 float e1 = roots.x;
337 float e2 = roots.y;
338 float e3 = roots.z;
339
340 /* Scale the root back by multiplying `b`.
341 * `a * x0 / (a - b * e2)` simplifies to `a/b * x0 / (a/b - e2)`,
342 * `b * y0 / (b - b * e2)` simplifies to `y0 / (1.0f - e2)`. */
343 float3 avg_dir = float3(ab * x0 / (ab - e2), y0 / (1.0f - e2), 1.0f);
344 float3x3 rotate = float3x3(V1, V2, V3);
345 avg_dir = rotate * avg_dir;
346 avg_dir = normalize(avg_dir);
347
348 /* L1, L2 are the extents of the front facing ellipse. From here, find the sphere form factor. */
349 float L1 = inversesqrt(-e3 / e2);
350 float L2 = inversesqrt(-e1 / e2);
351 float form_factor = saturate(L1 * L2 * inversesqrt((1.0f + L1 * L1) * (1.0f + L2 * L2)));
352 /* The form factor should always be finite. Check that the previous saturate works as filter. */
353 // assert(!isnan(form_factor) && !isinf(form_factor));
354
355 switch (ltc_data.form_factor_type) {
357 /* The clipped sphere approximation causes light leakage for low roughness;
358 * we attenuate with a fitted function that removes some energy below the horizon. */
359 form_factor *= detail::form_factor_attenuation(shape, ltc_data, lv.L);
360 form_factor *= detail::diffuse_sphere_integral(util_tx, avg_dir.z, form_factor);
361 break;
362 default: /* LTCFormFactorType::TwoSidedCosineSphere */
363 form_factor *= M_1_PI;
364 break;
365 }
366
367 return form_factor;
368}
369
375 sampler2DArray util_tx, LightData light, LightShape shape, LightVector lv, LTCData ltc_data)
376{
377 if (is_sphere_light(light.type) && lv.dist < light.local.local.shape_radius) {
378 /* Inside the sphere light, integrate over the hemisphere. */
379 return 1.0f;
380 }
381
382 if (light.type == LIGHT_RECT) {
383 return evaluate_quad(util_tx, shape, lv, ltc_data);
384 }
385 return evaluate_disk(util_tx, shape, lv, ltc_data);
386}
387
388} // namespace eevee::ltc
#define D
#define M_1_PI
#define M_PI
#define C
Definition RandGen.cpp:29
#define A
btScalar max
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
#define UTIL_DISK_INTEGRAL_COMP
#define UTIL_TEX_UV_BIAS
#define UTIL_DISK_INTEGRAL_LAYER
#define UTIL_TEX_UV_SCALE
#define FLT_MAX
#define atan
#define cos
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T inversesqrt(T) RET
#define abs
#define sqrt
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
constexpr T clamp(T, U, U) RET
TEX_TEMPLATE DataVec texture(T, FltCoord, float=0.0f) RET
SamplerBase< float, 2, false, true > sampler2DArray
float square(float v)
float3x3 scale(float3x3 mat, float2 scale)
float3x3 rotate(float3x3 mat, AxisAngle rotation)
float safe_divide(float a, float b)
VecT normalize_fallback(VecT vector, VecT fallback)
#define T
#define B
#define F
#define L
#define G(x, y, z)
float3 edge_integral_vec(float3 v1, float3 v2)
float diffuse_sphere_integral(sampler2DArray util_tx, float avg_dir_z, float form_factor)
float upper_parabole(float x, float scale, float y_1)
float3 solve_cubic(float4 coefs)
float form_factor_attenuation(LightShape shape, LTCData ltc_data, float3 L)
float3 dominant_bxdf_direction(float3x3 Minv)
float evaluate(sampler2DArray util_tx, LightData light, LightShape shape, LightVector lv, LTCData ltc_data)
float evaluate_disk(sampler2DArray util_tx, LightShape shape, LightVector lv, LTCData ltc_data)
float evaluate_quad(sampler2DArray util_tx, LightShape shape, LightVector lv, LTCData ltc_data)
#define saturate(a)
Definition smaa.cc:319
#define min(a, b)
Definition sort.cc:38
LTCFormFactorType form_factor_type
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
CCL_NAMESPACE_BEGIN struct Window V