Blender V4.5
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 Ng,
50 const float3 wi,
51 const float3 wo,
52 ccl_private float *pdf)
53{
54 const ccl_private MicrofacetBsdf *bsdf = (const ccl_private MicrofacetBsdf *)sc;
55 const float cosNgO = dot(Ng, wo);
56 const float3 N = bsdf->N;
57
58 float NdotI = dot(N, wi);
59 float NdotO = dot(N, wo);
60
61 float out = 0.0f;
62
63 if ((cosNgO < 0.0f) || fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f ||
64 !(NdotI > 0.0f && NdotO > 0.0f))
65 {
66 *pdf = 0.0f;
67 return zero_spectrum();
68 }
69
70 NdotI = fmaxf(NdotI, 1e-6f);
71 NdotO = fmaxf(NdotO, 1e-6f);
72 const float3 H = normalize(wi + wo);
73 const float HdotI = fmaxf(fabsf(dot(H, wi)), 1e-6f);
74 const float HdotN = fmaxf(dot(H, N), 1e-6f);
75
76 /* pump from original paper
77 * (first derivative disc., but cancels the HdotI in the pdf nicely) */
78 const float pump = 1.0f / fmaxf(1e-6f, (HdotI * fmaxf(NdotI, NdotO)));
79 /* `pump` from D-BRDF paper. */
80 // float pump = 1.0f / fmaxf(1e-4f, ((NdotI + NdotO) * (NdotI * NdotO)));
81
82 const float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
83 const float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
84
85 if (n_x == n_y) {
86 /* isotropic */
87 const float e = n_x;
88 const float lobe = powf(HdotN, e);
89 const float norm = (n_x + 1.0f) / (8.0f * M_PI_F);
90
91 out = NdotO * norm * lobe * pump;
92 /* this is p_h / 4(H.I) (conversion from `wh measure` to `wi measure`, eq. 8 in paper). */
93 *pdf = norm * lobe / HdotI;
94 }
95 else {
96 /* anisotropic */
97 float3 X;
98 float3 Y;
99 make_orthonormals_tangent(N, bsdf->T, &X, &Y);
100
101 const float HdotX = dot(H, X);
102 const float HdotY = dot(H, Y);
103 float lobe;
104 if (HdotN < 1.0f) {
105 const float e = (n_x * HdotX * HdotX + n_y * HdotY * HdotY) / (1.0f - HdotN * HdotN);
106 lobe = powf(HdotN, e);
107 }
108 else {
109 lobe = 1.0f;
110 }
111 const float norm = sqrtf((n_x + 1.0f) * (n_y + 1.0f)) / (8.0f * M_PI_F);
112
113 out = NdotO * norm * lobe * pump;
114 *pdf = norm * lobe / HdotI;
115 }
116
117 return make_spectrum(out);
118}
119
121 const float n_y,
122 const float2 rand,
123 ccl_private float *phi,
124 ccl_private float *cos_theta)
125{
126 *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * rand.x));
127 const float cos_phi = cosf(*phi);
128 const float sin_phi = sinf(*phi);
129 *cos_theta = powf(rand.y, 1.0f / (n_x * cos_phi * cos_phi + n_y * sin_phi * sin_phi + 1.0f));
130}
131
133 const float3 Ng,
134 const float3 wi,
135 float2 rand,
136 ccl_private Spectrum *eval,
138 ccl_private float *pdf,
139 ccl_private float2 *sampled_roughness)
140{
141 const ccl_private MicrofacetBsdf *bsdf = (const ccl_private MicrofacetBsdf *)sc;
142 *sampled_roughness = make_float2(bsdf->alpha_x, bsdf->alpha_y);
143 const float3 N = bsdf->N;
144 int label = LABEL_REFLECT | LABEL_GLOSSY;
145
146 const float NdotI = dot(N, wi);
147 if (!(NdotI > 0.0f)) {
148 *pdf = 0.0f;
149 *eval = zero_spectrum();
150 return LABEL_NONE;
151 }
152
153 const float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
154 const float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
155
156 /* get x,y basis on the surface for anisotropy */
157 float3 X;
158 float3 Y;
159
160 if (n_x == n_y) {
161 make_orthonormals(N, &X, &Y);
162 }
163 else {
164 make_orthonormals_tangent(N, bsdf->T, &X, &Y);
165 }
166
167 /* sample spherical coords for h in tangent space */
168 float phi;
169 float cos_theta;
170 if (n_x == n_y) {
171 /* isotropic sampling */
172 phi = M_2PI_F * rand.x;
173 cos_theta = powf(rand.y, 1.0f / (n_x + 1.0f));
174 }
175 else {
176 /* anisotropic sampling */
177 if (rand.x < 0.25f) { /* first quadrant */
178 rand.x *= 4.0f;
180 }
181 else if (rand.x < 0.5f) { /* second quadrant */
182 rand.x = 4.0f * (0.5f - rand.x);
184 phi = M_PI_F - phi;
185 }
186 else if (rand.x < 0.75f) { /* third quadrant */
187 rand.x = 4.0f * (rand.x - 0.5f);
189 phi = M_PI_F + phi;
190 }
191 else { /* fourth quadrant */
192 rand.x = 4.0f * (1.0f - rand.x);
194 phi = 2.0f * M_PI_F - phi;
195 }
196 }
197
198 /* get half vector in tangent space */
200
201 /* half vector to world space */
202 float3 H = to_global(h, X, Y, N);
203 const float HdotI = dot(H, wi);
204 if (HdotI < 0.0f) {
205 H = -H;
206 }
207
208 /* reflect wi on H to get wo */
209 *wo = -wi + (2.0f * HdotI) * H;
210
211 if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f) {
212 /* Some high number for MIS. */
213 *pdf = 1e6f;
214 *eval = make_spectrum(1e6f);
216 }
217 else {
218 /* leave the rest to eval */
219 *eval = bsdf_ashikhmin_shirley_eval(sc, Ng, wi, *wo, pdf);
220 }
221
222 return label;
223}
224
#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 Ng, 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 ccl_device
#define zero_spectrum
#define M_2PI_F
#define make_spectrum(f)
#define ccl_private
#define ccl_device_inline
#define M_PI_F
#define sinf(x)
#define cosf(x)
#define tanf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define atanf(x)
ccl_device_forceinline float2 make_float2(const float x, const float y)
#define fabsf(x)
#define sqrtf(x)
VecBase< float, D > normalize(VecOp< float, D >) RET
#define out
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)
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