Blender V5.3
eevee_bxdf_microfacet.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
10#include "gpu_shader_codegen_lib.glsl"
15#include "gpu_shader_ray.bsl.hh"
16
17/* -------------------------------------------------------------------- */
20
21float bxdf_ggx_D(float NH, float a2)
22{
23 float NH2 = square(NH);
24 return a2 / (M_PI * square((1.0f - NH2) + a2 * NH2));
25}
26
27float bxdf_ggx_smith_G1(float NX, float a2)
28{
29 return 2.0f / (1.0f + sqrt(1.0f + a2 * (1.0f / square(NX) - 1.0f)));
30}
31
33
34/* -------------------------------------------------------------------- */
40
52BsdfSample bxdf_ggx_sample_reflection(float3 rand, float3 Vt, float alpha, bool do_clamp)
53{
54 if (do_clamp && alpha < square(BSDF_ROUGHNESS_THRESHOLD)) {
55 BsdfSample samp;
56 samp.pdf = 1e6f;
57 samp.direction = reflect(-Vt, float3(0.0f, 0.0f, 1.0f));
58 return samp;
59 }
60
61 /* Transforming the view direction to the hemisphere configuration. */
62 float2 Vt_alpha = alpha * Vt.xy;
63 float Vh_norm;
64 float3 Vh = normalize_and_get_length(float3(Vt_alpha, Vt.z), Vh_norm);
65
66 /* Compute the bounded cap. */
67 float a2 = square(alpha);
68 float s2 = square(1.0f + length(Vt.xy));
69 float k = (1.0f - a2) * s2 / (s2 + a2 * square(Vt.z));
70
71 /* Sample a spherical cap in (-Vh.z * k, 1]. */
72 float cos_theta = mix(-Vh.z * k, 1.0f, rand.x);
73 float sin_theta = sqrt(saturate(1.0f - square(cos_theta)));
74 float3 Lh = float3(sin_theta * rand.yz, cos_theta);
75
76 /* Compute unnormalized halfway direction. */
77 float3 Hh = Vh + Lh;
78
79 /* Transforming the normal back to the ellipsoid configuration. */
80 float3 Ht = normalize(float3(alpha * Hh.xy, max(0.0f, Hh.z)));
81
82 /* Normal Distribution Function. */
83 float D = bxdf_ggx_D(saturate(Ht.z), a2);
84
85 float pdf;
86 if (Vt.z >= 0.0f) {
87 pdf = D / (2.0f * (k * Vt.z + Vh_norm));
88 }
89 else {
90 float len_Vt_alpha_sqr = length_squared(Vt_alpha);
91 float t = sqrt(len_Vt_alpha_sqr + square(Vt.z));
92 pdf = D * (t - Vt.z) / (2.0f * len_Vt_alpha_sqr);
93 }
94
95 BsdfSample samp;
96 samp.direction = reflect(-Vt, Ht);
97 samp.pdf = pdf;
98 return samp;
99}
100
101/* Evaluate the GGX BRDF without the Fresnel term, multiplied by the cosine foreshortening term.
102 * Also evaluate the probability of sampling the reflection direction. */
103BsdfEval bxdf_ggx_eval_reflection(float3 N, float3 L, float3 V, float alpha, bool do_clamp)
104{
105 float NV = dot(N, V);
106 if (NV <= 0.0f) {
107#if 0 /* TODO(fclem): Creates black areas when denoising. Find out why. */
108 /* Impossible configuration for reflection. */
109 BsdfEval eval;
110 eval.throughput = 0.0f;
111 eval.pdf = 0.0f;
112 eval.weight = 0.0f;
113 return eval;
114#endif
115 }
116
118
119 /* This threshold was computed based on precision of NVidia compiler (see #118997).
120 * These drivers tend to produce NaNs in the computation of the NDF (`D`) if alpha is close to 0.
121 */
122 if (do_clamp) {
123 alpha = max(1e-3f, alpha);
124 }
125
126 float a2 = square(alpha);
127
128 BsdfEval eval;
129
130 float NV2 = square(NV);
131 float one_minus_NV2 = saturate(1.0f - NV2);
132 float s2 = square(1.0f + sqrt(one_minus_NV2));
133 float len_ai_sqr = a2 * one_minus_NV2;
134 float t = sqrt(len_ai_sqr + NV2);
135 if (NV >= 0.0f) {
136 float k = (1.0f - a2) * s2 / (s2 + a2 * NV2);
137 eval.pdf = 1.0f / (2.0f * (k * NV + t));
138 }
139 else {
140 eval.pdf = (t - NV) / (2.0f * len_ai_sqr);
141 }
142
143 float NH = dot(N, H);
144 float NL = dot(N, L);
145 if (do_clamp) {
146 NH = max(NH, 1e-8f);
147 NL = max(NL, 1e-8f);
148 }
149
150 float D = bxdf_ggx_D(NH, a2);
151 float G_V = bxdf_ggx_smith_G1(NV, a2);
152 float G_L = bxdf_ggx_smith_G1(NL, a2);
153
154 eval.throughput = (0.25f * G_V * G_L) / NV;
155 eval.weight = eval.throughput / eval.pdf;
156
157 /* Multiply `D` after computing the weighted throughput for numerical stability. */
158 eval.throughput *= D;
159 eval.pdf *= D;
160
161 return eval;
162}
163
164
165/* -------------------------------------------------------------------- */
171
172float3 bxdf_ggx_sample_vndf(float3 rand, float3 Vt, float alpha, float &G_V)
173{
174 /* Transforming the view direction to the hemisphere configuration. */
175 float3 Vh = normalize(float3(alpha * Vt.xy, Vt.z));
176
177 /* Smith shadowing-masking term. */
178 G_V = 2.0f * Vh.z / (1.0f + Vh.z);
179
180 /* Sample a spherical cap in (-Vh.z, 1]. */
181 float cos_theta = mix(-Vh.z, 1.0f, rand.x);
182 float sin_theta = sqrt(saturate(1.0f - square(cos_theta)));
183 float3 Lh = float3(sin_theta * rand.yz, cos_theta);
184
185 /* Compute unnormalized halfway direction. */
186 float3 Hh = Vh + Lh;
187
188 /* Transforming the normal back to the ellipsoid configuration. */
189 return normalize(float3(alpha * Hh.xy, max(0.0f, Hh.z)));
190}
191
205 float3 rand, float3 Vt, float alpha, float ior, Thickness thickness, const bool do_clamp)
206{
207 if (thickness.value() != 0.0f) {
208 /* The incoming ray is inside the material for the second refraction event. */
209 ior = 1.0f / ior;
210 }
211
212 if (do_clamp && alpha < square(BSDF_ROUGHNESS_THRESHOLD)) {
213 BsdfSample samp;
214 samp.pdf = 1e6f;
215 samp.direction = refract(-Vt, float3(0.0f, 0.0f, 1.0f), 1.0f / ior);
216 return samp;
217 }
218
219 float G_V;
220 float3 Ht = bxdf_ggx_sample_vndf(rand, Vt, alpha, G_V);
221
222 float3 Lt = refract(-Vt, Ht, 1.0f / ior);
223
224 /* Normal Distribution Function. */
225 float D = bxdf_ggx_D(saturate(Ht.z), square(alpha));
226
227 float VH = dot(Vt, Ht);
228 float LH = dot(Lt, Ht);
229 float Ht2 = square(ior * LH + VH);
230 float pdf = D * G_V * abs(VH * LH) * square(ior) / (Vt.z * Ht2);
231
232 BsdfSample samp;
233 samp.direction = Lt;
234 samp.pdf = pdf;
235 return samp;
236}
237
238/* Evaluate the GGX BTDF without the Fresnel term, multiplied by the cosine foreshortening term.
239 * Also evaluate the probability of sampling the refraction direction. */
241 float3 N, float3 L, float3 V, float alpha, float ior, Thickness thickness, const bool do_clamp)
242{
243 if (thickness.value() != 0.0f) {
244 ior = 1.0f / ior;
245 }
246
247 float LV = dot(L, V);
248 if (do_clamp && is_equal(ior, 1.0f, 1e-4f)) {
249 /* Only valid when `L` and `V` point in the opposite directions. */
250 BsdfEval eval;
251 eval.throughput = float(is_equal(LV, -1.0f, 1e-3f));
252 eval.pdf = 1e6f;
253 eval.weight = eval.throughput;
254 return eval;
255 }
256
257 bool valid = (ior < 1.0f) ? (LV < -ior) : (LV * ior < -1.0f);
258 if (!valid) {
259 /* Impossible configuration for transmission due to total internal reflection. */
260 BsdfEval eval;
261 eval.throughput = 0.0f;
262 eval.pdf = 0.0f;
263 eval.weight = 0.0f;
264 return eval;
265 }
266
267 float3 H = ior * L + V;
268 /* Ensure `H` is on the same hemisphere as `V`. */
269 H = (ior < 1.0f) ? H : -H;
270 float inv_len_H = safe_rcp(length(H));
271 H *= inv_len_H;
272
273 /* For transmission, `L` lies in the opposite hemisphere as `H`, therefore negate `L`. */
274 float NL = dot(N, -L);
275 float NH = dot(N, H);
276 float NV = dot(N, V);
277
278 if (do_clamp) {
279 NL = max(NL, 1e-8f);
280 NH = max(NH, 1e-8f);
281 /* This threshold was computed based on precision of NVidia compiler (see #118997).
282 * These drivers tend to produce NaNs in the computation of the NDF (`D`) if alpha is close to
283 * 0. */
284 alpha = max(1e-3f, alpha);
285 }
286
287 float a2 = square(alpha);
288 float G_V = bxdf_ggx_smith_G1(NV, a2);
289 float G_L = bxdf_ggx_smith_G1(NL, a2);
290 float D = bxdf_ggx_D(NH, a2);
291
292 float VH = saturate(dot(V, H));
293 float LH = saturate(dot(-L, H));
294
295 BsdfEval eval;
296 eval.pdf = (D * G_V * VH * LH * square(ior * inv_len_H)) / NV;
297 eval.throughput = eval.pdf * G_L;
298 eval.weight = G_L;
299
300 return eval;
301}
302
304
305/* -------------------------------------------------------------------- */
308
313{
314 return roughness;
315}
316
321float bxdf_ggx_perceived_roughness_transmission(float roughness, float ior)
322{
323 /* This is a very rough mapping used by manually curve fitting the apparent roughness
324 * (blurriness) of GGX reflections and GGX refraction.
325 * A better fit is desirable if it is in the same order of complexity. */
326 return roughness *
327 sqrt_fast((ior > 1.0f) ? (1.0f - 1.0f / ior) : (saturate(1.0f - ior) * 0.64f));
328}
329
335{
336 /* From Frostbite PBR Course
337 * http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf
338 * Listing 22.
339 * Note that the reference labels squared roughness (GGX input) as roughness. */
340 float m = square(roughness);
341 float3 R = -reflect(V, N);
342 float smoothness = 1.0f - m;
343 float fac = smoothness * (sqrt(smoothness) + m);
344 return normalize(mix(N, R, fac));
345}
346
353{
354 /* Reusing same thing as bxdf_ggx_dominant_direction_reflection for now with the roughness mapped
355 * to reflection roughness. */
356 float m = square(roughness);
357 float3 R = refract(-V, N, 1.0f / ior);
358 float smoothness = 1.0f - m;
359 float fac = smoothness * (sqrt(smoothness) + m);
360 return normalize(mix(-N, R, fac));
361}
362
364{
365 LightProbeRay probe;
366 probe.perceptual_roughness = cl.roughness;
368 cl.N, V, probe.perceptual_roughness);
369 return probe;
370}
371
373{
374 LightProbeRay probe;
377 cl.N, V, thickness.value() != 0.0f ? 1.0f / cl.ior : cl.ior, probe.perceptual_roughness);
378 return probe;
379}
380
382{
383 LightProbeRay probe;
384 probe.perceptual_roughness = cl.roughness;
386 -cl.N, reflect(V, cl.N), probe.perceptual_roughness);
387 return probe;
388}
389
390void bxdf_ggx_context_amend_transmission(ClosureUndetermined &cl, float3 &V, Thickness thickness)
391{
392 if (thickness.value() != 0.0f) {
393 ClosureRefraction bsdf = to_closure_refraction(cl);
394 float perceived_roughness = bxdf_ggx_perceived_roughness_transmission(bsdf.roughness,
395 bsdf.ior);
396 float3 L = bxdf_ggx_dominant_direction_transmission(bsdf.N, V, bsdf.ior, perceived_roughness);
397 cl.N = -thickness.shape_intersect(bsdf.N, L).hit_N;
398 V = -L;
399 }
400}
401
402Ray bxdf_ggx_ray_amend_transmission(ClosureUndetermined cl, float3 V, Ray ray, Thickness thickness)
403{
404 if (thickness.value() != 0.0f) {
405 ClosureRefraction bsdf = to_closure_refraction(cl);
406 float perceived_roughness = bxdf_ggx_perceived_roughness_transmission(bsdf.roughness,
407 bsdf.ior);
408 float3 L = bxdf_ggx_dominant_direction_transmission(bsdf.N, V, bsdf.ior, perceived_roughness);
409 ray.origin += thickness.shape_intersect(bsdf.N, L).hit_P;
410 }
411 return ray;
412}
413
415 ClosureReflection cl,
416 float3 V)
417{
418 float cos_theta = dot(cl.N, V);
419
420 ClosureLight light;
421 light.N = cl.N;
422 light.type = LIGHT_SPECULAR;
423 eevee::LTCData::pack_ltc_lut(light, util_tx, light.N, V, cos_theta, cl.roughness);
424
425 return light;
426}
427
429 ClosureRefraction cl,
430 float3 V,
431 Thickness thickness)
432{
433 float perceptual_roughness = bxdf_ggx_perceived_roughness_transmission(cl.roughness, cl.ior);
434
435 /* Given non-zero thickness, simulate transmission through a slab/sphere, and output
436 * an amended view vector as the direction of the first scatter. */
437 if (thickness.value() != 0.0f) {
438 float3 L = bxdf_ggx_dominant_direction_transmission(cl.N, V, cl.ior, perceptual_roughness);
439 ThicknessIsect isect = thickness.shape_intersect(cl.N, L);
440 cl.N = -isect.hit_N;
441 V = -L;
442 }
443
444 ClosureLight light;
445 light.N = -cl.N;
446 light.type = LIGHT_TRANSMISSION;
447
448 /* Reuse the isotropic LTC LUT by using the refracted exitant view as direction. */
449 float3 R = refract(-V, cl.N, (thickness.value() != 0.0f) ? cl.ior : (1.0f / cl.ior));
450 float cos_theta = dot(light.N, R);
451 eevee::LTCData::pack_ltc_lut(light, util_tx, light.N, V, cos_theta, perceptual_roughness);
452
453 return light;
454}
455
457 ClosureThinRefraction cl,
458 float3 V)
459{
460 float cos_theta = dot(cl.N, V);
461
462 ClosureLight light;
463 light.N = -cl.N;
464 light.type = LIGHT_TRANSMISSION;
465 eevee::LTCData::pack_ltc_lut(light, util_tx, light.N, V, cos_theta, cl.roughness);
466
467 return light;
468}
469
#define D
#define M_PI
ccl_device_inline float cos_theta(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
btScalar max
ThicknessIsect shape_intersect(float3 N, float3 L)
float value() const
#define NL
BsdfSample bxdf_ggx_sample_refraction(float3 rand, float3 Vt, float alpha, float ior, Thickness thickness, const bool do_clamp)
float bxdf_ggx_perceived_roughness_reflection(float roughness)
float3 bxdf_ggx_dominant_direction_reflection(float3 N, float3 V, float roughness)
float bxdf_ggx_D(float NH, float a2)
LightProbeRay bxdf_ggx_lightprobe_reflection(ClosureReflection cl, float3 V)
LightProbeRay bxdf_ggx_lightprobe_thin_glass_transmission(ClosureThinRefraction cl, float3 V)
float3 bxdf_ggx_sample_vndf(float3 rand, float3 Vt, float alpha, float &G_V)
LightProbeRay bxdf_ggx_lightprobe_transmission(ClosureRefraction cl, float3 V, Thickness thickness)
ClosureLight bxdf_ggx_light_reflection(const UtilityTexture &util_tx, ClosureReflection cl, float3 V)
void bxdf_ggx_context_amend_transmission(ClosureUndetermined &cl, float3 &V, Thickness thickness)
BsdfEval bxdf_ggx_eval_refraction(float3 N, float3 L, float3 V, float alpha, float ior, Thickness thickness, const bool do_clamp)
BsdfEval bxdf_ggx_eval_reflection(float3 N, float3 L, float3 V, float alpha, bool do_clamp)
ClosureLight bxdf_ggx_light_thin_glass_transmission(const UtilityTexture &util_tx, ClosureThinRefraction cl, float3 V)
ClosureLight bxdf_ggx_light_transmission(const UtilityTexture &util_tx, ClosureRefraction cl, float3 V, Thickness thickness)
float3 bxdf_ggx_dominant_direction_transmission(float3 N, float3 V, float ior, float roughness)
BsdfSample bxdf_ggx_sample_reflection(float3 rand, float3 Vt, float alpha, bool do_clamp)
float bxdf_ggx_perceived_roughness_transmission(float roughness, float ior)
float bxdf_ggx_smith_G1(float NX, float a2)
Ray bxdf_ggx_ray_amend_transmission(ClosureUndetermined cl, float3 V, Ray ray, Thickness thickness)
#define BSDF_ROUGHNESS_THRESHOLD
VecBase< T, D > refract(VecOp< T, D >, VecOp< T, D >, float) RET
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define sqrt
float length(VecOp< float, D >) RET
float square(float v)
bool is_equal(float a, float b, const float epsilon)
float sqrt_fast(float v)
float safe_rcp(float a)
VecT normalize_and_get_length(VecT vector, float &out_length)
float length_squared(VecT a)
VecT safe_normalize(VecT vector)
#define N
#define R
#define L
#define H(x, y, z)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
packed_float3 direction
packed_float3 dominant_direction
packed_float3 origin
static void pack_ltc_lut(ClosureLight &cl, const UtilityTexture &util_tx, float3 N, float3 V, float cos_theta, float roughness)
float z
Definition sky_math.h:136
float x
Definition sky_math.h:136
CCL_NAMESPACE_BEGIN struct Window V