1 /*
2 * Copyright 2011-2017 Blender Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __BSDF_PRINCIPLED_SHEEN_H__
18 #define __BSDF_PRINCIPLED_SHEEN_H__
19
20 /* DISNEY PRINCIPLED SHEEN BRDF
21 *
22 * Shading model by Brent Burley (Disney): "Physically Based Shading at Disney" (2012)
23 */
24
25 CCL_NAMESPACE_BEGIN
26
27 typedef ccl_addr_space struct PrincipledSheenBsdf {
28 SHADER_CLOSURE_BASE;
29 float avg_value;
30 } PrincipledSheenBsdf;
31
32 static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledSheenBsdf),
33 "PrincipledSheenBsdf is too large!");
34
calculate_avg_principled_sheen_brdf(float3 N,float3 I)35 ccl_device_inline float calculate_avg_principled_sheen_brdf(float3 N, float3 I)
36 {
37 /* To compute the average, we set the half-vector to the normal, resulting in
38 * NdotI = NdotL = NdotV = LdotH */
39 float NdotI = dot(N, I);
40 if (NdotI < 0.0f) {
41 return 0.0f;
42 }
43
44 return schlick_fresnel(NdotI) * NdotI;
45 }
46
47 ccl_device float3
calculate_principled_sheen_brdf(float3 N,float3 V,float3 L,float3 H,float * pdf)48 calculate_principled_sheen_brdf(float3 N, float3 V, float3 L, float3 H, float *pdf)
49 {
50 float NdotL = dot(N, L);
51 float NdotV = dot(N, V);
52
53 if (NdotL < 0 || NdotV < 0) {
54 *pdf = 0.0f;
55 return make_float3(0.0f, 0.0f, 0.0f);
56 }
57
58 float LdotH = dot(L, H);
59
60 float value = schlick_fresnel(LdotH) * NdotL;
61
62 return make_float3(value, value, value);
63 }
64
bsdf_principled_sheen_setup(const ShaderData * sd,PrincipledSheenBsdf * bsdf)65 ccl_device int bsdf_principled_sheen_setup(const ShaderData *sd, PrincipledSheenBsdf *bsdf)
66 {
67 bsdf->type = CLOSURE_BSDF_PRINCIPLED_SHEEN_ID;
68 bsdf->avg_value = calculate_avg_principled_sheen_brdf(bsdf->N, sd->I);
69 bsdf->sample_weight *= bsdf->avg_value;
70 return SD_BSDF | SD_BSDF_HAS_EVAL;
71 }
72
bsdf_principled_sheen_eval_reflect(const ShaderClosure * sc,const float3 I,const float3 omega_in,float * pdf)73 ccl_device float3 bsdf_principled_sheen_eval_reflect(const ShaderClosure *sc,
74 const float3 I,
75 const float3 omega_in,
76 float *pdf)
77 {
78 const PrincipledSheenBsdf *bsdf = (const PrincipledSheenBsdf *)sc;
79
80 float3 N = bsdf->N;
81 float3 V = I; // outgoing
82 float3 L = omega_in; // incoming
83 float3 H = normalize(L + V);
84
85 if (dot(N, omega_in) > 0.0f) {
86 *pdf = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
87 return calculate_principled_sheen_brdf(N, V, L, H, pdf);
88 }
89 else {
90 *pdf = 0.0f;
91 return make_float3(0.0f, 0.0f, 0.0f);
92 }
93 }
94
bsdf_principled_sheen_eval_transmit(const ShaderClosure * sc,const float3 I,const float3 omega_in,float * pdf)95 ccl_device float3 bsdf_principled_sheen_eval_transmit(const ShaderClosure *sc,
96 const float3 I,
97 const float3 omega_in,
98 float *pdf)
99 {
100 return make_float3(0.0f, 0.0f, 0.0f);
101 }
102
bsdf_principled_sheen_sample(const ShaderClosure * sc,float3 Ng,float3 I,float3 dIdx,float3 dIdy,float randu,float randv,float3 * eval,float3 * omega_in,float3 * domega_in_dx,float3 * domega_in_dy,float * pdf)103 ccl_device int bsdf_principled_sheen_sample(const ShaderClosure *sc,
104 float3 Ng,
105 float3 I,
106 float3 dIdx,
107 float3 dIdy,
108 float randu,
109 float randv,
110 float3 *eval,
111 float3 *omega_in,
112 float3 *domega_in_dx,
113 float3 *domega_in_dy,
114 float *pdf)
115 {
116 const PrincipledSheenBsdf *bsdf = (const PrincipledSheenBsdf *)sc;
117
118 float3 N = bsdf->N;
119
120 sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
121
122 if (dot(Ng, *omega_in) > 0) {
123 float3 H = normalize(I + *omega_in);
124
125 *eval = calculate_principled_sheen_brdf(N, I, *omega_in, H, pdf);
126
127 #ifdef __RAY_DIFFERENTIALS__
128 // TODO: find a better approximation for the diffuse bounce
129 *domega_in_dx = -((2 * dot(N, dIdx)) * N - dIdx);
130 *domega_in_dy = -((2 * dot(N, dIdy)) * N - dIdy);
131 #endif
132 }
133 else {
134 *pdf = 0.0f;
135 }
136 return LABEL_REFLECT | LABEL_DIFFUSE;
137 }
138
139 CCL_NAMESPACE_END
140
141 #endif /* __BSDF_PRINCIPLED_SHEEN_H__ */
142