Blender V5.3
eevee_bxdf_lut.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2017-2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#pragma once
11
12#include "eevee_bxdf.bsl.hh"
17#include "gpu_shader_compat.hh"
19
20namespace eevee::lut {
21
22/* -------------------------------------------------------------------- */
25
41 float roughness;
43
45 {
46 /* We use squared roughness for approximate perceptual linearity
47 * following [Physically Based Shading at Disney]
48 * (https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf)
49 * Section 5.4. */
50 float roughness = square(coords.x);
51
52 float NV = clamp(1.0f - square(coords.y), 1e-4f, 0.9999f);
53 float3 V = float3(sqrt(1.0f - square(NV)), 0.0f, NV);
54
55 return {roughness, V};
56 }
57
59 {
60 /* Geometric normal. */
61 constexpr float3 N = float3(0.0f, 0.0f, 1.0f);
62
63 /* Return values. */
64 GGXBrdfData brdf = {};
65
66 /* Sample light vector, recover half vector as microfacet normal. */
68 float3 H = normalize(V + L);
69
70 /* Restrict light vector to positive hemisphere. */
71 float NL = L.z;
72 if (NL > 0.0f) {
73 float weight = bxdf_ggx_eval_reflection(N, L, V, roughness, false).weight;
74 float VH = saturate(dot(V, H));
75
76 /* Schlick's Fresnel. */
77 float s = saturate(pow5f(1.0f - VH));
78
79 /* F82 tint effect. */
80 float b = VH * saturate(pow6f(1.0f - VH));
81
82 brdf.scale = (1.0f - s) * weight;
83 brdf.bias = s * weight;
84 brdf.metal_bias = b * weight;
85 }
86
87 return brdf;
88 }
89};
90
109 float ior;
111
113 {
114 /* We use squared roughness for approximate perceptual linearity
115 * following [Physically Based Shading at Disney]
116 * (https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf)
117 * Section 5.4. */
118 float roughness = square(coords.z);
119
120 /* ior is sin of critical angle. */
121 float ior = clamp(sqrt(coords.x), 1e-4f, 0.9999f);
122 float critical_cos = sqrt(1.0f - saturate(square(ior)));
123
124 /* Maximize texture usage on both sides of the critical angle. */
125 coords.y = coords.y * 2.0f - 1.0f;
126 coords.y *= (coords.y > 0.0f) ? (1.0f - critical_cos) : critical_cos;
127 /* Center LUT around critical angle to avoid strange interpolation
128 * issues when the critical angle is changing. */
129 coords.y += critical_cos;
130
131 float NV = clamp(coords.y, 1e-4f, 0.9999f);
132 float3 V = float3(sqrt(1.0f - square(NV)), 0.0f, NV);
133
134 return {roughness, ior, V};
135 }
136
138 {
139 /* Geometric normal. */
140 constexpr float3 N = float3(0.0f, 0.0f, 1.0f);
141
142 GGXBsdfData bsdf = {};
143
144 /* Reflection, restricted to positive hemisphere. */
146 float NR = R.z;
147 if (NR > 0.0f) {
148 /* Recover half vector as GGX normal. */
149 float3 H = normalize(V + R);
150 float3 L = refract(-V, H, 1.0f / ior);
151 float HL = abs(dot(H, L));
152
153 /* Schlick's Fresnel. */
154 float s = saturate(pow5f(1.0f - saturate(HL)));
155
156 float weight = bxdf_ggx_eval_reflection(N, R, V, roughness, false).weight;
157 bsdf.scale = (1.0f - s) * weight;
158 bsdf.bias = s * weight;
159 }
160
161 /* Refraction, restricted to negative hemisphere. */
162 float3 T = bxdf_ggx_sample_refraction(Xi, V, roughness, ior, Thickness{}, false).direction;
163 float NT = T.z;
164 /* In the case of TIR, `T == float3(0)`. */
165 if (NT < 0.0f) {
166 /* Recover half vector as GGX normal. */
167 float3 H = normalize(ior * T + V);
168 float HL = abs(dot(H, T));
169
170 /* Schlick's Fresnel. */
171 float s = saturate(pow5f(1.0f - saturate(HL)));
172
173 float weight = bxdf_ggx_eval_refraction(N, T, V, roughness, ior, Thickness{}, false).weight;
174 bsdf.transmission_factor = (1.0f - s) * weight;
175 }
176
177 return bsdf;
178 }
179};
180
197 float ior;
199
201 {
202 /* We use squared roughness for approximate perceptual linearity
203 * following [Physically Based Shading at Disney]
204 * (https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf)
205 * Section 5.4. */
206 float roughness = square(coords.z);
207
208 float F0 = clamp(square(coords.x), 1e-4f, 0.9999f);
209 float ior = (1.0f + F0) / (1.0f - F0);
210
211 float NV = clamp(1.0f - square(coords.y), 1e-4f, 0.9999f);
212 float3 V = float3(sqrt(1.0f - square(NV)), 0.0f, NV);
213
214 return {roughness, ior, V};
215 }
216
218 {
219 /* Geometric normal. */
220 constexpr float3 N = float3(0.0f, 0.0f, 1.0f);
221
222 GGXBtdfGt1Data btdf = {};
223
224 /* Refraction, restricted to negative hemisphere. */
225 float3 L = bxdf_ggx_sample_refraction(Xi, V, roughness, ior, Thickness{}, false).direction;
226 float NL = L.z;
227 if (NL < 0.0f) {
228 /* Recover half vector as GGX normal. */
229 float3 H = normalize(ior * L + V);
230 float HV = abs(dot(H, V));
231
232 /* Schlick's Fresnel. */
233 float s = saturate(pow5f(1.0f - saturate(HV)));
234
235 float weight = bxdf_ggx_eval_refraction(N, L, V, roughness, ior, Thickness{}, false).weight;
236 btdf.transmission_factor = (1.0f - s) * weight;
237 }
238
239 return btdf;
240 }
241};
242
256{
257 /* Note that we only store the 1st (radius == 1) component.
258 * The others are here for debugging overall appearance. */
259 float3 radii = float3(1.0f, 0.2f, 0.1f);
260 float thickness = coords.x * SSS_TRANSMIT_LUT_RADIUS;
261 float3 r = thickness / radii;
262
263 /* Manual fit based on cycles render of a backlit slab of varying thickness.
264 * Mean Error: 0.003
265 * Max Error: 0.015 */
266 float3 exponential = exp(-3.6f * pow(r, float3(1.11f)));
267 float3 gaussian = exp(-pow(3.4f * r, float3(1.6f)));
268 float3 fac = square(saturate(0.5f + r / 0.6f));
269 float3 profile = saturate(mix(gaussian, exponential, fac));
270
271 /* Mask off the end progressively to 0. */
272 profile *= saturate(1.0f - pow5f(coords.x));
273
274 return float4(profile, 0.0f);
275}
276
277/* Note: unused. */
279{
280 /* Note that we only store the 1st (radius == 1) component.
281 * The others are here for debugging overall appearance. */
282 float3 radii = float3(1.0f, 0.2f, 0.1f);
283 float thickness = coords.x * SSS_TRANSMIT_LUT_RADIUS;
284 float3 r = thickness / radii;
285
286 /* Manual fit based on cycles render of a backlit slab of varying thickness.
287 * Mean Error: 0.003
288 * Max Error: 0.016 */
289 float3 scale = float3(0.31f, 0.47f, 0.32f);
290 float3 exponent = float3(-22.0f, -5.8f, -0.5f);
291 float3 profile = float3(dot(scale, exp(exponent * r.r)),
292 dot(scale, exp(exponent * r.g)),
293 dot(scale, exp(exponent * r.b)));
294 profile = saturate(profile - 0.1f);
295
296 /* Mask off the end progressively to 0. */
297 profile *= saturate(1.0f - pow5f(coords.x));
298
299 return float4(profile, 0.0f);
300}
301
303
304/* -------------------------------------------------------------------- */
307
308template<typename F> float4 integrate(const F &func)
309{
310 constexpr uint sample_count = 512u * 512u;
311
312 /* TODO(not_mark): Remove workaround for BSL-spec #4. */
313 // F func = F::from_coords(coords);
314
315 /* Measure func using N samples. */
316 float4 measure = float4(0.0f);
317 for (uint i = 1u; i <= sample_count; i++) {
318 /* Warp sequence to a point on the unit cylinder. */
319 float2 rand = random::hammersley_2d(i, sample_count);
320 float3 Xi = sample_cylinder(rand);
321
322 /* Add sample to measure. */
323 measure += func.eval(Xi).pack();
324 }
325 return measure / float(sample_count);
326}
330
331struct LUT {
333 [[push_constant]] const int type;
335};
336
339{
340 /* Make sure coordinates are covering the whole [0..1] range at texel center. */
341 float3 lut_normalized_coordinate = float3(global_id) / float3(lut.extent - 1);
342
343 /* Make sure missing cases are noticeable. */
344 float4 result = float4(-1);
345 switch (uint(lut.type)) {
346 case LUT_GGX_BRDF_SPLIT_SUM:
347 result = integrate(GGXBrdfClosure::from_coords(lut_normalized_coordinate));
348 break;
349 case LUT_GGX_BSDF_SPLIT_SUM:
350 result = integrate(GGXBsdfClosure::from_coords(lut_normalized_coordinate));
351 break;
352 case LUT_GGX_BTDF_IOR_GT_ONE:
353 result = integrate(GGXBtdfGt1Closure::from_coords(lut_normalized_coordinate));
354 break;
355 case LUT_BURLEY_SSS_PROFILE:
356 result = burley_sss_translucency(lut_normalized_coordinate);
357 break;
358 case LUT_RANDOM_WALK_SSS_PROFILE:
359 result = random_walk_sss_translucency(lut_normalized_coordinate);
360 break;
361 }
362
363 imageStore(lut.image, int3(global_id), result);
364}
365
367
369
370} // namespace eevee::lut
#define NL
BsdfSample bxdf_ggx_sample_refraction(float3 rand, float3 Vt, float alpha, float ior, Thickness thickness, const bool do_clamp)
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)
BsdfSample bxdf_ggx_sample_reflection(float3 rand, float3 Vt, float alpha, bool do_clamp)
#define LUT_WORKGROUP_SIZE
float3 sample_cylinder(float2 rand)
@ SFLOAT_32_32_32_32
#define push_constant
#define resource_table
#define global_invocation_id
#define compute
#define local_size(...)
VecBase< T, D > refract(VecOp< T, D >, VecOp< T, D >, float) RET
#define pow
#define exp
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define abs
#define sqrt
constexpr T clamp(T, U, U) RET
ImageBase< float, 3 > image3D
IMG_TEMPLATE void imageStore(T &, IntCoord, DataVec)
float square(float v)
float pow6f(float x)
float pow5f(float x)
float3x3 scale(float3x3 mat, float2 scale)
#define N
#define T
#define F
#define R
#define L
#define H(x, y, z)
void comp_main(const uint3 global_id, LUT &lut)
float4 integrate(const F &func)
float4 burley_sss_translucency(float3 coords)
float4 random_walk_sss_translucency(float3 coords)
PipelineCompute comp(comp_main)
float2 hammersley_2d(T i, T sample_count)
#define mix
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define saturate(a)
Definition smaa.cc:319
packed_float3 direction
GGXBrdfData eval(float3 Xi) const
static GGXBrdfClosure from_coords(float3 coords)
GGXBsdfData eval(float3 Xi) const
static GGXBsdfClosure from_coords(float3 coords)
static GGXBtdfGt1Closure from_coords(float3 coords)
GGXBtdfGt1Data eval(float3 Xi) const
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136