Blender  V2.93
bsdf_principled_diffuse.h
Go to the documentation of this file.
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 
26 
29 
30  float roughness;
32 
33 static_assert(sizeof(ShaderClosure) >= sizeof(PrincipledDiffuseBsdf),
34  "PrincipledDiffuseBsdf is too large!");
35 
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 
59 {
61  return SD_BSDF | SD_BSDF_HAS_EVAL;
62 }
63 
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 
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 
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 
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 
139 
140 #endif /* __BSDF_PRINCIPLED_DIFFUSE_H__ */
ccl_device float3 bsdf_principled_diffuse_eval_reflect(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
ccl_device float3 bsdf_principled_diffuse_eval_transmit(const ShaderClosure *sc, const float3 I, const float3 omega_in, float *pdf)
ccl_device bool bsdf_principled_diffuse_merge(const ShaderClosure *a, const ShaderClosure *b)
ccl_device int 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)
CCL_NAMESPACE_BEGIN typedef ccl_addr_space struct PrincipledDiffuseBsdf PrincipledDiffuseBsdf
ccl_device int bsdf_principled_diffuse_setup(PrincipledDiffuseBsdf *bsdf)
ccl_device float3 calculate_principled_diffuse_brdf(const PrincipledDiffuseBsdf *bsdf, float3 N, float3 V, float3 L, float3 H, float *pdf)
ccl_device float schlick_fresnel(float u)
Definition: bsdf_util.h:130
#define ccl_addr_space
#define ccl_device
#define CCL_NAMESPACE_END
#define fmaxf(x, y)
#define make_float3(x, y, z)
ccl_device_inline void sample_cos_hemisphere(const float3 N, float randu, float randv, float3 *omega_in, float *pdf)
@ SD_BSDF_HAS_EVAL
Definition: kernel_types.h:849
@ SD_BSDF
Definition: kernel_types.h:847
@ LABEL_DIFFUSE
Definition: kernel_types.h:330
@ LABEL_REFLECT
Definition: kernel_types.h:329
ShaderClosure
Definition: kernel_types.h:831
#define L
static unsigned a[3]
Definition: RandGen.cpp:92
#define I
params N
@ CLOSURE_BSDF_PRINCIPLED_DIFFUSE_ID
Definition: svm_types.h:537
float max
#define M_1_PI_F
Definition: util_math.h:52
ccl_device_inline float2 normalize(const float2 &a)
ccl_device_inline float dot(const float2 &a, const float2 &b)
ccl_device_inline bool isequal_float3(const float3 a, const float3 b)
#define H(x, y, z)
CCL_NAMESPACE_BEGIN struct View V