Blender V5.3
gpu_shader_material_open_pbr_util.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5/* Util functions and closure setup for Principled BSDF. */
6
7#pragma once
8
14
15float ior_from_F0(const float F0)
16{
17 const float f = sqrt(clamp(F0, 0.0f, 0.99f));
18 return (-f - 1.0f) / (f - 1.0f);
19}
20
21/* Given the transmittance through a slab at normal incidence, compute the transmittance at a
22 * certain incident angle, based on Beer-Lambert law. */
23float3 slab_transmittance_at_angle(const float3 color, const float cos_theta_i, const float ior)
24{
25 const float inv_cos_theta_t = ior / sqrt_fast(square(ior) - (1.0f - square(cos_theta_i)));
26 return pow(color, float3(inv_cos_theta_t));
27}
28
29float thin_glass_transmission_roughness(const float roughness, const float ior)
30{
31 return saturate(roughness *
32 sqrt(3.4f * (ior - 1.0f) * square(ior - 0.5f) / (square(ior) * ior)));
33}
34
35float openpbr_fuzz(const float NV, const float rough)
36{
37 /* Empirical approximation (manual curve fitting) to the fuzz_weight albedo. Can be refined. */
38 const float den = 35.6694f * rough * rough - 24.4269f * rough * NV - 0.1405f * NV * NV +
39 6.1211f * rough + 0.28105f * NV - 0.1405f;
40 const float num = 58.5299f * rough * rough - 85.0941f * rough * NV + 9.8955f * NV * NV +
41 1.9250f * rough + 74.2268f * NV - 0.2246f;
42 return saturate(den / num);
43}
44
52
53struct Fuzz {
55 float roughness;
56 float weight;
57};
58
59struct Specular {
61 float roughness;
62 float ior;
63 float weight;
64};
65
70
77
79 ShadingData &sd,
80 const float3 weight,
81 const float opacity)
82{
83 ClosureTransparency transparency_data;
84 transparency_data.transmittance = (1.0f - opacity) * weight;
85 transparency_data.holdout = 0.0f;
86 closure_eval(kg, sd, transparency_data);
87
88 return weight * opacity;
89}
90
92 ShadingData & /*sd*/,
93 const float3 weight,
94 const Coat coat,
95 const Fuzz fuzz,
96 const float3 N,
97 const float3 V,
98 ClosureDiffuse &diffuse_data)
99{
100 if (fuzz.weight == 0.0f) {
101 diffuse_data.color = float3(0.0f);
102 return weight;
103 }
104
105 float fuzz_NV = dot(N, V);
106 if (coat.weight > 0.0f) {
107 const float3 fuzz_N = safe_normalize(mix(N, coat.N, coat.weight));
108 fuzz_NV = dot(fuzz_N, V);
109 }
110 fuzz_NV = saturate(fuzz_NV);
111
112 const float3 fuzz_color = fuzz.weight * fuzz.tint * openpbr_fuzz(fuzz_NV, fuzz.roughness);
113 /* Consider fuzz as diffuse until we have proper fuzz BSDF in EEVEE. */
114 /* TODO: Maybe fuzz should be specular. */
115 diffuse_data.color = weight * fuzz_color;
116 /* Attenuate lower layers */
117 return weight * max((1.0f - math_reduce_max(fuzz_color)), 0.0f);
118}
119
121 KernelGlobals &kg, ShadingData &sd, float3 weight, Coat coat, const float3 V)
122{
123 if (coat.weight == 0.0f) {
124 return weight;
125 }
126
127 const float coat_NV = dot(coat.N, V);
128 const float reflectance = bsdf_lut(kg, coat_NV, coat.roughness, coat.ior, false).x;
129
130 ClosureReflection coat_data;
131 coat_data.N = coat.N;
132 coat_data.roughness = coat.roughness;
133 coat_data.color = weight * coat.weight * reflectance;
134 closure_eval(kg, sd, coat_data);
135
136 if (!all(equal(coat.tint, float3(1.0f)))) {
137 coat.tint = slab_transmittance_at_angle(coat.tint, coat_NV, coat.ior);
138 }
139
140 /* Attenuate lower layers */
141 weight *= saturate(1.0f - (1.0f - coat.tint * (1.0f - reflectance)) * coat.weight);
142
143 return weight;
144}
145
147 ShadingData &sd,
148 const float3 weight,
149 const float3 color,
150 const float luminance)
151{
152 ClosureEmission emission_data;
153 emission_data.emission = color * luminance * weight;
154 closure_eval(kg, sd, emission_data);
155 return weight;
156}
157
159 const float3 weight,
160 const float3 F0,
161 const Specular specular,
162 const float metalness,
163 const float NV,
164 const bool multiggx,
165 ClosureReflection &reflection_data)
166{
167 if (metalness == 0.0f) {
168 reflection_data.color = float3(0.0f);
169 return weight;
170 }
171
172 float3 reflectance;
174 kg, saturate(F0), saturate(specular.tint), NV, specular.roughness, multiggx, reflectance);
175 reflection_data.color = weight * metalness * reflectance;
176
177 /* Attenuate lower layers */
178 return weight * max((1.0f - metalness), 0.0f);
179}
180
181float openpbr_modulate_ior(const float specular_weight, const float specular_ior)
182{
183 if (specular_weight == 1.0f) {
184 return specular_ior;
185 }
186
187 const float modulated_ior = ior_from_F0(specular_weight * F0_from_ior(specular_ior));
188 return (specular_ior < 1.0f) ? 1.0f / modulated_ior : modulated_ior;
189}
190
192 ShadingData &sd,
193 const float3 weight,
194 const float transmission,
195 const float3 reflectance,
196 const float3 transmittance,
197 const float3 N,
198 const Specular specular,
199 const bool thin_walled,
200 ClosureReflection &reflection_data)
201{
202 const float3 transmission_weight = weight * transmission;
203 reflection_data.color += transmission_weight * reflectance;
204
205 if (thin_walled) {
206 ClosureThinRefraction refraction_data;
207 refraction_data.color = transmittance * transmission_weight;
208 refraction_data.N = N;
209 refraction_data.roughness = thin_glass_transmission_roughness(specular.roughness,
210 specular.ior);
211 closure_eval(kg, sd, refraction_data);
212 }
213 else {
214 ClosureRefraction refraction_data;
215 refraction_data.N = N;
216 refraction_data.roughness = specular.roughness;
217 refraction_data.ior = specular.ior;
218 refraction_data.color = transmittance * transmission_weight;
219 closure_eval(kg, sd, refraction_data);
220 }
221
222 /* Attenuate lower layers */
223 return weight * max((1.0f - transmission), 0.0f);
224}
225
227 ShadingData &sd,
228 const float3 weight,
229 const Subsurface subsurface,
230 const bool thin_walled,
231 const float3 N,
232 ClosureDiffuse &diffuse_data)
233{
234 if (subsurface.weight == 0.0f) {
235 return weight;
236 }
237
238 if (thin_walled) {
239 /* Backward scattering is approximated by diffuse. */
240 diffuse_data.color += subsurface.tint * weight * subsurface.weight *
241 saturate(0.5f * (1.0f - subsurface.anisotropy));
242
243 const float transmit_weight = subsurface.weight *
244 saturate(0.5f * (1.0f + subsurface.anisotropy));
245 /* Forward scattering is approximated by translucent. */
246 ClosureTranslucent translucent_data;
247 translucent_data.color = subsurface.tint * transmit_weight * weight;
248 translucent_data.N = N;
249 closure_eval(kg, sd, translucent_data);
250 }
251 else {
252 ClosureSubsurface sss_data;
253 sss_data.N = N;
254 sss_data.sss_radius = subsurface.radius;
255 sss_data.color = subsurface.weight * weight * subsurface.tint;
256 closure_eval(kg, sd, sss_data);
257 }
258
259 /* Attenuate lower layers */
260 return weight * max((1.0f - subsurface.weight), 0.0f);
261}
262
264 ShadingData &sd,
265 const float3 weight,
266 const float3 color,
267 const float3 N,
268 ClosureDiffuse &diffuse_data)
269{
270 diffuse_data.N = N;
271 diffuse_data.color += weight * color;
272
273 closure_eval(kg, sd, diffuse_data);
274}
btScalar max
void bsdf_lut(KernelGlobals &kg, float3 F0, float3 F90, float3 transmission_tint, float cos_theta, float roughness, float ior, bool do_multiscatter, float3 &reflectance, float3 &transmittance)
void brdf_f82_tint_lut(KernelGlobals &kg, float3 F0, float3 F82, float cos_theta, float roughness, bool do_multiscatter, float3 &reflectance)
float F0_from_ior(float eta)
Closure closure_eval(KernelGlobals &kg, ShadingData &sd, ClosureDiffuse diffuse)
float math_reduce_max(float3 a)
#define pow
float dot(VecOp< float, D >, VecOp< float, D >) RET
bool all(VecOp< bool, D >) RET
#define sqrt
VecBase< bool, D > equal(VecOp< T, D >, VecOp< T, D >) RET
constexpr T clamp(T, U, U) RET
float openpbr_fuzz(const float NV, const float rough)
float3 openpbr_eval_metal(KernelGlobals &kg, const float3 weight, const float3 F0, const Specular specular, const float metalness, const float NV, const bool multiggx, ClosureReflection &reflection_data)
float3 openpbr_eval_coat(KernelGlobals &kg, ShadingData &sd, float3 weight, Coat coat, const float3 V)
float thin_glass_transmission_roughness(const float roughness, const float ior)
float3 slab_transmittance_at_angle(const float3 color, const float cos_theta_i, const float ior)
float3 openpbr_eval_transparency(KernelGlobals &kg, ShadingData &sd, const float3 weight, const float opacity)
float3 openpbr_eval_fuzz(KernelGlobals &, ShadingData &, const float3 weight, const Coat coat, const Fuzz fuzz, const float3 N, const float3 V, ClosureDiffuse &diffuse_data)
float3 openpbr_eval_subsurface(KernelGlobals &kg, ShadingData &sd, const float3 weight, const Subsurface subsurface, const bool thin_walled, const float3 N, ClosureDiffuse &diffuse_data)
float ior_from_F0(const float F0)
float3 openpbr_eval_emission(KernelGlobals &kg, ShadingData &sd, const float3 weight, const float3 color, const float luminance)
float openpbr_modulate_ior(const float specular_weight, const float specular_ior)
void openpbr_eval_diffuse(KernelGlobals &kg, ShadingData &sd, const float3 weight, const float3 color, const float3 N, ClosureDiffuse &diffuse_data)
float3 openpbr_eval_translucent(KernelGlobals &kg, ShadingData &sd, const float3 weight, const float transmission, const float3 reflectance, const float3 transmittance, const float3 N, const Specular specular, const bool thin_walled, ClosureReflection &reflection_data)
float square(float v)
float sqrt_fast(float v)
VecT safe_normalize(VecT vector)
#define N
#define mix
#define saturate(a)
Definition smaa.cc:319
CCL_NAMESPACE_BEGIN struct Window V