Blender V5.0
bsdf_ashikhmin_shirley.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/*
6 * ASHIKHMIN SHIRLEY BSDF
7 *
8 * Implementation of
9 * Michael Ashikhmin and Peter Shirley: "An Anisotropic Phong BRDF Model" (2000)
10 *
11 * The Fresnel factor is missing to get a separable bsdf (intensity*color), as is
12 * the case with all other microfacet-based BSDF implementations in Cycles.
13 *
14 * Other than that, the implementation directly follows the paper.
15 */
16
17#pragma once
18
19#include "kernel/types.h"
20
22
24
26{
27 bsdf->alpha_x = clamp(bsdf->alpha_x, 1e-4f, 1.0f);
28 bsdf->alpha_y = clamp(bsdf->alpha_y, 1e-4f, 1.0f);
29
30 bsdf->fresnel_type = MicrofacetFresnel::NONE;
33}
34
35ccl_device void bsdf_ashikhmin_shirley_blur(ccl_private ShaderClosure *sc, const float roughness)
36{
38
39 bsdf->alpha_x = fmaxf(roughness, bsdf->alpha_x);
40 bsdf->alpha_y = fmaxf(roughness, bsdf->alpha_y);
41}
42
44{
45 return 2.0f / (roughness * roughness) - 2.0f;
46}
47
49 const float3 wi,
50 const float3 wo,
51 ccl_private float *pdf)
52{
53 const ccl_private MicrofacetBsdf *bsdf = (const ccl_private MicrofacetBsdf *)sc;
54 const float3 N = bsdf->N;
55
56 float NdotI = dot(N, wi);
57 float NdotO = dot(N, wo);
58
59 float out = 0.0f;
60
61 if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f || (NdotI < 0.0f) || (NdotO < 0.0f)) {
62 *pdf = 0.0f;
63 return zero_spectrum();
64 }
65
66 NdotI = fmaxf(NdotI, 1e-6f);
67 NdotO = fmaxf(NdotO, 1e-6f);
68 const float3 H = normalize(wi + wo);
69 const float HdotI = fmaxf(fabsf(dot(H, wi)), 1e-6f);
70 const float HdotN = fmaxf(dot(H, N), 1e-6f);
71
72 /* pump from original paper
73 * (first derivative disc., but cancels the HdotI in the pdf nicely) */
74 const float pump = 1.0f / fmaxf(1e-6f, (HdotI * fmaxf(NdotI, NdotO)));
75 /* `pump` from D-BRDF paper. */
76 // float pump = 1.0f / fmaxf(1e-4f, ((NdotI + NdotO) * (NdotI * NdotO)));
77
78 const float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
79 const float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
80
81 if (n_x == n_y) {
82 /* isotropic */
83 const float e = n_x;
84 const float lobe = powf(HdotN, e);
85 const float norm = (n_x + 1.0f) / (8.0f * M_PI_F);
86
87 out = NdotO * norm * lobe * pump;
88 /* this is p_h / 4(H.I) (conversion from `wh measure` to `wi measure`, eq. 8 in paper). */
89 *pdf = norm * lobe / HdotI;
90 }
91 else {
92 /* anisotropic */
93 float3 X;
94 float3 Y;
95 make_orthonormals_tangent(N, bsdf->T, &X, &Y);
96
97 const float HdotX = dot(H, X);
98 const float HdotY = dot(H, Y);
99 float lobe;
100 if (HdotN < 1.0f) {
101 const float e = (n_x * HdotX * HdotX + n_y * HdotY * HdotY) / (1.0f - HdotN * HdotN);
102 lobe = powf(HdotN, e);
103 }
104 else {
105 lobe = 1.0f;
106 }
107 const float norm = sqrtf((n_x + 1.0f) * (n_y + 1.0f)) / (8.0f * M_PI_F);
108
109 out = NdotO * norm * lobe * pump;
110 *pdf = norm * lobe / HdotI;
111 }
112
113 return make_spectrum(out);
114}
115
117 const float n_y,
118 const float2 rand,
119 ccl_private float *phi,
120 ccl_private float *cos_theta)
121{
122 *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * rand.x));
123 const float cos_phi = cosf(*phi);
124 const float sin_phi = sinf(*phi);
125 *cos_theta = powf(rand.y, 1.0f / (n_x * cos_phi * cos_phi + n_y * sin_phi * sin_phi + 1.0f));
126}
127
129 const float3 Ng,
130 const float3 wi,
131 float2 rand,
132 ccl_private Spectrum *eval,
134 ccl_private float *pdf,
135 ccl_private float2 *sampled_roughness)
136{
137 const ccl_private MicrofacetBsdf *bsdf = (const ccl_private MicrofacetBsdf *)sc;
138 *sampled_roughness = make_float2(bsdf->alpha_x, bsdf->alpha_y);
139 const float3 N = bsdf->N;
140 int label = LABEL_REFLECT | LABEL_GLOSSY;
141
142 const float NdotI = dot(N, wi);
143 if (!(NdotI > 0.0f)) {
144 *pdf = 0.0f;
145 *eval = zero_spectrum();
146 return LABEL_NONE;
147 }
148
149 const float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
150 const float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
151
152 /* get x,y basis on the surface for anisotropy */
153 float3 X;
154 float3 Y;
155
156 if (n_x == n_y) {
157 make_orthonormals(N, &X, &Y);
158 }
159 else {
160 make_orthonormals_tangent(N, bsdf->T, &X, &Y);
161 }
162
163 /* sample spherical coords for h in tangent space */
164 float phi;
165 float cos_theta;
166 if (n_x == n_y) {
167 /* isotropic sampling */
168 phi = M_2PI_F * rand.x;
169 cos_theta = powf(rand.y, 1.0f / (n_x + 1.0f));
170 }
171 else {
172 /* anisotropic sampling */
173 if (rand.x < 0.25f) { /* first quadrant */
174 rand.x *= 4.0f;
176 }
177 else if (rand.x < 0.5f) { /* second quadrant */
178 rand.x = 4.0f * (0.5f - rand.x);
180 phi = M_PI_F - phi;
181 }
182 else if (rand.x < 0.75f) { /* third quadrant */
183 rand.x = 4.0f * (rand.x - 0.5f);
185 phi = M_PI_F + phi;
186 }
187 else { /* fourth quadrant */
188 rand.x = 4.0f * (1.0f - rand.x);
190 phi = 2.0f * M_PI_F - phi;
191 }
192 }
193
194 /* get half vector in tangent space */
196
197 /* half vector to world space */
198 float3 H = to_global(h, X, Y, N);
199 const float HdotI = dot(H, wi);
200 if (HdotI < 0.0f) {
201 H = -H;
202 }
203
204 /* reflect wi on H to get wo */
205 *wo = -wi + (2.0f * HdotI) * H;
206
207 /* Check hemisphere. */
208 if (dot(Ng, *wo) < 0.0f) {
209 *pdf = 0.0f;
210 *eval = zero_spectrum();
211 return LABEL_NONE;
212 }
213
214 if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f) {
215 /* Some high number for MIS. */
216 *pdf = 1e6f;
217 *eval = make_spectrum(1e6f);
219 }
220 else {
221 /* leave the rest to eval */
222 *eval = bsdf_ashikhmin_shirley_eval(sc, wi, *wo, pdf);
223 }
224
225 return label;
226}
227
#define X
#define Y
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ccl_device void bsdf_ashikhmin_shirley_blur(ccl_private ShaderClosure *sc, const float roughness)
ccl_device_forceinline Spectrum bsdf_ashikhmin_shirley_eval(const ccl_private ShaderClosure *sc, const float3 wi, const float3 wo, ccl_private float *pdf)
ccl_device int bsdf_ashikhmin_shirley_sample(const ccl_private ShaderClosure *sc, const float3 Ng, const float3 wi, float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness)
ccl_device_inline void bsdf_ashikhmin_shirley_sample_first_quadrant(float n_x, const float n_y, const float2 rand, ccl_private float *phi, ccl_private float *cos_theta)
CCL_NAMESPACE_BEGIN ccl_device int bsdf_ashikhmin_shirley_setup(ccl_private MicrofacetBsdf *bsdf)
ccl_device_inline float bsdf_ashikhmin_shirley_roughness_to_exponent(const float roughness)
@ NONE
ccl_device_inline float cos_theta(const float3 w)
ccl_device float sin_phi(const float3 w)
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
dot(value.rgb, luminance_coefficients)") DEFINE_VALUE("REDUCE(lhs
ccl_device float3 spherical_cos_to_direction(const float cos_theta, const float phi)
ccl_device_inline T to_global(const float2 p, const T X, const T Y)
#define M_PI_2_F
#define ccl_device_forceinline
#define zero_spectrum
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define powf(x, y)
#define CCL_NAMESPACE_END
#define out
VecBase< float, D > normalize(VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
@ CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID
@ SD_BSDF_HAS_EVAL
@ SD_BSDF
@ LABEL_NONE
@ LABEL_SINGULAR
@ LABEL_GLOSSY
@ LABEL_REFLECT
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
#define N
#define H(x, y, z)
#define fabsf
#define sqrtf
#define ccl_device
#define M_2PI_F
#define atanf
#define fmaxf
#define sinf
#define make_float2
#define tanf
#define M_PI_F
#define cosf
ccl_device void make_orthonormals_tangent(const float3 N, const float3 T, ccl_private float3 *a, ccl_private float3 *b)
float x
float y
float3 Spectrum