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_DIFFUSE_H__
18 #define __BSDF_PRINCIPLED_DIFFUSE_H__
19
20 /* DISNEY PRINCIPLED DIFFUSE 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 PrincipledDiffuseBsdf {
28 SHADER_CLOSURE_BASE;
29
30 float roughness;
31 } PrincipledDiffuseBsdf;
32
33 static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledDiffuseBsdf),
34 "PrincipledDiffuseBsdf is too large!");
35
calculate_principled_diffuse_brdf(const PrincipledDiffuseBsdf * bsdf,float3 N,float3 V,float3 L,float3 H,float * pdf)36 ccl_device float3 calculate_principled_diffuse_brdf(
37 const PrincipledDiffuseBsdf *bsdf, float3 N, float3 V, float3 L, float3 H, float *pdf)
38 {
39 float NdotL = max(dot(N, L), 0.0f);
40 float NdotV = max(dot(N, V), 0.0f);
41
42 if (NdotL < 0 || NdotV < 0) {
43 *pdf = 0.0f;
44 return make_float3(0.0f, 0.0f, 0.0f);
45 }
46
47 float LdotH = dot(L, H);
48
49 float FL = schlick_fresnel(NdotL), FV = schlick_fresnel(NdotV);
50 const float Fd90 = 0.5f + 2.0f * LdotH * LdotH * bsdf->roughness;
51 float Fd = (1.0f * (1.0f - FL) + Fd90 * FL) * (1.0f * (1.0f - FV) + Fd90 * FV);
52
53 float value = M_1_PI_F * NdotL * Fd;
54
55 return make_float3(value, value, value);
56 }
57
bsdf_principled_diffuse_setup(PrincipledDiffuseBsdf * bsdf)58 ccl_device int bsdf_principled_diffuse_setup(PrincipledDiffuseBsdf *bsdf)
59 {
60 bsdf->type = CLOSURE_BSDF_PRINCIPLED_DIFFUSE_ID;
61 return SD_BSDF | SD_BSDF_HAS_EVAL;
62 }
63
bsdf_principled_diffuse_merge(const ShaderClosure * a,const ShaderClosure * b)64 ccl_device bool bsdf_principled_diffuse_merge(const ShaderClosure *a, const ShaderClosure *b)
65 {
66 const PrincipledDiffuseBsdf *bsdf_a = (const PrincipledDiffuseBsdf *)a;
67 const PrincipledDiffuseBsdf *bsdf_b = (const PrincipledDiffuseBsdf *)b;
68
69 return (isequal_float3(bsdf_a->N, bsdf_b->N) && bsdf_a->roughness == bsdf_b->roughness);
70 }
71
bsdf_principled_diffuse_eval_reflect(const ShaderClosure * sc,const float3 I,const float3 omega_in,float * pdf)72 ccl_device float3 bsdf_principled_diffuse_eval_reflect(const ShaderClosure *sc,
73 const float3 I,
74 const float3 omega_in,
75 float *pdf)
76 {
77 const PrincipledDiffuseBsdf *bsdf = (const PrincipledDiffuseBsdf *)sc;
78
79 float3 N = bsdf->N;
80 float3 V = I; // outgoing
81 float3 L = omega_in; // incoming
82 float3 H = normalize(L + V);
83
84 if (dot(N, omega_in) > 0.0f) {
85 *pdf = fmaxf(dot(N, omega_in), 0.0f) * M_1_PI_F;
86 return calculate_principled_diffuse_brdf(bsdf, N, V, L, H, pdf);
87 }
88 else {
89 *pdf = 0.0f;
90 return make_float3(0.0f, 0.0f, 0.0f);
91 }
92 }
93
bsdf_principled_diffuse_eval_transmit(const ShaderClosure * sc,const float3 I,const float3 omega_in,float * pdf)94 ccl_device float3 bsdf_principled_diffuse_eval_transmit(const ShaderClosure *sc,
95 const float3 I,
96 const float3 omega_in,
97 float *pdf)
98 {
99 return make_float3(0.0f, 0.0f, 0.0f);
100 }
101
bsdf_principled_diffuse_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)102 ccl_device int bsdf_principled_diffuse_sample(const ShaderClosure *sc,
103 float3 Ng,
104 float3 I,
105 float3 dIdx,
106 float3 dIdy,
107 float randu,
108 float randv,
109 float3 *eval,
110 float3 *omega_in,
111 float3 *domega_in_dx,
112 float3 *domega_in_dy,
113 float *pdf)
114 {
115 const PrincipledDiffuseBsdf *bsdf = (const PrincipledDiffuseBsdf *)sc;
116
117 float3 N = bsdf->N;
118
119 sample_cos_hemisphere(N, randu, randv, omega_in, pdf);
120
121 if (dot(Ng, *omega_in) > 0) {
122 float3 H = normalize(I + *omega_in);
123
124 *eval = calculate_principled_diffuse_brdf(bsdf, N, I, *omega_in, H, pdf);
125
126 #ifdef __RAY_DIFFERENTIALS__
127 // TODO: find a better approximation for the diffuse bounce
128 *domega_in_dx = -((2 * dot(N, dIdx)) * N - dIdx);
129 *domega_in_dy = -((2 * dot(N, dIdy)) * N - dIdy);
130 #endif
131 }
132 else {
133 *pdf = 0.0f;
134 }
135 return LABEL_REFLECT | LABEL_DIFFUSE;
136 }
137
138 CCL_NAMESPACE_END
139
140 #endif /* __BSDF_PRINCIPLED_DIFFUSE_H__ */
141