Blender V5.3
bsdf_oren_nayar.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/types.h"
8
11
13
20
26
27static_assert(sizeof(ShaderClosure) >= sizeof(OrenNayarBsdf), "OrenNayarBsdf is too large!");
28
29/* NOTE: This implements the improved Oren-Nayar model by Yasuhiro Fujii
30 * (https://mimosa-pudica.net/improved-oren-nayar.html), plus an
31 * energy-preserving multi-scattering term based on the OpenPBR specification
32 * (https://academysoftwarefoundation.github.io/OpenPBR). */
33
34/* Above certain roughness threshold we switch to Oren Nayar model. */
36{
37 return alpha < 1e-5f;
38}
39
40ccl_device_inline float bsdf_oren_nayar_G(const float cosTheta)
41{
42 if (cosTheta < 1e-6f) {
43 /* The tan(theta) term starts to act up at low cosTheta, so fall back to Taylor expansion. */
44 return (M_PI_2_F - 2.0f / 3.0f) - cosTheta;
45 }
46 const float sinTheta = sin_from_cos(cosTheta);
47 const float theta = safe_acosf(cosTheta);
48 return sinTheta * (theta - 2.0f / 3.0f - sinTheta * cosTheta) +
49 2.0f / 3.0f * (sinTheta / cosTheta) * (1.0f - sqr(sinTheta) * sinTheta);
50}
51
53 const float3 n,
54 const float3 v,
55 const float3 l)
56{
57 const OrenNayarParam param = bsdf->param;
58 const float nl = max(dot(n, l), 0.0f);
59 if (param.b <= 0.0f) {
60 return make_spectrum(nl * M_1_PI_F);
61 }
62 const float nv = max(dot(n, v), 0.0f);
63 float t = dot(l, v) - nl * nv;
64
65 if (t > 0.0f) {
66 t /= max(nl, nv) + FLT_MIN;
67 }
68
69 const float single_scatter = param.a + param.b * t;
70
71 const float El = param.a * M_PI_F + param.b * bsdf_oren_nayar_G(nl);
72 const Spectrum multi_scatter = param.multiscatter_term * (1.0f - El);
73
74 return nl * (make_spectrum(single_scatter) + multi_scatter);
75}
76
78 const float nv,
79 const float roughness)
80{
81 const float sigma = saturatef(roughness);
82 const float a = 1.0f / (M_PI_F + sigma * (M_PI_2_F - 2.0f / 3.0f));
83 const float b = sigma * a;
84
85 /* Compute energy compensation term (except for (1.0f - El) factor since it depends on wo). */
86 const Spectrum albedo = saturate(color);
87 const float Eavg = a * M_PI_F + ((M_2PI_F - 5.6f) / 3.0f) * b;
88 const Spectrum Ems = M_1_PI_F * albedo * (Eavg / (1.0f - Eavg)) /
89 (one_spectrum() - albedo * (1.0f - Eavg));
90 const float Ev = a * M_PI_F + b * bsdf_oren_nayar_G(max(nv, 0.0f));
91
92 return {/* .roughness = */ roughness,
93 /* .a = */ a,
94 /* .b = */ b,
95 /* .multiscatter_term = */ Ems * (1.0f - Ev)};
96}
97
99 const float3 N,
100 const Spectrum weight,
101 const float roughness,
102 const Spectrum color)
103{
105 sd, sizeof(OrenNayarBsdf), weight);
106 if (bsdf) {
107 bsdf->N = N;
108 bsdf->type = CLOSURE_BSDF_OREN_NAYAR_ID;
109 bsdf->param = bsdf_oren_nayar_param(color, dot(bsdf->N, sd->wi), roughness);
110 sd->runtime_flag |= SR_BSDF | SR_BSDF_HAS_EVAL;
111 }
112}
113
115 const float3 wi,
116 const float3 wo,
117 ccl_private float *pdf)
118{
119 const ccl_private OrenNayarBsdf *bsdf = (const ccl_private OrenNayarBsdf *)sc;
120 const float cosNO = dot(bsdf->N, wo);
121 if (cosNO > 0.0f) {
122 *pdf = cosNO * M_1_PI_F;
123 return bsdf_oren_nayar_get_intensity(bsdf, bsdf->N, wi, wo);
124 }
125 *pdf = 0.0f;
126 return zero_spectrum();
127}
128
130 const float3 Ng,
131 const float3 wi,
132 const float2 rand,
133 ccl_private Spectrum *eval,
135 ccl_private float *pdf,
136 ccl_private float2 *sampled_roughness,
137 ccl_private float *eta)
138{
139 const ccl_private OrenNayarBsdf *bsdf = (const ccl_private OrenNayarBsdf *)sc;
140
141 sample_cos_hemisphere(bsdf->N, rand, wo, pdf);
142
143 if (dot(Ng, *wo) > 0.0f) {
144 *eval = bsdf_oren_nayar_get_intensity(bsdf, bsdf->N, wi, *wo);
145 }
146 else {
147 *pdf = 0.0f;
148 *eval = zero_spectrum();
149 }
150
151 *sampled_roughness = one_float2();
152 *eta = 1.0f;
153
155}
156
157/* ---------------------------------------------------------------------------------------- */
168
170 const float3 N,
171 const Spectrum weight,
172 const float anisotropy,
173 const float roughness,
174 const Spectrum color)
175{
176 const Spectrum reflection_weight = saturatef(0.5f * (1.0f - anisotropy)) * weight;
177 const Spectrum transmission_weight = saturatef(0.5f * (1.0f + anisotropy)) * weight;
178 if (is_zero(reflection_weight) && is_zero(transmission_weight)) {
179 return;
180 }
181
182 if (sd->num_closure_left == 0) {
183 return;
184 }
185
186 if (diffuse_roughness_is_almost_zero(roughness)) {
187 bsdf_diffuse_setup(sd, N, reflection_weight);
188 bsdf_translucent_setup(sd, N, transmission_weight);
189 return;
190 }
191
192 const OrenNayarParam param = bsdf_oren_nayar_param(color, dot(N, sd->wi), roughness);
193
194 /* Reflection. */
195 {
197 sd, sizeof(OrenNayarBsdf), reflection_weight);
198 if (bsdf) {
199 bsdf->type = CLOSURE_BSDF_OREN_NAYAR_ID;
200 bsdf->N = N;
201 bsdf->param = param;
202 sd->runtime_flag |= SR_BSDF | SR_BSDF_HAS_EVAL;
203 }
204 }
205
206 /* Transmission. */
207 {
209 sd, sizeof(OrenNayarBsdf), transmission_weight);
210 if (bsdf) {
212 bsdf->N = -N;
213 bsdf->param = param;
214 sd->runtime_flag |= SR_BSDF | SR_BSDF_HAS_EVAL | SR_BSDF_HAS_TRANSMISSION;
215 }
216 }
217}
218
220 const float3 Ng,
221 const float3 wi,
222 const float2 rand,
223 ccl_private Spectrum *eval,
225 ccl_private float *pdf,
226 ccl_private float2 *sampled_roughness,
227 ccl_private float *eta)
228{
229 bsdf_oren_nayar_sample(sc, -Ng, reflect(wi, sc->N), rand, eval, wo, pdf, sampled_roughness, eta);
231}
232
234 const float3 wi,
235 const float3 wo,
236 ccl_private float *pdf)
237{
238 return bsdf_oren_nayar_eval(sc, reflect(wi, sc->N), wo, pdf);
239}
240
242
ccl_device_inline ccl_private ShaderClosure * bsdf_alloc(ccl_private ShaderData *sd, const int size, Spectrum weight)
Definition alloc.h:76
ccl_device void bsdf_translucent_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight)
ccl_device void bsdf_diffuse_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight)
ccl_device int bsdf_oren_nayar_sample(const ccl_private ShaderClosure *sc, const float3 Ng, const float3 wi, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness, ccl_private float *eta)
ccl_device void bsdf_oren_nayar_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight, const float roughness, const Spectrum color)
ccl_device_forceinline bool diffuse_roughness_is_almost_zero(const float alpha)
ccl_device int bsdf_rough_translucent_sample(const ccl_private ShaderClosure *sc, const float3 Ng, const float3 wi, const float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness, ccl_private float *eta)
ccl_device Spectrum bsdf_oren_nayar_eval(const ccl_private ShaderClosure *sc, const float3 wi, const float3 wo, ccl_private float *pdf)
ccl_device Spectrum bsdf_rough_translucent_eval(const ccl_private ShaderClosure *sc, const float3 wi, const float3 wo, ccl_private float *pdf)
ccl_device_inline void bsdf_thin_subsurface_setup(ccl_private ShaderData *sd, const float3 N, const Spectrum weight, const float anisotropy, const float roughness, const Spectrum color)
ccl_device Spectrum bsdf_oren_nayar_get_intensity(const ccl_private OrenNayarBsdf *bsdf, const float3 n, const float3 v, const float3 l)
ccl_device_inline OrenNayarParam bsdf_oren_nayar_param(const Spectrum color, const float nv, const float roughness)
ccl_device_inline float bsdf_oren_nayar_G(const float cosTheta)
btScalar max
#define M_PI_2_F
#define ccl_device_forceinline
#define one_spectrum
#define zero_spectrum
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define M_1_PI_F
#define CCL_NAMESPACE_END
#define saturatef(x)
float3 sample_cos_hemisphere(const float2 rand)
#define FLT_MIN
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
float sin_from_cos(float c)
bool is_zero(float3x3 a)
@ CLOSURE_BSDF_ROUGH_TRANSLUCENT_ID
@ CLOSURE_BSDF_OREN_NAYAR_ID
@ SR_BSDF_HAS_TRANSMISSION
@ SR_BSDF_HAS_EVAL
@ SR_BSDF
@ LABEL_TRANSMIT
@ LABEL_DIFFUSE
@ LABEL_REFLECT
ccl_device float safe_acosf(const float a)
Definition math_base.h:590
ccl_device_inline float2 one_float2()
Definition math_float2.h:18
#define N
#define sqr
#define ccl_device
#define M_2PI_F
#define M_PI_F
#define saturate(a)
Definition smaa.cc:319
OrenNayarParam param
Spectrum multiscatter_term
float3 Spectrum