Blender V4.3
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
20
22{
23 bsdf->alpha_x = clamp(bsdf->alpha_x, 1e-4f, 1.0f);
24 bsdf->alpha_y = clamp(bsdf->alpha_y, 1e-4f, 1.0f);
25
26 bsdf->fresnel_type = MicrofacetFresnel::NONE;
29}
30
32{
34
35 bsdf->alpha_x = fmaxf(roughness, bsdf->alpha_x);
36 bsdf->alpha_y = fmaxf(roughness, bsdf->alpha_y);
37}
38
40{
41 return 2.0f / (roughness * roughness) - 2.0f;
42}
43
45 const float3 Ng,
46 const float3 wi,
47 const float3 wo,
48 ccl_private float *pdf)
49{
50 ccl_private const MicrofacetBsdf *bsdf = (ccl_private const MicrofacetBsdf *)sc;
51 const float cosNgO = dot(Ng, wo);
52 float3 N = bsdf->N;
53
54 float NdotI = dot(N, wi);
55 float NdotO = dot(N, wo);
56
57 float out = 0.0f;
58
59 if ((cosNgO < 0.0f) || fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f ||
60 !(NdotI > 0.0f && NdotO > 0.0f))
61 {
62 *pdf = 0.0f;
63 return zero_spectrum();
64 }
65
66 NdotI = fmaxf(NdotI, 1e-6f);
67 NdotO = fmaxf(NdotO, 1e-6f);
68 float3 H = normalize(wi + wo);
69 float HdotI = fmaxf(fabsf(dot(H, wi)), 1e-6f);
70 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 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 float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
79 float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
80
81 if (n_x == n_y) {
82 /* isotropic */
83 float e = n_x;
84 float lobe = powf(HdotN, e);
85 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, Y;
94 make_orthonormals_tangent(N, bsdf->T, &X, &Y);
95
96 float HdotX = dot(H, X);
97 float HdotY = dot(H, Y);
98 float lobe;
99 if (HdotN < 1.0f) {
100 float e = (n_x * HdotX * HdotX + n_y * HdotY * HdotY) / (1.0f - HdotN * HdotN);
101 lobe = powf(HdotN, e);
102 }
103 else {
104 lobe = 1.0f;
105 }
106 float norm = sqrtf((n_x + 1.0f) * (n_y + 1.0f)) / (8.0f * M_PI_F);
107
108 out = NdotO * norm * lobe * pump;
109 *pdf = norm * lobe / HdotI;
110 }
111
112 return make_spectrum(out);
113}
114
116 float n_x, float n_y, const float2 rand, ccl_private float *phi, ccl_private float *cos_theta)
117{
118 *phi = atanf(sqrtf((n_x + 1.0f) / (n_y + 1.0f)) * tanf(M_PI_2_F * rand.x));
119 float cos_phi = cosf(*phi);
120 float sin_phi = sinf(*phi);
121 *cos_theta = powf(rand.y, 1.0f / (n_x * cos_phi * cos_phi + n_y * sin_phi * sin_phi + 1.0f));
122}
123
125 float3 Ng,
126 float3 wi,
127 float2 rand,
128 ccl_private Spectrum *eval,
130 ccl_private float *pdf,
131 ccl_private float2 *sampled_roughness)
132{
133 ccl_private const MicrofacetBsdf *bsdf = (ccl_private const MicrofacetBsdf *)sc;
134 *sampled_roughness = make_float2(bsdf->alpha_x, bsdf->alpha_y);
135 float3 N = bsdf->N;
137
138 float NdotI = dot(N, wi);
139 if (!(NdotI > 0.0f)) {
140 *pdf = 0.0f;
141 *eval = zero_spectrum();
142 return LABEL_NONE;
143 }
144
145 float n_x = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_x);
146 float n_y = bsdf_ashikhmin_shirley_roughness_to_exponent(bsdf->alpha_y);
147
148 /* get x,y basis on the surface for anisotropy */
149 float3 X, Y;
150
151 if (n_x == n_y)
152 make_orthonormals(N, &X, &Y);
153 else
154 make_orthonormals_tangent(N, bsdf->T, &X, &Y);
155
156 /* sample spherical coords for h in tangent space */
157 float phi;
158 float cos_theta;
159 if (n_x == n_y) {
160 /* isotropic sampling */
161 phi = M_2PI_F * rand.x;
162 cos_theta = powf(rand.y, 1.0f / (n_x + 1.0f));
163 }
164 else {
165 /* anisotropic sampling */
166 if (rand.x < 0.25f) { /* first quadrant */
167 rand.x *= 4.0f;
169 }
170 else if (rand.x < 0.5f) { /* second quadrant */
171 rand.x = 4.0f * (0.5f - rand.x);
173 phi = M_PI_F - phi;
174 }
175 else if (rand.x < 0.75f) { /* third quadrant */
176 rand.x = 4.0f * (rand.x - 0.5f);
178 phi = M_PI_F + phi;
179 }
180 else { /* fourth quadrant */
181 rand.x = 4.0f * (1.0f - rand.x);
183 phi = 2.0f * M_PI_F - phi;
184 }
185 }
186
187 /* get half vector in tangent space */
188 float sin_theta = sqrtf(fmaxf(0.0f, 1.0f - cos_theta * cos_theta));
189 float cos_phi = cosf(phi);
190 float sin_phi = sinf(phi); /* No `sqrt(1-cos^2)` here because it causes artifacts. */
192
193 /* half vector to world space */
194 float3 H = h.x * X + h.y * Y + h.z * N;
195 float HdotI = dot(H, wi);
196 if (HdotI < 0.0f)
197 H = -H;
198
199 /* reflect wi on H to get wo */
200 *wo = -wi + (2.0f * HdotI) * H;
201
202 if (fmaxf(bsdf->alpha_x, bsdf->alpha_y) <= 1e-4f) {
203 /* Some high number for MIS. */
204 *pdf = 1e6f;
205 *eval = make_spectrum(1e6f);
207 }
208 else {
209 /* leave the rest to eval */
210 *eval = bsdf_ashikhmin_shirley_eval(sc, Ng, wi, *wo, pdf);
211 }
212
213 return label;
214}
215
#define X
#define Y
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
ccl_device void bsdf_ashikhmin_shirley_blur(ccl_private ShaderClosure *sc, float roughness)
ccl_device_inline void bsdf_ashikhmin_shirley_sample_first_quadrant(float n_x, float n_y, const float2 rand, ccl_private float *phi, ccl_private float *cos_theta)
ccl_device int bsdf_ashikhmin_shirley_sample(ccl_private const ShaderClosure *sc, float3 Ng, float3 wi, float2 rand, ccl_private Spectrum *eval, ccl_private float3 *wo, ccl_private float *pdf, ccl_private float2 *sampled_roughness)
ccl_device_inline float bsdf_ashikhmin_shirley_roughness_to_exponent(float roughness)
ccl_device_forceinline Spectrum bsdf_ashikhmin_shirley_eval(ccl_private const ShaderClosure *sc, const float3 Ng, const float3 wi, const float3 wo, ccl_private float *pdf)
CCL_NAMESPACE_BEGIN ccl_device int bsdf_ashikhmin_shirley_setup(ccl_private MicrofacetBsdf *bsdf)
@ NONE
ccl_device_inline float cos_theta(const float3 w)
ccl_device float sin_phi(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
SIMD_FORCE_INLINE btScalar norm() const
Return the norm (length) of the vector.
Definition btVector3.h:263
SIMD_FORCE_INLINE btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303
additional_info("compositor_sum_squared_difference_float_shared") .push_constant(Type output_img float dot(value.rgb, luminance_coefficients)") .define("LOAD(value)"
const char * label
#define ccl_device_forceinline
#define sinf(x)
#define cosf(x)
#define ccl_device
#define ccl_private
#define ccl_device_inline
#define tanf(x)
#define powf(x, y)
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#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)
@ CLOSURE_BSDF_ASHIKHMIN_SHIRLEY_ID
@ SD_BSDF_HAS_EVAL
@ SD_BSDF
@ LABEL_NONE
@ LABEL_SINGULAR
@ LABEL_GLOSSY
@ LABEL_REFLECT
ShaderClosure
#define N
#define H(x, y, z)
#define M_PI_F
Definition mikk_util.hh:15
ccl_device void make_orthonormals_tangent(const float3 N, const float3 T, ccl_private float3 *a, ccl_private float3 *b)
#define M_PI_2_F
Definition sky_float3.h:20
#define M_2PI_F
Definition sky_float3.h:23
float x
float y
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
#define zero_spectrum
#define make_spectrum(f)
SPECTRUM_DATA_TYPE Spectrum
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
Definition util/math.h:593
ccl_device_inline int clamp(int a, int mn, int mx)
Definition util/math.h:379